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
//! The [`Point`] concept and the [`fold_dims`] dimension iterator.
//!
//! Mirrors `boost/geometry/geometries/concepts/point_concept.hpp`
//! together with the per-dimension `traits::access<P, D>::{get,set}`
//! free-function pair declared in
//! `boost/geometry/core/access.hpp:270-313`.
//!
//! Design notes (`specs/rust-port-proposal.md` §3.2):
//! * The five C++ trait specialisations collapse to four associated
//! items on one trait plus the super-bound on [`Geometry`].
//! * `get` and `set` take the dimension as a *const generic*, the
//! Rust analogue of Boost's `template <std::size_t Dim>` parameter
//! on `traits::access`. Out-of-range access becomes a compile
//! error (cf. `tests/ui/get_out_of_range.rs`).
//! * Dimension-by-dimension recursion (the C++ idiom in
//! `strategies/cartesian/distance_pythagoras.hpp:44-66`) is
//! expressed by [`fold_dims`], which descends through a private
//! sealed [`Recurse`] trait.
//!
//! Phase 01 split (KC1.T1): the v1 `Point` trait fused `Point` and
//! `ConstPoint` from the C++ side. It now carries only the *read*
//! surface; the *write* surface (`set::<D>`) lives on a
//! [`PointMut`]`: Point` subtrait. Mirrors the `Point` / `ConstPoint`
//! concept pair in
//! `boost/geometry/geometries/concepts/point_concept.hpp:82-133`.
use CoordinateScalar;
use CoordinateSystem;
use PointTag;
use crateGeometry;
/// The Point concept.
///
/// Models `doc/concept/point.qbk` — equivalent in C++ to the five
/// trait specialisations spelled out in
/// `boost/geometry/geometries/concepts/point_concept.hpp` and the
/// per-dimension accessor declared in
/// `boost/geometry/core/access.hpp:270-313`. The five C++ pieces
/// collapse here to four associated items plus the super-bound:
///
/// | C++ trait | Rust counterpart |
/// |---|---|
/// | `traits::tag<P>` (`core/tag.hpp`) | `Geometry<Kind = PointTag>` |
/// | `traits::dimension<P>` (`core/coordinate_dimension.hpp`) | `const DIM: usize` |
/// | `traits::coordinate_type<P>` (`core/coordinate_type.hpp`) | `type Scalar` |
/// | `traits::coordinate_system<P>` (`core/coordinate_system.hpp`) | `type Cs` |
/// | `traits::access<P, D>::get` (`core/access.hpp:270-313`) | `fn get::<D>` |
///
/// The read-write half — `traits::access<P, D>::set` — lives on the
/// [`PointMut`] subtrait, mirroring the way Boost's mutating `Point`
/// concept sits above the read-only `ConstPoint`.
///
/// The `Point = Self` projection on the [`Geometry`] super-trait is
/// the Rust spelling of Boost's "a Point is its own point type"
/// invariant — `point_type<P>::type = P` whenever
/// `tag<P>::type = point_tag`.
///
/// # Examples
///
/// ```
/// use geometry_cs::Cartesian;
/// use geometry_tag::PointTag;
/// use geometry_trait::{Geometry, Point};
///
/// #[derive(Default)]
/// struct Xy { x: f64, y: f64 }
///
/// impl Geometry for Xy { type Kind = PointTag; type Point = Self; }
///
/// impl Point for Xy {
/// type Scalar = f64;
/// type Cs = Cartesian;
/// const DIM: usize = 2;
/// fn get<const D: usize>(&self) -> f64 { if D == 0 { self.x } else { self.y } }
/// }
///
/// let p = Xy { x: 3.0, y: 4.0 };
/// assert_eq!(p.get::<0>(), 3.0);
/// ```
/// The *mutating* half of the Point concept.
///
/// Mirrors Boost's read-write `Point` concept sitting above the
/// read-only `ConstPoint` in
/// `boost/geometry/geometries/concepts/point_concept.hpp:82-133`.
/// Algorithms that need to *construct* a Point — e.g. the
/// [`segment_start`](crate::segment_start) /
/// [`segment_end`](crate::segment_end) materialisers, the
/// [`box_min`](crate::box_min) / [`box_max`](crate::box_max) helpers,
/// the envelope output builder in `geometry-strategy::envelope` —
/// bound on `PointMut`; everything
/// else (`distance`, `length`, `area`, `within`, `intersects`,
/// `equals`, `comparable_distance`) bounds on the read-only
/// [`Point`] only.
///
/// Implementers that already provide [`Point`] get `PointMut` for
/// almost free: add `impl PointMut for MyPoint { fn set::<D>(…) }`
/// next to the existing `impl Point`. Every kernel concrete `Point`
/// impl (model, derive, `Adapt` array/tuple, `WithCs`) ships the
/// matching `PointMut` impl.
///
/// # Examples
///
/// ```
/// use geometry_cs::Cartesian;
/// use geometry_tag::PointTag;
/// use geometry_trait::{Geometry, Point, PointMut};
///
/// #[derive(Default)]
/// struct Xy { x: f64, y: f64 }
/// impl Geometry for Xy { type Kind = PointTag; type Point = Self; }
/// impl Point for Xy {
/// type Scalar = f64;
/// type Cs = Cartesian;
/// const DIM: usize = 2;
/// fn get<const D: usize>(&self) -> f64 { if D == 0 { self.x } else { self.y } }
/// }
/// impl PointMut for Xy {
/// fn set<const D: usize>(&mut self, v: f64) { if D == 0 { self.x = v } else { self.y = v } }
/// }
///
/// let mut p = Xy::default();
/// p.set::<0>(3.0);
/// assert_eq!(p.get::<0>(), 3.0);
/// ```
/// Fold a closure over the dimensions `0, 1, …, P::DIM - 1` of a
/// [`Point`] at compile time.
///
/// Used by Pythagoras, equality, transforms — anything that needs
/// to iterate over coordinates without paying a runtime bounds
/// check. Mirrors the C++ template-recursion pattern in
/// `boost/geometry/strategies/cartesian/distance_pythagoras.hpp:44-66`
/// (the `detail::compute_pythagoras<I, T>` recursion).
///
/// The closure is invoked once per dimension, in ascending order.
/// It receives the running accumulator, the point, and the
/// dimension index (as a `usize`, only as a label — `get::<D>`
/// itself must still be called with a const-generic `D`, which is
/// what the `Recurse` sealed trait below provides for the
/// dimensions kernel code actually needs).
///
/// # Supported dimensions
///
/// Const-generic recursion via `I + 1` requires the unstable
/// `generic_const_exprs` feature. To stay on stable, the
/// recursive descent is unrolled by `Recurse` for the dimensions
/// the kernel needs (1‥=`MAX_DIM`). `Point` impls with
/// `DIM > MAX_DIM` are a compile-time error at the call site —
/// raise `MAX_DIM` and add the matching `impl_recurse!` row.
///
/// # Examples
///
/// ```
/// use geometry_cs::Cartesian;
/// use geometry_tag::PointTag;
/// use geometry_trait::{fold_dims, Geometry, Point};
///
/// #[derive(Default)]
/// struct Xy { x: f64, y: f64 }
/// impl Geometry for Xy { type Kind = PointTag; type Point = Self; }
/// impl Point for Xy {
/// type Scalar = f64;
/// type Cs = Cartesian;
/// const DIM: usize = 2;
/// fn get<const D: usize>(&self) -> f64 { if D == 0 { self.x } else { self.y } }
/// }
///
/// let p = Xy { x: 3.0, y: 4.0 };
/// // Fold the closure over each dimension; the recursion supplies the
/// // dimension index, the closure does its own per-D dispatch.
/// let sum = fold_dims(0.0_f64, &p, |acc, p, i| {
/// let v = match i { 0 => p.get::<0>(), 1 => p.get::<1>(), _ => 0.0 };
/// acc + v
/// });
/// assert_eq!(sum, 7.0);
/// ```
///
/// In practice algorithm code uses [`fold_dims`] indirectly via
/// the `Recurse` machinery already wired below: the closure can
/// rely on the dimension index being supplied to it, but
/// `Point::get::<D>` calls inside the closure are issued with
/// hard-coded `D`s by the recursion impls themselves.
///
/// # Panics
///
/// Panics if `P::DIM` exceeds `MAX_DIM`. Raise `MAX_DIM` and
/// add the matching `impl_recurse!` row to support a wider point.
/// Largest `DIM` the const-recursive [`fold_dims`] supports on
/// stable Rust. Raise this constant and add the corresponding
/// `impl_recurse!` row below when a strategy needs more.
///
/// Three is the working assumption (XYZ); four is included so
/// future homogeneous-coordinate kernels do not stall waiting for
/// `generic_const_exprs` to stabilise.
pub const MAX_DIM: usize = 4;
/// Marker carrying a `(cursor, end)` pair of dimensions to descend
/// over. Private; reachable only via [`fold_dims`].
;
/// Sealed const-recursive iterator over the dimensions of a
/// [`Point`]. The two const parameters are the *current* dimension
/// `I` and the *end* dimension `N` (= `P::DIM`).
///
/// Mirrors the `template <size_t I, typename T> compute_pythagoras`
/// recursion in
/// `boost/geometry/strategies/cartesian/distance_pythagoras.hpp:44-66`,
/// except the C++ side counts *down* from `I = DIM` to `0` while
/// we count *up* — same shape, more natural Rust read order.
///
/// On nightly with `generic_const_exprs` this trait would collapse
/// to a single blanket impl
/// `impl<const I: usize, const N: usize> Recurse<I, N> for Cursor<I, N>
/// where Cursor<{I + 1}, N>: Recurse<{I + 1}, N>`. On stable we
/// unroll the recursion via the [`impl_recurse!`] macro below for
/// every dimension up to [`MAX_DIM`].
// All (I, N) pairs with 0 <= I < N <= MAX_DIM. Keep `MAX_DIM` and
// this block in sync.
impl_recurse!;
impl_recurse!;
impl_recurse!;
impl_recurse!;
impl_recurse!;
impl_recurse!;
impl_recurse!;
impl_recurse!;
impl_recurse!;
impl_recurse!;