1
2pub struct BoundingBox {
18 pub min_x: f64,
20 pub max_x: f64,
22 pub min_y: f64,
24 pub max_y: f64,
26 pub min_z: f64,
28 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 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}