-
Notifications
You must be signed in to change notification settings - Fork 345
/
MaximumInscribedCircle.cpp
250 lines (210 loc) · 7.67 KB
/
MaximumInscribedCircle.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
/**********************************************************************
*
* 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: algorithm/construct/MaximumInscribedCircle.java
* https://github.com/locationtech/jts/commit/98274a7ea9b40651e9de6323dc10fb2cac17a245
*
**********************************************************************/
#include <geos/algorithm/construct/MaximumInscribedCircle.h>
#include <geos/geom/Coordinate.h>
#include <geos/geom/CoordinateSequence.h>
#include <geos/geom/Envelope.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/LineString.h>
#include <geos/geom/Polygon.h>
#include <geos/geom/MultiPolygon.h>
#include <geos/algorithm/locate/IndexedPointInAreaLocator.h>
#include <geos/operation/distance/IndexedFacetDistance.h>
#include <geos/util/Interrupt.h>
#include <typeinfo> // for dynamic_cast
#include <cassert>
using namespace geos::geom;
namespace geos {
namespace algorithm { // geos.algorithm
namespace construct { // geos.algorithm.construct
/* public */
MaximumInscribedCircle::MaximumInscribedCircle(const Geometry* polygonal, double p_tolerance)
: inputGeom(polygonal)
, inputGeomBoundary(polygonal->getBoundary())
, tolerance(p_tolerance)
, indexedDistance(inputGeomBoundary.get())
, ptLocator(*polygonal)
, factory(polygonal->getFactory())
, done(false)
{
if (!(typeid(*polygonal) == typeid(Polygon) ||
typeid(*polygonal) == typeid(MultiPolygon))) {
throw util::IllegalArgumentException("Input geometry must be a Polygon or MultiPolygon");
}
if (polygonal->isEmpty()) {
throw util::IllegalArgumentException("Empty input geometry is not supported");
}
}
/* public static */
std::unique_ptr<Point>
MaximumInscribedCircle::getCenter(const Geometry* polygonal, double tolerance)
{
MaximumInscribedCircle mic(polygonal, tolerance);
return mic.getCenter();
}
/* public static */
std::unique_ptr<LineString>
MaximumInscribedCircle::getRadiusLine(const Geometry* polygonal, double tolerance)
{
MaximumInscribedCircle mic(polygonal, tolerance);
return mic.getRadiusLine();
}
/* public static */
std::size_t
MaximumInscribedCircle::computeMaximumIterations(const Geometry* geom, double toleranceDist)
{
double diam = geom->getEnvelopeInternal()->getDiameter();
double ncells = diam / toleranceDist;
//-- Using log of ncells allows control over number of iterations
std::size_t factor = (std::size_t) std::log(ncells);
if (factor < 1) factor = 1;
return 2000 + 2000 * factor;
}
/* public */
std::unique_ptr<Point>
MaximumInscribedCircle::getCenter()
{
compute();
return factory->createPoint(centerPt);
}
/* public */
std::unique_ptr<Point>
MaximumInscribedCircle::getRadiusPoint()
{
compute();
return factory->createPoint(radiusPt);
}
/* public */
std::unique_ptr<LineString>
MaximumInscribedCircle::getRadiusLine()
{
compute();
auto cl = detail::make_unique<CoordinateSequence>(2u);
cl->setAt(centerPt, 0);
cl->setAt(radiusPt, 1);
return factory->createLineString(std::move(cl));
}
int INITIAL_GRID_SIDE = 25;
/* private */
void
MaximumInscribedCircle::createInitialGrid(const Envelope* env, Cell::CellQueue& cellQueue)
{
if (!env->isFinite()) {
throw util::GEOSException("Non-finite envelope encountered.");
}
double cellSize = std::max(env->getWidth(), env->getHeight());
double hSide = cellSize / 2.0;
// Collapsed geometries just end up using the centroid
// as the answer and skip all the other machinery
if (cellSize == 0) return;
CoordinateXY c;
env->centre(c);
cellQueue.emplace(c.x, c.y, hSide, distanceToBoundary(c.x, c.y));
}
/* private */
double
MaximumInscribedCircle::distanceToBoundary(const Coordinate& c)
{
std::unique_ptr<Point> pt(factory->createPoint(c));
double dist = indexedDistance.distance(pt.get());
// double dist = inputGeomBoundary->distance(pt.get());
bool isOutside = (Location::EXTERIOR == ptLocator.locate(&c));
if (isOutside) return -dist;
return dist;
}
/* private */
double
MaximumInscribedCircle::distanceToBoundary(double x, double y)
{
Coordinate coord(x, y);
return distanceToBoundary(coord);
}
/* private */
MaximumInscribedCircle::Cell
MaximumInscribedCircle::createInteriorPointCell(const Geometry* geom)
{
Coordinate c;
std::unique_ptr<Point> p = geom->getInteriorPoint();
Cell cell(p->getX(), p->getY(), 0, distanceToBoundary(c));
return cell;
}
/* private */
void
MaximumInscribedCircle::compute()
{
// check if already computed
if (done) return;
// Priority queue of cells, ordered by maximum distance from boundary
Cell::CellQueue cellQueue;
createInitialGrid(inputGeom->getEnvelopeInternal(), cellQueue);
// use the area centroid as the initial candidate center point
Cell farthestCell = createInteriorPointCell(inputGeom);
/**
* Carry out the branch-and-bound search
* of the cell space
*/
std::size_t maxIter = computeMaximumIterations(inputGeom, tolerance);
std::size_t iterationCount = 0;
while (!cellQueue.empty() && iterationCount < maxIter) {
// pick the most promising cell from the queue
Cell cell = cellQueue.top();
cellQueue.pop();
// std::cout << iterationCount << "] Dist: " << cell.getDistance() << " size: " << cell.getHSize() << std::endl;
if ((iterationCount++ % 1000) == 0) {
GEOS_CHECK_FOR_INTERRUPTS();
}
//-- if cell must be closer than furthest, terminate since all remaining cells in queue are even closer.
if (cell.getMaxDistance() < farthestCell.getDistance())
break;
// update the center cell if the candidate is further from the boundary
if (cell.getDistance() > farthestCell.getDistance()) {
farthestCell = cell;
}
/**
* Refine this cell if the potential distance improvement
* is greater than the required tolerance.
* Otherwise the cell is pruned (not investigated further),
* since no point in it is further than
* the current farthest distance.
*/
double potentialIncrease = cell.getMaxDistance() - farthestCell.getDistance();
if (potentialIncrease > tolerance) {
// split the cell into four sub-cells
double h2 = cell.getHSize() / 2;
cellQueue.emplace(cell.getX()-h2, cell.getY()-h2, h2, distanceToBoundary(cell.getX()-h2, cell.getY()-h2));
cellQueue.emplace(cell.getX()+h2, cell.getY()-h2, h2, distanceToBoundary(cell.getX()+h2, cell.getY()-h2));
cellQueue.emplace(cell.getX()-h2, cell.getY()+h2, h2, distanceToBoundary(cell.getX()-h2, cell.getY()+h2));
cellQueue.emplace(cell.getX()+h2, cell.getY()+h2, h2, distanceToBoundary(cell.getX()+h2, cell.getY()+h2));
}
}
// the farthest cell is the best approximation to the MIC center
Cell centerCell = farthestCell;
centerPt.x = centerCell.getX();
centerPt.y = centerCell.getY();
// compute radius point
std::unique_ptr<Point> centerPoint(factory->createPoint(centerPt));
const auto& nearestPts = indexedDistance.nearestPoints(centerPoint.get());
radiusPt = nearestPts->getAt(0);
// flag computation
done = true;
}
} // namespace geos.algorithm.construct
} // namespace geos.algorithm
} // namespace geos