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
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
//! Math-related utilities/constants/type definitions.
//!
//! The vek crate that we use to provide math primitives is very generic, but we will always want
//! to use it with floats. This module exports type aliases that allow us to not have to specify
//! that we are using "f32" all the time.

mod decompose;

pub mod transforms;

pub use decompose::Decompose;

use serde::{Serialize, Deserialize};

pub type Vec2 = vek::Vec2<f32>;
pub type Vec3 = vek::Vec3<f32>;
pub type Vec4 = vek::Vec4<f32>;

pub type Mat2 = vek::Mat2<f32>;
pub type Mat3 = vek::Mat3<f32>;
pub type Mat4 = vek::Mat4<f32>;

pub type Quaternion = vek::Quaternion<f32>;

pub type Rgb = vek::Rgb<f32>;
pub type Rgba = vek::Rgba<f32>;

pub type FrustumPlanes = vek::FrustumPlanes<f32>;

pub type Transforms = transforms::Transforms<f32>;

/// A "newtype" to represent a value with the unit "radians"
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Radians(f32);

impl From<Degrees> for Radians {
    fn from(value: Degrees) -> Self {
        Radians::from_radians(value.get_radians())
    }
}

impl Radians {
    pub fn from_degrees(value: f32) -> Self {
        Radians(value.to_radians())
    }

    pub fn from_radians(value: f32) -> Self {
        Radians(value)
    }

    pub fn get_radians(self) -> f32 {
        self.0
    }

    pub fn get_degrees(self) -> f32 {
        self.0.to_degrees()
    }
}

/// A "newtype" to represent a value with the unit "degrees"
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Degrees(f32);

impl From<Radians> for Degrees {
    fn from(value: Radians) -> Self {
        Degrees::from_degrees(value.get_degrees())
    }
}

impl Degrees {
    pub fn from_degrees(value: f32) -> Self {
        Degrees(value)
    }

    pub fn from_radians(value: f32) -> Self {
        Degrees(value.to_degrees())
    }

    pub fn get_radians(self) -> f32 {
        self.0.to_radians()
    }

    pub fn get_degrees(self) -> f32 {
        self.0
    }
}

#[derive(Default, Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Milliseconds(f32);

impl Milliseconds {
    pub fn from_msec(value: f32) -> Self {
        Milliseconds(value)
    }

    pub fn from_sec(value: f32) -> Self {
        Milliseconds(value * 1000.0)
    }

    pub fn to_sec(self) -> f32 {
        self.0 / 1000.0
    }

    pub fn to_msec(self) -> f32 {
        self.0
    }
}