GEOS  3.11.0
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 #pragma once
20 
21 #include <geos/export.h>
22 #include <geos/algorithm/distance/PointPairDistance.h> // for composition
23 #include <geos/algorithm/distance/DistanceToPoint.h> // for composition
24 #include <geos/util/IllegalArgumentException.h> // for inlines
25 #include <geos/geom/Geometry.h> // for inlines
26 #include <geos/util/math.h> // for inlines
27 #include <geos/geom/CoordinateFilter.h> // for inheritance
28 #include <geos/geom/CoordinateSequenceFilter.h> // for inheritance
29 
30 #include <cstddef>
31 #include <vector>
32 
33 #ifdef _MSC_VER
34 #pragma warning(push)
35 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
36 #endif
37 
38 namespace geos {
39 namespace algorithm {
40 //class RayCrossingCounter;
41 }
42 namespace geom {
43 class Geometry;
44 class Coordinate;
45 //class CoordinateSequence;
46 }
47 namespace index {
48 namespace intervalrtree {
49 //class SortedPackedIntervalRTree;
50 }
51 }
52 }
53 
54 namespace geos {
55 namespace algorithm { // geos::algorithm
56 namespace distance { // geos::algorithm::distance
57 
99 class GEOS_DLL DiscreteHausdorffDistance {
100 public:
101 
102  static double distance(const geom::Geometry& g0,
103  const geom::Geometry& g1);
104 
105  static double distance(const geom::Geometry& g0,
106  const geom::Geometry& g1, double densifyFrac);
107 
109  const geom::Geometry& p_g1)
110  :
111  g0(p_g0),
112  g1(p_g1),
113  ptDist(),
114  densifyFrac(0.0)
115  {}
116 
125  void setDensifyFraction(double dFrac);
126 
127  double
128  distance()
129  {
130  compute(g0, g1);
131  return ptDist.getDistance();
132  }
133 
134  double
135  orientedDistance()
136  {
137  computeOrientedDistance(g0, g1, ptDist);
138  return ptDist.getDistance();
139  }
140 
141  const std::array<geom::Coordinate, 2>
142  getCoordinates() const
143  {
144  return ptDist.getCoordinates();
145  }
146 
147  class MaxPointDistanceFilter : public geom::CoordinateFilter {
148  public:
149  MaxPointDistanceFilter(const geom::Geometry& p_geom)
150  :
151  geom(p_geom)
152  {}
153 
154  void
155  filter_ro(const geom::Coordinate* pt) override
156  {
157  minPtDist.initialize();
158  DistanceToPoint::computeDistance(geom, *pt,
159  minPtDist);
160  maxPtDist.setMaximum(minPtDist);
161  }
162 
163  const PointPairDistance&
164  getMaxPointDistance() const
165  {
166  return maxPtDist;
167  }
168 
169  private:
170  PointPairDistance maxPtDist;
171  PointPairDistance minPtDist;
172  DistanceToPoint euclideanDist;
173  const geom::Geometry& geom;
174 
175  // Declare type as noncopyable
176  MaxPointDistanceFilter(const MaxPointDistanceFilter& other);
177  MaxPointDistanceFilter& operator=(const MaxPointDistanceFilter& rhs);
178  };
179 
180  class MaxDensifiedByFractionDistanceFilter
181  : public geom::CoordinateSequenceFilter {
182  public:
183 
184  MaxDensifiedByFractionDistanceFilter(
185  const geom::Geometry& p_geom, double fraction)
186  :
187  geom(p_geom),
188  // Validity of the cast to size_t has been verified in setDensifyFraction()
189  numSubSegs(std::size_t(util::round(1.0 / fraction)))
190  {
191  }
192 
193  void filter_ro(const geom::CoordinateSequence& seq,
194  std::size_t index) override;
195 
196  bool
197  isGeometryChanged() const override
198  {
199  return false;
200  }
201 
202  bool
203  isDone() const override
204  {
205  return false;
206  }
207 
208  const PointPairDistance&
209  getMaxPointDistance() const
210  {
211  return maxPtDist;
212  }
213 
214  private:
215  PointPairDistance maxPtDist;
216  PointPairDistance minPtDist;
217  const geom::Geometry& geom;
218  std::size_t numSubSegs; // = 0;
219 
220  // Declare type as noncopyable
221  MaxDensifiedByFractionDistanceFilter(const MaxDensifiedByFractionDistanceFilter& other);
222  MaxDensifiedByFractionDistanceFilter& operator=(const MaxDensifiedByFractionDistanceFilter& rhs);
223  };
224 
225 private:
226 
227  void
228  compute(const geom::Geometry& p_g0,
229  const geom::Geometry& p_g1)
230  {
231  computeOrientedDistance(p_g0, p_g1, ptDist);
232  computeOrientedDistance(p_g1, p_g0, ptDist);
233  }
234 
235  void computeOrientedDistance(const geom::Geometry& discreteGeom,
236  const geom::Geometry& geom,
237  PointPairDistance& ptDist);
238 
239  const geom::Geometry& g0;
240 
241  const geom::Geometry& g1;
242 
243  PointPairDistance ptDist;
244 
246  double densifyFrac; // = 0.0;
247 
248  // Declare type as noncopyable
249  DiscreteHausdorffDistance(const DiscreteHausdorffDistance& other) = delete;
250  DiscreteHausdorffDistance& operator=(const DiscreteHausdorffDistance& rhs) = delete;
251 };
252 
253 } // geos::algorithm::distance
254 } // geos::algorithm
255 } // geos
256 
257 #ifdef _MSC_VER
258 #pragma warning(pop)
259 #endif
260 
An algorithm for computing a distance metric which is an approximation to the Hausdorff Distance base...
Definition: DiscreteHausdorffDistance.h:99
Geometry classes support the concept of applying a coordinate filter to every coordinate in the Geome...
Definition: CoordinateFilter.h:41
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:186
double round(double val)
Definition: math.h:36
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25