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