| 1 | // License: GPL. For details, see LICENSE file. |
| 2 | package org.openstreetmap.josm.tools; |
| 3 | |
| 4 | import java.awt.Shape; |
| 5 | import java.awt.geom.Path2D; |
| 6 | import java.awt.geom.PathIterator; |
| 7 | import java.awt.geom.Rectangle2D; |
| 8 | import java.util.Arrays; |
| 9 | |
| 10 | /** |
| 11 | * Tools to clip a shape based on the Sutherland-Hodgman algorithm. |
| 12 | * See https://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm |
| 13 | * @author Gerd Petermann |
| 14 | * |
| 15 | */ |
| 16 | public final class ShapeClipper { |
| 17 | private static final int LEFT = 0; |
| 18 | private static final int TOP = 1; |
| 19 | private static final int RIGHT = 2; |
| 20 | private static final int BOTTOM = 3; |
| 21 | |
| 22 | private ShapeClipper() { |
| 23 | // Hide default constructor for utils classes |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Clip a given (closed) shape with a given rectangle. |
| 28 | * @param shape the subject shape to clip |
| 29 | * @param clippingRect the clipping rectangle |
| 30 | * @return the intersection of the shape and the rectangle |
| 31 | * or null if they don't intersect or the shape is not closed. |
| 32 | * The intersection may contain dangling edges. |
| 33 | */ |
| 34 | public static Path2D.Double clipShape(Shape shape, Rectangle2D clippingRect) { |
| 35 | Path2D.Double result = null; |
| 36 | double minX, minY, maxX, maxY; |
| 37 | int num = 0; |
| 38 | minX = minY = Double.POSITIVE_INFINITY; |
| 39 | maxX = maxY = Double.NEGATIVE_INFINITY; |
| 40 | |
| 41 | PathIterator pit = shape.getPathIterator(null); |
| 42 | double[] points = new double[512]; |
| 43 | double[] res = new double[8]; |
| 44 | while (true) { |
| 45 | int type = pit.currentSegment(res); |
| 46 | if (num > 0 && (type == PathIterator.SEG_CLOSE || type == PathIterator.SEG_MOVETO || pit.isDone())) { |
| 47 | // we have extracted a single segment, maybe unclosed |
| 48 | Path2D.Double segment = null; |
| 49 | if (clippingRect.contains(minX, minY) && clippingRect.contains(maxX, maxY)) { |
| 50 | // all points are inside clipping rectangle |
| 51 | segment = pointsToPath2D(points, num); |
| 52 | } else { |
| 53 | Rectangle2D.Double bbox = new Rectangle2D.Double(minX, minY, maxX - minX, maxY - minY); |
| 54 | segment = clipSinglePathWithSutherlandHodgman(points, num, clippingRect, bbox); |
| 55 | } |
| 56 | if (segment != null) { |
| 57 | if (type == PathIterator.SEG_CLOSE) |
| 58 | segment.closePath(); |
| 59 | if (result == null) |
| 60 | result = segment; |
| 61 | else |
| 62 | result.append(segment, false); |
| 63 | } |
| 64 | if (pit.isDone()) |
| 65 | break; |
| 66 | num = 0; |
| 67 | minX = minY = Double.POSITIVE_INFINITY; |
| 68 | maxX = maxY = Double.NEGATIVE_INFINITY; |
| 69 | } |
| 70 | double x = res[0]; |
| 71 | double y = res[1]; |
| 72 | if (x < minX) |
| 73 | minX = x; |
| 74 | if (x > maxX) |
| 75 | maxX = x; |
| 76 | if (y < minY) |
| 77 | minY = y; |
| 78 | if (y > maxY) |
| 79 | maxY = y; |
| 80 | if (type == PathIterator.SEG_LINETO || type == PathIterator.SEG_MOVETO) { |
| 81 | if (num + 2 >= points.length) { |
| 82 | points = Arrays.copyOf(points, points.length * 2); |
| 83 | } |
| 84 | points[num++] = x; |
| 85 | points[num++] = y; |
| 86 | } else if (type != PathIterator.SEG_CLOSE) { |
| 87 | //Logging.warn("unhandled path iterator"); |
| 88 | } |
| 89 | pit.next(); |
| 90 | } |
| 91 | |
| 92 | return result; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Convert a list of points to a Path2D.Double |
| 97 | * @param points the pairs |
| 98 | * @param num the number of valid values in points |
| 99 | * @return the path or null if the path describes a point or line. |
| 100 | */ |
| 101 | private static Path2D.Double pointsToPath2D(double[] points, int num) { |
| 102 | if (num < 2) |
| 103 | return null; |
| 104 | if (points[0] == points[num - 2] && points[1] == points[num - 1]) |
| 105 | num -= 2; |
| 106 | if (num < 6) |
| 107 | return null; |
| 108 | Path2D.Double path = new Path2D.Double(); |
| 109 | double lastX = points[0], lastY = points[1]; |
| 110 | path.moveTo(lastX, lastY); |
| 111 | int numOut = 1; |
| 112 | for (int i = 2; i < num;) { |
| 113 | double x = points[i++], y = points[i++]; |
| 114 | if (x != lastX || y != lastY) { |
| 115 | path.lineTo(x, y); |
| 116 | lastX = x; |
| 117 | lastY = y; |
| 118 | ++numOut; |
| 119 | } |
| 120 | } |
| 121 | if (numOut < 3) |
| 122 | return null; |
| 123 | return path; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Clip a single path with a given rectangle using the Sutherland-Hodgman algorithm. This is much faster compared to |
| 128 | * the area.intersect method, but may create dangling edges. |
| 129 | * @param points a list of longitude+latitude pairs |
| 130 | * @param num the number of valid values in points |
| 131 | * @param clippingRect the clipping rectangle |
| 132 | * @param bbox the bounding box of the path |
| 133 | * @return the clipped path as a Path2D.Double or null if the result is empty |
| 134 | */ |
| 135 | private static Path2D.Double clipSinglePathWithSutherlandHodgman(double[] points, int num, Rectangle2D clippingRect, |
| 136 | Rectangle2D.Double bbox) { |
| 137 | if (num <= 2 || !bbox.intersects(clippingRect)) { |
| 138 | return null; |
| 139 | } |
| 140 | |
| 141 | int countVals = num; |
| 142 | if (points[0] == points[num - 2] && points[1] == points[num - 1]) { |
| 143 | countVals -= 2; |
| 144 | } |
| 145 | double[] outputList = points; |
| 146 | double[] input; |
| 147 | |
| 148 | double leftX = clippingRect.getMinX(); |
| 149 | double rightX = clippingRect.getMaxX(); |
| 150 | double lowerY = clippingRect.getMinY(); |
| 151 | double upperY = clippingRect.getMaxY(); |
| 152 | boolean eIsIn = false, sIsIn = false; |
| 153 | for (int side = LEFT; side <= BOTTOM; side++) { |
| 154 | if (countVals < 6) |
| 155 | return null; // ignore point or line |
| 156 | |
| 157 | boolean skipTestForThisSide; |
| 158 | switch (side) { |
| 159 | case LEFT: |
| 160 | skipTestForThisSide = (bbox.getMinX() >= leftX); |
| 161 | break; |
| 162 | case TOP: |
| 163 | skipTestForThisSide = (bbox.getMaxY() < upperY); |
| 164 | break; |
| 165 | case RIGHT: |
| 166 | skipTestForThisSide = (bbox.getMaxX() < rightX); |
| 167 | break; |
| 168 | default: |
| 169 | skipTestForThisSide = (bbox.getMinY() >= lowerY); |
| 170 | } |
| 171 | if (skipTestForThisSide) |
| 172 | continue; |
| 173 | |
| 174 | input = outputList; |
| 175 | outputList = new double[countVals + 16]; |
| 176 | double sLon = 0, sLat = 0; |
| 177 | double pLon = 0, pLat = 0; // intersection |
| 178 | int posIn = countVals - 2; |
| 179 | int posOut = 0; |
| 180 | for (int i = 0; i < countVals + 2; i += 2) { |
| 181 | if (posIn >= countVals) |
| 182 | posIn = 0; |
| 183 | double eLon = input[posIn++]; |
| 184 | double eLat = input[posIn++]; |
| 185 | switch (side) { |
| 186 | case LEFT: |
| 187 | eIsIn = (eLon >= leftX); |
| 188 | break; |
| 189 | case TOP: |
| 190 | eIsIn = (eLat < upperY); |
| 191 | break; |
| 192 | case RIGHT: |
| 193 | eIsIn = (eLon < rightX); |
| 194 | break; |
| 195 | default: |
| 196 | eIsIn = (eLat >= lowerY); |
| 197 | } |
| 198 | if (i > 0) { |
| 199 | if (eIsIn != sIsIn) { |
| 200 | // compute intersection |
| 201 | double slope; |
| 202 | if (eLon != sLon) |
| 203 | slope = (eLat - sLat) / (eLon - sLon); |
| 204 | else |
| 205 | slope = 1; |
| 206 | |
| 207 | switch (side) { |
| 208 | case LEFT: |
| 209 | pLon = leftX; |
| 210 | pLat = slope * (leftX - sLon) + sLat; |
| 211 | break; |
| 212 | case RIGHT: |
| 213 | pLon = rightX; |
| 214 | pLat = slope * (rightX - sLon) + sLat; |
| 215 | break; |
| 216 | |
| 217 | case TOP: |
| 218 | if (eLon != sLon) |
| 219 | pLon = sLon + (upperY - sLat) / slope; |
| 220 | else |
| 221 | pLon = sLon; |
| 222 | pLat = upperY; |
| 223 | break; |
| 224 | default: // BOTTOM |
| 225 | if (eLon != sLon) |
| 226 | pLon = sLon + (lowerY - sLat) / slope; |
| 227 | else |
| 228 | pLon = sLon; |
| 229 | pLat = lowerY; |
| 230 | break; |
| 231 | |
| 232 | } |
| 233 | } |
| 234 | int toAdd = 0; |
| 235 | if (eIsIn) { |
| 236 | if (!sIsIn) { |
| 237 | toAdd += 2; |
| 238 | } |
| 239 | toAdd += 2; |
| 240 | } else { |
| 241 | if (sIsIn) { |
| 242 | toAdd += 2; |
| 243 | } |
| 244 | } |
| 245 | if (posOut + toAdd >= outputList.length) { |
| 246 | // unlikely |
| 247 | outputList = Arrays.copyOf(outputList, outputList.length * 2); |
| 248 | } |
| 249 | if (eIsIn) { |
| 250 | if (!sIsIn) { |
| 251 | outputList[posOut++] = pLon; |
| 252 | outputList[posOut++] = pLat; |
| 253 | } |
| 254 | outputList[posOut++] = eLon; |
| 255 | outputList[posOut++] = eLat; |
| 256 | } else { |
| 257 | if (sIsIn) { |
| 258 | outputList[posOut++] = pLon; |
| 259 | outputList[posOut++] = pLat; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | // S = E |
| 264 | sLon = eLon; |
| 265 | sLat = eLat; |
| 266 | sIsIn = eIsIn; |
| 267 | } |
| 268 | countVals = posOut; |
| 269 | } |
| 270 | return pointsToPath2D(outputList, countVals); |
| 271 | } |
| 272 | } |