svn-gvsig-desktop / trunk / libraries / libFMap / src / com / vividsolutions / jts / geomgraph / SnappingPlanarGraph.java @ 9178
History | View | Annotate | Download (6.84 KB)
1 |
/*
|
---|---|
2 |
* Created on 11-sep-2006 by azabala
|
3 |
*
|
4 |
*/
|
5 |
package com.vividsolutions.jts.geomgraph; |
6 |
|
7 |
import java.util.ArrayList; |
8 |
import java.util.Collection; |
9 |
import java.util.Iterator; |
10 |
import java.util.List; |
11 |
|
12 |
import com.vividsolutions.jts.algorithm.CGAlgorithms; |
13 |
import com.vividsolutions.jts.geom.Coordinate; |
14 |
import com.vividsolutions.jts.geom.Location; |
15 |
import com.vividsolutions.jts.geomgraph.DirectedEdgeStar; |
16 |
import com.vividsolutions.jts.geomgraph.Edge; |
17 |
import com.vividsolutions.jts.geomgraph.EdgeEnd; |
18 |
import com.vividsolutions.jts.geomgraph.EdgeEndStar; |
19 |
import com.vividsolutions.jts.geomgraph.Label; |
20 |
import com.vividsolutions.jts.geomgraph.Node; |
21 |
import com.vividsolutions.jts.geomgraph.NodeFactory; |
22 |
import com.vividsolutions.jts.geomgraph.PlanarGraph; |
23 |
import com.vividsolutions.jts.geomgraph.Quadrant; |
24 |
|
25 |
/**
|
26 |
* @author alzabord
|
27 |
*
|
28 |
* TODO To change the template for this generated type comment go to Window -
|
29 |
* Preferences - Java - Code Style - Code Templates
|
30 |
*/
|
31 |
public class SnappingPlanarGraph extends PlanarGraph { |
32 |
|
33 |
public static final CGAlgorithms cga = new CGAlgorithms(); |
34 |
|
35 |
protected List edges = new ArrayList(); |
36 |
|
37 |
protected SnappingNodeMap nodes;
|
38 |
|
39 |
protected List edgeEndList = new ArrayList(); |
40 |
|
41 |
|
42 |
/**
|
43 |
* For nodes in the Collection, link the DirectedEdges at the node that are
|
44 |
* in the result. This allows clients to link only a subset of nodes in the
|
45 |
* graph, for efficiency (because they know that only a subset is of
|
46 |
* interest).
|
47 |
*/
|
48 |
public static void linkResultDirectedEdges(Collection nodes) { |
49 |
for (Iterator nodeit = nodes.iterator(); nodeit.hasNext();) { |
50 |
Node node = (Node) nodeit.next(); |
51 |
DirectedEdgeStar edgeStar = (DirectedEdgeStar) node.getEdges(); |
52 |
edgeStar.linkResultDirectedEdges(); |
53 |
} |
54 |
} |
55 |
|
56 |
public SnappingPlanarGraph(NodeFactory nodeFact, double tolerance) { |
57 |
nodes = new SnappingNodeMap(nodeFact, tolerance);
|
58 |
} |
59 |
|
60 |
|
61 |
public SnappingPlanarGraph(double tolerance) { |
62 |
nodes = new SnappingNodeMap(new NodeFactory(), tolerance); |
63 |
} |
64 |
|
65 |
|
66 |
public Iterator getEdgeIterator() { |
67 |
return edges.iterator();
|
68 |
} |
69 |
|
70 |
public Collection getEdgeEnds() { |
71 |
return edgeEndList;
|
72 |
} |
73 |
|
74 |
|
75 |
public boolean isBoundaryNode(int geomIndex, Coordinate coord) { |
76 |
Node node = nodes.find(coord); |
77 |
if (node == null) |
78 |
return false; |
79 |
Label label = node.getLabel();
|
80 |
if (label != null && label.getLocation(geomIndex) == Location.BOUNDARY) |
81 |
return true; |
82 |
return false; |
83 |
} |
84 |
|
85 |
protected void insertEdge(Edge e) { |
86 |
edges.add(e); |
87 |
} |
88 |
|
89 |
|
90 |
|
91 |
public void add(EdgeEnd e) { |
92 |
nodes.add(e); |
93 |
edgeEndList.add(e); |
94 |
} |
95 |
|
96 |
|
97 |
|
98 |
public Iterator getNodeIterator() { |
99 |
return nodes.iterator();
|
100 |
} |
101 |
|
102 |
public Collection getNodes() { |
103 |
return nodes.values();
|
104 |
} |
105 |
|
106 |
public Node addNode(Node node) {
|
107 |
return nodes.addNode(node);
|
108 |
} |
109 |
|
110 |
public Node addNode(Coordinate coord) {
|
111 |
return nodes.addNode(coord);
|
112 |
} |
113 |
|
114 |
/**
|
115 |
* @return the node if found; null otherwise
|
116 |
*/
|
117 |
public Node find(Coordinate coord) {
|
118 |
return nodes.find(coord);
|
119 |
} |
120 |
|
121 |
|
122 |
/**
|
123 |
* Add a set of edges to the graph.
|
124 |
* For each edge two DirectedEdges will be
|
125 |
* created. DirectedEdges are NOT linked by this method.
|
126 |
*/
|
127 |
public void addEdges(List edgesToAdd) { |
128 |
// create all the nodes for the edges
|
129 |
for (Iterator it = edgesToAdd.iterator(); it.hasNext();) { |
130 |
Edge e = (Edge) it.next(); |
131 |
edges.add(e); |
132 |
SnapDirectedEdge de1 = new SnapDirectedEdge(e, true); |
133 |
SnapDirectedEdge de2 = new SnapDirectedEdge(e, false); |
134 |
de1.setSym(de2); |
135 |
de2.setSym(de1); |
136 |
//IMPORTAR VER QUE PASA AQU?: Para cada nuevo Edge, construye un EdgeEnd que puede dar a lugar a un nodo y que origina un Edge
|
137 |
add(de1); |
138 |
add(de2); |
139 |
} |
140 |
} |
141 |
|
142 |
/**
|
143 |
* Link the DirectedEdges at the nodes of the graph. This allows clients to
|
144 |
* link only a subset of nodes in the graph, for efficiency (because they
|
145 |
* know that only a subset is of interest).
|
146 |
*/
|
147 |
public void linkResultDirectedEdges() { |
148 |
for (Iterator nodeit = nodes.iterator(); nodeit.hasNext();) { |
149 |
Node node = (Node) nodeit.next(); |
150 |
((DirectedEdgeStar) node.getEdges()).linkResultDirectedEdges(); |
151 |
} |
152 |
} |
153 |
|
154 |
/**
|
155 |
* Link the DirectedEdges at the nodes of the graph. This allows clients to
|
156 |
* link only a subset of nodes in the graph, for efficiency (because they
|
157 |
* know that only a subset is of interest).
|
158 |
*/
|
159 |
public void linkAllDirectedEdges() { |
160 |
for (Iterator nodeit = nodes.iterator(); nodeit.hasNext();) { |
161 |
Node node = (Node) nodeit.next(); |
162 |
((DirectedEdgeStar) node.getEdges()).linkAllDirectedEdges(); |
163 |
} |
164 |
} |
165 |
|
166 |
/**
|
167 |
* Returns the EdgeEnd which has edge e as its base edge (MD 18 Feb 2002 -
|
168 |
* this should return a pair of edges)
|
169 |
*
|
170 |
* @return the edge, if found <code>null</code> if the edge was not found
|
171 |
*/
|
172 |
public EdgeEnd findEdgeEnd(Edge e) {
|
173 |
for (Iterator i = getEdgeEnds().iterator(); i.hasNext();) { |
174 |
EdgeEnd ee = (EdgeEnd) i.next(); |
175 |
if (ee.getEdge() == e)
|
176 |
return ee;
|
177 |
} |
178 |
return null; |
179 |
} |
180 |
|
181 |
/**
|
182 |
* Returns the edge whose first two coordinates are p0 and p1
|
183 |
*
|
184 |
* @return the edge, if found <code>null</code> if the edge was not found
|
185 |
*/
|
186 |
public Edge findEdge(Coordinate p0, Coordinate p1) {
|
187 |
for (int i = 0; i < edges.size(); i++) { |
188 |
Edge e = (Edge) edges.get(i); |
189 |
Coordinate[] eCoord = e.getCoordinates();
|
190 |
if (p0.equals(eCoord[0]) && p1.equals(eCoord[1])) |
191 |
return e;
|
192 |
} |
193 |
return null; |
194 |
} |
195 |
|
196 |
/**
|
197 |
* Returns the edge which starts at p0 and whose first segment is parallel
|
198 |
* to p1
|
199 |
*
|
200 |
* @return the edge, if found <code>null</code> if the edge was not found
|
201 |
*/
|
202 |
public Edge findEdgeInSameDirection(Coordinate p0, Coordinate p1) {
|
203 |
for (int i = 0; i < edges.size(); i++) { |
204 |
Edge e = (Edge) edges.get(i); |
205 |
|
206 |
Coordinate[] eCoord = e.getCoordinates();
|
207 |
if (matchInSameDirection(p0, p1, eCoord[0], eCoord[1])) |
208 |
return e;
|
209 |
|
210 |
if (matchInSameDirection(p0, p1, eCoord[eCoord.length - 1], |
211 |
eCoord[eCoord.length - 2]))
|
212 |
return e;
|
213 |
} |
214 |
return null; |
215 |
} |
216 |
|
217 |
/**
|
218 |
* The coordinate pairs match if they define line segments lying in the same
|
219 |
* direction. E.g. the segments are parallel and in the same quadrant (as
|
220 |
* opposed to parallel and opposite!).
|
221 |
*/
|
222 |
private boolean matchInSameDirection(Coordinate p0, Coordinate p1, |
223 |
Coordinate ep0, Coordinate ep1) { |
224 |
if (!p0.equals(ep0))
|
225 |
return false; |
226 |
|
227 |
if (CGAlgorithms.computeOrientation(p0, p1, ep1) == CGAlgorithms.COLLINEAR
|
228 |
&& Quadrant.quadrant(p0, p1) == Quadrant.quadrant(ep0, ep1)) |
229 |
return true; |
230 |
return false; |
231 |
} |
232 |
|
233 |
public void dump(){ |
234 |
System.out.println("EDGES"); |
235 |
Iterator it = this.getEdgeIterator(); |
236 |
while(it.hasNext()){
|
237 |
Edge e = (Edge) it.next(); |
238 |
e.print(System.out);
|
239 |
System.out.println(""); |
240 |
} |
241 |
System.out.println("NODES"); |
242 |
it = this.getNodeIterator();
|
243 |
while(it.hasNext()){
|
244 |
Node node = (Node) it.next(); |
245 |
System.out.println(node.getCoordinate());
|
246 |
System.out.println(node.getLabel());
|
247 |
List edges = node.getEdges().getEdges();
|
248 |
for(int z = 0; z < edges.size(); z++){ |
249 |
EdgeEnd ee = (EdgeEnd) edges.get(z); |
250 |
Label eeL = ee.getLabel();
|
251 |
System.out.println(ee.toString() + "," + eeL.toString()); |
252 |
} |
253 |
} |
254 |
// nodes.dump();
|
255 |
} |
256 |
|
257 |
} |