Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / vividsolutions / jts / geomgraph / SnappingPlanarGraph.java @ 10627

History | View | Annotate | Download (6.4 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

    
16
/**
17
 * @author alzabord
18
 * 
19
 * TODO To change the template for this generated type comment go to Window -
20
 * Preferences - Java - Code Style - Code Templates
21
 */
22
public class SnappingPlanarGraph extends PlanarGraph {
23

    
24
        public static final CGAlgorithms cga = new CGAlgorithms();
25

    
26
        protected List edges = new ArrayList();
27

    
28
        protected SnappingNodeMap nodes;
29

    
30
        protected List edgeEndList = new ArrayList();
31

    
32
        
33
        /**
34
         * For nodes in the Collection, link the DirectedEdges at the node that are
35
         * in the result. This allows clients to link only a subset of nodes in the
36
         * graph, for efficiency (because they know that only a subset is of
37
         * interest).
38
         */
39
        public static void linkResultDirectedEdges(Collection nodes) {
40
                for (Iterator nodeit = nodes.iterator(); nodeit.hasNext();) {
41
                        Node node = (Node) nodeit.next();
42
                        DirectedEdgeStar edgeStar =  (DirectedEdgeStar) node.getEdges();
43
                        edgeStar.linkResultDirectedEdges();
44
                }
45
        }
46

    
47
        public SnappingPlanarGraph(NodeFactory nodeFact, double tolerance) {
48
                nodes = new SnappingNodeMap(nodeFact, tolerance);
49
        }
50
        
51

    
52
        public SnappingPlanarGraph(double tolerance) {
53
                nodes = new SnappingNodeMap(new NodeFactory(), tolerance);
54
        }
55
        
56

    
57
        public Iterator getEdgeIterator() {
58
                return edges.iterator();
59
        }
60

    
61
        public Collection getEdgeEnds() {
62
                return edgeEndList;
63
        }
64
        
65

    
66
        public boolean isBoundaryNode(int geomIndex, Coordinate coord) {
67
                Node node = nodes.find(coord);
68
                if (node == null)
69
                        return false;
70
                Label label = node.getLabel();
71
                if (label != null && label.getLocation(geomIndex) == Location.BOUNDARY)
72
                        return true;
73
                return false;
74
        }
75

    
76
        protected void insertEdge(Edge e) {
77
                edges.add(e);
78
        }
79
        
80

    
81
        
82
        public void add(EdgeEnd e) {
83
                nodes.add(e);
84
                edgeEndList.add(e);
85
        }
86
        
87
        
88

    
89
        public Iterator getNodeIterator() {
90
                return nodes.iterator();
91
        }
92

    
93
        public Collection getNodes() {
94
                return nodes.values();
95
        }
96

    
97
        public Node addNode(Node node) {
98
                return nodes.addNode(node);
99
        }
100

    
101
        public Node addNode(Coordinate coord) {
102
                return nodes.addNode(coord);
103
        }
104

    
105
        /**
106
         * @return the node if found; null otherwise
107
         */
108
        public Node find(Coordinate coord) {
109
                return nodes.find(coord);
110
        }
111

    
112
        
113
        /**
114
         * Add a set of edges to the graph. 
115
         * For each edge two DirectedEdges will be
116
         * created. DirectedEdges are NOT linked by this method.
117
         */
118
        public void addEdges(List edgesToAdd) {
119
                // create all the nodes for the edges
120
                for (Iterator it = edgesToAdd.iterator(); it.hasNext();) {
121
                        Edge e = (Edge) it.next();
122
                        edges.add(e);
123
                        SnapDirectedEdge de1 = new SnapDirectedEdge(e, true);
124
                        SnapDirectedEdge de2 = new SnapDirectedEdge(e, false);
125
                        de1.setSym(de2);
126
                        de2.setSym(de1);
127
//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
128
                        add(de1);
129
                        add(de2);
130
                }
131
        }
132

    
133
        /**
134
         * Link the DirectedEdges at the nodes of the graph. This allows clients to
135
         * link only a subset of nodes in the graph, for efficiency (because they
136
         * know that only a subset is of interest).
137
         */
138
        public void linkResultDirectedEdges() {
139
                for (Iterator nodeit = nodes.iterator(); nodeit.hasNext();) {
140
                        Node node = (Node) nodeit.next();
141
                        ((DirectedEdgeStar) node.getEdges()).linkResultDirectedEdges();
142
                }
143
        }
144

    
145
        /**
146
         * Link the DirectedEdges at the nodes of the graph. This allows clients to
147
         * link only a subset of nodes in the graph, for efficiency (because they
148
         * know that only a subset is of interest).
149
         */
150
        public void linkAllDirectedEdges() {
151
                for (Iterator nodeit = nodes.iterator(); nodeit.hasNext();) {
152
                        Node node = (Node) nodeit.next();
153
                        ((DirectedEdgeStar) node.getEdges()).linkAllDirectedEdges();
154
                }
155
        }
156

    
157
        /**
158
         * Returns the EdgeEnd which has edge e as its base edge (MD 18 Feb 2002 -
159
         * this should return a pair of edges)
160
         * 
161
         * @return the edge, if found <code>null</code> if the edge was not found
162
         */
163
        public EdgeEnd findEdgeEnd(Edge e) {
164
                for (Iterator i = getEdgeEnds().iterator(); i.hasNext();) {
165
                        EdgeEnd ee = (EdgeEnd) i.next();
166
                        if (ee.getEdge() == e)
167
                                return ee;
168
                }
169
                return null;
170
        }
171

    
172
        /**
173
         * Returns the edge whose first two coordinates are p0 and p1
174
         * 
175
         * @return the edge, if found <code>null</code> if the edge was not found
176
         */
177
        public Edge findEdge(Coordinate p0, Coordinate p1) {
178
                for (int i = 0; i < edges.size(); i++) {
179
                        Edge e = (Edge) edges.get(i);
180
                        Coordinate[] eCoord = e.getCoordinates();
181
                        if (p0.equals(eCoord[0]) && p1.equals(eCoord[1]))
182
                                return e;
183
                }
184
                return null;
185
        }
186

    
187
        /**
188
         * Returns the edge which starts at p0 and whose first segment is parallel
189
         * to p1
190
         * 
191
         * @return the edge, if found <code>null</code> if the edge was not found
192
         */
193
        public Edge findEdgeInSameDirection(Coordinate p0, Coordinate p1) {
194
                for (int i = 0; i < edges.size(); i++) {
195
                        Edge e = (Edge) edges.get(i);
196

    
197
                        Coordinate[] eCoord = e.getCoordinates();
198
                        if (matchInSameDirection(p0, p1, eCoord[0], eCoord[1]))
199
                                return e;
200

    
201
                        if (matchInSameDirection(p0, p1, eCoord[eCoord.length - 1],
202
                                        eCoord[eCoord.length - 2]))
203
                                return e;
204
                }
205
                return null;
206
        }
207

    
208
        /**
209
         * The coordinate pairs match if they define line segments lying in the same
210
         * direction. E.g. the segments are parallel and in the same quadrant (as
211
         * opposed to parallel and opposite!).
212
         */
213
        private boolean matchInSameDirection(Coordinate p0, Coordinate p1,
214
                        Coordinate ep0, Coordinate ep1) {
215
                if (!p0.equals(ep0))
216
                        return false;
217

    
218
                if (CGAlgorithms.computeOrientation(p0, p1, ep1) == CGAlgorithms.COLLINEAR
219
                                && Quadrant.quadrant(p0, p1) == Quadrant.quadrant(ep0, ep1))
220
                        return true;
221
                return false;
222
        }
223
        
224
        public void dump(){
225
                System.out.println("EDGES");
226
                Iterator it = this.getEdgeIterator();
227
                while(it.hasNext()){
228
                        Edge e = (Edge) it.next();
229
                        e.print(System.out);
230
                        System.out.println("");
231
                }
232
                System.out.println("NODES");
233
                it = this.getNodeIterator();
234
                while(it.hasNext()){
235
                        Node node = (Node) it.next();
236
                        System.out.println(node.getCoordinate());
237
                        System.out.println(node.getLabel());
238
                        List edges = node.getEdges().getEdges();
239
                        for(int z = 0; z < edges.size(); z++){
240
                                EdgeEnd ee = (EdgeEnd) edges.get(z);
241
                                Label eeL = ee.getLabel();
242
                                System.out.println(ee.toString() + "," + eeL.toString());
243
                        }
244
                }
245
//                nodes.dump();
246
        }
247

    
248
}