test_class.declarator: OFFSET_X = 11000000
test_class.modifier: static final
test_class.original_string: static final int OFFSET_X = 11000000;
test_class.type: int
test_class.var_name: OFFSET_X
test_class.declarator: OFFSET_Y = 50000000
test_class.modifier: static final
test_class.original_string: static final int OFFSET_Y = 50000000;
test_class.type: int
test_class.var_name: OFFSET_Y
test_class.declarator: polygon
test_class.modifier: static
test_class.original_string: static OsmNogoPolygon polygon;
test_class.type: OsmNogoPolygon
test_class.var_name: polygon
test_class.declarator: polyline
test_class.modifier: static
test_class.original_string: static OsmNogoPolygon polyline;
test_class.type: OsmNogoPolygon
test_class.var_name: polyline
test_class.declarator: lons = {  1.0,  1.0,  0.5, 0.5, 1.0, 1.0, -1.1, -1.0 }
test_class.modifier: static final
test_class.original_string: static final double[] lons = {  1.0,  1.0,  0.5, 0.5, 1.0, 1.0, -1.1, -1.0 };
test_class.type: double[]
test_class.var_name: lons
test_class.declarator: lats = { -1.0, -0.1, -0.1, 0.1, 0.1, 1.0,  1.1, -1.0 }
test_class.modifier: static final
test_class.original_string: static final double[] lats = { -1.0, -0.1, -0.1, 0.1, 0.1, 1.0,  1.1, -1.0 };
test_class.type: double[]
test_class.var_name: lats
test_class.file: brouter-core/src/test/java/btools/router/OsmNogoPolygonTest.java
test_class.identifier: OsmNogoPolygonTest
test_case.body: @Test
    public void testDistanceWithinPolygon() {
        // Testing polygon
        final double[] lons = { 2.333523, 2.333432, 2.333833, 2.333983, 2.334815, 2.334766 };
        final double[] lats = { 48.823778, 48.824091, 48.82389, 48.824165, 48.824232, 48.82384 };
        OsmNogoPolygon polygon = new OsmNogoPolygon(true);
        for (int i = 0; i < lons.length; i++) {
            polygon.addVertex(toOsmLon(lons[i], 0), toOsmLat(lats[i], 0));
        }
        OsmNogoPolygon polyline = new OsmNogoPolygon(false);
        for (int i = 0; i < lons.length; i++) {
            polyline.addVertex(toOsmLon(lons[i], 0), toOsmLat(lats[i], 0));
        }
  
        // Check with a segment with a single intersection with the polygon
        int lon1 = toOsmLon(2.33308732509613, 0);
        int lat1 = toOsmLat(48.8238790443901, 0);
        int lon2 = toOsmLon(2.33378201723099, 0);
        int lat2 = toOsmLat(48.8239585098974, 0);
        assertEquals(
          "Should give the correct length for a segment with a single intersection",
          17.5,
          polygon.distanceWithinPolygon(lon1, lat1, lon2, lat2),
          0.05 * 17.5
        );
  
        // Check with a segment crossing multiple times the polygon
        lon2 = toOsmLon(2.33488172292709, 0);
        lat2 = toOsmLat(48.8240891862353, 0);
        assertEquals(
          "Should give the correct length for a segment with multiple intersections",
          85,
          polygon.distanceWithinPolygon(lon1, lat1, lon2, lat2),
          0.05 * 85
        );
  
        // Check that it works when a point is within the polygon
        lon2 = toOsmLon(2.33433187007904, 0);
        lat2 = toOsmLat(48.8240238480664, 0);
        assertEquals(
          "Should give the correct length when last point is within the polygon",
          50,
          polygon.distanceWithinPolygon(lon1, lat1, lon2, lat2),
          0.05 * 50
        );
        lon1 = toOsmLon(2.33433187007904, 0);
        lat1 = toOsmLat(48.8240238480664, 0);
        lon2 = toOsmLon(2.33488172292709, 0);
        lat2 = toOsmLat(48.8240891862353, 0);
        assertEquals(
          "Should give the correct length when first point is within the polygon",
          35,
          polygon.distanceWithinPolygon(lon1, lat1, lon2, lat2),
          0.05 * 35
        );
  
        lon1 = toOsmLon(2.333523, 0);
        lat1 = toOsmLat(48.823778, 0);
        lon2 = toOsmLon(2.333432, 0);
        lat2 = toOsmLat(48.824091, 0);
        assertEquals(
          "Should give the correct length if the segment overlaps with an edge of the polygon",
          CheapRuler.distance(lon1, lat1, lon2, lat2),
          polygon.distanceWithinPolygon(lon1, lat1, lon2, lat2),
          0.05 * CheapRuler.distance(lon1, lat1, lon2, lat2)
        );
  
        lon1 = toOsmLon(2.333523, 0);
        lat1 = toOsmLat(48.823778, 0);
        lon2 = toOsmLon(2.3334775, 0);
        lat2 = toOsmLat(48.8239345, 0);
        assertEquals(
          "Should give the correct length if the segment overlaps with a polyline",
          CheapRuler.distance(lon1, lat1, lon2, lat2),
          polyline.distanceWithinPolygon(lon1, lat1, lon2, lat2),
          0.05 * CheapRuler.distance(lon1, lat1, lon2, lat2)
        );
    }
