fix radius-calculation, clockwise windingnumber and wp-init

This commit is contained in:
ntruchsess 2018-01-24 15:40:34 +01:00
parent 144c0018de
commit 1fc6c9e62c
3 changed files with 36 additions and 38 deletions

View file

@ -27,11 +27,6 @@ import java.util.List;
public class OsmNogoPolygon extends OsmNodeNamed public class OsmNogoPolygon extends OsmNodeNamed
{ {
public OsmNogoPolygon()
{
isNogo = true;
}
private final static class Point private final static class Point
{ {
final int y; final int y;
@ -44,7 +39,7 @@ public class OsmNogoPolygon extends OsmNodeNamed
} }
} }
private final List<Point> points = new ArrayList<Point>(); public final List<Point> points = new ArrayList<Point>();
public final void addVertex(int lon, int lat) public final void addVertex(int lon, int lat)
{ {
@ -160,7 +155,7 @@ public class OsmNogoPolygon extends OsmNodeNamed
dpx = cx - ilon; // rounding error dpx = cx - ilon; // rounding error
dpy = cy - ilat; dpy = cy - ilat;
// compensate rounding error of center-point // compensate rounding error of center-point
radius = rad + Math.sqrt(dpx * dpx + dpy * dpy); radius = (rad + Math.sqrt(dpx * dpx + dpy * dpy)) * 0.000001;
return; return;
} }
@ -178,7 +173,7 @@ public class OsmNogoPolygon extends OsmNodeNamed
*/ */
public boolean isWithin(int lon, int lat) public boolean isWithin(int lon, int lat)
{ {
return wn_PnPoly(new Point(lon,lat),points) > 0; return wn_PnPoly(lon,lat,points) != 0;
} }
/** /**
@ -196,13 +191,13 @@ public class OsmNogoPolygon extends OsmNodeNamed
*/ */
public boolean intersectsOrIsWithin(int lon0, int lat0, int lon1, int lat1) public boolean intersectsOrIsWithin(int lon0, int lat0, int lon1, int lat1)
{ {
final Point p0 = new Point (lon0,lat0);
final Point p1 = new Point (lon1,lat1);
// is start or endpoint within polygon? // is start or endpoint within polygon?
if ((wn_PnPoly(p0, points) > 0) || (wn_PnPoly(p1, points) > 0)) if ((wn_PnPoly(lon0,lat0, points) != 0) || (wn_PnPoly(lon1,lat1, points) != 0))
{ {
return true; return true;
} }
final Point p0 = new Point (lon0,lat0);
final Point p1 = new Point (lon1,lat1);
int i_last = points.size()-1; int i_last = points.size()-1;
Point p2 = points.get(i_last); Point p2 = points.get(i_last);
for (int i = 0; i <= i_last; i++) for (int i = 0; i <= i_last; i++)
@ -275,12 +270,9 @@ public class OsmNogoPolygon extends OsmNodeNamed
* is implicitly closed connecting the last and first point. * is implicitly closed connecting the last and first point.
* @return the winding number (=0 only when P is outside) * @return the winding number (=0 only when P is outside)
*/ */
private static int wn_PnPoly(final Point p, final List<Point> v) { private static int wn_PnPoly(final long px, final long py, final List<Point> v) {
int wn = 0; // the winding number counter int wn = 0; // the winding number counter
final long px = p.x;
final long py = p.y;
// loop through all edges of the polygon // loop through all edges of the polygon
final int i_last = v.size()-1; final int i_last = v.size()-1;
final Point p0 = v.get(i_last); final Point p0 = v.get(i_last);

View file

@ -62,7 +62,7 @@ public class OsmNogoPolygonTest {
double py = toOsmLat(lats[i]); double py = toOsmLat(lats[i]);
double dpx = (toOsmLon(lons[i]) - p.ilon) * coslat(p.ilat); double dpx = (toOsmLon(lons[i]) - p.ilon) * coslat(p.ilat);
double dpy = py - p.ilat; double dpy = py - p.ilat;
double r1 = Math.sqrt(dpx * dpx + dpy * dpy); double r1 = Math.sqrt(dpx * dpx + dpy * dpy) * 0.000001;
double diff = r-r1; double diff = r-r1;
assertTrue("i: "+i+" r("+r+") >= r1("+r1+")", diff >= 0); assertTrue("i: "+i+" r("+r+") >= r1("+r1+")", diff >= 0);
} }

View file

@ -56,24 +56,22 @@ public class ServerHandler extends RequestHandler {
rc.setAlternativeIdx(Integer.parseInt(params.get( "alternativeidx" ))); rc.setAlternativeIdx(Integer.parseInt(params.get( "alternativeidx" )));
List<OsmNodeNamed> nogoList = readNogoList(); List<OsmNodeNamed> nogoList = readNogoList();
List<OsmNodeNamed> nogoPolygonsList = readNogoPolygons();
if ( nogoList != null ) if ( nogoList != null )
{ {
RoutingContext.prepareNogoPoints( nogoList ); RoutingContext.prepareNogoPoints( nogoList );
rc.nogopoints = nogoList; rc.nogopoints = nogoList;
} }
List<OsmNodeNamed> nogoPolygonsList = readNogoPolygons();
if ( nogoPolygonsList != null )
{
if (rc.nogopoints == null) if (rc.nogopoints == null)
{ {
rc.nogopoints = nogoPolygonsList; rc.nogopoints = nogoPolygonsList;
} }
else else if ( nogoPolygonsList != null )
{ {
rc.nogopoints.addAll(nogoPolygonsList); rc.nogopoints.addAll(nogoPolygonsList);
} }
}
return rc; return rc;
} }
@ -251,6 +249,8 @@ public class ServerHandler extends RequestHandler {
for (int i = 0; i < polygonList.length; i++) for (int i = 0; i < polygonList.length; i++)
{ {
String[] lonLatList = polygonList[i].split(","); String[] lonLatList = polygonList[i].split(",");
if ( lonLatList.length > 1 )
{
OsmNogoPolygon polygon = new OsmNogoPolygon(); OsmNogoPolygon polygon = new OsmNogoPolygon();
for (int j = 0; j < lonLatList.length-1;) for (int j = 0; j < lonLatList.length-1;)
{ {
@ -260,9 +260,15 @@ public class ServerHandler extends RequestHandler {
int lat = (int)( ( Double.parseDouble(slat) + 90. ) *1000000. + 0.5); int lat = (int)( ( Double.parseDouble(slat) + 90. ) *1000000. + 0.5);
polygon.addVertex(lon, lat); polygon.addVertex(lon, lat);
} }
if ( polygon.points.size() > 0 )
{
polygon.name = "";
polygon.isNogo = true;
polygon.calcBoundingCircle(); polygon.calcBoundingCircle();
nogoPolygonList.add(polygon); nogoPolygonList.add(polygon);
} }
}
}
return nogoPolygonList; return nogoPolygonList;
} }
} }