GEOS  3.9.0dev
DiscreteHausdorffDistance.h
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2009 Sandro Santilli <strk@kbt.io>
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.
12  *
13  **********************************************************************
14  *
15  * Last port: algorithm/distance/DiscreteHausdorffDistance.java 1.5 (JTS-1.10)
16  *
17  **********************************************************************/
18
19 #ifndef GEOS_ALGORITHM_DISTANCE_DISCRETEHAUSDORFFDISTANCE_H
20 #define GEOS_ALGORITHM_DISTANCE_DISCRETEHAUSDORFFDISTANCE_H
21
22 #include <geos/export.h>
23 #include <geos/algorithm/distance/PointPairDistance.h> // for composition
24 #include <geos/algorithm/distance/DistanceToPoint.h> // for composition
25 #include <geos/util/IllegalArgumentException.h> // for inlines
26 #include <geos/geom/Geometry.h> // for inlines
27 #include <geos/util/math.h> // for inlines
28 #include <geos/geom/CoordinateFilter.h> // for inheritance
29 #include <geos/geom/CoordinateSequenceFilter.h> // for inheritance
30
31 #include <cstddef>
32 #include <vector>
33
34 #ifdef _MSC_VER
35 #pragma warning(push)
36 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
37 #endif
38
39 namespace geos {
40 namespace algorithm {
41 //class RayCrossingCounter;
42 }
43 namespace geom {
44 class Geometry;
45 class Coordinate;
46 //class CoordinateSequence;
47 }
48 namespace index {
49 namespace intervalrtree {
50 //class SortedPackedIntervalRTree;
51 }
52 }
53 }
54
55 namespace geos {
56 namespace algorithm { // geos::algorithm
57 namespace distance { // geos::algorithm::distance
58
101 public:
102
103  static double distance(const geom::Geometry& g0,
104  const geom::Geometry& g1);
105
106  static double distance(const geom::Geometry& g0,
107  const geom::Geometry& g1, double densifyFrac);
108
110  const geom::Geometry& p_g1)
111  :
112  g0(p_g0),
113  g1(p_g1),
114  ptDist(),
115  densifyFrac(0.0)
116  {}
117
126  void
127  setDensifyFraction(double dFrac)
128  {
129  if(dFrac > 1.0 || dFrac <= 0.0) {
131  "Fraction is not in range (0.0 - 1.0]");
132  }
133
134  densifyFrac = dFrac;
135  }
136
137  double
138  distance()
139  {
140  compute(g0, g1);
141  return ptDist.getDistance();
142  }
143
144  double
145  orientedDistance()
146  {
147  computeOrientedDistance(g0, g1, ptDist);
148  return ptDist.getDistance();
149  }
150
151  const std::array<geom::Coordinate, 2>
152  getCoordinates() const
153  {
154  return ptDist.getCoordinates();
155  }
156
157  class MaxPointDistanceFilter : public geom::CoordinateFilter {
158  public:
159  MaxPointDistanceFilter(const geom::Geometry& p_geom)
160  :
161  geom(p_geom)
162  {}
163
164  void
165  filter_ro(const geom::Coordinate* pt) override
166  {
167  minPtDist.initialize();
168  DistanceToPoint::computeDistance(geom, *pt,
169  minPtDist);
170  maxPtDist.setMaximum(minPtDist);
171  }
172
173  const PointPairDistance&
174  getMaxPointDistance() const
175  {
176  return maxPtDist;
177  }
178
179  private:
180  PointPairDistance maxPtDist;
181  PointPairDistance minPtDist;
182  DistanceToPoint euclideanDist;
183  const geom::Geometry& geom;
184
185  // Declare type as noncopyable
186  MaxPointDistanceFilter(const MaxPointDistanceFilter& other);
187  MaxPointDistanceFilter& operator=(const MaxPointDistanceFilter& rhs);
188  };
189
190  class MaxDensifiedByFractionDistanceFilter
191  : public geom::CoordinateSequenceFilter {
192  public:
193
194  MaxDensifiedByFractionDistanceFilter(
195  const geom::Geometry& p_geom, double fraction)
196  :
197  geom(p_geom),
198  numSubSegs(std::size_t(util::round(1.0 / fraction)))
199  {
200  }
201
202  void filter_ro(const geom::CoordinateSequence& seq,
203  std::size_t index) override;
204
205  bool
206  isGeometryChanged() const override
207  {
208  return false;
209  }
210
211  bool
212  isDone() const override
213  {
214  return false;
215  }
216
217  const PointPairDistance&
218  getMaxPointDistance() const
219  {
220  return maxPtDist;
221  }
222
223  private:
224  PointPairDistance maxPtDist;
225  PointPairDistance minPtDist;
226  const geom::Geometry& geom;
227  std::size_t numSubSegs; // = 0;
228
229  // Declare type as noncopyable
230  MaxDensifiedByFractionDistanceFilter(const MaxDensifiedByFractionDistanceFilter& other);
231  MaxDensifiedByFractionDistanceFilter& operator=(const MaxDensifiedByFractionDistanceFilter& rhs);
232  };
233
234 private:
235
236  void
237  compute(const geom::Geometry& p_g0,
238  const geom::Geometry& p_g1)
239  {
240  computeOrientedDistance(p_g0, p_g1, ptDist);
241  computeOrientedDistance(p_g1, p_g0, ptDist);
242  }
243
244  void computeOrientedDistance(const geom::Geometry& discreteGeom,
245  const geom::Geometry& geom,
246  PointPairDistance& ptDist);
247
248  const geom::Geometry& g0;
249
250  const geom::Geometry& g1;
251
252  PointPairDistance ptDist;
253
255  double densifyFrac; // = 0.0;
256
257  // Declare type as noncopyable
258  DiscreteHausdorffDistance(const DiscreteHausdorffDistance& other) = delete;
259  DiscreteHausdorffDistance& operator=(const DiscreteHausdorffDistance& rhs) = delete;
260 };
261
262 } // geos::algorithm::distance
263 } // geos::algorithm
264 } // geos
265
266 #ifdef _MSC_VER
267 #pragma warning(pop)
268 #endif
269
270 #endif // GEOS_ALGORITHM_DISTANCE_DISCRETEHAUSDORFFDISTANCE_H
271
void setDensifyFraction(double dFrac)
Definition: DiscreteHausdorffDistance.h:127
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:188
Indicates one or more illegal arguments.
Definition: IllegalArgumentException.h:34
An algorithm for computing a distance metric which is an approximation to the Hausdorff Distance base...
Definition: DiscreteHausdorffDistance.h:100
double round(double val)
Definition: math.h:36
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:26