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
//! The [`DistanceStrategy`] trait, default strategy selection, and
//! the [`Reversed`] argument-swapping adapter.
//!
//! Mirrors three pieces of Boost.Geometry that collaborate to make
//! `boost::geometry::distance(a, b)` work for any geometry pair in
//! any coordinate system, with implicit or explicit strategy
//! selection:
//!
//! * `boost/geometry/strategies/distance.hpp` — the
//! `boost::geometry::strategy::distance` namespace and the per-CS
//! strategy concept.
//! * `boost/geometry/strategies/distance/services.hpp` — the
//! `services::default_strategy<G1, G2>` metafunction that picks
//! the right strategy for a CS pair (Pythagoras for cartesian,
//! Haversine / Andoyer for spherical / geographic, …).
//! * `boost/geometry/strategies/default_strategy.hpp` plus
//! `boost/geometry/core/reverse_dispatch.hpp` and the partial
//! specialisation at
//! `boost/geometry/algorithms/detail/distance/interface.hpp:53-77`
//! — the symmetry-collapsing layer that gives you
//! `distance(segment, point)` for free once you have
//! `distance(point, segment)`.
//!
//! T21 lands the *trait surface* only — no concrete strategies yet.
//! T22 adds `Pythagoras` / `ComparablePythagoras` and the first
//! [`DefaultDistance`] impls; T23 wires this into the free-function
//! `geometry-algorithm::distance` entry point.
use CoordinateScalar;
use CoordinateSystem;
use ;
/// A strategy for computing the distance between two geometries.
///
/// Mirrors the per-CS strategy concept declared in
/// `boost/geometry/strategies/distance.hpp` and refined per
/// coordinate system in `strategies/{cartesian,spherical,geographic}/
/// distance_*.hpp`. The Boost concept exposes
/// `apply(g1, g2)` plus a `return_type<G1, G2>` metafunction plus a
/// `comparable_type<...>` sibling; we collapse those three onto one
/// Rust trait via associated types.
///
/// # Associated items
///
/// * [`Self::Out`] — the scalar the distance comes back as.
/// Equivalent to Boost's
/// `boost::geometry::strategy::distance::services::return_type<...>`
/// (`strategies/distance/services.hpp`). Typically the wider of
/// `A`'s and `B`'s coordinate scalars after promotion.
/// * [`Self::Comparable`] — the "skip the sqrt" form of
/// this strategy. For Pythagoras this is `ComparablePythagoras`;
/// for strategies where there is nothing to save by deferring a
/// square root (Andoyer, Vincenty), set `type Comparable = Self;`
/// and the optimiser collapses the indirection. Mirrors
/// `comparable_type<...>` from the same Boost services header.
///
/// # Method shape
///
/// `distance(&self, &A, &B)` mirrors `apply(g1, g2)` on Boost's
/// strategy structs (e.g.
/// `strategies/cartesian/distance_pythagoras.hpp:75-95` for the
/// comparable form, `:99-115` for the sqrt-paying form).
/// `comparable(&self)` mirrors `get_comparable(strategy)` from
/// `strategies/distance/services.hpp`.
/// "Which distance strategy do we pick by default for a pair of
/// coordinate-system *families* `Self` (for the first geometry's
/// CS family) and `Other` (for the second)?"
///
/// Mirrors `boost::geometry::strategy::distance::services::
/// default_strategy<Tag1, Tag2, Point1, Point2, CsTag1, CsTag2>`
/// from `boost/geometry/strategies/distance/services.hpp`, together
/// with the per-CS specialisations in
/// `strategies/{cartesian,spherical,geographic}/distance.hpp` and
/// the umbrella `strategies/default_strategy.hpp`.
///
/// In Boost the dispatch keys on the *coordinate-system tag*
/// (`cs_tag<Point>::type`). The Rust analogue is the
/// [`CoordinateSystem::Family`] associated type defined in
/// `geometry-cs` — every concrete CS (`Cartesian`,
/// `Spherical<Degree>`, `Spherical<Radian>`, `Geographic<Degree>`,
/// …) reports one of the family marker types
/// (`CartesianFamily`, `SphericalFamily`, `GeographicFamily`,
/// `PolarFamily`).
///
/// Implementations land in `geometry-strategy` alongside each
/// concrete strategy:
///
/// ```ignore
/// impl DefaultDistance<CartesianFamily> for CartesianFamily { type Strategy = Pythagoras; }
/// impl DefaultDistance<SphericalFamily> for SphericalFamily { type Strategy = Haversine; }
/// impl DefaultDistance<GeographicFamily> for GeographicFamily { type Strategy = Andoyer; }
/// ```
///
/// The `Strategy: Default` bound matches Boost's expectation that
/// `services::default_strategy<...>::type` is default-constructible
/// (every `strategies/.../distance_*.hpp` concrete strategy has a
/// no-arg constructor).
/// Type alias resolving the default distance strategy for the pair
/// `(A, B)` by walking
/// `A -> A::Point -> Cs -> Family -> DefaultDistance<…B's Family…>::Strategy`.
///
/// Replaces the C++ free function
/// `boost::geometry::strategy::distance::services::
/// default_strategy<G1, G2>::type` from
/// `strategies/distance/services.hpp`, plumbed through the
/// per-CS files.
///
/// At call sites in `geometry-algorithm::distance`, this is the
/// type the strategy-less `distance(a, b)` overload monomorphises
/// against:
///
/// ```ignore
/// pub fn distance<A: Geometry, B: Geometry>(a: &A, b: &B)
/// -> <DefaultDistanceStrategy<A, B> as DistanceStrategy<A, B>>::Out
/// where
/// DefaultDistanceStrategy<A, B>: DistanceStrategy<A, B>,
/// {
/// DefaultDistanceStrategy::<A, B>::default().distance(a, b)
/// }
/// ```
pub type DefaultDistanceStrategy<A, B> =
Strategy;
/// `Reversed<S>` lifts a `DistanceStrategy<A, B>` into a
/// `DistanceStrategy<B, A>` by calling the inner strategy with the
/// arguments swapped.
///
/// Replaces `boost::geometry::reverse_dispatch`
/// (`boost/geometry/core/reverse_dispatch.hpp`) together with the
/// partial specialisation that uses it in
/// `boost/geometry/algorithms/detail/distance/interface.hpp:53-77`.
/// Boost has one such specialisation *per algorithm*; in Rust the
/// symmetry is expressed once, at the strategy-trait layer, with a
/// single blanket impl below — every algorithm that goes through a
/// `DistanceStrategy<A, B>` automatically gets the swap.
///
/// # Cost
///
/// `#[repr(transparent)]` is deliberately *not* applied — `Reversed`
/// is a one-field tuple struct and monomorphisation collapses the
/// indirection. There is no runtime overhead: the wrapper exists
/// solely to give the trait system a *different* `Self` type to
/// dispatch on, the same way `boost::geometry::detail::distance::
/// reverse_dispatch_call` exists in C++ only to give the partial
/// specialisation something to bind to.
;