GEOS 3.14.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.
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
38namespace geos {
39namespace geom {
40class Geometry;
41class Coordinate;
42//class CoordinateSequence;
43}
44}
45
46namespace geos {
47namespace algorithm { // geos::algorithm
48namespace distance { // geos::algorithm::distance
49
92public:
93
94 static double distance(const geom::Geometry& g0,
95 const geom::Geometry& g1);
96
97 static double distance(const geom::Geometry& g0,
98 const geom::Geometry& g1, double densifyFrac);
99
101 const geom::Geometry& p_g1)
102 :
103 g0(p_g0),
104 g1(p_g1),
105 ptDist(),
106 densifyFrac(0.0)
107 {}
108
117 void setDensifyFraction(double dFrac);
118
119 double
120 distance()
121 {
122 compute(g0, g1);
123 return ptDist.getDistance();
124 }
125
126 double
127 orientedDistance()
128 {
129 computeOrientedDistance(g0, g1, ptDist);
130 return ptDist.getDistance();
131 }
132
133 const std::array<geom::CoordinateXY, 2>
134 getCoordinates() const
135 {
136 return ptDist.getCoordinates();
137 }
138
139 class MaxPointDistanceFilter : public geom::CoordinateFilter {
140 public:
141 MaxPointDistanceFilter(const geom::Geometry& p_geom)
142 :
143 geom(p_geom)
144 {}
145
146 void
147 filter_ro(const geom::CoordinateXY* pt) override
148 {
149 minPtDist.initialize();
150 DistanceToPoint::computeDistance(geom, *pt,
151 minPtDist);
152 maxPtDist.setMaximum(minPtDist);
153 }
154
155 const PointPairDistance&
156 getMaxPointDistance() const
157 {
158 return maxPtDist;
159 }
160
161 private:
162 PointPairDistance maxPtDist;
163 PointPairDistance minPtDist;
164 DistanceToPoint euclideanDist;
165 const geom::Geometry& geom;
166
167 // Declare type as noncopyable
168 MaxPointDistanceFilter(const MaxPointDistanceFilter& other);
169 MaxPointDistanceFilter& operator=(const MaxPointDistanceFilter& rhs);
170 };
171
172 class MaxDensifiedByFractionDistanceFilter
173 : public geom::CoordinateSequenceFilter {
174 public:
175
176 MaxDensifiedByFractionDistanceFilter(
177 const geom::Geometry& p_geom, double fraction)
178 :
179 geom(p_geom),
180 // Validity of the cast to size_t has been verified in setDensifyFraction()
181 numSubSegs(std::size_t(util::round(1.0 / fraction)))
182 {
183 }
184
185 void filter_ro(const geom::CoordinateSequence& seq,
186 std::size_t index) override;
187
188 bool
189 isGeometryChanged() const override
190 {
191 return false;
192 }
193
194 bool
195 isDone() const override
196 {
197 return false;
198 }
199
200 const PointPairDistance&
201 getMaxPointDistance() const
202 {
203 return maxPtDist;
204 }
205
206 private:
207 PointPairDistance maxPtDist;
208 PointPairDistance minPtDist;
209 const geom::Geometry& geom;
210 std::size_t numSubSegs; // = 0;
211
212 // Declare type as noncopyable
213 MaxDensifiedByFractionDistanceFilter(const MaxDensifiedByFractionDistanceFilter& other);
214 MaxDensifiedByFractionDistanceFilter& operator=(const MaxDensifiedByFractionDistanceFilter& rhs);
215 };
216
217private:
218
219 void
220 compute(const geom::Geometry& p_g0,
221 const geom::Geometry& p_g1)
222 {
223 computeOrientedDistance(p_g0, p_g1, ptDist);
224 computeOrientedDistance(p_g1, p_g0, ptDist);
225 }
226
227 void computeOrientedDistance(const geom::Geometry& discreteGeom,
228 const geom::Geometry& geom,
229 PointPairDistance& ptDist);
230
231 const geom::Geometry& g0;
232
233 const geom::Geometry& g1;
234
235 PointPairDistance ptDist;
236
238 double densifyFrac; // = 0.0;
239
240 // Declare type as noncopyable
241 DiscreteHausdorffDistance(const DiscreteHausdorffDistance& other) = delete;
242 DiscreteHausdorffDistance& operator=(const DiscreteHausdorffDistance& rhs) = delete;
243};
244
245} // geos::algorithm::distance
246} // geos::algorithm
247} // geos
248
249#ifdef _MSC_VER
250#pragma warning(pop)
251#endif
252
An algorithm for computing a distance metric which is an approximation to the Hausdorff Distance base...
Definition DiscreteHausdorffDistance.h:91
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:197
double round(double val)
Definition math.h:36
Basic namespace for all GEOS functionalities.
Definition geos.h:39