File: SegmentNodeList.cpp

package info (click to toggle)
geos 3.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 10,060 kB
  • ctags: 8,674
  • sloc: cpp: 64,513; xml: 23,384; sh: 8,965; ruby: 1,295; makefile: 1,124; python: 824; ansic: 289
file content (355 lines) | stat: -rw-r--r-- 9,442 bytes parent folder | download
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
/**********************************************************************
 * $Id: SegmentNodeList.cpp 1820 2006-09-06 16:54:23Z mloskot $
 *
 * GEOS - Geometry Engine Open Source
 * http://geos.refractions.net
 *
 * Copyright (C) 2006      Refractions Research Inc.
 * 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 Licence as published
 * by the Free Software Foundation. 
 * See the COPYING file for more information.
 *
 **********************************************************************
 *
 * Last port: noding/SegmentNodeList.java rev. 1.7 (JTS-1.7)
 *
 **********************************************************************/

#include <cassert>
#include <set>

#include <geos/profiler.h>
#include <geos/util/GEOSException.h>
#include <geos/noding/SegmentNodeList.h>
#include <geos/noding/SegmentString.h>
#include <geos/geom/Coordinate.h>
#include <geos/geom/CoordinateSequence.h>
#include <geos/geom/CoordinateArraySequence.h> // FIXME: should we really be using this ?

#ifndef GEOS_DEBUG
#define GEOS_DEBUG 0
#endif

#ifdef GEOS_DEBUG
#include <iostream>
#endif

//using namespace std;
using namespace geos::geom;

