Skip to main content

box2d_rust/math_functions/
validate.rs

1// Validity checks (is_valid_*). Part of the math_functions module.
2
3use super::*;
4
5/// Is this a valid number? Not NaN or infinity.
6pub fn is_valid_float(a: f32) -> bool {
7    if a.is_nan() {
8        return false;
9    }
10
11    if a.is_infinite() {
12        return false;
13    }
14
15    true
16}
17
18/// Is this a valid vector? Not NaN or infinity.
19pub fn is_valid_vec2(v: Vec2) -> bool {
20    if v.x.is_nan() || v.y.is_nan() {
21        return false;
22    }
23
24    if v.x.is_infinite() || v.y.is_infinite() {
25        return false;
26    }
27
28    true
29}
30
31/// Is this a valid rotation? Not NaN or infinity. Is normalized.
32pub fn is_valid_rotation(q: Rot) -> bool {
33    if q.s.is_nan() || q.c.is_nan() {
34        return false;
35    }
36
37    if q.s.is_infinite() || q.c.is_infinite() {
38        return false;
39    }
40
41    is_normalized_rot(q)
42}
43
44/// Is this a valid transform? Not NaN or infinity. Rotation is normalized.
45pub fn is_valid_transform(t: Transform) -> bool {
46    if !is_valid_vec2(t.p) {
47        return false;
48    }
49
50    is_valid_rotation(t.q)
51}
52
53/// Is this a valid bounding box? Not NaN or infinity. Upper bound greater than or equal to lower bound.
54pub fn is_valid_aabb(aabb: Aabb) -> bool {
55    let d = sub(aabb.upper_bound, aabb.lower_bound);
56    let mut valid = d.x >= 0.0 && d.y >= 0.0;
57    valid = valid && is_valid_vec2(aabb.lower_bound) && is_valid_vec2(aabb.upper_bound);
58    valid
59}
60
61/// Is this a valid plane? Normal is a unit vector. Not NaN or infinity.
62pub fn is_valid_plane(a: Plane) -> bool {
63    is_valid_vec2(a.normal) && is_normalized(a.normal) && is_valid_float(a.offset)
64}
65
66/// Is this a valid world position? Not NaN or infinity.
67pub fn is_valid_position(p: Pos) -> bool {
68    if p.x.is_nan() || p.y.is_nan() {
69        return false;
70    }
71
72    if p.x.is_infinite() || p.y.is_infinite() {
73        return false;
74    }
75
76    true
77}
78
79/// Is this a valid world transform? Not NaN or infinity. Rotation is normalized.
80pub fn is_valid_world_transform(t: WorldTransform) -> bool {
81    if !is_valid_position(t.p) {
82        return false;
83    }
84
85    is_valid_rotation(t.q)
86}