forked from libgeos/geos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VoronoiDiagramBuilder.cpp
187 lines (155 loc) · 5.24 KB
/
VoronoiDiagramBuilder.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
/**********************************************************************
*
* GEOS - Geometry Engine Open Source
* http://geos.osgeo.org
*
* Copyright (C) 2012 Excensus LLC.
*
* 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: triangulate/VoronoiDiagramBuilder.java rev. r524
*
**********************************************************************/
#include <geos/triangulate/VoronoiDiagramBuilder.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <iostream>
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/Coordinate.h>
#include <geos/geom/Envelope.h>
#include <geos/geom/CoordinateSequence.h>
#include <geos/triangulate/IncrementalDelaunayTriangulator.h>
#include <geos/triangulate/DelaunayTriangulationBuilder.h>
#include <geos/triangulate/quadedge/QuadEdgeSubdivision.h>
#include <geos/operation/valid/RepeatedPointRemover.h>
#include <geos/util.h>
using geos::detail::make_unique;
namespace geos {
namespace triangulate { //geos.triangulate
using namespace geos::geom;
VoronoiDiagramBuilder::VoronoiDiagramBuilder() :
tolerance(0.0), clipEnv(nullptr)
{
}
void
VoronoiDiagramBuilder::setSites(const geom::Geometry& geom)
{
siteCoords = DelaunayTriangulationBuilder::extractUniqueCoordinates(geom);
}
void
VoronoiDiagramBuilder::setSites(const geom::CoordinateSequence& coords)
{
siteCoords = DelaunayTriangulationBuilder::unique(&coords);
}
void
VoronoiDiagramBuilder::setClipEnvelope(const geom::Envelope* nClipEnv)
{
clipEnv = nClipEnv;
}
void
VoronoiDiagramBuilder::setTolerance(double nTolerance)
{
tolerance = nTolerance;
}
void
VoronoiDiagramBuilder::create()
{
if(subdiv) {
return;
}
if (siteCoords->isEmpty()) {
return;
}
diagramEnv = siteCoords->getEnvelope();
//adding buffer around the final envelope
double expandBy = std::max(diagramEnv.getWidth(), diagramEnv.getHeight());
diagramEnv.expandBy(expandBy);
if(clipEnv) {
diagramEnv.expandToInclude(clipEnv);
}
auto vertices = DelaunayTriangulationBuilder::toVertices(*siteCoords);
std::sort(vertices.begin(), vertices.end()); // Best performance from locator when inserting points near each other
subdiv.reset(new quadedge::QuadEdgeSubdivision(diagramEnv, tolerance));
IncrementalDelaunayTriangulator triangulator(subdiv.get());
triangulator.insertSites(vertices);
}
std::unique_ptr<quadedge::QuadEdgeSubdivision>
VoronoiDiagramBuilder::getSubdivision()
{
create();
// NOTE: Apparently, this is 'source' method giving up the object resource.
return std::move(subdiv);
}
std::unique_ptr<geom::GeometryCollection>
VoronoiDiagramBuilder::getDiagram(const geom::GeometryFactory& geomFact)
{
create();
std::unique_ptr<GeometryCollection> ret;
if (subdiv) {
auto polys = subdiv->getVoronoiCellPolygons(geomFact);
ret = clipGeometryCollection(polys, diagramEnv);
}
if (ret == nullptr) {
return std::unique_ptr<geom::GeometryCollection>(geomFact.createGeometryCollection());
}
return ret;
}
std::unique_ptr<MultiLineString>
VoronoiDiagramBuilder::getDiagramEdges(const geom::GeometryFactory& geomFact)
{
create();
if (!subdiv) {
return geomFact.createMultiLineString();
}
std::unique_ptr<geom::MultiLineString> edges = subdiv->getVoronoiDiagramEdges(geomFact);
if(edges->isEmpty()) {
return edges.release();
}
std::unique_ptr<geom::Geometry> clipPoly(geomFact.toGeometry(&diagramEnv));
std::unique_ptr<Geometry> clipped(clipPoly->intersection(edges.get()));
switch (clipped->getGeometryTypeId()) {
case GEOS_LINESTRING: {
std::vector<std::unique_ptr<LineString>> lines;
lines.emplace_back(static_cast<LineString*>(clipped.release()));
return geomFact.createMultiLineString(std::move(lines));
}
case GEOS_MULTILINESTRING: {
return clipped
}
default: {
throw util::GEOSException("Unknown state");
}
}
}
std::unique_ptr<geom::GeometryCollection>
VoronoiDiagramBuilder::clipGeometryCollection(std::vector<std::unique_ptr<Geometry>> & geoms, const geom::Envelope& clipEnv)
{
if (geoms.empty()) {
return nullptr;
}
auto gfact = geoms[0]->getFactory();
std::unique_ptr<geom::Geometry> clipPoly(gfact->toGeometry(&clipEnv));
std::vector<std::unique_ptr<Geometry>> clipped;
for(auto& g : geoms) {
// don't clip unless necessary
if(clipEnv.contains(g->getEnvelopeInternal())) {
clipped.push_back(std::move(g));
// TODO: check if userData is correctly cloned here?
} else if(clipEnv.intersects(g->getEnvelopeInternal())) {
auto result = clipPoly->intersection(g.get());
result->setUserData(g->getUserData()); // TODO: needed ?
if (!result->isEmpty()) {
clipped.push_back(std::move(result));
}
}
}
return gfact->createGeometryCollection(std::move(clipped));
}
} //namespace geos.triangulate
} //namespace geos