GEOS  3.14.0dev
LargestEmptyCircle.h
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2020 Paul Ramsey <pramsey@cleverelephant.ca>
7  *
8  * This is free software; you can redistribute and/or modify it under
9  * the terms of the GNU Lesser General Public Licence as published
10  * by the Free Software Foundation.
11  * See the COPYING file for more information.
12  *
13  **********************************************************************
14  *
15  * Last port: algorithm/construct/LargestEmptyCircle.java
16  * https://github.com/locationtech/jts/commit/98274a7ea9b40651e9de6323dc10fb2cac17a245
17  *
18  **********************************************************************/
19 
20 #pragma once
21 
22 #include <geos/geom/Coordinate.h>
23 #include <geos/geom/Point.h>
24 #include <geos/geom/Envelope.h>
25 #include <geos/algorithm/construct/IndexedDistanceToPoint.h>
26 #include <geos/algorithm/locate/IndexedPointInAreaLocator.h>
27 #include <geos/operation/distance/IndexedFacetDistance.h>
28 
29 #include <memory>
30 #include <queue>
31 
32 namespace geos {
33 namespace geom {
34 class Coordinate;
35 class Envelope;
36 class Geometry;
37 class GeometryFactory;
38 class LineString;
39 class Point;
40 }
41 }
42 
43 namespace geos {
44 namespace algorithm { // geos::algorithm
45 namespace construct { // geos::algorithm::construct
46 
75 class GEOS_DLL LargestEmptyCircle {
77 
78 public:
79 
88  LargestEmptyCircle(const geom::Geometry* p_obstacles, double p_tolerance);
89 
102  LargestEmptyCircle(const geom::Geometry* p_obstacles, const geom::Geometry* p_boundary, double p_tolerance);
103 
104  ~LargestEmptyCircle() = default;
105 
115  static std::unique_ptr<geom::Point> getCenter(const geom::Geometry* p_obstacles, double p_tolerance);
116 
126  static std::unique_ptr<geom::LineString> getRadiusLine(const geom::Geometry* p_obstacles, double p_tolerance);
127 
128  std::unique_ptr<geom::Point> getCenter();
129  std::unique_ptr<geom::Point> getRadiusPoint();
130  std::unique_ptr<geom::LineString> getRadiusLine();
131 
132 
133 private:
134 
135  /* private members */
136  double tolerance;
137  const geom::Geometry* obstacles;
138  std::unique_ptr<geom::Geometry> boundary;
139  const geom::GeometryFactory* factory;
140  geom::Envelope gridEnv;
141  bool done;
142  std::unique_ptr<algorithm::locate::IndexedPointInAreaLocator> boundaryPtLocater;
143  IndexedDistanceToPoint obstacleDistance;
144  std::unique_ptr<IndexedFacetDistance> boundaryDistance;
145  geom::CoordinateXY centerPt;
146  geom::CoordinateXY radiusPt;
147 
158  double distanceToConstraints(const geom::Coordinate& c);
159  double distanceToConstraints(double x, double y);
160  void initBoundary();
161  void compute();
162 
163  /* private class */
164  class Cell {
165  private:
166  static constexpr double SQRT2 = 1.4142135623730951;
167  double x;
168  double y;
169  double hSize;
170  double distance;
171  double maxDist;
172 
173  public:
174  Cell(double p_x, double p_y, double p_hSize, double p_distanceToConstraints)
175  : x(p_x)
176  , y(p_y)
177  , hSize(p_hSize)
178  , distance(p_distanceToConstraints)
179  , maxDist(p_distanceToConstraints + (p_hSize*SQRT2))
180  {};
181 
182  geom::Envelope getEnvelope() const
183  {
184  geom::Envelope env(x-hSize, x+hSize, y-hSize, y+hSize);
185  return env;
186  }
187 
188  bool isFullyOutside() const
189  {
190  return maxDist < 0.0;
191  }
192  bool isOutside() const
193  {
194  return distance < 0.0;
195  }
196  double getMaxDistance() const
197  {
198  return maxDist;
199  }
200  double getDistance() const
201  {
202  return distance;
203  }
204  double getHSize() const
205  {
206  return hSize;
207  }
208  double getX() const
209  {
210  return x;
211  }
212  double getY() const
213  {
214  return y;
215  }
216  bool operator< (const Cell& rhs) const
217  {
218  return maxDist < rhs.maxDist;
219  }
220  bool operator> (const Cell& rhs) const
221  {
222  return maxDist > rhs.maxDist;
223  }
224  bool operator==(const Cell& rhs) const
225  {
226  return maxDist == rhs.maxDist;
227  }
228  };
229 
230  bool mayContainCircleCenter(const Cell& cell, const Cell& farthestCell);
231  void createInitialGrid(const geom::Envelope* env, std::priority_queue<Cell>& cellQueue);
232  Cell createCentroidCell(const geom::Geometry* geom);
233 
234 };
235 
236 
237 } // geos::algorithm::construct
238 } // geos::algorithm
239 } // geos
Computes the distance between a point and a geometry (which may be a collection containing any type o...
Definition: IndexedDistanceToPoint.h:41
Definition: LargestEmptyCircle.h:75
static std::unique_ptr< geom::Point > getCenter(const geom::Geometry *p_obstacles, double p_tolerance)
LargestEmptyCircle(const geom::Geometry *p_obstacles, const geom::Geometry *p_boundary, double p_tolerance)
static std::unique_ptr< geom::LineString > getRadiusLine(const geom::Geometry *p_obstacles, double p_tolerance)
LargestEmptyCircle(const geom::Geometry *p_obstacles, double p_tolerance)
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:217
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:197
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: Angle.h:25