test_case.class_method_signature: OsmNogoPolygonTest.testDistanceWithinPolygon()
test_case.full_signature: @Test public void testDistanceWithinPolygon()
test_case.identifier: testDistanceWithinPolygon
test_case.invocations: addVertex toOsmLon toOsmLat addVertex toOsmLon toOsmLat toOsmLon toOsmLat toOsmLon toOsmLat assertEquals distanceWithinPolygon toOsmLon toOsmLat assertEquals distanceWithinPolygon toOsmLon toOsmLat assertEquals distanceWithinPolygon toOsmLon toOsmLat toOsmLon toOsmLat assertEquals distanceWithinPolygon toOsmLon toOsmLat toOsmLon toOsmLat assertEquals distance distanceWithinPolygon distance toOsmLon toOsmLat toOsmLon toOsmLat assertEquals distance distanceWithinPolygon distance
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testDistanceWithinPolygon()
focal_class.declarator: points = new ArrayList<Point>()
focal_class.modifier: public final
focal_class.original_string: public final List<Point> points = new ArrayList<Point>();
focal_class.type: List<Point>
focal_class.var_name: points
focal_class.declarator: isClosed
focal_class.modifier: public final
focal_class.original_string: public final boolean isClosed;
focal_class.type: boolean
focal_class.var_name: isClosed
focal_class.file: brouter-core/src/main/java/btools/router/OsmNogoPolygon.java
focal_class.identifier: OsmNogoPolygon
focal_class.class_method_signature: OsmNogoPolygon.OsmNogoPolygon(boolean closed)
focal_class.full_signature: public  OsmNogoPolygon(boolean closed)
focal_class.identifier: OsmNogoPolygon
focal_class.modifiers: public
focal_class.parameters: (boolean closed)
focal_class.signature:  OsmNogoPolygon(boolean closed)
focal_class.class_method_signature: OsmNogoPolygon.addVertex(int lon, int lat)
focal_class.full_signature: public final void addVertex(int lon, int lat)
focal_class.identifier: addVertex
focal_class.modifiers: public final
focal_class.parameters: (int lon, int lat)
focal_class.return: void
focal_class.signature: void addVertex(int lon, int lat)
focal_class.class_method_signature: OsmNogoPolygon.calcBoundingCircle()
focal_class.full_signature: public void calcBoundingCircle()
focal_class.identifier: calcBoundingCircle
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: void
focal_class.signature: void calcBoundingCircle()
focal_class.class_method_signature: OsmNogoPolygon.intersects(int lon0, int lat0, int lon1, int lat1)
focal_class.full_signature: public boolean intersects(int lon0, int lat0, int lon1, int lat1)
focal_class.identifier: intersects
focal_class.modifiers: public
focal_class.parameters: (int lon0, int lat0, int lon1, int lat1)
focal_class.return: boolean
focal_class.signature: boolean intersects(int lon0, int lat0, int lon1, int lat1)
focal_class.class_method_signature: OsmNogoPolygon.isOnPolyline( long px, long py )
focal_class.full_signature: public boolean isOnPolyline( long px, long py )
focal_class.identifier: isOnPolyline
focal_class.modifiers: public
focal_class.parameters: ( long px, long py )
focal_class.return: boolean
focal_class.signature: boolean isOnPolyline( long px, long py )
focal_class.class_method_signature: OsmNogoPolygon.isOnLine( long px, long py, long p0x, long p0y, long p1x, long p1y )
focal_class.full_signature: public static boolean isOnLine( long px, long py, long p0x, long p0y, long p1x, long p1y )
focal_class.identifier: isOnLine
focal_class.modifiers: public static
focal_class.parameters: ( long px, long py, long p0x, long p0y, long p1x, long p1y )
focal_class.return: boolean
focal_class.signature: boolean isOnLine( long px, long py, long p0x, long p0y, long p1x, long p1y )
focal_class.class_method_signature: OsmNogoPolygon.isWithin(final long px, final long py)
focal_class.full_signature: public boolean isWithin(final long px, final long py)
focal_class.identifier: isWithin
focal_class.modifiers: public
focal_class.parameters: (final long px, final long py)
focal_class.return: boolean
focal_class.signature: boolean isWithin(final long px, final long py)
focal_class.class_method_signature: OsmNogoPolygon.distanceWithinPolygon(int lon1, int lat1, int lon2, int lat2)
focal_class.full_signature: public double distanceWithinPolygon(int lon1, int lat1, int lon2, int lat2)
focal_class.identifier: distanceWithinPolygon
focal_class.modifiers: public
focal_class.parameters: (int lon1, int lat1, int lon2, int lat2)
focal_class.return: double
focal_class.signature: double distanceWithinPolygon(int lon1, int lat1, int lon2, int lat2)
focal_class.class_method_signature: OsmNogoPolygon.inSegment( final Point p, final Point seg_p0, final Point seg_p1)
focal_class.full_signature: private static boolean inSegment( final Point p, final Point seg_p0, final Point seg_p1)
focal_class.identifier: inSegment
focal_class.modifiers: private static
focal_class.parameters: ( final Point p, final Point seg_p0, final Point seg_p1)
focal_class.return: boolean
focal_class.signature: boolean inSegment( final Point p, final Point seg_p0, final Point seg_p1)
focal_class.class_method_signature: OsmNogoPolygon.intersect2D_2Segments( final Point s1p0, final Point s1p1, final Point s2p0, final Point s2p1 )
focal_class.full_signature: private static int intersect2D_2Segments( final Point s1p0, final Point s1p1, final Point s2p0, final Point s2p1 )
focal_class.identifier: intersect2D_2Segments
focal_class.modifiers: private static
focal_class.parameters: ( final Point s1p0, final Point s1p1, final Point s2p0, final Point s2p1 )
focal_class.return: int
focal_class.signature: int intersect2D_2Segments( final Point s1p0, final Point s1p1, final Point s2p0, final Point s2p1 )
focal_class.superclass: extends OsmNodeNamed
focal_method.body: public double distanceWithinPolygon(int lon1, int lat1, int lon2, int lat2) {
      double distance = 0.;
  
      // Extremities of the segments
      final Point p1 = new Point (lon1, lat1);
      final Point p2 = new Point (lon2, lat2);
  
      Point previousIntersectionOnSegment = null;
      if (isWithin(lon1, lat1))
      {
        // Start point of the segment is within the polygon, this is the first
        // "intersection".
        previousIntersectionOnSegment = p1;
      }
  
      // Loop over edges of the polygon to find intersections
      int i_last = points.size() - 1;
      for (int i = (isClosed ? 0 : 1), j = (isClosed ? i_last : 0); i <= i_last; j = i++)
      {
        Point edgePoint1 = points.get(j);
        Point edgePoint2 = points.get(i);
        int intersectsEdge = intersect2D_2Segments(p1, p2, edgePoint1, edgePoint2);
  
        if (isClosed && intersectsEdge == 1)
        {
          // Intersects with a (closed) polygon edge on a single point
          // Distance is zero when crossing a polyline.
          // Let's find this intersection point
          int xdiffSegment = lon1 - lon2;
          int xdiffEdge = edgePoint1.x - edgePoint2.x;
          int ydiffSegment = lat1 - lat2;
          int ydiffEdge = edgePoint1.y - edgePoint2.y;
          int div = xdiffSegment * ydiffEdge - xdiffEdge * ydiffSegment;
          long dSegment = (long) lon1 * (long) lat2 - (long) lon2 * (long) lat1;
          long dEdge = (long) edgePoint1.x * (long) edgePoint2.y - (long) edgePoint2.x * (long) edgePoint1.y;
          // Coordinates of the intersection
          Point intersection = new Point(
            (int) ((dSegment * xdiffEdge - dEdge * xdiffSegment) / div),
            (int) ((dSegment * ydiffEdge - dEdge * ydiffSegment) / div)
          );
          if (
            previousIntersectionOnSegment != null
            && isWithin(
              (intersection.x + previousIntersectionOnSegment.x) >> 1,
              (intersection.y + previousIntersectionOnSegment.y) >> 1
            )
          ) {
            // There was a previous match within the polygon and this part of the
            // segment is within the polygon.
            distance += CheapRuler.distance(
              previousIntersectionOnSegment.x, previousIntersectionOnSegment.y,
              intersection.x, intersection.y
            );
          }
          previousIntersectionOnSegment = intersection;
        }
        else if (intersectsEdge == 2) {
          // Segment and edge overlaps
          // FIXME: Could probably be done in a smarter way
          distance += Math.min(
            CheapRuler.distance(p1.x, p1.y, p2.x, p2.y),
            Math.min(
              CheapRuler.distance(edgePoint1.x, edgePoint1.y, edgePoint2.x, edgePoint2.y),
              Math.min(
                CheapRuler.distance(p1.x, p1.y, edgePoint2.x, edgePoint2.y),
                CheapRuler.distance(edgePoint1.x, edgePoint1.y, p2.x, p2.y)
              )
            )
          );
          // FIXME: We could store intersection.
          previousIntersectionOnSegment = null;
        }
      }
  
      if (
        previousIntersectionOnSegment != null
        && isWithin(lon2, lat2)
      ) {
        // Last point is within the polygon, add the remaining missing distance.
        distance += CheapRuler.distance(
          previousIntersectionOnSegment.x, previousIntersectionOnSegment.y,
          lon2, lat2
        );
      }
      return distance;
    }
focal_method.class_method_signature: OsmNogoPolygon.distanceWithinPolygon(int lon1, int lat1, int lon2, int lat2)
focal_method.full_signature: public double distanceWithinPolygon(int lon1, int lat1, int lon2, int lat2)
focal_method.identifier: distanceWithinPolygon
focal_method.invocations: isWithin size get get intersect2D_2Segments isWithin distance min distance min distance min distance distance isWithin distance
focal_method.modifiers: public
focal_method.parameters: (int lon1, int lat1, int lon2, int lat2)
focal_method.return: double
focal_method.signature: double distanceWithinPolygon(int lon1, int lat1, int lon2, int lat2)