namespace geos {
namespace noding { // geos.noding

#if PROFILE
static Profiler *profiler = Profiler::instance();
#endif


SegmentNodeList::~SegmentNodeList()
{
	std::set<SegmentNode *, SegmentNodeLT>::iterator it=nodeMap.begin();
	for(; it!=nodeMap.end(); it++)
	{
		delete *it;
	}

	for(size_t i=0, n=splitEdges.size(); i<n; ++i)
	{
		delete splitEdges[i];
	}

	for(size_t i=0, n=splitCoordLists.size(); i<n; ++i)
	{
		delete splitCoordLists[i];
	}
}

SegmentNode*
SegmentNodeList::add(const Coordinate& intPt, size_t segmentIndex)
{
	SegmentNode *eiNew=new SegmentNode(edge, intPt, segmentIndex,
			edge.getSegmentOctant(segmentIndex));

	std::pair<SegmentNodeList::iterator,bool> p = nodeMap.insert(eiNew);
	if ( p.second ) { // new SegmentNode inserted
		return eiNew;
	} else {

		// sanity check 
		assert(eiNew->coord.equals2D(intPt));

		delete eiNew;
		return *(p.first);
	}
}

void SegmentNodeList::addEndpoints()
{
	int maxSegIndex = edge.size() - 1;
	add(&(edge.getCoordinate(0)), 0);
	add(&(edge.getCoordinate(maxSegIndex)), maxSegIndex);
}

/* private */
void
SegmentNodeList::addCollapsedNodes()
{
	std::vector<size_t> collapsedVertexIndexes;

	findCollapsesFromInsertedNodes(collapsedVertexIndexes);
	findCollapsesFromExistingVertices(collapsedVertexIndexes);

	// node the collapses
	for (std::vector<size_t>::iterator
		i=collapsedVertexIndexes.begin(),
			e=collapsedVertexIndexes.end();
		i != e; ++i)
	{
		size_t vertexIndex = *i;
		add(edge.getCoordinate(vertexIndex), vertexIndex);
	}
}


/* private */
void
SegmentNodeList::findCollapsesFromExistingVertices(
			std::vector<size_t>& collapsedVertexIndexes)
{
	for (size_t i=0, n=edge.size()-2; i<n; ++i)
	{
		const Coordinate& p0 = edge.getCoordinate(i);
		//const Coordinate& p1 = edge.getCoordinate(i + 1);
		const Coordinate& p2 = edge.getCoordinate(i + 2);
		if (p0.equals2D(p2)) {
			// add base of collapse as node
			collapsedVertexIndexes.push_back(i + 1);
		}
	}
}

/* private */
void
SegmentNodeList::findCollapsesFromInsertedNodes(
		std::vector<size_t>& collapsedVertexIndexes)
{
	size_t collapsedVertexIndex;

	// there should always be at least two entries in the list,
	// since the endpoints are nodes
	iterator it = begin();
	SegmentNode* eiPrev = *it;
	++it;
	for(iterator itEnd=end(); it!=itEnd; ++it)
	{
		SegmentNode *ei=*it;
      		bool isCollapsed = findCollapseIndex(*eiPrev, *ei,
				collapsedVertexIndex);
		if (isCollapsed)
			collapsedVertexIndexes.push_back(collapsedVertexIndex);

		eiPrev = ei;
	}
}

/* private */
bool
SegmentNodeList::findCollapseIndex(SegmentNode& ei0, SegmentNode& ei1,
		size_t& collapsedVertexIndex)
{
	// only looking for equal nodes
	if (! ei0.coord.equals2D(ei1.coord)) return false;

	int numVerticesBetween = ei1.segmentIndex - ei0.segmentIndex;
	if (! ei1.isInterior()) {
		numVerticesBetween--;
	}

	// if there is a single vertex between the two equal nodes,
	// this is a collapse
	if (numVerticesBetween == 1) {
		collapsedVertexIndex = ei0.segmentIndex + 1;
		return true;
	}
	return false;
}


/* public */
void
SegmentNodeList::addSplitEdges(std::vector<SegmentString*>& edgeList)
{

	// testingOnly
#if GEOS_DEBUG
	std::cerr<<__FUNCTION__<<" entered"<<std::endl;
	std::vector<SegmentString*> testingSplitEdges;
#endif

	// ensure that the list has entries for the first and last
	// point of the edge
	addEndpoints();
	addCollapsedNodes();

	// there should always be at least two entries in the list
	// since the endpoints are nodes
	iterator it=begin();
	SegmentNode *eiPrev=*it;
	assert(eiPrev);
	it++;
	for(iterator itEnd=end(); it!=itEnd; ++it)
	{
		SegmentNode *ei=*it;
		assert(ei);

		if ( ! ei->compareTo(*eiPrev) ) continue;

		SegmentString *newEdge=createSplitEdge(eiPrev, ei);
		edgeList.push_back(newEdge);
#if GEOS_DEBUG
		testingSplitEdges.push_back(newEdge);
#endif
		eiPrev = ei;
	}
#if GEOS_DEBUG
	std::cerr<<__FUNCTION__<<" finished, now checking correctness"<<std::endl;
	checkSplitEdgesCorrectness(testingSplitEdges);
#endif
}

void
SegmentNodeList::checkSplitEdgesCorrectness(std::vector<SegmentString*>& splitEdges)
{
	const CoordinateSequence *edgePts=edge.getCoordinates();
	assert(edgePts);

	// check that first and last points of split edges
	// are same as endpoints of edge
	SegmentString *split0=splitEdges[0];
	assert(split0);

	const Coordinate& pt0=split0->getCoordinate(0);
	if (!(pt0==edgePts->getAt(0)))
		throw util::GEOSException("bad split edge start point at " + pt0.toString());

	SegmentString *splitn=splitEdges[splitEdges.size()-1];
	assert(splitn);

	const CoordinateSequence *splitnPts=splitn->getCoordinates();
	assert(splitnPts);

	const Coordinate &ptn=splitnPts->getAt(splitnPts->getSize()-1);
	if (!(ptn==edgePts->getAt(edgePts->getSize()-1)))
		throw util::GEOSException("bad split edge end point at " + ptn.toString());
}

/*private*/
SegmentString*
SegmentNodeList::createSplitEdge(SegmentNode *ei0, SegmentNode *ei1)
{
	assert(ei0);
	assert(ei1);

	size_t npts = ei1->segmentIndex - ei0->segmentIndex + 2;

	const Coordinate &lastSegStartPt=edge.getCoordinate(ei1->segmentIndex);

	// if the last intersection point is not equal to the its
	// segment start pt, add it to the points list as well.
	// (This check is needed because the distance metric is not
	// totally reliable!)

	// The check for point equality is 2D only - Z values are ignored

	// Added check for npts being == 2 as in that case NOT using second point
	// would mean creating a SegmentString with a single point
	// FIXME: check with mbdavis about this, ie: is it a bug in the caller ?
	//
	bool useIntPt1 = npts == 2 || (ei1->isInterior() || ! ei1->coord.equals2D(lastSegStartPt));

	if (! useIntPt1) {
		npts--;
	}

	CoordinateSequence *pts = new CoordinateArraySequence(npts); 
	size_t ipt = 0;
	pts->setAt(ei0->coord, ipt++);
	for (size_t i=ei0->segmentIndex+1; i<=ei1->segmentIndex; i++)
	{
		pts->setAt(edge.getCoordinate(i),ipt++);
	}
	if (useIntPt1) 	pts->setAt(ei1->coord, ipt++);

	SegmentString *ret = new SegmentString(pts, edge.getData());
#if GEOS_DEBUG
	std::cerr<<" SegmentString created"<<std::endl;
#endif
	splitEdges.push_back(ret);

	// Keep track of created CoordinateSequence to release
	// it at this SegmentNodeList destruction time
	splitCoordLists.push_back(pts);

	return ret;
}

std::ostream&
operator<< (std::ostream& os, const SegmentNodeList& nlist)
{
	os << "Intersections: (" << nlist.nodeMap.size() << "):" << std::endl;

	std::set<SegmentNode*,SegmentNodeLT>::const_iterator
			it = nlist.nodeMap.begin(),
			itEnd = nlist.nodeMap.end();

	for(; it!=itEnd; it++)
	{
		SegmentNode *ei=*it;
		os << " " << *ei;
	}
	return os;
}

} // namespace geos.noding
} // namespace geos

