1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import org.junit.Assert;
|
---|
5 | import org.junit.Test;
|
---|
6 | import org.junit.BeforeClass;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.JOSMFixture;
|
---|
9 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Unit tests of {@link Geometry} class.
|
---|
13 | */
|
---|
14 | public class GeometryTest {
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Setup test.
|
---|
18 | */
|
---|
19 | @BeforeClass
|
---|
20 | public static void setUp() {
|
---|
21 | JOSMFixture.createUnitTestFixture().init();
|
---|
22 | }
|
---|
23 |
|
---|
24 | @Test
|
---|
25 | public void testLineLineIntersection() {
|
---|
26 | EastNorth p1 = new EastNorth(-9477809.106349014, 1.5392960539974203E7);
|
---|
27 | EastNorth p2 = new EastNorth(-9477813.789091509, 1.5392954297092048E7);
|
---|
28 | EastNorth p3 = new EastNorth(-9477804.974058038, 1.539295490030348E7);
|
---|
29 | EastNorth p4 = new EastNorth(-9477814.628697459, 1.5392962142181376E7);
|
---|
30 |
|
---|
31 | EastNorth intersectionPoint = Geometry.getLineLineIntersection(p1, p2, p3, p4);
|
---|
32 |
|
---|
33 | EastNorth d1 = intersectionPoint.sub(p3);
|
---|
34 | EastNorth d2 = p2.sub(p1);
|
---|
35 | Double crossProduct = d1.east()*d2.north() - d1.north()*d2.east();
|
---|
36 | Double scalarProduct = d1.east()*d2.east() + d1.north()*d2.north();
|
---|
37 | Double len1 = d1.length();
|
---|
38 | Double len2 = d2.length();
|
---|
39 |
|
---|
40 | Double angle1 = Geometry.getCornerAngle(p1, p2, intersectionPoint);
|
---|
41 | Double angle2 = Geometry.getCornerAngle(p3, p4, intersectionPoint);
|
---|
42 | Assert.assertTrue("intersection point not on line, angle: " + angle1,
|
---|
43 | Math.abs(angle1) < 1e-10);
|
---|
44 | Assert.assertTrue("intersection point not on line, angle: " + angle2,
|
---|
45 | Math.abs(angle1) < 1e-10);
|
---|
46 |
|
---|
47 | Assert.assertTrue("cross product != 1 : " + Math.abs(crossProduct/len1/len2),
|
---|
48 | Math.abs(Math.abs(crossProduct/len1/len2) - 1) < 1e-10);
|
---|
49 | Assert.assertTrue("scalar product != 0 : " + scalarProduct/len1/len2,
|
---|
50 | Math.abs(scalarProduct/len1/len2) < 1e-10);
|
---|
51 | }
|
---|
52 | }
|
---|