cgmath_dolly/
transform.rs

1use cgmath::{Vector3, Quaternion};
2use cgmath::{Zero, One};
3use std::marker::PhantomData;
4
5use crate::handedness::Handedness;
6
7#[derive(Clone, Copy, Debug)]
8pub struct Transform<H: Handedness> {
9    pub position: Vector3<f32>,
10    pub rotation: Quaternion<f32>,
11    pub phantom: PhantomData<H>
12}
13
14
15impl<H: Handedness> Transform<H> {
16    pub fn from_position_rotation(position: Vector3<f32>, rotation: Quaternion<f32>) -> Self  {
17        Self  {
18            position,
19            rotation,
20            phantom: PhantomData
21        }
22    }
23
24    pub fn into_position_rotation(self) -> (Vector3<f32>, Quaternion<f32>) {
25        (self.position, self.rotation)
26    }
27
28    pub fn right(&self) -> Vector3<f32> {
29        self.rotation * Vector3::unit_x()
30    }
31
32    pub fn up(&self) -> Vector3<f32> {
33        self.rotation * Vector3::unit_y()
34    }
35
36    pub fn forward(&self) -> Vector3<f32> {
37        self.rotation * H::forward()
38    }
39
40    pub fn identity() -> Transform<H> {
41        Transform {
42            position: Vector3::zero(),
43            rotation: Quaternion::one(),
44            phantom: PhantomData,
45        }
46    }
47}