/**********************************************************************
 * $Log$
 * Revision 1.33  2006/06/12 11:29:23  strk
 * unsigned int => size_t
 *
 * Revision 1.32  2006/05/05 10:19:06  strk
 * droppped SegmentString::getContext(), new name is getData() to reflect change in JTS
 *
 * Revision 1.31  2006/05/04 08:35:15  strk
 * noding/SegmentNodeList.cpp: cleanups, changed output operator to be more similar to JTS
 *
 * Revision 1.30  2006/03/15 09:51:12  strk
 * streamlined headers usage
 *
 * Revision 1.29  2006/03/09 15:39:25  strk
 * Fixed debugging lines
 *
 * Revision 1.28  2006/03/06 19:40:47  strk
 * geos::util namespace. New GeometryCollection::iterator interface, many cleanups.
 *
 * Revision 1.27  2006/03/03 10:46:21  strk
 * Removed 'using namespace' from headers, added missing headers in .cpp files, removed useless includes in headers (bug#46)
 *
 * Revision 1.26  2006/03/02 12:12:00  strk
 * Renamed DEBUG macros to GEOS_DEBUG, all wrapped in #ifndef block to allow global override (bug#43)
 *
 * Revision 1.25  2006/03/01 16:01:47  strk
 * Fixed const correctness of operator<<(ostream&, SegmentNodeList&) [bug#37]
 *
 * Revision 1.24  2006/02/28 17:44:27  strk
 * Added a check in SegmentNode::addSplitEdge to prevent attempts
 * to build SegmentString with less then 2 points.
 * This is a temporary fix for the buffer.xml assertion failure, temporary
 * as Martin Davis review would really be needed there.
 *
 **********************************************************************/