001:
002: /*
003: * The JTS Topology Suite is a collection of Java classes that
004: * implement the fundamental operations required to validate a given
005: * geo-spatial data set to a known topological specification.
006: *
007: * Copyright (C) 2001 Vivid Solutions
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: *
023: * For more information, contact:
024: *
025: * Vivid Solutions
026: * Suite #1A
027: * 2328 Government Street
028: * Victoria BC V8T 5G5
029: * Canada
030: *
031: * (250)385-6040
032: * www.vividsolutions.com
033: */
034: package com.vividsolutions.jts.geomgraph;
035:
036: import java.io.PrintStream;
037: import com.vividsolutions.jts.util.*;
038: import com.vividsolutions.jts.geom.*;
039:
040: /**
041: * @version 1.7
042: */
043: public class DirectedEdge extends EdgeEnd {
044:
045: /**
046: * Computes the factor for the change in depth when moving from one location to another.
047: * E.g. if crossing from the INTERIOR to the EXTERIOR the depth decreases, so the factor is -1
048: */
049: public static int depthFactor(int currLocation, int nextLocation) {
050: if (currLocation == Location.EXTERIOR
051: && nextLocation == Location.INTERIOR)
052: return 1;
053: else if (currLocation == Location.INTERIOR
054: && nextLocation == Location.EXTERIOR)
055: return -1;
056: return 0;
057: }
058:
059: protected boolean isForward;
060: private boolean isInResult = false;
061: private boolean isVisited = false;
062:
063: private DirectedEdge sym; // the symmetric edge
064: private DirectedEdge next; // the next edge in the edge ring for the polygon containing this edge
065: private DirectedEdge nextMin; // the next edge in the MinimalEdgeRing that contains this edge
066: private EdgeRing edgeRing; // the EdgeRing that this edge is part of
067: private EdgeRing minEdgeRing; // the MinimalEdgeRing that this edge is part of
068: /**
069: * The depth of each side (position) of this edge.
070: * The 0 element of the array is never used.
071: */
072: private int[] depth = { 0, -999, -999 };
073:
074: public DirectedEdge(Edge edge, boolean isForward) {
075: super (edge);
076: this .isForward = isForward;
077: if (isForward) {
078: init(edge.getCoordinate(0), edge.getCoordinate(1));
079: } else {
080: int n = edge.getNumPoints() - 1;
081: init(edge.getCoordinate(n), edge.getCoordinate(n - 1));
082: }
083: computeDirectedLabel();
084: }
085:
086: public Edge getEdge() {
087: return edge;
088: }
089:
090: public void setInResult(boolean isInResult) {
091: this .isInResult = isInResult;
092: }
093:
094: public boolean isInResult() {
095: return isInResult;
096: }
097:
098: public boolean isVisited() {
099: return isVisited;
100: }
101:
102: public void setVisited(boolean isVisited) {
103: this .isVisited = isVisited;
104: }
105:
106: public void setEdgeRing(EdgeRing edgeRing) {
107: this .edgeRing = edgeRing;
108: }
109:
110: public EdgeRing getEdgeRing() {
111: return edgeRing;
112: }
113:
114: public void setMinEdgeRing(EdgeRing minEdgeRing) {
115: this .minEdgeRing = minEdgeRing;
116: }
117:
118: public EdgeRing getMinEdgeRing() {
119: return minEdgeRing;
120: }
121:
122: public int getDepth(int position) {
123: return depth[position];
124: }
125:
126: public void setDepth(int position, int depthVal) {
127: if (depth[position] != -999) {
128: // if (depth[position] != depthVal) {
129: // Debug.print(this);
130: // }
131: if (depth[position] != depthVal)
132: throw new TopologyException(
133: "assigned depths do not match", getCoordinate());
134: //Assert.isTrue(depth[position] == depthVal, "assigned depths do not match at " + getCoordinate());
135: }
136: depth[position] = depthVal;
137: }
138:
139: public int getDepthDelta() {
140: int depthDelta = edge.getDepthDelta();
141: if (!isForward)
142: depthDelta = -depthDelta;
143: return depthDelta;
144: }
145:
146: /**
147: * setVisitedEdge marks both DirectedEdges attached to a given Edge.
148: * This is used for edges corresponding to lines, which will only
149: * appear oriented in a single direction in the result.
150: */
151: public void setVisitedEdge(boolean isVisited) {
152: setVisited(isVisited);
153: sym.setVisited(isVisited);
154: }
155:
156: /**
157: * Each Edge gives rise to a pair of symmetric DirectedEdges, in opposite
158: * directions.
159: * @return the DirectedEdge for the same Edge but in the opposite direction
160: */
161: public DirectedEdge getSym() {
162: return sym;
163: }
164:
165: public boolean isForward() {
166: return isForward;
167: }
168:
169: public void setSym(DirectedEdge de) {
170: sym = de;
171: }
172:
173: public DirectedEdge getNext() {
174: return next;
175: }
176:
177: public void setNext(DirectedEdge next) {
178: this .next = next;
179: }
180:
181: public DirectedEdge getNextMin() {
182: return nextMin;
183: }
184:
185: public void setNextMin(DirectedEdge nextMin) {
186: this .nextMin = nextMin;
187: }
188:
189: /**
190: * This edge is a line edge if
191: * <ul>
192: * <li> at least one of the labels is a line label
193: * <li> any labels which are not line labels have all Locations = EXTERIOR
194: * </ul>
195: */
196: public boolean isLineEdge() {
197: boolean isLine = label.isLine(0) || label.isLine(1);
198: boolean isExteriorIfArea0 = !label.isArea(0)
199: || label.allPositionsEqual(0, Location.EXTERIOR);
200: boolean isExteriorIfArea1 = !label.isArea(1)
201: || label.allPositionsEqual(1, Location.EXTERIOR);
202:
203: return isLine && isExteriorIfArea0 && isExteriorIfArea1;
204: }
205:
206: /**
207: * This is an interior Area edge if
208: * <ul>
209: * <li> its label is an Area label for both Geometries
210: * <li> and for each Geometry both sides are in the interior.
211: * </ul>
212: *
213: * @return true if this is an interior Area edge
214: */
215: public boolean isInteriorAreaEdge() {
216: boolean isInteriorAreaEdge = true;
217: for (int i = 0; i < 2; i++) {
218: if (!(label.isArea(i)
219: && label.getLocation(i, Position.LEFT) == Location.INTERIOR && label
220: .getLocation(i, Position.RIGHT) == Location.INTERIOR)) {
221: isInteriorAreaEdge = false;
222: }
223: }
224: return isInteriorAreaEdge;
225: }
226:
227: /**
228: * Compute the label in the appropriate orientation for this DirEdge
229: */
230: private void computeDirectedLabel() {
231: label = new Label(edge.getLabel());
232: if (!isForward)
233: label.flip();
234: }
235:
236: /**
237: * Set both edge depths. One depth for a given side is provided. The other is
238: * computed depending on the Location transition and the depthDelta of the edge.
239: */
240: public void setEdgeDepths(int position, int depth) {
241: // get the depth transition delta from R to L for this directed Edge
242: int depthDelta = getEdge().getDepthDelta();
243: if (!isForward)
244: depthDelta = -depthDelta;
245:
246: // if moving from L to R instead of R to L must change sign of delta
247: int directionFactor = 1;
248: if (position == Position.LEFT)
249: directionFactor = -1;
250:
251: int oppositePos = Position.opposite(position);
252: int delta = depthDelta * directionFactor;
253: //TESTINGint delta = depthDelta * DirectedEdge.depthFactor(loc, oppositeLoc);
254: int oppositeDepth = depth + delta;
255: setDepth(position, depth);
256: setDepth(oppositePos, oppositeDepth);
257: }
258:
259: public void print(PrintStream out) {
260: super .print(out);
261: out.print(" " + depth[Position.LEFT] + "/"
262: + depth[Position.RIGHT]);
263: out.print(" (" + getDepthDelta() + ")");
264: //out.print(" " + this.hashCode());
265: //if (next != null) out.print(" next:" + next.hashCode());
266: if (isInResult)
267: out.print(" inResult");
268: }
269:
270: public void printEdge(PrintStream out) {
271: print(out);
272: out.print(" ");
273: if (isForward)
274: edge.print(out);
275: else
276: edge.printReverse(out);
277: }
278:
279: }
|