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