| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
package org.locationtech.jts.noding; |
| 13 |
|
| 14 |
import org.locationtech.jts.geom.Coordinate; |
| 15 |
import org.locationtech.jts.geom.CoordinateArrays; |
| 16 |
|
| 17 |
/** |
| 18 |
* Allows comparing {@link Coordinate} arrays |
| 19 |
* in an orientation-independent way. |
| 20 |
* |
| 21 |
* @author Martin Davis |
| 22 |
* @version 1.7 |
| 23 |
*/ |
| 24 |
public class OrientedCoordinateArray |
| 25 |
implements Comparable |
| 26 |
{ |
| 27 |
private Coordinate[] pts; |
| 28 |
private boolean orientation; |
| 29 |
|
| 30 |
/** |
| 31 |
* Creates a new {@link OrientedCoordinateArray} |
| 32 |
* for the given {@link Coordinate} array. |
| 33 |
* |
| 34 |
* @param pts the coordinates to orient |
| 35 |
*/ |
| 36 |
public OrientedCoordinateArray(Coordinate[] pts) |
| 37 |
{ |
| 38 |
this.pts = pts; |
| 39 |
orientation = orientation(pts); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Computes the canonical orientation for a coordinate array. |
| 44 |
* |
| 45 |
* @param pts the array to test |
| 46 |
* @return <code>true</code> if the points are oriented forwards |
| 47 |
* or <code>false</code if the points are oriented in reverse |
| 48 |
*/ |
| 49 |
private static boolean orientation(Coordinate[] pts) |
| 50 |
{ |
| 51 |
return CoordinateArrays.increasingDirection(pts) == 1; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Compares two {@link OrientedCoordinateArray}s for their relative order |
| 56 |
* |
| 57 |
* @return -1 this one is smaller; |
| 58 |
* 0 the two objects are equal; |
| 59 |
* 1 this one is greater |
| 60 |
*/ |
| 61 |
|
| 62 |
public int compareTo(Object o1) { |
| 63 |
OrientedCoordinateArray oca = (OrientedCoordinateArray) o1; |
| 64 |
int comp = compareOriented(pts, orientation, |
| 65 |
oca.pts, oca.orientation); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
return comp; |
| 80 |
} |
| 81 |
|
| 82 |
private static int compareOriented(Coordinate[] pts1, |
| 83 |
boolean orientation1, |
| 84 |
Coordinate[] pts2, |
| 85 |
boolean orientation2) |
| 86 |
{ |
| 87 |
int dir1 = orientation1 ? 1 : -1; |
| 88 |
int dir2 = orientation2 ? 1 : -1; |
| 89 |
int limit1 = orientation1 ? pts1.length : -1; |
| 90 |
int limit2 = orientation2 ? pts2.length : -1; |
| 91 |
|
| 92 |
int i1 = orientation1 ? 0 : pts1.length - 1; |
| 93 |
int i2 = orientation2 ? 0 : pts2.length - 1; |
| 94 |
while (true) { |
| 95 |
int compPt = pts1[i1].compareTo(pts2[i2]); |
| 96 |
if (compPt != 0) |
| 97 |
return compPt; |
| 98 |
i1 += dir1; |
| 99 |
i2 += dir2; |
| 100 |
boolean done1 = i1 == limit1; |
| 101 |
boolean done2 = i2 == limit2; |
| 102 |
if (done1 && ! done2) return -1; |
| 103 |
if (! done1 && done2) return 1; |
| 104 |
if (done1 && done2) return 0; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
} |
| 110 |
|