meshmeshmesh/
bounding_box.rs

1
2/// Represents a three-dimensional bounding box (AABB: axis-aligned bounding box).
3///
4/// # Example
5///
6/// ```
7/// use meshmeshmesh::bounding_box::BoundingBox;
8///
9/// let result = BoundingBox::new(1.5, 1.65, -2.3, 0.7, 3.9, 4.1);
10/// assert_eq!(result.min_x, 1.5);
11/// assert_eq!(result.max_x, 1.65);
12/// assert_eq!(result.min_y, -2.3);
13/// assert_eq!(result.max_y, 0.7);
14/// assert_eq!(result.min_z, 3.9);
15/// assert_eq!(result.max_z, 4.1);
16/// ```
17pub struct BoundingBox {
18    /// Minimum x value.
19    pub min_x: f64,
20    /// Maximum x value.
21    pub max_x: f64,
22    /// Minimum y value.
23    pub min_y: f64,
24    /// Maximum y value.
25    pub max_y: f64,
26    /// Minimum z value.
27    pub min_z: f64,
28    /// Maximum z value.
29    pub max_z: f64,
30}
31
32impl PartialEq for BoundingBox {
33    fn eq(&self, other: &Self) -> bool {
34        self.min_x == other.min_x && self.max_x == other.max_x &&
35        self.min_y == other.min_y && self.max_y == other.max_y &&
36        self.min_z == other.min_z && self.max_z == other.max_z
37    }
38}
39
40impl BoundingBox {
41    /// Creates a new [Bounding Box](BoundingBox)
42    ///
43    /// # Example
44    ///
45    /// ```
46    /// use meshmeshmesh::bounding_box::BoundingBox;
47    ///
48    /// let result = BoundingBox::new(1.5, 1.65, -2.3, 0.7, 3.9, 4.1);
49    /// assert_eq!(result.min_x, 1.5);
50    /// assert_eq!(result.max_x, 1.65);
51    /// assert_eq!(result.min_y, -2.3);
52    /// assert_eq!(result.max_y, 0.7);
53    /// assert_eq!(result.min_z, 3.9);
54    /// assert_eq!(result.max_z, 4.1);
55    /// ```
56    pub fn new(min_x: f64, max_x: f64, min_y: f64, max_y: f64, min_z: f64, max_z: f64) -> BoundingBox {
57        if min_x > max_x {
58            panic!("Invalid BoundingBox (min x > max_x)");
59        }
60        if min_y > max_y {
61            panic!("Invalid BoundingBox (min y > max_y)");
62        }
63        if min_z > max_z {
64            panic!("Invalid BoundingBox (min z > max_z)");
65        }
66
67        BoundingBox {min_x, max_x, min_y, max_y, min_z, max_z}
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn test_new() {
77        let result = BoundingBox::new(1.5, 1.65, -2.3, 0.7, 3.9, 4.1);
78        assert_eq!(result.min_x, 1.5);
79        assert_eq!(result.max_x, 1.65);
80        assert_eq!(result.min_y, -2.3);
81        assert_eq!(result.max_y, 0.7);
82        assert_eq!(result.min_z, 3.9);
83        assert_eq!(result.max_z, 4.1);
84    }
85
86    #[test]
87    fn test_new_empty_y() {
88        let result = BoundingBox::new(1.5, 1.65, -2.3, -2.3, 3.9, 4.1);
89        assert_eq!(result.min_x, 1.5);
90        assert_eq!(result.max_x, 1.65);
91        assert_eq!(result.min_y, -2.3);
92        assert_eq!(result.max_y, -2.3);
93        assert_eq!(result.min_z, 3.9);
94        assert_eq!(result.max_z, 4.1);
95    }
96
97    #[test]
98    #[should_panic(expected = "Invalid BoundingBox (min x > max_x)")]
99    fn test_new_wrong_x_should_panic() {
100        BoundingBox::new(1.5, 1.45, -2.3, 0.7, 3.9, 4.1);
101    }
102
103    #[test]
104    #[should_panic(expected = "Invalid BoundingBox (min y > max_y)")]
105    fn test_new_wrong_y_should_panic() {
106        BoundingBox::new(1.5, 1.65, -2.3, -2.30001, 3.9, 4.1);
107    }
108
109    #[test]
110    #[should_panic(expected = "Invalid BoundingBox (min z > max_z)")]
111    fn test_new_wrong_z_should_panic() {
112        BoundingBox::new(1.5, 1.65, -2.3, 0.7, 4.101, 4.1);
113    }
114
115    #[test]
116    fn test_partialeq_true() {
117        let a = BoundingBox::new(1.5, 1.65, -2.3, 0.7, 3.9, 4.1);
118        let b = BoundingBox::new(1.5, 1.65, -2.3, 0.7, 3.9, 4.1);
119        assert_eq!(a.eq(&b), true);
120        assert_eq!(b.eq(&a), true);
121    }
122
123    #[test]
124    fn test_partialeq_x_different_false() {
125        let a = BoundingBox::new(1.5, 1.65, -2.3, 0.7, 3.9, 4.1);
126        let b = BoundingBox::new(1.5, 1.66, -2.3, 0.7, 3.9, 4.1);
127        assert_eq!(a.eq(&b), false);
128        assert_eq!(b.eq(&a), false);
129    }
130
131    #[test]
132    fn test_partialeq_y_different_false() {
133        let a = BoundingBox::new(1.5, 1.65, -2.3, 0.71, 3.9, 4.1);
134        let b = BoundingBox::new(1.5, 1.65, -2.3, 0.7, 3.9, 4.1);
135        assert_eq!(a.eq(&b), false);
136        assert_eq!(b.eq(&a), false);
137    }
138
139    #[test]
140    fn test_partialeq_z_different_false() {
141        let a = BoundingBox::new(1.5, 1.65, -2.3, 0.7, 3.9, 4.12);
142        let b = BoundingBox::new(1.5, 1.65, -2.3, 0.7, 3.9, 4.1);
143        assert_eq!(a.eq(&b), false);
144        assert_eq!(b.eq(&a), false);
145    }
146
147    #[test]
148    fn test_partialeq_all_different_false() {
149        let a = BoundingBox::new(1.51, 1.65, -2.31, 0.7, 3.91, 4.1);
150        let b = BoundingBox::new(1.5, 1.651, -2.3, 0.71, 3.9, 4.12);
151        assert_eq!(a.eq(&b), false);
152        assert_eq!(b.eq(&a), false);
153    }
154}