1 /*
2  * Copyright (c) 2016 Vivid Solutions.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
8  * and the Eclipse Distribution License is available at
9  *
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  */
12 
13 package org.locationtech.jts.noding;
14 
15 
16 import org.locationtech.jts.geom.Coordinate;
17 import org.locationtech.jts.geom.LineSegment;
18 import org.locationtech.jts.geom.PrecisionModel;
19 
20 import junit.framework.TestCase;
21 
22 
23 /**
24  * Test IntersectionSegment#compareNodePosition using an exhaustive set
25  * of test cases
26  *
27  * @version 1.7
28  */
29 public class SegmentPointComparatorFullTest
30  extends TestCase
31 {
32 
33   private PrecisionModel pm = new PrecisionModel(1.0);
34 
SegmentPointComparatorFullTest(String name)35   public SegmentPointComparatorFullTest(String name) {
36     super(name);
37   }
38 
main(String[] args)39   public static void main(String[] args) {
40     junit.textui.TestRunner.run(SegmentPointComparatorFullTest.class);
41   }
42 
testQuadrant0()43   public void testQuadrant0()
44   {
45     checkSegment(100, 0);
46     checkSegment(100, 50);
47     checkSegment(100, 100);
48     checkSegment(100, 150);
49     checkSegment(0, 100);
50   }
51 
testQuadrant4()52   public void testQuadrant4()
53   {
54     checkSegment(100, -50);
55     checkSegment(100, -100);
56     checkSegment(100, -150);
57     checkSegment(0, -100);
58   }
59 
testQuadrant1()60   public void testQuadrant1()
61   {
62     checkSegment(-100, 0);
63     checkSegment(-100, 50);
64     checkSegment(-100, 100);
65     checkSegment(-100, 150);
66   }
67 
testQuadrant2()68   public void testQuadrant2()
69   {
70     checkSegment(-100, 0);
71     checkSegment(-100, -50);
72     checkSegment(-100, -100);
73     checkSegment(-100, -150);
74   }
75 
checkSegment(double x, double y)76   private void checkSegment(double x, double y)
77   {
78     Coordinate seg0 = new Coordinate(0, 0);
79     Coordinate seg1 = new Coordinate(x, y);
80     LineSegment seg = new LineSegment(seg0, seg1);
81 
82     for (int i = 0; i < 4; i++) {
83       double dist = i;
84 
85       double gridSize = 1 / pm.getScale();
86 
87       checkPointsAtDistance(seg, dist, dist + 1.0 * gridSize);
88       checkPointsAtDistance(seg, dist, dist + 2.0 * gridSize);
89       checkPointsAtDistance(seg, dist, dist + 3.0 * gridSize);
90       checkPointsAtDistance(seg, dist, dist + 4.0 * gridSize);
91     }
92   }
93 
computePoint(LineSegment seg, double dist)94   private Coordinate computePoint(LineSegment seg, double dist)
95   {
96     double dx = seg.p1.x - seg.p0.x;
97     double dy = seg.p1.y - seg.p0.y;
98     double len = seg.getLength();
99     Coordinate pt = new Coordinate(dist * dx / len, dist * dy / len);
100     pm.makePrecise(pt);
101     return pt;
102   }
103 
checkPointsAtDistance(LineSegment seg, double dist0, double dist1)104   private void checkPointsAtDistance(LineSegment seg, double dist0, double dist1)
105   {
106     Coordinate p0 = computePoint(seg, dist0);
107     Coordinate p1 = computePoint(seg, dist1);
108     if (p0.equals(p1)) {
109       checkNodePosition(seg, p0, p1, 0);
110     }
111     else {
112       checkNodePosition(seg, p0, p1, -1);
113       checkNodePosition(seg, p1, p0, 1);
114     }
115   }
116 
checkNodePosition(LineSegment seg, Coordinate p0, Coordinate p1, int expectedPositionValue)117   private void checkNodePosition(LineSegment seg, Coordinate p0, Coordinate p1, int expectedPositionValue)
118   {
119     int octant = Octant.octant(seg.p0, seg.p1);
120     int posValue = SegmentPointComparator.compare(octant, p0, p1);
121     //System.out.println(octant + " " + p0 + " " + p1 + " " + posValue);
122     assertTrue(posValue == expectedPositionValue);
123   }
124 
125 }
126