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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! Mathmatics types and functions, mostly based on
//! [glam-rs](https://github.com/bitshifter/glam-rs).
//!
//! Also contains some geometric math types.

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};

/// A rectangular area with u32 position and size values.
pub type Area = GenericRect<u32, u32>;

/// A coordinate with u8 values.
pub type Coord8 = GenericCoord<u8>;

/// A coordinate with i32 values.
pub type Coord = GenericCoord<i32>;

/// A coordinate with f32 values.
pub type CoordF = GenericCoord<f32>;

/// A coordinate with u32 values.
pub type Extent = GenericCoord<u32>;

/// A rectangular area with u32 position and i32 size values.
pub type Rect = GenericRect<u32, i32>;

/// A rectangular area with f32 position and size values.
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
}

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