Class SegmentNode

 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 java.io.PrintStream;
15  
16 import org.locationtech.jts.geom.Coordinate;
17  
18 /**
19  * Represents an intersection point between two {@link SegmentString}s.
20  *
21  * @version 1.7
22  */
23 public class SegmentNode
24     implements Comparable
25 {
26   private final NodedSegmentString segString;
27   public final Coordinate coord;   // the point of intersection
28   public final int segmentIndex;   // the index of the containing line segment in the parent edge
29   private final int segmentOctant;
30   private final boolean isInterior;
31  
32   public SegmentNode(NodedSegmentString segString, Coordinate coord, int segmentIndex, int segmentOctant) {
33     this.segString = segString;
34     this.coord = new Coordinate(coord);
35     this.segmentIndex = segmentIndex;
36     this.segmentOctant = segmentOctant;
37     isInterior = ! coord.equals2D(segString.getCoordinate(segmentIndex));
38   }
39  
40   /**
41    * Gets the {@link Coordinate} giving the location of this node.
42    * 
43    * @return the coordinate of the node
44    */
45   public Coordinate getCoordinate() 
46   {
47     return coord;
48   }
49   
50   public boolean isInterior() { return isInterior; }
51  
52   public boolean isEndPoint(int maxSegmentIndex)
53   {
54     if (segmentIndex == 0 && ! isInterior) return true;
55     if (segmentIndex == maxSegmentIndex) return true;
56     return false;
57   }
58  
59   /**
60    * @return -1 this SegmentNode is located before the argument location;
61    * 0 this SegmentNode is at the argument location;
62    * 1 this SegmentNode is located after the argument location
63    */
64   public int compareTo(Object obj)
65   {
66     SegmentNode other = (SegmentNode) obj;
67  
68     if (segmentIndex < other.segmentIndex) return -1;
69     if (segmentIndex > other.segmentIndex) return 1;
70  
71     if (coord.equals2D(other.coord)) return 0;
72  
73     // an exterior node is the segment start point, so always sorts first
74     // this guards against a robustness problem where the octants are not reliable
75     if (! isInterior) return -1;
76     if (! other.isInterior) return 1;
77     
78     return SegmentPointComparator.compare(segmentOctant, coord, other.coord);
79     //return segment.compareNodePosition(this, other);
80   }
81  
82   public void print(PrintStream out)
83   {
84     out.print(coord);
85     out.print(" seg # = " + segmentIndex);
86   }
87   
88   public String toString() {
89     return segmentIndex + ":" + coord.toString();
90   }
91 }
92