Class OrientedCoordinateArray

  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.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     // MD - testing only
 68     int oldComp = SegmentStringDissolver.ptsComp.compare(pts, oca.pts);
 69     if ((oldComp == 0 || comp == 0) && oldComp != comp) {
 70       System.out.println("bidir mismatch");
 71
 72       boolean orient1 = orientation(pts);
 73       boolean orient2 = orientation(oca.pts);
 74       int comp2 = compareOriented(pts, orientation,
 75                                oca.pts, oca.orientation);
 76       int oldComp2 = SegmentStringDissolver.ptsComp.compare(pts, oca.pts);
 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