| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
package org.locationtech.jts.algorithm; |
| 13 |
|
| 14 |
import org.locationtech.jts.geom.Coordinate; |
| 15 |
import org.locationtech.jts.geom.CoordinateSequence; |
| 16 |
|
| 17 |
/** |
| 18 |
* Functions to compute the orientation of basic geometric structures |
| 19 |
* including point triplets (triangles) and rings. |
| 20 |
* Orientation is a fundamental property of planar geometries |
| 21 |
* (and more generally geometry on two-dimensional manifolds). |
| 22 |
* <p> |
| 23 |
* Orientation is notoriously subject to numerical precision errors |
| 24 |
* in the case of collinear or nearly collinear points. |
| 25 |
* JTS uses extended-precision arithmetic to increase |
| 26 |
* the robustness of the computation. |
| 27 |
* |
| 28 |
* @author Martin Davis |
| 29 |
* |
| 30 |
*/ |
| 31 |
public class Orientation { |
| 32 |
/** |
| 33 |
* A value that indicates an orientation of clockwise, or a right turn. |
| 34 |
*/ |
| 35 |
public static final int CLOCKWISE = -1; |
| 36 |
/** |
| 37 |
* A value that indicates an orientation of clockwise, or a right turn. |
| 38 |
*/ |
| 39 |
public static final int RIGHT = CLOCKWISE; |
| 40 |
/** |
| 41 |
* A value that indicates an orientation of counterclockwise, or a left turn. |
| 42 |
*/ |
| 43 |
public static final int COUNTERCLOCKWISE = 1; |
| 44 |
/** |
| 45 |
* A value that indicates an orientation of counterclockwise, or a left turn. |
| 46 |
*/ |
| 47 |
public static final int LEFT = COUNTERCLOCKWISE; |
| 48 |
/** |
| 49 |
* A value that indicates an orientation of collinear, or no turn (straight). |
| 50 |
*/ |
| 51 |
public static final int COLLINEAR = 0; |
| 52 |
/** |
| 53 |
* A value that indicates an orientation of collinear, or no turn (straight). |
| 54 |
*/ |
| 55 |
public static final int STRAIGHT = COLLINEAR; |
| 56 |
|
| 57 |
/** |
| 58 |
* Returns the orientation index of the direction of the point <code>q</code> relative to |
| 59 |
* a directed infinite line specified by <code>p1-p2</code>. |
| 60 |
* The index indicates whether the point lies to the {@link #LEFT} or {@link #RIGHT} |
| 61 |
* of the line, or lies on it {@link #COLLINEAR}. |
| 62 |
* The index also indicates the orientation of the triangle formed by the three points |
| 63 |
* ( {@link #COUNTERCLOCKWISE}, {@link #CLOCKWISE}, or {@link #STRAIGHT} ) |
| 64 |
* |
| 65 |
* @param p1 the origin point of the line vector |
| 66 |
* @param p2 the final point of the line vector |
| 67 |
* @param q the point to compute the direction to |
| 68 |
* |
| 69 |
* @return -1 ( {@link #CLOCKWISE} or {@link #RIGHT} ) if q is clockwise (right) from p1-p2; |
| 70 |
* 1 ( {@link #COUNTERCLOCKWISE} or {@link #LEFT} ) if q is counter-clockwise (left) from p1-p2; |
| 71 |
* 0 ( {@link #COLLINEAR} or {@link #STRAIGHT} ) if q is collinear with p1-p2 |
| 72 |
*/ |
| 73 |
public static int index(Coordinate p1, Coordinate p2, Coordinate q) |
| 74 |
{ |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
return CGAlgorithmsDD.orientationIndex(p1, p2, q); |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Computes whether a ring defined by an array of {@link Coordinate}s is |
| 107 |
* oriented counter-clockwise. |
| 108 |
* <ul> |
| 109 |
* <li>The list of points is assumed to have the first and last points equal. |
| 110 |
* <li>This will handle coordinate lists which contain repeated points. |
| 111 |
* </ul> |
| 112 |
* This algorithm is <b>only</b> guaranteed to work with valid rings. If the |
| 113 |
* ring is invalid (e.g. self-crosses or touches), the computed result may not |
| 114 |
* be correct. |
| 115 |
* |
| 116 |
* @param ring |
| 117 |
* an array of Coordinates forming a ring |
| 118 |
* @return true if the ring is oriented counter-clockwise. |
| 119 |
* @throws IllegalArgumentException |
| 120 |
* if there are too few points to determine orientation (< 4) |
| 121 |
*/ |
| 122 |
public static boolean isCCW(Coordinate[] ring) |
| 123 |
{ |
| 124 |
|
| 125 |
int nPts = ring.length - 1; |
| 126 |
|
| 127 |
if (nPts < 3) |
| 128 |
throw new IllegalArgumentException( |
| 129 |
"Ring has fewer than 4 points, so orientation cannot be determined"); |
| 130 |
|
| 131 |
|
| 132 |
Coordinate hiPt = ring[0]; |
| 133 |
int hiIndex = 0; |
| 134 |
for (int i = 1; i <= nPts; i++) { |
| 135 |
Coordinate p = ring[i]; |
| 136 |
if (p.y > hiPt.y) { |
| 137 |
hiPt = p; |
| 138 |
hiIndex = i; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
int iPrev = hiIndex; |
| 144 |
do { |
| 145 |
iPrev = iPrev - 1; |
| 146 |
if (iPrev < 0) |
| 147 |
iPrev = nPts; |
| 148 |
} while (ring[iPrev].equals2D(hiPt) && iPrev != hiIndex); |
| 149 |
|
| 150 |
|
| 151 |
int iNext = hiIndex; |
| 152 |
do { |
| 153 |
iNext = (iNext + 1) % nPts; |
| 154 |
} while (ring[iNext].equals2D(hiPt) && iNext != hiIndex); |
| 155 |
|
| 156 |
Coordinate prev = ring[iPrev]; |
| 157 |
Coordinate next = ring[iNext]; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
if (prev.equals2D(hiPt) || next.equals2D(hiPt) || prev.equals2D(next)) |
| 166 |
return false; |
| 167 |
|
| 168 |
int disc = Orientation.index(prev, hiPt, next); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
boolean isCCW; |
| 180 |
if (disc == 0) { |
| 181 |
|
| 182 |
isCCW = (prev.x > next.x); |
| 183 |
} |
| 184 |
else { |
| 185 |
|
| 186 |
isCCW = (disc > 0); |
| 187 |
} |
| 188 |
return isCCW; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Computes whether a ring defined by an {@link CoordinateSequence} is |
| 193 |
* oriented counter-clockwise. |
| 194 |
* <ul> |
| 195 |
* <li>The list of points is assumed to have the first and last points equal. |
| 196 |
* <li>This will handle coordinate lists which contain repeated points. |
| 197 |
* </ul> |
| 198 |
* This algorithm is <b>only</b> guaranteed to work with valid rings. If the |
| 199 |
* ring is invalid (e.g. self-crosses or touches), the computed result may not |
| 200 |
* be correct. |
| 201 |
* |
| 202 |
* @param ring |
| 203 |
* a CoordinateSequence forming a ring |
| 204 |
* @return true if the ring is oriented counter-clockwise. |
| 205 |
* @throws IllegalArgumentException |
| 206 |
* if there are too few points to determine orientation (< 4) |
| 207 |
*/ |
| 208 |
public static boolean isCCW(CoordinateSequence ring) |
| 209 |
{ |
| 210 |
|
| 211 |
int nPts = ring.size() - 1; |
| 212 |
|
| 213 |
if (nPts < 3) |
| 214 |
throw new IllegalArgumentException( |
| 215 |
"Ring has fewer than 4 points, so orientation cannot be determined"); |
| 216 |
|
| 217 |
|
| 218 |
Coordinate hiPt = ring.getCoordinate(0); |
| 219 |
int hiIndex = 0; |
| 220 |
for (int i = 1; i <= nPts; i++) { |
| 221 |
Coordinate p = ring.getCoordinate(i); |
| 222 |
if (p.y > hiPt.y) { |
| 223 |
hiPt = p; |
| 224 |
hiIndex = i; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
Coordinate prev; |
| 230 |
int iPrev = hiIndex; |
| 231 |
do { |
| 232 |
iPrev = iPrev - 1; |
| 233 |
if (iPrev < 0) |
| 234 |
iPrev = nPts; |
| 235 |
prev = ring.getCoordinate(iPrev); |
| 236 |
} while (prev.equals2D(hiPt) && iPrev != hiIndex); |
| 237 |
|
| 238 |
|
| 239 |
Coordinate next; |
| 240 |
int iNext = hiIndex; |
| 241 |
do { |
| 242 |
iNext = (iNext + 1) % nPts; |
| 243 |
next = ring.getCoordinate(iNext); |
| 244 |
} while (next.equals2D(hiPt) && iNext != hiIndex); |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
if (prev.equals2D(hiPt) || next.equals2D(hiPt) || prev.equals2D(next)) |
| 253 |
return false; |
| 254 |
|
| 255 |
int disc = Orientation.index(prev, hiPt, next); |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
boolean isCCW; |
| 267 |
if (disc == 0) { |
| 268 |
|
| 269 |
isCCW = (prev.x > next.x); |
| 270 |
} |
| 271 |
else { |
| 272 |
|
| 273 |
isCCW = (disc > 0); |
| 274 |
} |
| 275 |
return isCCW; |
| 276 |
} |
| 277 |
|
| 278 |
} |
| 279 |
|