|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Martin Davis. |
| 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.algorithm.construct; |
| 13 | + |
| 14 | +import org.locationtech.jts.algorithm.Angle; |
| 15 | +import org.locationtech.jts.algorithm.Orientation; |
| 16 | +import org.locationtech.jts.geom.Coordinate; |
| 17 | +import org.locationtech.jts.geom.CoordinateArrays; |
| 18 | +import org.locationtech.jts.geom.CoordinateSequence; |
| 19 | +import org.locationtech.jts.geom.Geometry; |
| 20 | +import org.locationtech.jts.geom.LineSegment; |
| 21 | +import org.locationtech.jts.geom.LinearRing; |
| 22 | +import org.locationtech.jts.geom.Polygon; |
| 23 | +import org.locationtech.jts.geom.Triangle; |
| 24 | + |
| 25 | +/** |
| 26 | + * Computes the Maximum Inscribed Circle for some kinds of convex polygons. |
| 27 | + * It determines the circle center point by computing Voronoi node points |
| 28 | + * and testing them for distance to generating edges. |
| 29 | + * This is more precise than iterated approximation, |
| 30 | + * and faster for small polygons (such as triangles and convex quadrilaterals). |
| 31 | + * |
| 32 | + * @author Martin Davis |
| 33 | + * |
| 34 | + */ |
| 35 | +class ExactMaxInscribedCircle { |
| 36 | + |
| 37 | + /** |
| 38 | + * Tests whether a given geometry is supported by this class. |
| 39 | + * Currently only triangles and convex quadrilaterals are supported. |
| 40 | + * |
| 41 | + * @param geom an areal geometry |
| 42 | + * @return true if the geometry shape can be evaluated |
| 43 | + */ |
| 44 | + public static boolean isSupported(Geometry geom) { |
| 45 | + if (! isSimplePolygon(geom)) |
| 46 | + return false; |
| 47 | + Polygon polygon = (Polygon) geom; |
| 48 | + if (isTriangle(polygon)) |
| 49 | + return true; |
| 50 | + if (isQuadrilateral(polygon) && isConvex(polygon)) |
| 51 | + return true; |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + private static boolean isSimplePolygon(Geometry geom) { |
| 56 | + return geom instanceof Polygon |
| 57 | + && ((Polygon) geom).getNumInteriorRing() == 0; |
| 58 | + } |
| 59 | + |
| 60 | + private static boolean isTriangle(Polygon polygon) { |
| 61 | + return polygon.getNumPoints() == 4; |
| 62 | + } |
| 63 | + |
| 64 | + private static boolean isQuadrilateral(Polygon polygon) { |
| 65 | + return polygon.getNumPoints() == 5; |
| 66 | + } |
| 67 | + |
| 68 | + public static Coordinate[] computeRadius(Polygon polygon) { |
| 69 | + Coordinate[] ring = polygon.getExteriorRing().getCoordinates(); |
| 70 | + if (ring.length == 4) |
| 71 | + return computeTriangle(ring); |
| 72 | + else if (ring.length == 5) |
| 73 | + return computeConvexQuadrilateral(ring); |
| 74 | + throw new IllegalArgumentException("Input must be a triangle or convex quadrilateral"); |
| 75 | + } |
| 76 | + |
| 77 | + private static Coordinate[] computeTriangle(Coordinate[] ring) { |
| 78 | + Coordinate center = Triangle.inCentre(ring[0], ring[1], ring[2]); |
| 79 | + LineSegment seg = new LineSegment(ring[0], ring[1]); |
| 80 | + Coordinate radius = seg.project(center); |
| 81 | + return new Coordinate[] { center, radius }; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * The Voronoi nodes of a convex polygon occur at the intersection point |
| 86 | + * of two bisectors of each triplet of edges. |
| 87 | + * The Maximum Inscribed Circle center is the node |
| 88 | + * is the farthest distance from the generating edges. |
| 89 | + * For a quadrilateral there are 4 distinct edge triplets, |
| 90 | + * at each edge with its adjacent edges. |
| 91 | + * |
| 92 | + * @param ring the polygon ring |
| 93 | + * @return an array containing the incircle center and radius points |
| 94 | + */ |
| 95 | + private static Coordinate[] computeConvexQuadrilateral(Coordinate[] ring) { |
| 96 | + Coordinate[] ringCW = CoordinateArrays.orient(ring, true); |
| 97 | + |
| 98 | + double diameter = CoordinateArrays.envelope(ringCW).getDiameter(); |
| 99 | + |
| 100 | + //-- compute corner bisectors |
| 101 | + LineSegment[] bisector = computeBisectors(ringCW, diameter); |
| 102 | + //-- compute nodes and find interior one farthest from sides |
| 103 | + double maxDist = -1; |
| 104 | + Coordinate center = null; |
| 105 | + Coordinate radius = null; |
| 106 | + for (int i = 0; i < 4; i++) { |
| 107 | + LineSegment b1 = bisector[i]; |
| 108 | + int i2 = (i + 1) % 4; |
| 109 | + LineSegment b2 = bisector[i2]; |
| 110 | + |
| 111 | + Coordinate nodePt = b1.intersection(b2); |
| 112 | + |
| 113 | + //-- only interior nodes are considered |
| 114 | + if (! isPointInConvexRing(ringCW, nodePt)) { |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + //-- check if node is further than current max center |
| 119 | + Coordinate r = nearestEdgePt(ringCW, nodePt); |
| 120 | + double dist = nodePt.distance(r); |
| 121 | + if (maxDist < 0 || dist > maxDist) { |
| 122 | + center = nodePt; |
| 123 | + radius = r; |
| 124 | + maxDist = dist; |
| 125 | + //System.out.println(WKTWriter.toLineString(center, radius)); |
| 126 | + } |
| 127 | + } |
| 128 | + return new Coordinate[] { center, radius }; |
| 129 | + } |
| 130 | + |
| 131 | + private static LineSegment[] computeBisectors(Coordinate[] ptsCW, double diameter) { |
| 132 | + LineSegment[] bisector = new LineSegment[4]; |
| 133 | + for (int i = 0; i < 4; i++) { |
| 134 | + bisector[i] = computeConvexBisector(ptsCW, i, diameter); |
| 135 | + } |
| 136 | + return bisector; |
| 137 | + } |
| 138 | + |
| 139 | + private static Coordinate nearestEdgePt(Coordinate[] ring, Coordinate pt) { |
| 140 | + Coordinate nearestPt = null; |
| 141 | + double minDist = -1; |
| 142 | + for (int i = 0; i < ring.length - 1; i++) { |
| 143 | + LineSegment edge = new LineSegment(ring[i], ring[i + 1]); |
| 144 | + Coordinate r = edge.closestPoint(pt); |
| 145 | + double dist = pt.distance(r); |
| 146 | + if (minDist < 0 || dist < minDist) { |
| 147 | + minDist = dist; |
| 148 | + nearestPt = r; |
| 149 | + } |
| 150 | + } |
| 151 | + return nearestPt; |
| 152 | + } |
| 153 | + |
| 154 | + private static LineSegment computeConvexBisector(Coordinate[] pts, int index, double len) { |
| 155 | + Coordinate basePt = pts[index]; |
| 156 | + int iPrev = index == 0 ? pts.length - 2 : index - 1; |
| 157 | + int iNext = index >= pts.length ? 0 : index + 1; |
| 158 | + Coordinate pPrev = pts[iPrev]; |
| 159 | + Coordinate pNext = pts[iNext]; |
| 160 | + if (! isConvex(pPrev, basePt, pNext)) |
| 161 | + throw new IllegalArgumentException("Input is not convex"); |
| 162 | + double bisectAng = Angle.bisector(pPrev, basePt, pNext); |
| 163 | + Coordinate endPt = Angle.project(basePt, bisectAng, len); |
| 164 | + return new LineSegment(basePt.copy(), endPt); |
| 165 | + } |
| 166 | + |
| 167 | + private static boolean isConvex(Polygon polygon) { |
| 168 | + LinearRing shell = polygon.getExteriorRing(); |
| 169 | + return isConvex(shell.getCoordinateSequence()); |
| 170 | + } |
| 171 | + |
| 172 | + private static boolean isConvex(CoordinateSequence ring) { |
| 173 | + /** |
| 174 | + * A ring cannot be all concave, so if it has a consistent |
| 175 | + * orientation it must be convex. |
| 176 | + */ |
| 177 | + int n = ring.size(); |
| 178 | + if (n < 4) |
| 179 | + return false; |
| 180 | + int ringOrient = 0; |
| 181 | + for (int i = 0; i < n - 1; i++) { |
| 182 | + int i1 = i + 1; |
| 183 | + int i2 = (i1 >= n - 1) ? 1 : i1 + 1; |
| 184 | + int orient = Orientation.index(ring.getCoordinate(i), |
| 185 | + ring.getCoordinate(i1), ring.getCoordinate(i2)); |
| 186 | + if (orient == Orientation.COLLINEAR) |
| 187 | + continue; |
| 188 | + if (ringOrient == 0) { |
| 189 | + ringOrient = orient; |
| 190 | + } |
| 191 | + else if (orient != ringOrient) { |
| 192 | + return false; |
| 193 | + } |
| 194 | + } |
| 195 | + return true; |
| 196 | + } |
| 197 | + |
| 198 | + private static boolean isConvex(Coordinate p0, Coordinate p1, Coordinate p2) { |
| 199 | + return Orientation.CLOCKWISE == Orientation.index(p0, p1, p2); |
| 200 | + } |
| 201 | + |
| 202 | + private static boolean isPointInConvexRing(Coordinate[] ringCW, Coordinate p) { |
| 203 | + for (int i = 0; i < ringCW.length - 1; i++) { |
| 204 | + Coordinate p0 = ringCW[i]; |
| 205 | + Coordinate p1 = ringCW[i + 1]; |
| 206 | + int orient = Orientation.index(p0, p1, p); |
| 207 | + if (orient == Orientation.COUNTERCLOCKWISE) |
| 208 | + return false; |
| 209 | + } |
| 210 | + return true; |
| 211 | + } |
| 212 | +} |
0 commit comments