Skip to main content

box3d_rust/math_functions/
validate.rs

1// Validity checks (is_valid_*). Part of the math_functions module.
2
3use super::*;
4use crate::constants;
5
6/// Is this a valid number? Not NaN or infinity.
7pub fn is_valid_float(a: f32) -> bool {
8    if a.is_nan() {
9        return false;
10    }
11
12    if a.is_infinite() {
13        return false;
14    }
15
16    true
17}
18
19/// Is this a valid vector? Not NaN or infinity.
20pub fn is_valid_vec3(a: Vec3) -> bool {
21    if a.x.is_nan() || a.y.is_nan() || a.z.is_nan() {
22        return false;
23    }
24
25    if a.x.is_infinite() || a.y.is_infinite() || a.z.is_infinite() {
26        return false;
27    }
28
29    true
30}
31
32/// Is this a valid quaternion? Not NaN or infinity. Is normalized.
33pub fn is_valid_quat(a: Quat) -> bool {
34    if a.v.x.is_nan() || a.v.y.is_nan() || a.v.z.is_nan() || a.s.is_nan() {
35        return false;
36    }
37
38    if a.v.x.is_infinite() || a.v.y.is_infinite() || a.v.z.is_infinite() || a.s.is_infinite() {
39        return false;
40    }
41
42    is_normalized_quat(a)
43}
44
45/// Is this a valid transform? Not NaN or infinity. Is normalized.
46pub fn is_valid_transform(a: Transform) -> bool {
47    is_valid_vec3(a.p) && is_valid_quat(a.q)
48}
49
50/// Is this a valid matrix? Not NaN or infinity.
51pub fn is_valid_matrix3(a: Matrix3) -> bool {
52    is_valid_vec3(a.cx) && is_valid_vec3(a.cy) && is_valid_vec3(a.cz)
53}
54
55/// Is this a valid bounding box? Not Nan or infinity. Upper bound greater than or equal to lower bound.
56pub fn is_valid_aabb(a: Aabb) -> bool {
57    if !is_valid_vec3(a.lower_bound) {
58        return false;
59    }
60
61    if !is_valid_vec3(a.upper_bound) {
62        return false;
63    }
64
65    if a.lower_bound.x > a.upper_bound.x {
66        return false;
67    }
68
69    if a.lower_bound.y > a.upper_bound.y {
70        return false;
71    }
72
73    if a.lower_bound.z > a.upper_bound.z {
74        return false;
75    }
76
77    true
78}
79
80/// Is this AABB reasonably close to the origin? See B3_HUGE.
81pub fn is_bounded_aabb(a: Aabb) -> bool {
82    let huge = constants::huge();
83    if a.lower_bound.x < -huge || a.lower_bound.y < -huge || a.lower_bound.z < -huge {
84        return false;
85    }
86
87    if a.upper_bound.x > huge || a.upper_bound.y > huge || a.upper_bound.z > huge {
88        return false;
89    }
90
91    true
92}
93
94/// Is this AABB valid and reasonable?
95pub fn is_sane_aabb(a: Aabb) -> bool {
96    if !is_valid_aabb(a) {
97        return false;
98    }
99
100    let huge = constants::huge();
101    if a.lower_bound.x < -huge || a.lower_bound.y < -huge || a.lower_bound.z < -huge {
102        return false;
103    }
104
105    if a.upper_bound.x > huge || a.upper_bound.y > huge || a.upper_bound.z > huge {
106        return false;
107    }
108
109    true
110}
111
112/// Is this a valid plane? Normal is a unit vector. Not Nan or infinity.
113pub fn is_valid_plane(a: Plane) -> bool {
114    if !is_valid_vec3(a.normal) {
115        return false;
116    }
117
118    if !is_normalized(a.normal) {
119        return false;
120    }
121
122    is_valid_float(a.offset)
123}
124
125/// Is this a valid world position? Not NaN or infinity.
126pub fn is_valid_position(p: Pos) -> bool {
127    if p.x.is_nan() || p.y.is_nan() || p.z.is_nan() {
128        return false;
129    }
130
131    if p.x.is_infinite() || p.y.is_infinite() || p.z.is_infinite() {
132        return false;
133    }
134
135    true
136}
137
138/// Is this a valid world transform? Not NaN or infinity. Rotation is normalized.
139pub fn is_valid_world_transform(t: WorldTransform) -> bool {
140    is_valid_position(t.p) && is_valid_quat(t.q)
141}