File: PlanarGraph.cpp

package info (click to toggle)
geos 2.1.1-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,784 kB
  • ctags: 3,505
  • sloc: cpp: 24,991; sh: 8,431; xml: 6,597; makefile: 401; python: 77
file content (368 lines) | stat: -rw-r--r-- 9,378 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**********************************************************************
 * $Id: PlanarGraph.cpp,v 1.11 2004/11/17 08:13:16 strk Exp $
 *
 * GEOS - Geometry Engine Open Source
 * http://geos.refractions.net
 *
 * Copyright (C) 2001-2002 Vivid Solutions Inc.
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the GNU Lesser General Public Licence as published
 * by the Free Software Foundation. 
 * See the COPYING file for more information.
 *
 **********************************************************************/

#include <geos/geomgraph.h>

#ifndef DEBUG
#define DEBUG 0
#endif

namespace geos {

CGAlgorithms* PlanarGraph::cga=new CGAlgorithms();
//LineIntersector* PlanarGraph::li=new RobustLineIntersector();

/**
 * For nodes in the vector, link the DirectedEdges at the node that are in the result.
 * This allows clients to link only a subset of nodes in the graph, for
 * efficiency (because they know that only a subset is of interest).
 */
void
PlanarGraph::linkResultDirectedEdges(vector<Node*> *allNodes)
	//throw(TopologyException *)
{
	vector<Node*>::iterator nodeit;
	for(nodeit=allNodes->begin(); nodeit<allNodes->end(); nodeit++)
	{
		Node *node=*nodeit;
		// this might throw an exception
		((DirectedEdgeStar*) node->getEdges())->linkResultDirectedEdges();
	}
}

PlanarGraph::PlanarGraph(NodeFactory *nodeFact)
{
	nodes=new NodeMap(nodeFact);
	edges=new vector<Edge*>();
	edgeEndList=new vector<EdgeEnd*>();

}

PlanarGraph::PlanarGraph()
{
	nodes=new NodeMap(new NodeFactory());
	edges=new vector<Edge*>();
	edgeEndList=new vector<EdgeEnd*>();
}

PlanarGraph::~PlanarGraph()
{
	delete nodes;
	int i;
	for(i=0;i<(int)edges->size();i++) {
		delete (*edges)[i];
	}
	delete edges;
	for(i=0;i<(int)edgeEndList->size();i++) {
		delete (*edgeEndList)[i];
	}
	delete edgeEndList;
}

vector<Edge*>::iterator
PlanarGraph::getEdgeIterator()
{
	return edges->begin();
}

vector<EdgeEnd*> *
PlanarGraph::getEdgeEnds()
{
	return edgeEndList;
}

bool
PlanarGraph::isBoundaryNode(int geomIndex,Coordinate& coord)
{
	Node *node=nodes->find(coord);
	if (node==NULL) return false;
	Label *label=node->getLabel();
	if (label!=NULL && label->getLocation(geomIndex)==Location::BOUNDARY) return true;
	return false;
}

void
PlanarGraph::insertEdge(Edge *e)
{
	edges->push_back(e);
}

void
PlanarGraph::add(EdgeEnd *e)
{
	nodes->add(e);
	edgeEndList->push_back(e);
}

map<Coordinate,Node*,CoordLT>::iterator
PlanarGraph::getNodeIterator()
{
	return nodes->iterator();
}

vector<Node*>*
PlanarGraph::getNodes()
{
	vector<Node*> *values=new vector<Node*>();
	map<Coordinate,Node*,CoordLT>::iterator it=nodes->nodeMap->begin();
	while(it!=nodes->nodeMap->end()) {
		values->push_back(it->second);
		it++;
	}
	return values;
}

// arg cannot be const, NodeMap::addNode will
// occasionally label-merge first arg.
Node*
PlanarGraph::addNode(Node *node)
{
#if DEBUG
	cerr<<"PlanarGraph::addNode(Node * "<<node->print()<<")"<<endl;
#endif
	return nodes->addNode(node);
}

Node*
PlanarGraph::addNode(const Coordinate& coord)
{
#if DEBUG
	cerr<<"PlanarGraph::addNode(Coordinate& "<<coord.toString()<<")"<<endl;
#endif
	return nodes->addNode(coord);
}

/*
 * @return the node if found; null otherwise
 */
Node*
PlanarGraph::find(Coordinate& coord)
{
	return nodes->find(coord);
}

/*
 * Add a set of edges to the graph.  For each edge two DirectedEdges
 * will be created.  DirectedEdges are NOT linked by this method.
 */
void
PlanarGraph::addEdges(vector<Edge*>* edgesToAdd)
{
	// create all the nodes for the edges
	for (vector<Edge*>::iterator it=edgesToAdd->begin();it<edgesToAdd->end();it++) {
		Edge *e=*it;
		edges->push_back(e);
		DirectedEdge *de1=new DirectedEdge(e, true);
		DirectedEdge *de2=new DirectedEdge(e, false);
		de1->setSym(de2);
		de2->setSym(de1);
		add(de1);
		add(de2);
	}
}

/*
 * Link the DirectedEdges at the nodes of the graph.
 * This allows clients to link only a subset of nodes in the graph, for
 * efficiency (because they know that only a subset is of interest).
 */
void
PlanarGraph::linkResultDirectedEdges()
{
#if DEBUG
	cerr<<"PlanarGraph::linkResultDirectedEdges called"<<endl;
#endif
	for (map<Coordinate,Node*,CoordLT>::iterator nodeit=nodes->iterator();nodeit!=nodes->nodeMap->end();nodeit++) {
		Node *node=nodeit->second;
		((DirectedEdgeStar*)node->getEdges())->linkResultDirectedEdges();
	}
}

/*
 * Link the DirectedEdges at the nodes of the graph.
 * This allows clients to link only a subset of nodes in the graph, for
 * efficiency (because they know that only a subset is of interest).
 */
void
PlanarGraph::linkAllDirectedEdges()
{
#if DEBUG
	cerr<<"PlanarGraph::linkAllDirectedEdges called"<<endl;
#endif
	for (map<Coordinate,Node*,CoordLT>::iterator nodeit=nodes->iterator();nodeit!=nodes->nodeMap->end();nodeit++) {
		Node *node=nodeit->second;
		((DirectedEdgeStar*)node->getEdges())->linkAllDirectedEdges();
	}
}

/*
 * Returns the EdgeEnd which has edge e as its base edge
 * (MD 18 Feb 2002 - this should return a pair of edges)
 *
 * @return the edge, if found
 *    <code>null</code> if the edge was not found
 */
EdgeEnd*
PlanarGraph::findEdgeEnd(Edge *e)
{
	for (vector<EdgeEnd*>::iterator i=getEdgeEnds()->begin();i<getEdgeEnds()->end();i++) {
		EdgeEnd *ee=*i;
		if (ee->getEdge()==e)
			return ee;
	}
	return NULL;
}

/*
 * Returns the edge whose first two coordinates are p0 and p1
 *
 * @return the edge, if found
 *    <code>null</code> if the edge was not found
 */
Edge*
PlanarGraph::findEdge(const Coordinate& p0, const Coordinate& p1)
{
    unsigned int i;
	for(i=0; i<edges->size();i++) {
//        Edge *e=edges->at(i);
        Edge *e=(*edges)[i];
		const CoordinateSequence* eCoord=e->getCoordinates();
		if (p0==eCoord->getAt(0) && p1==eCoord->getAt(1))
			return e;
	}
	return NULL;
}

/*
 * Returns the edge which starts at p0 and whose first segment is
 * parallel to p1
 *
 * @return the edge, if found
 *    <code>null</code> if the edge was not found
 */
Edge*
PlanarGraph::findEdgeInSameDirection(const Coordinate& p0, const Coordinate& p1)
{
	for(unsigned int i=0; i<edges->size();i++) {
		Edge *e=(*edges)[i];
//		Edge *e=edges->at(i);
		const CoordinateSequence* eCoord=e->getCoordinates();
		if (matchInSameDirection(p0,p1,eCoord->getAt(0),eCoord->getAt(1)))
			return e;
		if (matchInSameDirection(p0,p1,eCoord->getAt(eCoord->getSize()-1),eCoord->getAt(eCoord->getSize()-2)))
			return e;
	}
	return NULL;
}

/*
 * The coordinate pairs match if they define line segments
 * lying in the same direction.
 * E.g. the segments are parallel and in the same quadrant
 * (as opposed to parallel and opposite!).
 */
bool
PlanarGraph::matchInSameDirection(const Coordinate& p0, const Coordinate& p1, const Coordinate& ep0, const Coordinate& ep1)
{
	if (!(p0==ep0))
		return false;

	if (CGAlgorithms::computeOrientation(p0,p1,ep1)==CGAlgorithms::COLLINEAR
		&& Quadrant::quadrant(p0,p1)==Quadrant::quadrant(ep0,ep1))
			return true;
	return false;
}

string
PlanarGraph::printEdges()
{
	string out="Edges: ";
	for(unsigned int i=0;i<edges->size();i++) {
		out+="edge ";
		out+=i;
		out+=":\n";
		Edge *e=(*edges)[i];
//		Edge *e=edges->at(i);
		out+=e->print();
		out+=e->eiList->print();
	}
	return out;
}

NodeMap*
PlanarGraph::getNodeMap()
{
	return nodes;
}

} // namespace geos

