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
//! Declarative macros that register a user-owned container as a
//! [`Linestring`], [`Ring`], or [`Polygon`].
//!
//! Mirrors the role of `BOOST_GEOMETRY_REGISTER_LINESTRING`
//! (`boost/geometry/geometries/register/linestring.hpp`) and
//! `BOOST_GEOMETRY_REGISTER_RING`
//! (`boost/geometry/geometries/register/ring.hpp`). Boost has no
//! `register/polygon.hpp` — adapting a polygon there is done by
//! hand-specialising five traits (see
//! `doc/example_adapting_a_legacy_geometry_object_model.qbk`); the
//! [`register_polygon!`] macro consolidates that pattern into one
//! declaration on the Rust side.
//!
//! Why macros instead of a blanket impl: Rust's orphan rule forbids
//! `impl<P: Point, C: AsRef<[P]>> Linestring for C` from a downstream
//! crate, so we mint the impl per-user-type with a macro — exactly
//! the role the `BOOST_GEOMETRY_REGISTER_*` macros play in C++ for
//! the same coherence reason (see proposal §3.7, Option D).
//!
//! The macros expand to references inside [`__macros`], a hidden
//! re-export module, so the user's `use` graph never has to mention
//! `geometry_trait` or `geometry_tag` directly.
/// Hidden re-exports the macros expand to. Not part of the public
/// API — users never name this path.
/// Register a user-owned multi-point container.
///
/// Emits the `Geometry<Kind = MultiPointTag>` and [`geometry_trait::MultiPoint`]
/// impls needed to expose the supplied point iterator. Mirrors
/// `BOOST_GEOMETRY_REGISTER_MULTI_POINT` from
/// `geometries/register/multi_point.hpp:36-40`; Rust additionally accepts the
/// iterator expression because native containers do not share a Boost.Range
/// base class.
/// Register a user-owned multi-linestring container.
///
/// Mirrors `BOOST_GEOMETRY_REGISTER_MULTI_LINESTRING` from
/// `geometries/register/multi_linestring.hpp:36-40`. The item type and
/// iterator are explicit on Rust's side because there is no common
/// Boost.Range-style container base.
/// Register a user-owned multi-polygon container.
///
/// Mirrors `BOOST_GEOMETRY_REGISTER_MULTI_POLYGON` from
/// `geometries/register/multi_polygon.hpp:36-40`. The item type and iterator
/// make the collection shape explicit in the generated Rust concept impl.
/// Register a user-owned linestring type whose storage is iterable as
/// `&Point` via a user-supplied expression.
///
/// Emits `impl Geometry for $Ty` with `Kind = LinestringTag` and
/// `Point = $Point`, plus `impl Linestring for $Ty` whose `points()`
/// returns the iterator the closure-shaped argument produces.
///
/// Mirrors `BOOST_GEOMETRY_REGISTER_LINESTRING` from
/// `boost/geometry/geometries/register/linestring.hpp`. The Boost
/// macro only has to specialise `traits::tag<G>` because the rest of
/// the linestring concept rides on `boost::range` for free; the Rust
/// counterpart has to spell the iterator out, which is why the
/// macro takes a closure-shaped expression for the iteration.
///
/// The iterator expression must yield an
/// `ExactSizeIterator<Item = &$Point> + Clone` — the same bound
/// [`geometry_trait::Linestring::points`] requires. For the common
/// case of a `Vec<$Point>` field, `|s| s.field.iter()` is the right
/// shape.
///
/// # Example
///
/// ```
/// use geometry_adapt::register_linestring;
/// use geometry_cs::Cartesian;
/// use geometry_model::Point2D;
/// use geometry_trait::Linestring;
///
/// struct MyLineString { points: Vec<Point2D<f64, Cartesian>> }
///
/// register_linestring!(MyLineString, Point2D<f64, Cartesian>, |s| s.points.iter());
///
/// let ls = MyLineString {
/// points: vec![Point2D::new(0.0, 0.0), Point2D::new(1.0, 1.0)],
/// };
/// assert_eq!(ls.points().count(), 2);
/// ```
/// Register a user-owned ring type whose storage is iterable as
/// `&Point` via a user-supplied expression.
///
/// Emits `impl Geometry for $Ty` with `Kind = RingTag` and
/// `Point = $Point`, plus `impl Ring for $Ty` whose `points()` returns
/// the iterator the closure-shaped argument produces. Optional
/// trailing `closure = …` and `point_order = …` clauses override the
/// defaults [`geometry_trait::Ring`] inherits from
/// `boost/geometry/core/{closure,point_order}.hpp` (closed,
/// clockwise).
///
/// Mirrors `BOOST_GEOMETRY_REGISTER_RING` from
/// `boost/geometry/geometries/register/ring.hpp`, plus the
/// per-ring `traits::closure<R>` / `traits::point_order<R>`
/// specialisations from `boost/geometry/core/closure.hpp` and
/// `boost/geometry/core/point_order.hpp`.
///
/// # Examples
///
/// Default-orientation ring (`closed`, `clockwise`):
///
/// ```
/// use geometry_adapt::register_ring;
/// use geometry_cs::Cartesian;
/// use geometry_model::Point2D;
/// use geometry_trait::Ring;
///
/// struct MyRing { points: Vec<Point2D<f64, Cartesian>> }
///
/// register_ring!(MyRing, Point2D<f64, Cartesian>, |s| s.points.iter());
///
/// let r = MyRing {
/// points: vec![
/// Point2D::new(0.0, 0.0),
/// Point2D::new(1.0, 0.0),
/// Point2D::new(1.0, 1.0),
/// Point2D::new(0.0, 0.0),
/// ],
/// };
/// assert_eq!(r.points().count(), 4);
/// ```
///
/// Override `closure` and `point_order`:
///
/// ```
/// use geometry_adapt::register_ring;
/// use geometry_cs::Cartesian;
/// use geometry_model::Point2D;
/// use geometry_trait::{Closure, PointOrder, Ring};
///
/// struct OpenCcwRing { points: Vec<Point2D<f64, Cartesian>> }
///
/// register_ring!(
/// OpenCcwRing,
/// Point2D<f64, Cartesian>,
/// |s| s.points.iter(),
/// closure = Closure::Open,
/// point_order = PointOrder::CounterClockwise
/// );
///
/// let r = OpenCcwRing { points: vec![] };
/// assert_eq!(r.closure(), Closure::Open);
/// assert_eq!(r.point_order(), PointOrder::CounterClockwise);
/// ```
/// Register a user-owned polygon type whose interior decomposes as
/// `{ outer: $Ring, inners: <iterable of $Ring> }`.
///
/// Emits `impl Geometry for $Ty` with `Kind = PolygonTag` and
/// `Point = $Point`, plus `impl Polygon for $Ty` with the supplied
/// `$Ring` as the associated `Ring` type. The `outer = …` expression
/// must produce `&$Ring`; the `inners = …` expression must produce
/// `impl ExactSizeIterator<Item = &$Ring>` — the bound
/// [`geometry_trait::Polygon::interiors`] requires.
///
/// Mirrors the five-trait pattern in
/// `doc/example_adapting_a_legacy_geometry_object_model.qbk`
/// (`tag`, `ring_const_type`, `interior_const_type`, `exterior_ring`,
/// `interior_rings`) — Boost.Geometry has no `register/polygon.hpp`,
/// so this macro is the equivalent shortcut on the Rust side.
///
/// # Example
///
/// ```
/// use geometry_adapt::{register_polygon, register_ring};
/// use geometry_cs::Cartesian;
/// use geometry_model::Point2D;
/// use geometry_trait::{Polygon, Ring};
///
/// type P = Point2D<f64, Cartesian>;
///
/// struct MyRing { points: Vec<P> }
/// register_ring!(MyRing, P, |s| s.points.iter());
///
/// struct MyPoly { outer: MyRing, inners: Vec<MyRing> }
/// register_polygon!(
/// MyPoly,
/// P,
/// ring = MyRing,
/// |s| outer = &s.outer, inners = s.inners.iter()
/// );
///
/// let poly = MyPoly {
/// outer: MyRing { points: vec![] },
/// inners: vec![],
/// };
/// assert_eq!(poly.exterior().points().count(), 0);
/// assert_eq!(poly.interiors().count(), 0);
/// ```