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 test.jts.perf.operation.union;
14 
15 import java.util.ArrayList;
16 import java.util.List;
17 
18 import org.locationtech.jts.geom.Coordinate;
19 import org.locationtech.jts.geom.Geometry;
20 import org.locationtech.jts.geom.GeometryFactory;
21 import org.locationtech.jts.geom.PrecisionModel;
22 import org.locationtech.jts.io.WKTReader;
23 import org.locationtech.jts.io.WKTWriter;
24 import org.locationtech.jts.util.GeometricShapeFactory;
25 
26 
27 public class PolygonUnionPerfTest
28 {
29 
30   static final int MAX_ITER = 1;
31 
32   static PrecisionModel pm = new PrecisionModel();
33   static GeometryFactory fact = new GeometryFactory(pm, 0);
34   static WKTReader wktRdr = new WKTReader(fact);
35   static WKTWriter wktWriter = new WKTWriter();
36 
37   GeometryFactory factory = new GeometryFactory();
38 
main(String[] args)39   public static void main(String[] args) {
40     PolygonUnionPerfTest test = new PolygonUnionPerfTest();
41 
42 //    test.test();
43     test.testRampItems();
44 
45   }
46 
47   boolean testFailed = false;
48 
PolygonUnionPerfTest()49   public PolygonUnionPerfTest() {
50   }
51 
testRampItems()52   public void testRampItems()
53   {
54     int nPts = 1000;
55 
56     test(5, nPts, 10.0);
57     test(5, nPts, 10.0);
58     test(25, nPts, 10.0);
59     test(50, nPts, 10.0);
60     test(100, nPts, 10.0);
61     test(200, nPts, 10.0);
62     test(400, nPts, 10.0);
63     test(500, nPts, 10.0);
64     test(1000, nPts, 10.0);
65     test(2000, nPts, 10.0);
66     test(4000, nPts, 10.0);
67   }
68 
test()69   public void test()
70   {
71 //    test(5, 100, 10.0);
72     test(1000, 100, 10.0);
73   }
74 
test(int nItems, int nPts, double size)75   public void test(int nItems, int nPts, double size)
76   {
77     System.out.println("---------------------------------------------------------");
78     System.out.println("# pts/item: " + nPts);
79 
80     List polys = createPolys(nItems, size, nPts);
81 
82 //    System.out.println();
83     //System.out.println("Running with " + nPts + " points");
84 
85     UnionPerfTester tester = new UnionPerfTester(polys);
86     tester.runAll();
87   }
88 
89   /**
90    * Creates a grid of circles with a small percentage of overlap
91    * in both directions.
92    * This approximated likely real-world cases well,
93    * and seems to produce
94    * close to worst-case performance for the Iterated algorithm.
95    *
96    * Sample times:
97    * 1000 items/100 pts - Cascaded: 2718 ms, Iterated 150 s
98    *
99    * @param nItems
100    * @param size
101    * @param nPts
102    * @return
103    */
createPolys(int nItems, double size, int nPts)104   List createPolys(int nItems, double size, int nPts)
105   {
106 
107   	// between 0 and 1
108     double overlapPct = 0.2;
109 
110 
111     int nCells = (int) Math.sqrt(nItems);
112 
113     List geoms = new ArrayList();
114 //    double width = env.getWidth();
115     double width = nCells * (1 - overlapPct) * size;
116 
117     // this results in many final polys
118     double height = nCells * 2 * size;
119 
120     // this results in a single final polygon
121 //    double height = width;
122 
123     double xInc = width / nCells;
124     double yInc = height / nCells;
125     for (int i = 0; i < nCells; i++) {
126       for (int j = 0; j < nCells; j++) {
127         Coordinate base = new Coordinate(
128             i * xInc,
129             j * yInc);
130         Geometry poly = createPoly(base, size, nPts);
131         geoms.add(poly);
132 //        System.out.println(poly);
133       }
134     }
135     return geoms;
136   }
137 
createPoly(Coordinate base, double size, int nPts)138   Geometry createPoly(Coordinate base, double size, int nPts)
139   {
140     GeometricShapeFactory gsf = new GeometricShapeFactory(factory);
141     gsf.setCentre(base);
142     gsf.setSize(size);
143     gsf.setNumPoints(nPts);
144 
145     Geometry poly = gsf.createCircle();
146 //    Geometry poly = gsf.createRectangle();
147 
148 //    System.out.println(circle);
149     return poly;
150   }
151 
152 
153 }
154