Changeset 15008 in josm for trunk/test


Ignore:
Timestamp:
2019-04-21T07:43:00+02:00 (5 years ago)
Author:
GerdP
Message:

fix #17614:

  • don't test integer bounding box of intersection area, use doubles
  • in case of multiple intersections, check each individual intersection
  • introduce new constant INTERSECTION_EPS_EAST_NORTH, the value 1e-4 is just a reasonable small value, might be decreased
  • improve javadoc to make clear that east/north space is assumed
  • small refactorings to fix SonarLint issues
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/tools/GeometryTest.java

    r13712 r15008  
    33
    44import static org.junit.Assert.assertEquals;
     5import static org.junit.Assert.assertNotEquals;
    56
    67import java.io.FileInputStream;
     
    159160        assertEquals(new EastNorth(150, 266d + 2d/3d), Geometry.getCentroidEN(Arrays.asList(en1, en2, en3)));
    160161    }
     162
     163
     164    /**
     165     * Test of {@link Geometry#polygonIntersection} method with two triangles.
     166     */
     167    @Test
     168    public void testPolygonIntersectionTriangles() {
     169        Node node1 = new Node(new LatLon(0.0, 1.0));
     170        Node node2 = new Node(new LatLon(0.0, 2.0));
     171        Node node3 = new Node(new LatLon(5.0, 1.5));
     172        List<Node> poly1 = Arrays.asList(node1, node2, node3, node1);
     173        Node node4 = new Node(new LatLon(10.0, 1.0));
     174        Node node5 = new Node(new LatLon(10.0, 2.0));
     175        Node node6 = new Node(new LatLon(5.000001, 1.5));
     176        List<Node> poly2 = Arrays.asList(node4, node5, node6, node4);
     177        // no intersection, not even touching
     178        assertEquals(Geometry.PolygonIntersection.OUTSIDE, Geometry.polygonIntersection(poly1, poly2));
     179
     180        node5.setCoor(new LatLon(5.0, 1.5));
     181        // touching in a single point with two different nodes
     182        assertEquals(Geometry.PolygonIntersection.OUTSIDE, Geometry.polygonIntersection(poly1, poly2));
     183
     184        node5.setCoor(new LatLon(4.99999999, 1.5));
     185        // now node5 lies inside way1, intersection is a very small area, in OSM precision nodes are equal
     186        assertEquals(node5.getCoor().getRoundedToOsmPrecision(), node3.getCoor().getRoundedToOsmPrecision());
     187        assertEquals(Geometry.PolygonIntersection.CROSSING, Geometry.polygonIntersection(poly1, poly2));
     188
     189        node5.setCoor(new LatLon(4.9999999, 1.5));
     190        // intersection area is too big to ignore
     191        assertNotEquals(node5.getCoor().getRoundedToOsmPrecision(), node3.getCoor().getRoundedToOsmPrecision());
     192        assertEquals(Geometry.PolygonIntersection.CROSSING, Geometry.polygonIntersection(poly1, poly2));
     193    }
     194
     195    /**
     196     * Test of {@link Geometry#polygonIntersection} method with two V-shapes
     197     */
     198    @Test
     199    public void testPolygonIntersectionVShapes() {
     200        Node node1 = new Node(new LatLon(1.0, 1.0));
     201        Node node2 = new Node(new LatLon(2.0, 2.0));
     202        Node node3 = new Node(new LatLon(0.9, 1.0));
     203        Node node4 = new Node(new LatLon(2.0, 0.0));
     204        List<Node> poly1 = Arrays.asList(node1, node2, node3, node4, node1);
     205        Node node5 = new Node(new LatLon(3.0, 1.0));
     206        Node node6 = new Node(new LatLon(2.0, 2.0)); // like node2
     207        Node node7 = new Node(new LatLon(3.1, 1.0));
     208        Node node8 = new Node(new LatLon(2.0, 0.0)); // like node4
     209        List<Node> poly2 = Arrays.asList(node5, node6, node7, node8, node5);
     210
     211        // touching in two points but not overlapping
     212        assertEquals(Geometry.PolygonIntersection.OUTSIDE, Geometry.polygonIntersection(poly1, poly2));
     213
     214        // touching in one point, small overlap at the other
     215        node6.setCoor(new LatLon(1.9999999, 2.0));
     216        assertEquals(Geometry.PolygonIntersection.CROSSING, Geometry.polygonIntersection(poly1, poly2));
     217
     218        // two small overlaps, but clearly visible because lines are crossing
     219        node6.setCoor(new LatLon(1.99999999, 2.0));
     220        node8.setCoor(new LatLon(1.99999999, 0.0));
     221        assertEquals(Geometry.PolygonIntersection.OUTSIDE, Geometry.polygonIntersection(poly1, poly2));
     222    }
    161223}
Note: See TracChangeset for help on using the changeset viewer.