GEOS  3.10.3
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.
11  * See the COPYING file for more information.
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 setDensifyFraction(double dFrac);
127 
128  double
129  distance()
130  {
131  compute(g0, g1);
132  return ptDist.getDistance();
133  }
134 
135  double
136  orientedDistance()
137  {
138  computeOrientedDistance(g0, g1, ptDist);
139  return ptDist.getDistance();
140  }
141 
142  const std::array<geom::Coordinate, 2>
143  getCoordinates() const
144  {
145  return ptDist.getCoordinates();
146  }
147 
148  class MaxPointDistanceFilter : public geom::CoordinateFilter {
149  public:
150  MaxPointDistanceFilter(const geom::Geometry& p_geom)
151  :
152  geom(p_geom)
153  {}
154 
155  void
156  filter_ro(const geom::Coordinate* pt) override
157  {
158  minPtDist.initialize();
159  DistanceToPoint::computeDistance(geom, *pt,
160  minPtDist);
161  maxPtDist.setMaximum(minPtDist);
162  }
163 
164  const PointPairDistance&
165  getMaxPointDistance() const
166  {
167  return maxPtDist;
168  }
169 
170  private:
171  PointPairDistance maxPtDist;
172  PointPairDistance minPtDist;
173  DistanceToPoint euclideanDist;
174  const geom::Geometry& geom;
175 
176  // Declare type as noncopyable
177  MaxPointDistanceFilter(const MaxPointDistanceFilter& other);
178  MaxPointDistanceFilter& operator=(const MaxPointDistanceFilter& rhs);
179  };
180 
181  class MaxDensifiedByFractionDistanceFilter
182  : public geom::CoordinateSequenceFilter {
183  public:
184 
185  MaxDensifiedByFractionDistanceFilter(
186  const geom::Geometry& p_geom, double fraction)
187  :
188  geom(p_geom),
189  // Validity of the cast to size_t has been verified in setDensifyFraction()
190  numSubSegs(std::size_t(util::round(1.0 / fraction)))
191  {
192  }
193 
194  void filter_ro(const geom::CoordinateSequence& seq,
195  std::size_t index) override;
196 
197  bool
198  isGeometryChanged() const override
199  {
200  return false;
201  }
202 
203  bool
204  isDone() const override
205  {
206  return false;
207  }
208 
209  const PointPairDistance&
210  getMaxPointDistance() const
211  {
212  return maxPtDist;
213  }
214 
215  private:
216  PointPairDistance maxPtDist;
217  PointPairDistance minPtDist;
218  const geom::Geometry& geom;
219  std::size_t numSubSegs; // = 0;
220 
221  // Declare type as noncopyable
222  MaxDensifiedByFractionDistanceFilter(const MaxDensifiedByFractionDistanceFilter& other);
223  MaxDensifiedByFractionDistanceFilter& operator=(const MaxDensifiedByFractionDistanceFilter& rhs);
224  };
225 
226 private:
227 
228  void
229  compute(const geom::Geometry& p_g0,
230  const geom::Geometry& p_g1)
231  {
232  computeOrientedDistance(p_g0, p_g1, ptDist);
233  computeOrientedDistance(p_g1, p_g0, ptDist);
234  }
235 
236  void computeOrientedDistance(const geom::Geometry& discreteGeom,
237  const geom::Geometry& geom,
238  PointPairDistance& ptDist);
239 
240  const geom::Geometry& g0;
241 
242  const geom::Geometry& g1;
243 
244  PointPairDistance ptDist;
245 
247  double densifyFrac; // = 0.0;
248 
249  // Declare type as noncopyable
250  DiscreteHausdorffDistance(const DiscreteHausdorffDistance& other) = delete;
251  DiscreteHausdorffDistance& operator=(const DiscreteHausdorffDistance& rhs) = delete;
252 };
253 
254 } // geos::algorithm::distance
255 } // geos::algorithm
256 } // geos
257 
258 #ifdef _MSC_VER
259 #pragma warning(pop)
260 #endif
261 
262 #endif // GEOS_ALGORITHM_DISTANCE_DISCRETEHAUSDORFFDISTANCE_H
263 
An algorithm for computing a distance metric which is an approximation to the Hausdorff Distance base...
Definition: DiscreteHausdorffDistance.h:100
Geometry classes support the concept of applying a coordinate filter to every coordinate in the Geome...
Definition: CoordinateFilter.h:43
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:188
double round(double val)
Definition: math.h:36
Basic namespace for all GEOS functionalities.
Definition: Angle.h:26