physx/math/
vec3.rs

1use crate::traits::Class;
2
3#[rustfmt::skip]
4use physx_sys::{
5    PxVec3_abs,
6    PxVec3_cross,
7    PxVec3_dot,
8    PxVec3_getNormalized,
9    PxVec3_isFinite,
10    PxVec3_isNormalized,
11    PxVec3_isZero,
12    PxVec3_magnitude,
13    PxVec3_magnitudeSquared,
14    PxVec3_maxElement,
15    PxVec3_maximum,
16    PxVec3_minElement,
17    PxVec3_minimum,
18    PxVec3_multiply,
19    PxVec3_new_1,
20    PxVec3_new_3,
21    PxVec3_normalize_mut,
22};
23
24#[derive(Copy, Clone)]
25#[repr(transparent)]
26pub struct PxVec3 {
27    pub(super) obj: physx_sys::PxVec3,
28}
29
30crate::DeriveClassForNewType!(PxVec3: PxVec3);
31
32impl Default for PxVec3 {
33    fn default() -> Self {
34        unsafe { PxVec3_new_1(physx_sys::PxZERO::PxZero).into() }
35    }
36}
37
38impl From<physx_sys::PxVec3> for PxVec3 {
39    fn from(vec3: physx_sys::PxVec3) -> Self {
40        PxVec3 { obj: vec3 }
41    }
42}
43
44impl From<PxVec3> for physx_sys::PxVec3 {
45    fn from(value: PxVec3) -> Self {
46        value.obj
47    }
48}
49
50impl From<physx_sys::PxExtendedVec3> for PxVec3 {
51    fn from(vec3: physx_sys::PxExtendedVec3) -> Self {
52        PxVec3 {
53            obj: physx_sys::PxVec3 {
54                x: vec3.x as f32,
55                y: vec3.y as f32,
56                z: vec3.z as f32,
57            },
58        }
59    }
60}
61
62impl From<PxVec3> for physx_sys::PxExtendedVec3 {
63    fn from(value: PxVec3) -> Self {
64        Self {
65            x: value.obj.x as f64,
66            y: value.obj.y as f64,
67            z: value.obj.z as f64,
68        }
69    }
70}
71
72impl PxVec3 {
73    pub fn x(&self) -> f32 {
74        self.obj.x
75    }
76
77    pub fn y(&self) -> f32 {
78        self.obj.y
79    }
80
81    pub fn z(&self) -> f32 {
82        self.obj.z
83    }
84
85    pub fn x_mut(&mut self) -> &mut f32 {
86        &mut self.obj.x
87    }
88
89    pub fn y_mut(&mut self) -> &mut f32 {
90        &mut self.obj.y
91    }
92
93    pub fn z_mut(&mut self) -> &mut f32 {
94        &mut self.obj.z
95    }
96
97    pub fn new(x: f32, y: f32, z: f32) -> PxVec3 {
98        unsafe {
99            PxVec3 {
100                obj: PxVec3_new_3(x, y, z),
101            }
102        }
103    }
104
105    pub fn abs(&self) -> PxVec3 {
106        unsafe { PxVec3_abs(self.as_ptr()).into() }
107    }
108
109    pub fn cross(&self, other: &PxVec3) -> PxVec3 {
110        unsafe { PxVec3_cross(self.as_ptr(), other.as_ptr()).into() }
111    }
112
113    pub fn dot(&self, other: &PxVec3) -> f32 {
114        unsafe { PxVec3_dot(self.as_ptr(), other.as_ptr()) }
115    }
116
117    pub fn get_normalized(&self) -> PxVec3 {
118        unsafe { PxVec3_getNormalized(self.as_ptr()).into() }
119    }
120
121    pub fn is_finite(&self) -> bool {
122        unsafe { PxVec3_isFinite(self.as_ptr()) }
123    }
124
125    pub fn is_normalized(&self) -> bool {
126        unsafe { PxVec3_isNormalized(self.as_ptr()) }
127    }
128
129    pub fn is_zero(&self) -> bool {
130        unsafe { PxVec3_isZero(self.as_ptr()) }
131    }
132
133    pub fn magnitude(&self) -> f32 {
134        unsafe { PxVec3_magnitude(self.as_ptr()) }
135    }
136
137    pub fn magnitude_squared(&self) -> f32 {
138        unsafe { PxVec3_magnitudeSquared(self.as_ptr()) }
139    }
140
141    pub fn max_element(&self) -> f32 {
142        unsafe { PxVec3_maxElement(self.as_ptr()) }
143    }
144
145    pub fn maximum(&self, other: &PxVec3) -> PxVec3 {
146        unsafe { PxVec3_maximum(self.as_ptr(), other.as_ptr()).into() }
147    }
148
149    pub fn min_element(&self) -> f32 {
150        unsafe { PxVec3_minElement(self.as_ptr()) }
151    }
152
153    pub fn minimum(&self, other: &PxVec3) -> PxVec3 {
154        unsafe { PxVec3_minimum(self.as_ptr(), other.as_ptr()).into() }
155    }
156
157    pub fn multiply(&self, other: &PxVec3) -> PxVec3 {
158        unsafe { PxVec3_multiply(self.as_ptr(), other.as_ptr()).into() }
159    }
160
161    /// normalize in place and returns the original magnitude.
162    pub fn normalize(&mut self) -> f32 {
163        unsafe { PxVec3_normalize_mut(self.as_mut_ptr()) }
164    }
165}
166
167unsafe impl Send for PxVec3 {}
168unsafe impl Sync for PxVec3 {}