1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// src/ops/hausdorff.rs
//! The discrete Hausdorff distance, `hausdorff_distance`, and its exact
//! predicate, `hausdorff_within`.
//!
//! # The definition, and whose it is
//!
//! The **discrete Hausdorff distance** between `a` and `b` is the larger of
//! two directed values:
//!
//! - `h(a → b)`: the largest, over the vertices `v` of `a`, of the distance
//! from `v` to the **linework** of `b`;
//! - `h(b → a)`: the same with the roles swapped.
//!
//! The linework of a geometry is its boundary elements, as in
//! [`distance`](super::distance()): the segments of its lines, the segments
//! of every polygon ring (exterior and holes), and its isolated points. **A
//! polygon is measured by its rings: its interior does not count.** A vertex
//! of `a` strictly inside a polygon of `b` contributes its distance to the
//! nearest ring of that polygon, not 0. The vertices of a geometry are its
//! stored coordinates: points, line vertices, and ring vertices.
//!
//! This is the definition of JTS's `DiscreteHausdorffDistance`
//! (`org.locationtech.jts.algorithm.distance`, Martin Davis). It visits every
//! coordinate of one geometry and measures it with `DistanceToPoint.computeDistance`,
//! which for a polygon measures to the exterior ring and to each hole, for a line
//! to each segment, and for a point to the point. Matching it means the number is
//! the one PostGIS's `ST_HausdorffDistance` and Shapely report for the same input
//! (without densification). A *region* reading (a polygon measured as a point set,
//! interior at distance 0) is a different quantity and is **not** what this function returns.
//! Densified variants are not offered.
//!
//! The discrete value never exceeds the continuous Hausdorff distance
//! between the same linework: it maximizes over a subset (the vertices) of
//! the points the continuous one maximizes over.
//!
//! # The predicate is exact
//!
//! [`hausdorff_within`]`(a, b, d)` is `true` exactly when every vertex of `a`
//! is within `d` of the linework of `b`, and every vertex of `b` is within `d`
//! of the linework of `a`. Each test is one of `arithmetica`'s exact
//! predicates, point to point
//! ([`compare_point_distance`](arithmetica::compare_point_distance)) or point
//! to closed segment
//! ([`compare_segment_distance`](arithmetica::compare_segment_distance)). These
//! are the predicates [`dwithin`](super::dwithin) decides boundary elements
//! with, so the answer is exact for every input. The first vertex that fails
//! ends the search.
//!
//! # The quantity: an exact zero, and a bound
//!
//! [`hausdorff_distance`] is **exactly `0.0` if and only if every vertex of
//! each geometry lies on the linework of the other**, decided by the same
//! exact predicates at `d = 0` (a point on a closed segment, or equal to a
//! point). Otherwise it is an `f64` within `δ = c·u·M` of the true value,
//! with `c = 64`, `u = 2⁻⁵³` and `M` the largest coordinate magnitude in
//! either operand. This is the bound of [`distance`](super::distance()),
//! whose module documentation derives it for one point-to-element distance.
//!
//! **The bound carries through maxima and minima.** Let `xᵢ` be computed
//! values with `|xᵢ − Xᵢ| ≤ δ` for each `i`. With `Xₖ` the largest true
//! value, `max x ≥ xₖ ≥ Xₖ − δ`; and `max x = xⱼ ≤ Xⱼ + δ ≤ max X + δ`. So
//! `|max x − max X| ≤ δ`, and the same argument with the inequalities
//! reversed gives `|min x − min X| ≤ δ`. The discrete Hausdorff distance is
//! a maximum (over both directions and their vertices) of minima (over the
//! other side's elements) of point-to-element distances, each within `δ`;
//! so the computed value is within `δ` of the true one.
//!
//! **One guard remains.** When the zero test fails, the true value is positive, but
//! every computed point-to-segment distance could round to `0.0` (a vertex
//! a hair off a segment). Returning `0.0` would read as "the zero test
//! holds"; the function returns the smallest positive `f64`, `2⁻¹⁰⁷⁴`,
//! instead. The true value is then at most `δ`, so the result is within
//! `max(δ, 2⁻¹⁰⁷⁴)` of it, the bound stated on the function.
//!
//! **Consistency with the predicate** follows: if
//! `hausdorff_within(a, b, d)` is `false`, the true value exceeds `d`, so
//! `hausdorff_distance(a, b) > d − δ`; if `true`, the true value is at most
//! `d`, so `hausdorff_distance(a, b) ≤ d + δ`. `hausdorff_distance(a, b) <=
//! d` is not the predicate: within `δ` of the true value, the
//! comparison can fall on either side.
//!
//! # Indexing never changes the answer
//!
//! Above a fixed number of vertex–element pairs (the threshold `distance`
//! uses), each direction indexes the target's elements in a static box index
//! (through the crate's private `situs` seam) and queries it once per vertex.
//! For the predicate the query window is the vertex grown outward by `d`; for
//! the quantity, by `(best + δ)` rounded upward, where `best` is the vertex's
//! smallest distance so far. These are the windows, and the pruning argument,
//! of [`distance`](super::distance()): an element outside the window has true
//! distance greater than `best + δ`, so its computed distance exceeds `best`
//! and cannot change the minimum. The minimum of `f64` values does not depend
//! on the order they are visited in, so the indexed result equals the
//! exhaustive one bit for bit.
//!
//! # Errors and empties
//!
//! `d` must be finite and at least 0 ([`GeometryError::InvalidTolerance`]).
//! An empty operand gives `false` from the predicate and
//! [`GeometryError::EmptyOperand`] from the quantity, as for `dwithin` and
//! `distance`. No topology is consulted, so a collection whose polygonal
//! leaves overlap is accepted: its linework is well defined.
//!
//! Neither function returns a coordinate.
use ControlFlow;
use ;
use crateGeometryError;
use crate;
use crateCoord2d;
// ===========================================================================
// The public functions
// ===========================================================================
/// Whether the discrete Hausdorff distance between `a` and `b` is at most
/// `d`, decided **exactly**.
///
/// `true` when every vertex of `a` is within `d` of the linework of `b`
/// (its segments, ring segments and points; a polygon's interior does not
/// count), and every vertex of `b` within `d` of the linework of `a`. Each
/// test is an exact `arithmetica` predicate; see the
/// [module documentation](self) for the definition.
///
/// ```
/// use geometres::ops::hausdorff_within;
/// use geometres::types::{Coord2d, CoordSeq, LineString};
///
/// let line = |v: &[(f64, f64)]| {
/// LineString::new(CoordSeq::new(v.iter().map(|&(x, y)| Coord2d::new(x, y)).collect()))
/// };
/// let a = line(&[(0.0, 0.0), (10.0, 0.0)])?;
/// let b = line(&[(0.0, 3.0), (10.0, 3.0)])?;
/// assert!(hausdorff_within(&a, &b, 3.0)?);
/// assert!(!hausdorff_within(&a, &b, 3.0_f64.next_down())?);
/// # Ok::<(), geometres::GeometryError>(())
/// ```
///
/// An empty operand gives `false`.
///
/// # Errors
///
/// - [`GeometryError::InvalidTolerance`] for a negative or non-finite `d`.
/// - [`GeometryError::TooManyEntries`] for more than `u32::MAX` elements on
/// an indexed side.
/// The discrete Hausdorff distance between `a` and `b`: **exactly `0.0` if
/// and only if every vertex of each lies on the linework of the other**,
/// and otherwise an `f64` within `max(64·u·M, 2⁻¹⁰⁷⁴)` of the true value,
/// with `u = 2⁻⁵³` and `M` the largest coordinate magnitude in either
/// operand (the definition and the derivation are in the
/// [module documentation](self)).
///
/// For the question "is the Hausdorff distance at most `d`?", use
/// [`hausdorff_within`], which is exact: if it is `false` then this value
/// is greater than `d − 64·u·M`, and if `true` then at most `d + 64·u·M`.
///
/// # Errors
///
/// - [`GeometryError::EmptyOperand`] when either operand has no points.
/// - [`GeometryError::TooManyEntries`], as for [`hausdorff_within`].
// ===========================================================================
// Vertices and targets
// ===========================================================================
/// Every vertex of `g`, in storage order: points, line vertices, and each
/// polygon ring's stored vertices (exterior then holes; rings are stored
/// without a closing vertex). Collections are walked depth first.
pub
/// What a vertex is measured to: the linework of `g`, meaning its segments,
/// ring segments and points, as `distance` enumerates them.
pub
// ===========================================================================
// The exact directed predicate
// ===========================================================================
/// Whether every vertex in `vs` is within `d` of some element of `es`,
/// decided exactly. Stops at the first vertex that is not.
pub
/// Whether `v` is within `d` of some element of `es`, exhaustively.
// ===========================================================================
// The f64 directed value
// ===========================================================================
/// `h(vs → es)` in `f64`: the largest, over `vs`, of the smallest computed
/// distance to an element of `es`. Exhaustive below the pair threshold,
/// indexed above it, with the same result bit for bit (module
/// documentation).
pub
/// `h(vs → es)` over every pair.
pub
/// `h(vs → es)` through the box index. Each vertex's minimum is seeded
/// from the element nearest the previous vertex (consecutive vertices are
/// usually close, so the first window is already small), then searched in
/// the window grown by `(best + δ)` rounded upward.
pub