File: Node.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 (315 lines) | stat: -rw-r--r-- 6,673 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
/**********************************************************************
 * $Id: Node.cpp 1820 2006-09-06 16:54:23Z mloskot $
 *
 * GEOS - Geometry Engine Open Source
 * http://geos.refractions.net
 *
 * Copyright (C) 2005-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 Public Licence as published
 * by the Free Software Foundation. 
 * See the COPYING file for more information.
 *
 **********************************************************************
 *
 * Last port: geomgraph/Node.java rev. 1.6 (JTS-1.7)
 *
 **********************************************************************/

#include <geos/geom/Coordinate.h>
#include <geos/geomgraph/Node.h>
#include <geos/geomgraph/Edge.h>
#include <geos/geomgraph/EdgeEndStar.h>
#include <geos/geomgraph/Label.h>
#include <geos/geomgraph/DirectedEdge.h>
#include <geos/geom/Location.h>

#include <string>
#include <sstream>
#include <vector>
#include <algorithm>

#ifndef GEOS_DEBUG
#define GEOS_DEBUG 0
#endif
#ifndef COMPUTE_Z
#define COMPUTE_Z 1
#endif

using namespace std;
using namespace geos::geom;

namespace geos {
namespace geomgraph { // geos.geomgraph

/*public*/
Node::Node(const Coordinate& newCoord, EdgeEndStar* newEdges)
	:
	GraphComponent(new Label(0,Location::UNDEF)),
	coord(newCoord),
	edges(newEdges)

{
#if GEOS_DEBUG
	cerr<<"["<<this<<"] Node::Node("<<newCoord.toString()<<")"<<endl;
#endif

#if COMPUTE_Z
	ztot = 0;
	addZ(newCoord.z);
	if ( edges )
	{
		EdgeEndStar::iterator endIt = edges->end();
		for (EdgeEndStar::iterator it=edges->begin(); it!=endIt; ++it)
		{
			EdgeEnd *ee = *it;
			addZ(ee->getCoordinate().z);
		}
	}
#endif // COMPUTE_Z

	testInvariant();
}

/*public*/
Node::~Node()
{
	testInvariant();
#if GEOS_DEBUG
	cerr<<"["<<this<<"] Node::~Node()"<<endl;
#endif
	delete edges;
}

/*public*/
const Coordinate&
Node::getCoordinate() const
{
	testInvariant();
	return coord;
}

/*public*/
EdgeEndStar *
Node::getEdges()
{
	testInvariant();

	return edges;
}

/*public*/
bool
Node::isIsolated() const
{
	testInvariant();

	return (label->getGeometryCount()==1);
}

/*public*/
bool
Node::isIncidentEdgeInResult() const
{
	testInvariant();

	if (!edges) return false;

	EdgeEndStar::iterator it=edges->begin();
	EdgeEndStar::iterator endIt=edges->end();
	for ( ; it!=endIt; ++it)
	{
		assert(*it);
		assert(dynamic_cast<DirectedEdge *>(*it));
		DirectedEdge *de = static_cast<DirectedEdge *>(*it);
		if ( de->getEdge()->isInResult() ) return true;
	}
	return false;
}

void
Node::add(EdgeEnd *e)
{
	assert(e);
#if GEOS_DEBUG
	cerr<<"["<<this<<"] Node::add("<<e->print()<<")"<<endl;
#endif
	// Assert: start pt of e is equal to node point
	assert(e->getCoordinate().equals2D(coord));

	// It seems it's legal for edges to be NULL
	// we'd not be honouring the promise of adding
	// an EdgeEnd in this case, though ...
	assert(edges);
	//if (edges==NULL) return;

	edges->insert(e);
	e->setNode(this);
#if COMPUTE_Z
	addZ(e->getCoordinate().z);
#endif
	testInvariant();
}

/*public*/
void
Node::mergeLabel(const Node& n)
{
	assert(n.label);
	mergeLabel(*(n.label));
	testInvariant();
}

/*public*/
void
Node::mergeLabel(const Label& label2)
{
	for (int i=0; i<2; i++) {
		int loc=computeMergedLocation(&label2, i);
		int thisLoc=label->getLocation(i);
		if (thisLoc==Location::UNDEF) label->setLocation(i,loc);
	}
	testInvariant();
}

/*public*/
void
Node::setLabel(int argIndex, int onLocation)
{
	if (label==NULL) {
		label=new Label(argIndex, onLocation);
	} else
		label->setLocation(argIndex, onLocation);

	testInvariant();
}

/*public*/
void
Node::setLabelBoundary(int argIndex)
{
	int loc=Location::UNDEF;
	if (label!=NULL)
		loc=label->getLocation(argIndex);
	// flip the loc
	int newLoc;
	switch (loc){
		case Location::BOUNDARY: newLoc=Location::INTERIOR; break;
		case Location::INTERIOR: newLoc=Location::BOUNDARY; break;
		default: newLoc=Location::BOUNDARY;  break;
	}
	label->setLocation(argIndex, newLoc);

	testInvariant();
}

/*public*/
int
Node::computeMergedLocation(const Label* label2, int eltIndex)
{
	int loc=Location::UNDEF;
	loc=label->getLocation(eltIndex);
	if (!label2->isNull(eltIndex)) {
		int nLoc=label2->getLocation(eltIndex);
		if (loc!=Location::BOUNDARY) loc=nLoc;
	}

	testInvariant();

	return loc;
}

/*public*/
string
Node::print()
{
	testInvariant();

	ostringstream ss;
	ss<<*this;
	return ss.str();
}

/*public*/
void
Node::addZ(double z)
{
#if GEOS_DEBUG
	cerr<<"["<<this<<"] Node::addZ("<<z<<")";
#endif
	if ( ISNAN(z) )
	{
#if GEOS_DEBUG
		cerr<<" skipped"<<endl;
#endif
		return;
	}
	if ( find(zvals.begin(), zvals.end(), z) != zvals.end() )
	{
#if GEOS_DEBUG
		cerr<<" already stored"<<endl;
#endif
		return;
	}
	zvals.push_back(z);
	ztot+=z;
	coord.z=ztot/zvals.size();
#if GEOS_DEBUG
	cerr<<" added "<<z<<": ["<<ztot<<"/"<<zvals.size()<<"="<<coord.z<<"]"<<endl;
#endif
}

/*public*/
const vector<double>&
Node::getZ() const
{
	return zvals;
}

std::ostream& operator<< (std::ostream& os, const Node& node)
{
	os << "Node["<<&node<<"]" << std::endl
	   << "  POINT(" << node.coord << ")" << std::endl
	   << "  lbl: "+node.label->toString();
	return os;
}

} // namespace geos.geomgraph
} // namespace geos

/**********************************************************************
 * $Log$
 * Revision 1.24  2006/04/27 15:03:48  strk
 * standard algorithm used in addZ() for vector seek
 *
 * Revision 1.23  2006/04/07 16:01:51  strk
 * Port info, doxygen comments, testInvariant(), many assertionss, handling of
 * the NULL EdgeEndStar member
 *
 * Revision 1.22  2006/03/15 16:27:54  strk
 * operator<< for Node class
 *
 * Revision 1.21  2006/03/14 15:31:39  strk
 * Cleaned up toString funx (more WKT friendly)
 *
 * Revision 1.20  2006/03/14 12:55:55  strk
 * Headers split: geomgraphindex.h, nodingSnapround.h
 *
 * Revision 1.19  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.18  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.17  2006/02/19 19:46:49  strk
 * Packages <-> namespaces mapping for most GEOS internal code (uncomplete, but working). Dir-level libs for index/ subdirs.
 *
 * Revision 1.16  2005/11/29 00:48:35  strk
 * Removed edgeList cache from EdgeEndRing. edgeMap is enough.
 * Restructured iterated access by use of standard ::iterator abstraction
 * with scoped typedefs.
 *
 **********************************************************************/