-
Notifications
You must be signed in to change notification settings - Fork 347
/
EdgeNodingBuilder.cpp
415 lines (357 loc) · 11.5 KB
/
EdgeNodingBuilder.cpp
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/**********************************************************************
*
* GEOS - Geometry Engine Open Source
* http://geos.osgeo.org
*
* Copyright (C) 2020 Paul Ramsey <pramsey@cleverelephant.ca>
*
* 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: operation/overlayng/EdgeNodingBuilder.java 6ef89b096
*
**********************************************************************/
#include <geos/operation/overlayng/EdgeNodingBuilder.h>
#include <geos/operation/overlayng/EdgeMerger.h>
using geos::operation::valid::RepeatedPointRemover;
namespace geos { // geos
namespace operation { // geos.operation
namespace overlayng { // geos.operation.overlayng
/*private*/
Noder*
EdgeNodingBuilder::getNoder()
{
if (customNoder != nullptr) {
return customNoder;
}
if (OverlayUtil::isFloating(pm)) {
internalNoder = createFloatingPrecisionNoder(IS_NODING_VALIDATED);
}
else {
internalNoder = createFixedPrecisionNoder(pm);
}
return internalNoder.get();
}
/*private*/
std::unique_ptr<Noder>
EdgeNodingBuilder::createFixedPrecisionNoder(const PrecisionModel* p_pm)
{
std::unique_ptr<Noder> srNoder(new SnapRoundingNoder(p_pm));
return srNoder;
}
/*private*/
std::unique_ptr<Noder>
EdgeNodingBuilder::createFloatingPrecisionNoder(bool doValidation)
{
std::unique_ptr<MCIndexNoder> mcNoder(new MCIndexNoder());
mcNoder->setSegmentIntersector(&intAdder);
if (doValidation) {
spareInternalNoder = std::move(mcNoder);
std::unique_ptr<Noder> validNoder(new ValidatingNoder(*spareInternalNoder));
return validNoder;
}
return std::unique_ptr<Noder>(mcNoder.release());
}
/*public*/
void
EdgeNodingBuilder::setClipEnvelope(const Envelope* p_clipEnv)
{
clipEnv = p_clipEnv;
clipper.reset(new RingClipper(p_clipEnv));
limiter.reset(new LineLimiter(p_clipEnv));
}
/*public*/
std::vector<Edge*>
EdgeNodingBuilder::build(const Geometry* geom0, const Geometry* geom1)
{
add(geom0, 0);
add(geom1, 1);
std::vector<Edge*> nodedEdges = node(inputEdges.get());
/**
* Merge the noded edges to eliminate duplicates.
* Labels are combined.
*/
return EdgeMerger::merge(nodedEdges);
}
/*private*/
std::vector<Edge*>
EdgeNodingBuilder::node(std::vector<SegmentString*>* segStrings)
{
std::vector<Edge*> nodedEdges;
Noder* noder = getNoder();
noder->computeNodes(segStrings);
std::unique_ptr<std::vector<SegmentString*>> nodedSS(noder->getNodedSubstrings());
nodedEdges = createEdges(nodedSS.get());
// Clean up now that all the info is transferred to Edges
for (SegmentString* ss : *nodedSS) {
delete ss;
}
return nodedEdges;
}
/*private*/
std::vector<Edge*>
EdgeNodingBuilder::createEdges(std::vector<SegmentString*>* segStrings)
{
std::vector<Edge*> createdEdges;
for (SegmentString* ss : *segStrings) {
const CoordinateSequence* pts = ss->getCoordinates();
// don't create edges from collapsed lines
if (Edge::isCollapsed(pts)) continue;
// This EdgeSourceInfo is already managed locally in a std::deque
const EdgeSourceInfo* info = static_cast<const EdgeSourceInfo*>(ss->getData());
// Record that a non-collapsed edge exists for the parent geometry
hasEdges[info->getIndex()] = true;
// Allocate the new Edge locally in a std::deque
std::unique_ptr<CoordinateSequence> ssPts = ss->getCoordinates()->clone();
edgeQue.emplace_back(ssPts.release(), info);
Edge* newEdge = &(edgeQue.back());
createdEdges.push_back(newEdge);
}
return createdEdges;
}
/*public*/
bool
EdgeNodingBuilder::hasEdgesFor(int geomIndex) const
{
assert(geomIndex < 2);
return hasEdges[geomIndex];
}
/*private*/
void
EdgeNodingBuilder::add(const Geometry* g, int geomIndex)
{
if (g == nullptr || g->isEmpty())
return;
if (isClippedCompletely(g->getEnvelopeInternal()))
return;
switch (g->getGeometryTypeId())
{
case GEOS_POLYGON:
return addPolygon(static_cast<const Polygon*>(g), geomIndex);
case GEOS_LINESTRING:
case GEOS_LINEARRING:
return addLine(static_cast<const LineString*>(g), geomIndex);
case GEOS_MULTILINESTRING:
case GEOS_MULTIPOLYGON:
return addCollection(static_cast<const GeometryCollection*>(g), geomIndex);
case GEOS_GEOMETRYCOLLECTION:
return addGeometryCollection(static_cast<const GeometryCollection*>(g), geomIndex, g->getDimension());
case GEOS_POINT:
case GEOS_MULTIPOINT:
return; // do nothing
default:
return; // do nothing
}
}
/*private*/
void
EdgeNodingBuilder::addCollection(const GeometryCollection* gc, int geomIndex)
{
for (std::size_t i = 0; i < gc->getNumGeometries(); i++) {
const Geometry* g = gc->getGeometryN(i);
add(g, geomIndex);
}
}
/*private*/
void
EdgeNodingBuilder::addGeometryCollection(const GeometryCollection* gc, int geomIndex, int expectedDim)
{
for (std::size_t i = 0; i < gc->getNumGeometries(); i++) {
const Geometry* g = gc->getGeometryN(i);
if (g->getDimension() != expectedDim) {
throw geos::util::IllegalArgumentException("Overlay input is mixed-dimension");
}
add(g, geomIndex);
}
}
/*private*/
void
EdgeNodingBuilder::addPolygon(const Polygon* poly, int geomIndex)
{
const LinearRing* shell = poly->getExteriorRing();
addPolygonRing(shell, false, geomIndex);
for (std::size_t i = 0; i < poly->getNumInteriorRing(); i++) {
const LinearRing* hole = poly->getInteriorRingN(i);
// Holes are topologically labelled opposite to the shell, since
// the interior of the polygon lies on their opposite side
// (on the left, if the hole is oriented CW)
addPolygonRing(hole, true, geomIndex);
}
}
/*private*/
void
EdgeNodingBuilder::addPolygonRing(const LinearRing* ring, bool isHole, int index)
{
// don't add empty rings
if (ring->isEmpty()) return;
if (isClippedCompletely(ring->getEnvelopeInternal()))
return;
std::unique_ptr<geom::CoordinateArraySequence> pts = clip(ring);
/**
* Don't add edges that collapse to a point
*/
if (pts->size() < 2) {
return;
}
int depthDelta = computeDepthDelta(ring, isHole);
addEdge(pts, createEdgeSourceInfo(index, depthDelta, isHole));
}
/*private*/
const EdgeSourceInfo*
EdgeNodingBuilder::createEdgeSourceInfo(int index)
{
// Concentrate small memory allocations via std::deque and
// retain ownership of the EdgeSourceInfo* in the EdgeNodingBuilder
edgeSourceInfoQue.emplace_back(index);
return &(edgeSourceInfoQue.back());
}
/*private*/
const EdgeSourceInfo*
EdgeNodingBuilder::createEdgeSourceInfo(int index, int depthDelta, bool isHole)
{
// Concentrate small memory allocations via std::deque and
// retain ownership of the EdgeSourceInfo* in the EdgeNodingBuilder
edgeSourceInfoQue.emplace_back(index, depthDelta, isHole);
return &(edgeSourceInfoQue.back());
}
/*private*/
void
EdgeNodingBuilder::addEdge(std::unique_ptr<CoordinateArraySequence>& cas, const EdgeSourceInfo* info)
{
// TODO: manage these internally to EdgeNodingBuilder in a std::deque,
// since they do not have a life span longer than the EdgeNodingBuilder
// in OverlayNG::buildGraph()
NodedSegmentString* ss = new NodedSegmentString(cas.release(), reinterpret_cast<const void*>(info));
inputEdges->push_back(ss);
}
/*private*/
void
EdgeNodingBuilder::addEdge(std::unique_ptr<std::vector<Coordinate>> pts, const EdgeSourceInfo* info)
{
CoordinateArraySequence* cas = new CoordinateArraySequence(pts.release());
NodedSegmentString* ss = new NodedSegmentString(cas, reinterpret_cast<const void*>(info));
inputEdges->push_back(ss);
}
/*private*/
bool
EdgeNodingBuilder::isClippedCompletely(const Envelope* env)
{
if (clipEnv == nullptr) return false;
return clipEnv->disjoint(env);
}
/* private */
std::unique_ptr<geom::CoordinateArraySequence>
EdgeNodingBuilder::clip(const LinearRing* ring)
{
const Envelope* env = ring->getEnvelopeInternal();
/**
* If no clipper or ring is completely contained then no need to clip.
* But repeated points must be removed to ensure correct noding.
*/
if (clipper == nullptr || clipEnv->covers(env)) {
return removeRepeatedPoints(ring);
}
return clipper->clip(ring->getCoordinatesRO());
}
/*private*/
std::unique_ptr<CoordinateArraySequence>
EdgeNodingBuilder::removeRepeatedPoints(const LineString* line)
{
const CoordinateSequence* pts = line->getCoordinatesRO();
return RepeatedPointRemover::removeRepeatedPoints(pts);
}
/*private*/
int
EdgeNodingBuilder::computeDepthDelta(const LinearRing* ring, bool isHole)
{
/**
* Compute the orientation of the ring, to
* allow assigning side interior/exterior labels correctly.
* JTS canonical orientation is that shells are CW, holes are CCW.
*
* It is important to compute orientation on the original ring,
* since topology collapse can make the orientation computation give the wrong answer.
*/
bool isCCW = algorithm::Orientation::isCCW(ring->getCoordinatesRO());
/**
* Compute whether ring is in canonical orientation or not.
* Canonical orientation for the overlay process is
* Shells : CW, Holes: CCW
*/
bool isOriented = true;
if (!isHole) {
isOriented = !isCCW;
}
else {
isOriented = isCCW;
}
/**
* Depth delta can now be computed.
* Canonical depth delta is 1 (Exterior on L, Interior on R).
* It is flipped to -1 if the ring is oppositely oriented.
*/
int depthDelta = isOriented ? 1 : -1;
return depthDelta;
}
/*private*/
void
EdgeNodingBuilder::addLine(const LineString* line, int geomIndex)
{
// don't add empty lines
if (line->isEmpty()) return;
if (isClippedCompletely(line->getEnvelopeInternal()))
return;
if (isToBeLimited(line)) {
std::vector<std::unique_ptr<CoordinateArraySequence>>& sections = limit(line);
for (auto& pts : sections) {
addLine(pts, geomIndex);
}
}
else {
std::unique_ptr<CoordinateArraySequence> ptsNoRepeat = removeRepeatedPoints(line);
addLine(ptsNoRepeat, geomIndex);
}
}
/*private*/
void
EdgeNodingBuilder::addLine(std::unique_ptr<CoordinateArraySequence>& pts, int geomIndex)
{
/**
* Don't add edges that collapse to a point
*/
if (pts->size() < 2) {
return;
}
addEdge(pts, createEdgeSourceInfo(geomIndex));
}
/*private*/
bool
EdgeNodingBuilder::isToBeLimited(const LineString* line) const
{
const CoordinateSequence* pts = line->getCoordinatesRO();
if (limiter == nullptr || pts->size() <= MIN_LIMIT_PTS) {
return false;
}
const Envelope* env = line->getEnvelopeInternal();
/**
* If line is completely contained then no need to limit
*/
if (clipEnv->covers(env)) {
return false;
}
return true;
}
/*private*/
std::vector<std::unique_ptr<CoordinateArraySequence>>&
EdgeNodingBuilder::limit(const LineString* line)
{
const CoordinateSequence* pts = line->getCoordinatesRO();
return limiter->limit(pts);
}
} // namespace geos.operation.overlayng
} // namespace geos.operation
} // namespace geos