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 package org.locationtech.jts.geom.prep;
13 
14 import org.locationtech.jts.geom.Coordinate;
15 import org.locationtech.jts.geom.Envelope;
16 import org.locationtech.jts.geom.Geometry;
17 import org.locationtech.jts.geom.GeometryFactory;
18 import org.locationtech.jts.geom.LineString;
19 import org.locationtech.jts.geom.PrecisionModel;
20 import org.locationtech.jts.geom.util.SineStarFactory;
21 import org.locationtech.jts.io.WKTReader;
22 import org.locationtech.jts.io.WKTWriter;
23 import org.locationtech.jts.util.GeometricShapeFactory;
24 
25 import junit.framework.TestCase;
26 import junit.textui.TestRunner;
27 
28 
29 /**
30  * Stress tests {@link PreparedPolygon#intersects(Geometry)}
31  * to confirm it finds intersections correctly.
32  *
33  * @author Martin Davis
34  *
35  */
36 public class PreparedPolygonIntersectsStressTest extends TestCase
37 {
38   static final int MAX_ITER = 10000;
39 
40   static PrecisionModel pm = new PrecisionModel();
41   static GeometryFactory fact = new GeometryFactory(pm, 0);
42   static WKTReader wktRdr = new WKTReader(fact);
43   static WKTWriter wktWriter = new WKTWriter();
44 
main(String args[])45   public static void main(String args[]) {
46     TestRunner.run(PreparedPolygonIntersectsStressTest.class);
47   }
48 
49   boolean testFailed = false;
50 
PreparedPolygonIntersectsStressTest(String name)51   public PreparedPolygonIntersectsStressTest(String name) {
52     super(name);
53   }
54 
test()55   public void test()
56   {
57     run(1000);
58   }
59 
run(int nPts)60   public void run(int nPts)
61   {
62 //  	Geometry poly = createCircle(new Coordinate(0, 0), 100, nPts);
63   	Geometry poly = createSineStar(new Coordinate(0, 0), 100, nPts);
64   	//System.out.println(poly);
65 
66     //System.out.println();
67     //System.out.println("Running with " + nPts + " points");
68     test(poly);
69   }
70 
createCircle(Coordinate origin, double size, int nPts)71   Geometry createCircle(Coordinate origin, double size, int nPts) {
72 		GeometricShapeFactory gsf = new GeometricShapeFactory();
73 		gsf.setCentre(origin);
74 		gsf.setSize(size);
75 		gsf.setNumPoints(nPts);
76 		Geometry circle = gsf.createCircle();
77 		// Polygon gRect = gsf.createRectangle();
78 		// Geometry g = gRect.getExteriorRing();
79 		return circle;
80 	}
81 
createSineStar(Coordinate origin, double size, int nPts)82   Geometry createSineStar(Coordinate origin, double size, int nPts) {
83 		SineStarFactory gsf = new SineStarFactory();
84 		gsf.setCentre(origin);
85 		gsf.setSize(size);
86 		gsf.setNumPoints(nPts);
87 		gsf.setArmLengthRatio(0.1);
88 		gsf.setNumArms(20);
89 		Geometry poly = gsf.createSineStar();
90 		return poly;
91 	}
92 
createTestLine(Envelope env, double size, int nPts)93   LineString createTestLine(Envelope env, double size, int nPts)
94   {
95   	double width = env.getWidth();
96   	double xOffset = width * Math.random();
97   	double yOffset = env.getHeight() * Math.random();
98     Coordinate basePt = new Coordinate(
99     				env.getMinX() + xOffset,
100     				env.getMinY() + yOffset);
101     LineString line = createTestLine(basePt, size, nPts);
102     return line;
103   }
104 
createTestLine(Coordinate base, double size, int nPts)105   LineString createTestLine(Coordinate base, double size, int nPts)
106   {
107     GeometricShapeFactory gsf = new GeometricShapeFactory();
108     gsf.setCentre(base);
109     gsf.setSize(size);
110     gsf.setNumPoints(nPts);
111     Geometry circle = gsf.createCircle();
112 //    System.out.println(circle);
113     return (LineString) circle.getBoundary();
114   }
115 
test(Geometry g)116   public void test(Geometry g) {
117   	int count = 0;
118   	while (count < MAX_ITER) {
119   		count++;
120   		LineString line = createTestLine(g.getEnvelopeInternal(), 10, 20);
121 
122 //      System.out.println("Test # " + count);
123 //  		System.out.println(line);
124   		testResultsEqual(g, line);
125   	}
126 	}
127 
testResultsEqual(Geometry g, LineString line)128   public void testResultsEqual(Geometry g, LineString line)
129   {
130 		boolean slowIntersects = g.intersects(line);
131 
132     PreparedGeometryFactory pgFact = new PreparedGeometryFactory();
133     PreparedGeometry prepGeom = pgFact.create(g);
134 
135 		boolean fastIntersects = prepGeom.intersects(line);
136 
137 		if (slowIntersects != fastIntersects) {
138 			System.out.println(line);
139 			System.out.println("Slow = " + slowIntersects + ", Fast = " + fastIntersects);
140 			throw new RuntimeException("Different results found for intersects() !");
141 		}
142 	}
143 }
144