Class Segment

  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  
 13 package org.locationtech.jts.triangulate;
 14  
 15 import org.locationtech.jts.geom.Coordinate;
 16 import org.locationtech.jts.geom.LineSegment;
 17  
 18 /**
 19  * Models a constraint segment in a triangulation.
 20  * A constraint segment is an oriented straight line segment between a start point
 21  * and an end point.
 22  * 
 23  * @author David Skea
 24  * @author Martin Davis
 25  */
 26 public class Segment 
 27 {
 28     private LineSegment ls;
 29     private Object data = null;
 30  
 31     /** 
 32      * Creates a new instance for the given ordinates.
 33      */
 34     public Segment(double x1, double y1, double z1, double x2, double y2, double z2) {
 35       this(new Coordinate(x1, y1, z1), new Coordinate(x2, y2, z2));
 36     }
 37  
 38     /** 
 39      * Creates a new instance for the given ordinates,  with associated external data. 
 40      */
 41     public Segment(double x1, double y1, double z1, double x2, double y2, double z2, Object data) {
 42       this(new Coordinate(x1, y1, z1), new Coordinate(x2, y2, z2), data);
 43     }
 44  
 45     /** 
 46      * Creates a new instance for the given points, with associated external data.
 47      * 
 48      * @param p0 the start point
 49      * @param p1 the end point
 50      * @param data an external data object
 51      */
 52     public Segment(Coordinate p0, Coordinate p1, Object data) {
 53         ls = new LineSegment(p0, p1);
 54         this.data = data;
 55     }
 56  
 57     /** 
 58      * Creates a new instance for the given points.
 59      * 
 60      * @param p0 the start point
 61      * @param p1 the end point
 62      */
 63     public Segment(Coordinate p0, Coordinate p1) {
 64         ls = new LineSegment(p0, p1);
 65     }
 66  
 67     /**
 68      * Gets the start coordinate of the segment
 69      * 
 70      * @return a Coordinate
 71      */
 72     public Coordinate getStart() {
 73         return ls.getCoordinate(0);
 74     }
 75  
 76     /**
 77      * Gets the end coordinate of the segment
 78      * 
 79      * @return a Coordinate
 80      */
 81     public Coordinate getEnd() {
 82         return ls.getCoordinate(1);
 83     }
 84  
 85     /**
 86      * Gets the start X ordinate of the segment
 87      * 
 88      * @return the X ordinate value
 89      */
 90     public double getStartX() {
 91         Coordinate p = ls.getCoordinate(0);
 92         return p.x;
 93     }
 94  
 95     /**
 96      * Gets the start Y ordinate of the segment
 97      * 
 98      * @return the Y ordinate value
 99      */
100     public double getStartY() {
101         Coordinate p = ls.getCoordinate(0);
102         return p.y;
103     }
104  
105     /**
106      * Gets the start Z ordinate of the segment
107      * 
108      * @return the Z ordinate value
109      */
110     public double getStartZ() {
111         Coordinate p = ls.getCoordinate(0);
112         return p.getZ();
113     }
114  
115     /**
116      * Gets the end X ordinate of the segment
117      * 
118      * @return the X ordinate value
119      */
120     public double getEndX() {
121         Coordinate p = ls.getCoordinate(1);
122         return p.x;
123     }
124  
125     /**
126      * Gets the end Y ordinate of the segment
127      * 
128      * @return the Y ordinate value
129      */
130     public double getEndY() {
131         Coordinate p = ls.getCoordinate(1);
132         return p.y;
133     }
134  
135     /**
136      * Gets the end Z ordinate of the segment
137      * 
138      * @return the Z ordinate value
139      */
140     public double getEndZ() {
141         Coordinate p = ls.getCoordinate(1);
142         return p.getZ();
143     }
144  
145     /**
146      * Gets a <tt>LineSegment</tt> modelling this segment.
147      * 
148      * @return a LineSegment
149      */
150     public LineSegment getLineSegment() {
151         return ls;
152     }
153  
154     /**
155      * Gets the external data associated with this segment
156      * 
157      * @return a data object
158      */
159     public Object getData() {
160         return data;
161     }
162     
163     /**
164      * Sets the external data to be associated with this segment
165      * 
166      * @param data a data object
167      */
168     public void setData(Object data) {
169         this.data = data;
170     }
171  
172     /**
173      * Determines whether two segments are topologically equal.
174      * I.e. equal up to orientation.
175      * 
176      * @param s a segment
177      * @return true if the segments are topologically equal
178      */
179     public boolean equalsTopo(Segment s) {
180         return ls.equalsTopo(s.getLineSegment());
181     }
182  
183     /**
184      * Computes the intersection point between this segment and another one.
185      * 
186      * @param s a segment
187      * @return the intersection point, or <code>null</code> if there is none
188      */
189     public Coordinate intersection(Segment s) {
190         return ls.intersection(s.getLineSegment());
191     }
192  
193     /**
194      * Computes a string representation of this segment.
195      * 
196      * @return a string
197      */
198     public String toString() {
199         return ls.toString();
200     }
201 }
202