1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
mod cone;
mod coord;
mod plane;
mod rect;
mod sphere;

pub use {
    self::{cone::Cone, plane::Plane, sphere::Sphere},
    glam::{mat4, quat, vec2, vec3, vec4, Mat3, Mat4, Quat, Vec2, Vec3, Vec4},
};

use self::{coord::Coord as GenericCoord, rect::Rect as GenericRect};

pub type Area = GenericRect<u32, u32>;
pub type Coord8 = GenericCoord<u8>;
pub type Coord = GenericCoord<i32>;
pub type CoordF = GenericCoord<f32>;
pub type Extent = GenericCoord<u32>;
pub type Rect = GenericRect<u32, i32>;
pub type RectF = GenericRect<f32, f32>;

/// Returns `true` if the given vector is neither infinite nor `NaN`.
#[inline]
pub fn vec2_is_finite(val: Vec2) -> bool {
    let x = val.x.is_finite() as u8;
    let y = val.y.is_finite() as u8;

    x * y == 1
}

/// Returns `true` if the given vector is neither infinite nor `NaN`.
#[inline]
pub fn vec3_is_finite(val: Vec3) -> bool {
    // Use saturating casts so we can swap three `jbe` instructions for two `and` instructions.
    // Probably not needed but branchless code sure is fun: https://godbolt.org/z/P58cdq
    let x = val.x.is_finite() as u8;
    let y = val.y.is_finite() as u8;
    let z = val.z.is_finite() as u8;

    x * y * z == 1
}

/// Returns `true` if the given vector is neither infinite nor `NaN`.
#[inline]
pub fn vec4_is_finite(val: Vec4) -> bool {
    // Use saturating casts so we can swap three `jbe` instructions for two `and` instructions.
    // Probably not needed but branchless code sure is fun: https://godbolt.org/z/P58cdq
    let x = val.x.is_finite() as u8;
    let y = val.y.is_finite() as u8;
    let z = val.z.is_finite() as u8;
    let w = val.w.is_finite() as u8;

    x * y * z * w == 1
}

#[inline]
pub fn vec4_from_vec3(vec: Vec3, w: f32) -> Vec4 {
    vec4(vec.x, vec.y, vec.z, w)
}