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 XYZ
109pub trait NewXYZ {
110    /// Create a new point with xyz
111    fn new_xyz(x: f64, y: f64, z: f64) -> Self;
112}
113/// Trait to create a new XYZM
114pub trait NewXYZM<M> {
115    /// Create a new point with xyzm
116    fn new_xyzm(x: f64, y: f64, z: f64, m: M) -> Self;
117}
118
119// Finally lets make "full" traits for ease of use
120
121/// Composite Trait for XY use cases
122pub trait FullXY: GetXYZ + SetXY + NewXY + Clone + PartialEq + Ord {}
123impl<T> FullXY for T where T: GetXYZ + SetXY + NewXY + Clone + PartialEq + Ord {}
124
125/// Composite Trait for XYZ use cases
126pub trait FullXYZ: GetXYZ + SetXYZ + NewXYZ + Clone + PartialEq + Ord {}
127impl<T> FullXYZ for T where T: GetXYZ + SetXYZ + NewXYZ + Clone + PartialEq + Ord {}
128
129/// Composite Trait for XYZM use cases
130pub trait FullXYZM<M>: GetXYZM<M> + SetXYZM<M> + NewXYZM<M> + Clone + PartialEq + Ord {}
131impl<T, M> FullXYZM<M> for T where T: GetXYZM<M> + SetXYZM<M> + NewXYZM<M> + Clone + PartialEq + Ord {}
132
133/// The axis to apply an operation to
134#[derive(Debug, PartialEq, Eq, Clone, Copy)]
135pub enum Axis {
136    /// X axis
137    X = 0,
138    /// Y axis
139    Y = 1,
140}
141
142/// A Point in S2 Space with a Face
143#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
144pub struct STPoint<M> {
145    /// The face of the point
146    pub face: Face,
147    /// The s coordinate
148    pub s: f64,
149    /// The t coordinate
150    pub t: f64,
151    /// The z coordinate
152    pub z: Option<f64>,
153    /// The m coordinate
154    pub m: Option<M>,
155}