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 15 import org.locationtech.jts.geom.Coordinate; 16 import org.locationtech.jts.geom.Envelope; 17 import org.locationtech.jts.geom.Geometry; 18 import org.locationtech.jts.geom.GeometryFactory; 19 import org.locationtech.jts.geom.Polygon; 20 import org.locationtech.jts.geom.PrecisionModel; 21 import org.locationtech.jts.geom.util.SineStarFactory; 22 import org.locationtech.jts.io.WKTReader; 23 import org.locationtech.jts.io.WKTWriter; 24 import org.locationtech.jts.util.GeometricShapeFactory; 25 26 public abstract class StressTestHarness 27 { 28 static final int MAX_ITER = 10000; 29 30 static PrecisionModel pm = new PrecisionModel(); 31 static GeometryFactory fact = new GeometryFactory(pm, 0); 32 static WKTReader wktRdr = new WKTReader(fact); 33 static WKTWriter wktWriter = new WKTWriter(); 34 35 private int numTargetPts = 1000; 36 StressTestHarness()37 public StressTestHarness() { 38 } 39 setTargetSize(int nPts)40 public void setTargetSize(int nPts) 41 { 42 numTargetPts =nPts; 43 } 44 run(int nIter)45 public void run(int nIter) 46 { 47 //System.out.println("Running " + nIter + " tests"); 48 // Geometry poly = createCircle(new Coordinate(0, 0), 100, nPts); 49 Geometry poly = createSineStar(new Coordinate(0, 0), 100, numTargetPts); 50 //System.out.println(poly); 51 52 //System.out.println(); 53 //System.out.println("Running with " + nPts + " points"); 54 run(nIter, poly); 55 } 56 createCircle(Coordinate origin, double size, int nPts)57 Geometry createCircle(Coordinate origin, double size, int nPts) { 58 GeometricShapeFactory gsf = new GeometricShapeFactory(); 59 gsf.setCentre(origin); 60 gsf.setSize(size); 61 gsf.setNumPoints(nPts); 62 Geometry circle = gsf.createCircle(); 63 // Polygon gRect = gsf.createRectangle(); 64 // Geometry g = gRect.getExteriorRing(); 65 return circle; 66 } 67 createSineStar(Coordinate origin, double size, int nPts)68 Geometry createSineStar(Coordinate origin, double size, int nPts) { 69 SineStarFactory gsf = new SineStarFactory(); 70 gsf.setCentre(origin); 71 gsf.setSize(size); 72 gsf.setNumPoints(nPts); 73 gsf.setArmLengthRatio(0.1); 74 gsf.setNumArms(20); 75 Geometry poly = gsf.createSineStar(); 76 return poly; 77 } 78 createRandomTestGeometry(Envelope env, double size, int nPts)79 Geometry createRandomTestGeometry(Envelope env, double size, int nPts) 80 { 81 double width = env.getWidth(); 82 double xOffset = width * Math.random(); 83 double yOffset = env.getHeight() * Math.random(); 84 Coordinate basePt = new Coordinate( 85 env.getMinX() + xOffset, 86 env.getMinY() + yOffset); 87 Geometry test = createTestCircle(basePt, size, nPts); 88 if (test instanceof Polygon && Math.random() > 0.5) { 89 test = test.getBoundary(); 90 } 91 return test; 92 } 93 createTestCircle(Coordinate base, double size, int nPts)94 Geometry createTestCircle(Coordinate base, double size, int nPts) 95 { 96 GeometricShapeFactory gsf = new GeometricShapeFactory(); 97 gsf.setCentre(base); 98 gsf.setSize(size); 99 gsf.setNumPoints(nPts); 100 Geometry circle = gsf.createCircle(); 101 // System.out.println(circle); 102 return circle; 103 } 104 run(int nIter, Geometry target)105 public void run(int nIter, Geometry target) { 106 int count = 0; 107 while (count < nIter) { 108 count++; 109 Geometry test = createRandomTestGeometry(target.getEnvelopeInternal(), 10, 20); 110 111 // System.out.println("Test # " + count); 112 // System.out.println(line); 113 // System.out.println("Test[" + count + "] " + target.getClass() + "/" + test.getClass()); 114 boolean isResultCorrect = checkResult(target, test); 115 if (! isResultCorrect) { 116 throw new RuntimeException("Invalid result found"); 117 } 118 } 119 } 120 checkResult(Geometry target, Geometry test)121 public abstract boolean checkResult(Geometry target, Geometry test); 122 123 124 } 125