/**********************************************************************
 * $Log: PlanarGraph.cpp,v $
 * Revision 1.11  2004/11/17 08:13:16  strk
 * Indentation changes.
 * Some Z_COMPUTATION activated by default.
 *
 * Revision 1.10  2004/11/01 16:43:04  strk
 * Added Profiler code.
 * Temporarly patched a bug in DoubleBits (must check drawbacks).
 * Various cleanups and speedups.
 *
 * Revision 1.9  2004/10/21 22:29:54  strk
 * Indentation changes and some more COMPUTE_Z rules
 *
 * Revision 1.8  2004/10/20 17:32:14  strk
 * Initial approach to 2.5d intersection()
 *
 * Revision 1.7  2004/10/13 10:03:02  strk
 * Added missing linemerge and polygonize operation.
 * Bug fixes and leaks removal from the newly added modules and
 * planargraph (used by them).
 * Some comments and indentation changes.
 *
 * Revision 1.6  2004/07/08 19:34:49  strk
 * Mirrored JTS interface of CoordinateSequence, factory and
 * default implementations.
 * Added DefaultCoordinateSequenceFactory::instance() function.
 *
 * Revision 1.5  2004/07/02 13:28:26  strk
 * Fixed all #include lines to reflect headers layout change.
 * Added client application build tips in README.
 *
 * Revision 1.4  2004/05/03 10:43:42  strk
 * Exception specification considered harmful - left as comment.
 *
 * Revision 1.3  2004/04/19 15:14:45  strk
 * Added missing virtual destructor in SpatialIndex class.
 * Memory leaks fixes. Const and throw specifications added.
 *
 * Revision 1.2  2004/04/10 08:40:01  ybychkov
 * "operation/buffer" upgraded to JTS 1.4
 *
 * Revision 1.1  2004/03/19 09:48:45  ybychkov
 * "geomgraph" and "geomgraph/indexl" upgraded to JTS 1.4
 *
 * Revision 1.19  2003/11/12 18:02:56  strk
 * Added throw specification. Fixed leaks on exceptions.
 *
 * Revision 1.18  2003/11/07 01:23:42  pramsey
 * Add standard CVS headers licence notices and copyrights to all cpp and h
 * files.
 *
 * Revision 1.17  2003/10/15 16:39:03  strk
 * Made Edge::getCoordinates() return a 'const' value. Adapted code set.
 *
 **********************************************************************/