s2json_core/geometry/
mod.rs

1/// BBox and BBox3D shapes and utilities
2pub mod bbox;
3/// Impls that we want to hide to make the code more readable
4pub mod impls;
5/// Primitive geometry types (used by GeoJSON spec)
6pub mod primitive;
7/// Vector geometry types (used by the s2json spec for both WGS84 and S2Geometry)
8pub mod vector;
9/// The VectorPoint struct is a powerful tool for 2D and 3D points
10pub mod vector_point;
11
12use crate::Face;
13pub use bbox::*;
14pub use primitive::*;
15use serde::{Deserialize, Serialize};
16pub use vector::*;
17pub use vector_point::*;
18
19/// Trait to extract the x and y values
20pub trait GetXY {
21    /// Returns the x value
22    fn x(&self) -> f64;
23    /// Returns the y value
24    fn y(&self) -> f64;
25    /// Returns the x and y values
26    fn xy(&self) -> (f64, f64) {
27        (self.x(), self.y())
28    }
29}
30/// Trait to extract the z value
31pub trait GetZ {
32    /// Returns the z value
33    fn z(&self) -> Option<f64>;
34}
35/// Trait to extract the m value
36pub trait GetM<M> {
37    /// Returns the m value
38    fn m(&self) -> Option<&M>;
39}
40
41/// Composite Trait: XY + Z
42pub trait GetXYZ: GetXY + GetZ {}
43impl<T> GetXYZ for T where T: GetXY + GetZ {}
44/// Composite Trait: XY + M
45pub trait GetXYM<M>: GetXY + GetM<M> {}
46impl<T, M> GetXYM<M> for T where T: GetXY + GetM<M> {}
47/// Composite Trait: XY + Z + M
48pub trait GetXYZM<M>: GetXY + GetZ + GetM<M> {}
49impl<T, M> GetXYZM<M> for T where T: GetXY + GetZ + GetM<M> {}
50
51/// Trait to set the x and y values
52pub trait SetXY {
53    /// Set the x value
54    fn set_x(&mut self, x: f64);
55    /// Set the y value
56    fn set_y(&mut self, y: f64);
57    /// Set both x and y
58    fn set_xy(&mut self, x: f64, y: f64) {
59        self.set_x(x);
60        self.set_y(y);
61    }
62}
63/// Trait to set the z value
64pub trait SetZ {
65    /// Set the z value
66    fn set_z(&mut self, z: f64);
67}
68/// Trait to set the m value
69pub trait SetM<M> {
70    /// Set the m value
71    fn set_m(&mut self, m: M);
72}
73
74/// Composite Trait: XY + Z
75pub trait SetXYZ: SetXY + SetZ {
76    /// Set x, y and z
77    fn set_xyz(&mut self, x: f64, y: f64, z: f64) {
78        self.set_xy(x, y);
79        self.set_z(z);
80    }
81}
82impl<T> SetXYZ for T where T: SetXY + SetZ {}
83/// Composite Trait: XY + M
84pub trait SetXYM<M>: SetXY + SetM<M> {
85    /// Set x, y and m
86    fn set_xym(&mut self, x: f64, y: f64, m: M) {
87        self.set_xy(x, y);
88        self.set_m(m);
89    }
90}
91impl<T, M> SetXYM<M> for T where T: SetXY + SetM<M> {}
92/// Composite Trait: XY + Z + M
93pub trait SetXYZM<M>: SetXY + SetZ + SetM<M> {
94    /// Set x, y, z and m
95    fn set_xyzm(&mut self, x: f64, y: f64, z: f64, m: M) {
96        self.set_xy(x, y);
97        self.set_z(z);
98        self.set_m(m);
99    }
100}
101impl<T, M> SetXYZM<M> for T where T: SetXY + SetZ + SetM<M> {}
102
103/// Trait to create a new XY
104pub trait NewXY {
105    /// Create a new point with xy
106    fn new_xy(x: f64, y: f64) -> Self;
107}
108/// Trait to create a new XY
109pub trait NewXYM<M> {
110    /// Create a new point with xy
111    fn new_xym(x: f64, y: f64, m: M) -> Self;
112}
113/// Trait to create a new XYZ
114pub trait NewXYZ {
115    /// Create a new point with xyz
116    fn new_xyz(x: f64, y: f64, z: f64) -> Self;
117}
118/// Trait to create a new XYZM
119pub trait NewXYZM<M> {
120    /// Create a new point with xyzm
121    fn new_xyzm(x: f64, y: f64, z: f64, m: M) -> Self;
122}
123
124// Finally lets make "full" traits for ease of use
125
126/// Composite Trait for XY use cases
127pub trait FullXY: GetXYZ + SetXY + NewXY + Clone + PartialEq + Ord {}
128impl<T> FullXY for T where T: GetXYZ + SetXY + NewXY + Clone + PartialEq + Ord {}
129
130/// Composite Trait for XYZ use cases
131pub trait FullXYZ: GetXYZ + SetXYZ + NewXYZ + Clone + PartialEq + Ord {}
132impl<T> FullXYZ for T where T: GetXYZ + SetXYZ + NewXYZ + Clone + PartialEq + Ord {}
133
134/// Composite Trait for XYZM use cases
135pub trait FullXYZM<M>: GetXYZM<M> + SetXYZM<M> + NewXYZM<M> + Clone + PartialEq + Ord {}
136impl<T, M> FullXYZM<M> for T where T: GetXYZM<M> + SetXYZM<M> + NewXYZM<M> + Clone + PartialEq + Ord {}
137
138/// The axis to apply an operation to
139#[derive(Debug, PartialEq, Eq, Clone, Copy)]
140pub enum Axis {
141    /// X axis
142    X = 0,
143    /// Y axis
144    Y = 1,
145}
146
147/// A Point in S2 Space with a Face
148#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
149pub struct STPoint<M> {
150    /// The face of the point
151    pub face: Face,
152    /// The s coordinate
153    pub s: f64,
154    /// The t coordinate
155    pub t: f64,
156    /// The z coordinate
157    pub z: Option<f64>,
158    /// The m coordinate
159    pub m: Option<M>,
160}