GEOS 3.14.0dev
MaximumInscribedCircle.h
1/**********************************************************************
2 *
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
5 *
6 * Copyright (c) 2020 Martin Davis
7 * Copyright (C) 2020 Paul Ramsey <pramsey@cleverelephant.ca>
8 *
9 * This is free software; you can redistribute and/or modify it under
10 * the terms of the GNU Lesser General Public Licence as published
11 * by the Free Software Foundation.
12 * See the COPYING file for more information.
13 *
14 **********************************************************************
15 *
16 * Last port: algorithm/construct/MaximumInscribedCircle.java
17 * https://github.com/locationtech/jts/commit/f0b9a808bdf8a973de435f737e37b7a221e231cb
18 *
19 **********************************************************************/
20
21#pragma once
22
23#include <geos/geom/Coordinate.h>
24#include <geos/geom/Point.h>
25#include <geos/geom/Envelope.h>
26#include <geos/algorithm/locate/IndexedPointInAreaLocator.h>
27#include <geos/operation/distance/IndexedFacetDistance.h>
28
29#include <memory>
30#include <queue>
31
32
33
34namespace geos {
35namespace geom {
36class Coordinate;
37class Envelope;
38class Geometry;
39class GeometryFactory;
40class LineString;
41class Point;
42}
43}
44
45namespace geos {
46namespace algorithm { // geos::algorithm
47namespace construct { // geos::algorithm::construct
48
90class GEOS_DLL MaximumInscribedCircle {
91
94
95public:
96
97 MaximumInscribedCircle(const geom::Geometry* polygonal, double tolerance);
98 ~MaximumInscribedCircle() = default;
99
106 std::unique_ptr<geom::Point> getCenter();
107
118 std::unique_ptr<geom::Point> getRadiusPoint();
119
125 std::unique_ptr<geom::LineString> getRadiusLine();
126
139 bool isRadiusWithin(double maxRadius);
140
149 static std::unique_ptr<geom::Point> getCenter(const geom::Geometry* polygonal, double tolerance);
150
159 static std::unique_ptr<geom::LineString> getRadiusLine(const geom::Geometry* polygonal, double tolerance);
160
171 static bool isRadiusWithin(const geom::Geometry* polygonal, double maxRadius);
172
187 static std::size_t computeMaximumIterations(const geom::Geometry* geom, double toleranceDist);
188
189private:
190
191 /* private members */
192 const geom::Geometry* inputGeom;
193 std::unique_ptr<geom::Geometry> inputGeomBoundary;
194 double tolerance;
195 IndexedFacetDistance indexedDistance;
197 const geom::GeometryFactory* factory;
198 bool done;
199 geom::CoordinateXY centerPt;
200 geom::CoordinateXY radiusPt;
201 double maximumRadius = -1;
202
203 /* private constant */
204 static constexpr double MAX_RADIUS_FRACTION = 0.0001;
205
206 /* private methods */
207 double distanceToBoundary(const geom::Point& pt);
208 double distanceToBoundary(double x, double y);
209 void compute();
210 void computeApproximation();
211 void createResult(const geom::CoordinateXY& c, const geom::CoordinateXY& r);
212
213 /* private class */
214 class Cell {
215 private:
216 static constexpr double SQRT2 = 1.4142135623730951;
217 double x;
218 double y;
219 double hSize;
220 double distance;
221 double maxDist;
222
223 public:
224 Cell(double p_x, double p_y, double p_hSize, double p_distanceToBoundary)
225 : x(p_x)
226 , y(p_y)
227 , hSize(p_hSize)
228 , distance(p_distanceToBoundary)
229 , maxDist(p_distanceToBoundary+(p_hSize*SQRT2))
230 {};
231
232 geom::Envelope getEnvelope() const
233 {
234 geom::Envelope env(x-hSize, x+hSize, y-hSize, y+hSize);
235 return env;
236 }
237
238 double getMaxDistance() const
239 {
240 return maxDist;
241 }
242 double getDistance() const
243 {
244 return distance;
245 }
246 double getHSize() const
247 {
248 return hSize;
249 }
250 double getX() const
251 {
252 return x;
253 }
254 double getY() const
255 {
256 return y;
257 }
258
259 bool operator< (const Cell& rhs) const
260 {
261 return maxDist < rhs.maxDist;
262 }
263
264 bool operator> (const Cell& rhs) const
265 {
266 return maxDist > rhs.maxDist;
267 }
268
269 bool operator==(const Cell& rhs) const
270 {
271 return maxDist == rhs.maxDist;
272 }
273
279 using CellQueue = std::priority_queue<Cell>;
280 };
281
282 void createInitialGrid(const geom::Envelope* env, Cell::CellQueue& cellQueue);
283 Cell createInteriorPointCell(const geom::Geometry* geom);
284
285};
286
287
288} // geos::algorithm::construct
289} // geos::algorithm
290} // geos
Definition MaximumInscribedCircle.h:90
std::unique_ptr< geom::Point > getCenter()
static std::unique_ptr< geom::Point > getCenter(const geom::Geometry *polygonal, double tolerance)
std::unique_ptr< geom::LineString > getRadiusLine()
static bool isRadiusWithin(const geom::Geometry *polygonal, double maxRadius)
static std::size_t computeMaximumIterations(const geom::Geometry *geom, double toleranceDist)
std::unique_ptr< geom::Point > getRadiusPoint()
static std::unique_ptr< geom::LineString > getRadiusLine(const geom::Geometry *polygonal, double tolerance)
Determines the location of Coordinates relative to an areal geometry, using indexing for efficiency.
Definition IndexedPointInAreaLocator.h:54
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition Envelope.h:59
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition GeometryFactory.h:70
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition Geometry.h:196
Definition Point.h:61
Computes the distance between the facets (segments and vertices) of two Geometrys using a Branch-and-...
Definition IndexedFacetDistance.h:46
Basic namespace for all GEOS functionalities.
Definition geos.h:38