5
* The JTS Topology Suite is a collection of Java classes that
6
* implement the fundamental operations required to validate a given
7
* geo-spatial data set to a known topological specification.
9
* Copyright (C) 2001 Vivid Solutions
11
* This library is free software; you can redistribute it and/or
12
* modify it under the terms of the GNU Lesser General Public
13
* License as published by the Free Software Foundation; either
14
* version 2.1 of the License, or (at your option) any later version.
16
* This library is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
* Lesser General Public License for more details.
21
* You should have received a copy of the GNU Lesser General Public
22
* License along with this library; if not, write to the Free Software
23
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
* For more information, contact:
29
* 2328 Government Street
34
* www.vividsolutions.com
36
package com.vividsolutions.jts.geomgraph;
38
import java.io.PrintStream;
39
import com.vividsolutions.jts.util.*;
40
import com.vividsolutions.jts.geom.*;
46
public class DirectedEdge
51
* Computes the factor for the change in depth when moving from one location to another.
52
* E.g. if crossing from the INTERIOR to the EXTERIOR the depth decreases, so the factor is -1
54
public static int depthFactor(int currLocation, int nextLocation)
56
if (currLocation == Location.EXTERIOR && nextLocation == Location.INTERIOR)
58
else if (currLocation == Location.INTERIOR && nextLocation == Location.EXTERIOR)
63
protected boolean isForward;
64
private boolean isInResult = false;
65
private boolean isVisited = false;
67
private DirectedEdge sym; // the symmetric edge
68
private DirectedEdge next; // the next edge in the edge ring for the polygon containing this edge
69
private DirectedEdge nextMin; // the next edge in the MinimalEdgeRing that contains this edge
70
private EdgeRing edgeRing; // the EdgeRing that this edge is part of
71
private EdgeRing minEdgeRing; // the MinimalEdgeRing that this edge is part of
73
* The depth of each side (position) of this edge.
74
* The 0 element of the array is never used.
76
private int[] depth = { 0, -999, -999 };
78
public DirectedEdge(Edge edge, boolean isForward)
81
this.isForward = isForward;
83
init(edge.getCoordinate(0), edge.getCoordinate(1));
86
int n = edge.getNumPoints() - 1;
87
init(edge.getCoordinate(n), edge.getCoordinate(n-1));
89
computeDirectedLabel();
91
public Edge getEdge() { return edge; }
92
public void setInResult(boolean isInResult) { this.isInResult = isInResult; }
93
public boolean isInResult() { return isInResult; }
94
public boolean isVisited() { return isVisited; }
95
public void setVisited(boolean isVisited) { this.isVisited = isVisited; }
96
public void setEdgeRing(EdgeRing edgeRing) { this.edgeRing = edgeRing; }
97
public EdgeRing getEdgeRing() { return edgeRing; }
98
public void setMinEdgeRing(EdgeRing minEdgeRing) { this.minEdgeRing = minEdgeRing; }
99
public EdgeRing getMinEdgeRing() { return minEdgeRing; }
100
public int getDepth(int position) { return depth[position]; }
102
public void setDepth(int position, int depthVal)
104
if (depth[position] != -999) {
105
if (depth[position] != depthVal) {
108
if (depth[position] != depthVal)
109
throw new TopologyException("assigned depths do not match", getCoordinate());
110
//Assert.isTrue(depth[position] == depthVal, "assigned depths do not match at " + getCoordinate());
112
depth[position] = depthVal;
115
public int getDepthDelta()
117
int depthDelta = edge.getDepthDelta();
118
if (! isForward) depthDelta = -depthDelta;
123
* setVisitedEdge marks both DirectedEdges attached to a given Edge.
124
* This is used for edges corresponding to lines, which will only
125
* appear oriented in a single direction in the result.
127
public void setVisitedEdge(boolean isVisited)
129
setVisited(isVisited);
130
sym.setVisited(isVisited);
133
* Each Edge gives rise to a pair of symmetric DirectedEdges, in opposite
135
* @return the DirectedEdge for the same Edge but in the opposite direction
137
public DirectedEdge getSym() { return sym; }
138
public boolean isForward() { return isForward; }
139
public void setSym(DirectedEdge de)
143
public DirectedEdge getNext() { return next; }
144
public void setNext(DirectedEdge next) { this.next = next; }
145
public DirectedEdge getNextMin() { return nextMin; }
146
public void setNextMin(DirectedEdge nextMin) { this.nextMin = nextMin; }
149
* This edge is a line edge if
151
* <li> at least one of the labels is a line label
152
* <li> any labels which are not line labels have all Locations = EXTERIOR
155
public boolean isLineEdge()
157
boolean isLine = label.isLine(0) || label.isLine(1);
158
boolean isExteriorIfArea0 =
159
! label.isArea(0) || label.allPositionsEqual(0, Location.EXTERIOR);
160
boolean isExteriorIfArea1 =
161
! label.isArea(1) || label.allPositionsEqual(1, Location.EXTERIOR);
163
return isLine && isExteriorIfArea0 && isExteriorIfArea1;
166
* This is an interior Area edge if
168
* <li> its label is an Area label for both Geometries
169
* <li> and for each Geometry both sides are in the interior.
172
* @return true if this is an interior Area edge
174
public boolean isInteriorAreaEdge()
176
boolean isInteriorAreaEdge = true;
177
for (int i = 0; i < 2; i++) {
178
if (! ( label.isArea(i)
179
&& label.getLocation(i, Position.LEFT ) == Location.INTERIOR
180
&& label.getLocation(i, Position.RIGHT) == Location.INTERIOR) ) {
181
isInteriorAreaEdge = false;
184
return isInteriorAreaEdge;
188
* Compute the label in the appropriate orientation for this DirEdge
190
private void computeDirectedLabel()
192
label = new Label(edge.getLabel());
198
* Set both edge depths. One depth for a given side is provided. The other is
199
* computed depending on the Location transition and the depthDelta of the edge.
201
public void setEdgeDepths(int position, int depth)
203
// get the depth transition delta from R to L for this directed Edge
204
int depthDelta = getEdge().getDepthDelta();
205
if (! isForward) depthDelta = -depthDelta;
207
// if moving from L to R instead of R to L must change sign of delta
208
int directionFactor = 1;
209
if (position == Position.LEFT)
210
directionFactor = -1;
212
int oppositePos = Position.opposite(position);
213
int delta = depthDelta * directionFactor;
214
//TESTINGint delta = depthDelta * DirectedEdge.depthFactor(loc, oppositeLoc);
215
int oppositeDepth = depth + delta;
216
setDepth(position, depth);
217
setDepth(oppositePos, oppositeDepth);
220
public void print(PrintStream out)
223
out.print(" " + depth[Position.LEFT] + "/" + depth[Position.RIGHT]);
224
out.print(" (" + getDepthDelta() + ")");
225
//out.print(" " + this.hashCode());
226
//if (next != null) out.print(" next:" + next.hashCode());
227
if (isInResult) out.print(" inResult");
229
public void printEdge(PrintStream out)
236
edge.printReverse(out);