Skip to main content

geometry_model/
geometry_rebind.rs

1//! Type-level stock-model rebinding for scalar and coordinate-system types.
2//!
3//! Mirrors `boost::geometry::helper_geometry` from
4//! `geometries/helper_geometry.hpp:38-121`.
5
6use geometry_coords::CoordinateScalar;
7use geometry_cs::CoordinateSystem;
8
9use crate::{
10    Box, Linestring, MultiLinestring, MultiPoint, MultiPolygon, Point, Polygon, Ring, Segment,
11};
12
13/// Select the stock model matching `Self` while replacing coordinate
14/// scalar and coordinate-system types.
15///
16/// Mirrors the specializations of `helper_geometry` for point, box,
17/// linestring, and ring in `geometries/helper_geometry.hpp:57-103`.
18/// Boost accepts any adapted geometry through tag dispatch; this Rust port is
19/// deliberately defined for the stock model types, which are the only types
20/// it can reconstruct without a user-provided output constructor.
21pub trait RebindGeometry<T: CoordinateScalar, Cs: CoordinateSystem> {
22    /// The corresponding rebound stock model.
23    type Output;
24}
25
26/// Rebound model for `G` with scalar `T` and coordinate system `Cs`.
27///
28/// Mirrors `helper_geometry<Geometry, NewCoordinateType, NewUnits>::type` at
29/// `geometries/helper_geometry.hpp:108-120`. Rust takes the complete output
30/// coordinate-system type instead of a C++ angular-units tag.
31pub type Rebound<G, T, Cs> = <G as RebindGeometry<T, Cs>>::Output;
32
33impl<A, const D: usize, OldCs, T, Cs> RebindGeometry<T, Cs> for Point<A, D, OldCs>
34where
35    A: CoordinateScalar,
36    OldCs: CoordinateSystem,
37    T: CoordinateScalar,
38    Cs: CoordinateSystem,
39{
40    type Output = Point<T, D, Cs>;
41}
42
43impl<A, const D: usize, OldCs, T, Cs> RebindGeometry<T, Cs> for Box<Point<A, D, OldCs>>
44where
45    A: CoordinateScalar,
46    OldCs: CoordinateSystem,
47    T: CoordinateScalar,
48    Cs: CoordinateSystem,
49{
50    type Output = Box<Point<T, D, Cs>>;
51}
52
53impl<A, const D: usize, OldCs, T, Cs> RebindGeometry<T, Cs> for Linestring<Point<A, D, OldCs>>
54where
55    A: CoordinateScalar,
56    OldCs: CoordinateSystem,
57    T: CoordinateScalar,
58    Cs: CoordinateSystem,
59{
60    type Output = Linestring<Point<T, D, Cs>>;
61}
62
63impl<A, const D: usize, OldCs, T, Cs, const CW: bool, const CL: bool> RebindGeometry<T, Cs>
64    for Ring<Point<A, D, OldCs>, CW, CL>
65where
66    A: CoordinateScalar,
67    OldCs: CoordinateSystem,
68    T: CoordinateScalar,
69    Cs: CoordinateSystem,
70{
71    type Output = Ring<Point<T, D, Cs>, CW, CL>;
72}
73
74impl<A, const D: usize, OldCs, T, Cs> RebindGeometry<T, Cs> for Segment<Point<A, D, OldCs>>
75where
76    A: CoordinateScalar,
77    OldCs: CoordinateSystem,
78    T: CoordinateScalar,
79    Cs: CoordinateSystem,
80{
81    type Output = Segment<Point<T, D, Cs>>;
82}
83
84impl<A, const D: usize, OldCs, T, Cs> RebindGeometry<T, Cs> for MultiPoint<Point<A, D, OldCs>>
85where
86    A: CoordinateScalar,
87    OldCs: CoordinateSystem,
88    T: CoordinateScalar,
89    Cs: CoordinateSystem,
90{
91    type Output = MultiPoint<Point<T, D, Cs>>;
92}
93
94impl<A, const D: usize, OldCs, T, Cs, const CW: bool, const CL: bool> RebindGeometry<T, Cs>
95    for Polygon<Point<A, D, OldCs>, CW, CL>
96where
97    A: CoordinateScalar,
98    OldCs: CoordinateSystem,
99    T: CoordinateScalar,
100    Cs: CoordinateSystem,
101{
102    type Output = Polygon<Point<T, D, Cs>, CW, CL>;
103}
104
105impl<A, const D: usize, OldCs, T, Cs> RebindGeometry<T, Cs>
106    for MultiLinestring<Linestring<Point<A, D, OldCs>>>
107where
108    A: CoordinateScalar,
109    OldCs: CoordinateSystem,
110    T: CoordinateScalar,
111    Cs: CoordinateSystem,
112{
113    type Output = MultiLinestring<Linestring<Point<T, D, Cs>>>;
114}
115
116impl<A, const D: usize, OldCs, T, Cs, const CW: bool, const CL: bool> RebindGeometry<T, Cs>
117    for MultiPolygon<Polygon<Point<A, D, OldCs>, CW, CL>>
118where
119    A: CoordinateScalar,
120    OldCs: CoordinateSystem,
121    T: CoordinateScalar,
122    Cs: CoordinateSystem,
123{
124    type Output = MultiPolygon<Polygon<Point<T, D, Cs>, CW, CL>>;
125}