GEOS  3.13.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 
44 
45 namespace geos {
46 namespace algorithm { // geos::algorithm
47 namespace construct { // geos::algorithm::construct
48 
77 class GEOS_DLL LargestEmptyCircle {
78 
79 public:
80 
89  LargestEmptyCircle(const geom::Geometry* p_obstacles, double p_tolerance);
90 
103  LargestEmptyCircle(const geom::Geometry* p_obstacles, const geom::Geometry* p_boundary, double p_tolerance);
104 
105  ~LargestEmptyCircle() = default;
106 
116  static std::unique_ptr<geom::Point> getCenter(const geom::Geometry* p_obstacles, double p_tolerance);
117 
127  static std::unique_ptr<geom::LineString> getRadiusLine(const geom::Geometry* p_obstacles, double p_tolerance);
128 
129  std::unique_ptr<geom::Point> getCenter();
130  std::unique_ptr<geom::Point> getRadiusPoint();
131  std::unique_ptr<geom::LineString> getRadiusLine();
132 
133 
134 private:
135 
136  /* private members */
137  double tolerance;
138  const geom::Geometry* obstacles;
139  std::unique_ptr<geom::Geometry> boundary;
140  const geom::GeometryFactory* factory;
141  geom::Envelope gridEnv;
142  bool done;
143  std::unique_ptr<algorithm::locate::IndexedPointInAreaLocator> boundaryPtLocater;
144  IndexedDistanceToPoint obstacleDistance;
145  std::unique_ptr<IndexedFacetDistance> boundaryDistance;
146  geom::CoordinateXY centerPt;
147  geom::CoordinateXY radiusPt;
148 
159  double distanceToConstraints(const geom::Coordinate& c);
160  double distanceToConstraints(double x, double y);
161  void initBoundary();
162  void compute();
163 
164  /* private class */
165  class Cell {
166  private:
167  static constexpr double SQRT2 = 1.4142135623730951;
168  double x;
169  double y;
170  double hSize;
171  double distance;
172  double maxDist;
173 
174  public:
175  Cell(double p_x, double p_y, double p_hSize, double p_distanceToConstraints)
176  : x(p_x)
177  , y(p_y)
178  , hSize(p_hSize)
179  , distance(p_distanceToConstraints)
180  , maxDist(p_distanceToConstraints + (p_hSize*SQRT2))
181  {};
182 
183  geom::Envelope getEnvelope() const
184  {
185  geom::Envelope env(x-hSize, x+hSize, y-hSize, y+hSize);
186  return env;
187  }
188 
189  bool isFullyOutside() const
190  {
191  return maxDist < 0.0;
192  }
193  bool isOutside() const
194  {
195  return distance < 0.0;
196  }
197  double getMaxDistance() const
198  {
199  return maxDist;
200  }
201  double getDistance() const
202  {
203  return distance;
204  }
205  double getHSize() const
206  {
207  return hSize;
208  }
209  double getX() const
210  {
211  return x;
212  }
213  double getY() const
214  {
215  return y;
216  }
217  bool operator< (const Cell& rhs) const
218  {
219  return maxDist < rhs.maxDist;
220  }
221  bool operator> (const Cell& rhs) const
222  {
223  return maxDist > rhs.maxDist;
224  }
225  bool operator==(const Cell& rhs) const
226  {
227  return maxDist == rhs.maxDist;
228  }
229  };
230 
231  bool mayContainCircleCenter(const Cell& cell, const Cell& farthestCell);
232  void createInitialGrid(const geom::Envelope* env, std::priority_queue<Cell>& cellQueue);
233  Cell createCentroidCell(const geom::Geometry* geom);
234 
235 };
236 
237 
238 } // geos::algorithm::construct
239 } // geos::algorithm
240 } // geos
Computes the distance between a point and a geometry (which may be a collection containing any type o...
Definition: IndexedDistanceToPoint.h:45
Definition: LargestEmptyCircle.h:77
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:216
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition: Envelope.h:58
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:65
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:186
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: Angle.h:25