graphene/
vec3.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{fmt, ops};
4
5use glib::translate::*;
6
7use crate::{ffi, Vec3};
8
9impl Vec3 {
10    /// Initializes a [`Vec3`][crate::Vec3] using the given values.
11    ///
12    /// This function can be called multiple times.
13    /// ## `x`
14    /// the X field of the vector
15    /// ## `y`
16    /// the Y field of the vector
17    /// ## `z`
18    /// the Z field of the vector
19    ///
20    /// # Returns
21    ///
22    /// a pointer to the initialized
23    ///  vector
24    #[doc(alias = "graphene_vec3_init")]
25    pub fn new(x: f32, y: f32, z: f32) -> Self {
26        assert_initialized_main_thread!();
27        unsafe {
28            let mut vec = Self::uninitialized();
29            ffi::graphene_vec3_init(vec.to_glib_none_mut().0, x, y, z);
30            vec
31        }
32    }
33
34    /// Initializes a [`Vec3`][crate::Vec3] with the values from an array.
35    /// ## `src`
36    /// an array of 3 floating point values
37    ///
38    /// # Returns
39    ///
40    /// the initialized vector
41    #[doc(alias = "graphene_vec3_init_from_float")]
42    #[doc(alias = "init_from_float")]
43    pub fn from_float(src: [f32; 3]) -> Self {
44        assert_initialized_main_thread!();
45        unsafe {
46            let mut vec = Self::uninitialized();
47            ffi::graphene_vec3_init_from_float(vec.to_glib_none_mut().0, src.as_ptr() as *const _);
48            vec
49        }
50    }
51
52    /// Copies the components of a [`Vec3`][crate::Vec3] into the given array.
53    ///
54    /// # Returns
55    ///
56    ///
57    /// ## `dest`
58    /// return location for
59    ///  an array of floating point values
60    #[doc(alias = "graphene_vec3_to_float")]
61    pub fn to_float(&self) -> [f32; 3] {
62        unsafe {
63            let mut out = std::mem::MaybeUninit::uninit();
64            ffi::graphene_vec3_to_float(self.to_glib_none().0, out.as_mut_ptr());
65            out.assume_init()
66        }
67    }
68}
69
70impl fmt::Debug for Vec3 {
71    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72        f.debug_struct("Vec3")
73            .field("x", &self.x())
74            .field("y", &self.y())
75            .field("z", &self.z())
76            .finish()
77    }
78}
79
80impl Default for Vec3 {
81    fn default() -> Self {
82        Self::zero()
83    }
84}
85
86// addition/subtraction
87impl ops::Add<Vec3> for Vec3 {
88    type Output = Vec3;
89
90    fn add(self, rhs: Vec3) -> Self::Output {
91        Vec3::add(&self, &rhs)
92    }
93}
94impl ops::AddAssign<Vec3> for Vec3 {
95    fn add_assign(&mut self, rhs: Vec3) {
96        *self = *self + rhs;
97    }
98}
99impl ops::Sub<Vec3> for Vec3 {
100    type Output = Vec3;
101
102    fn sub(self, rhs: Vec3) -> Self::Output {
103        Vec3::subtract(&self, &rhs)
104    }
105}
106impl ops::SubAssign<Vec3> for Vec3 {
107    fn sub_assign(&mut self, rhs: Vec3) {
108        *self = *self - rhs;
109    }
110}
111impl ops::Neg for Vec3 {
112    type Output = Vec3;
113
114    fn neg(self) -> Self::Output {
115        Vec3::negate(&self)
116    }
117}
118
119// scalar multiplication
120impl ops::Mul<f32> for Vec3 {
121    type Output = Vec3;
122
123    fn mul(self, rhs: f32) -> Self::Output {
124        Vec3::scale(&self, rhs)
125    }
126}
127impl ops::MulAssign<f32> for Vec3 {
128    fn mul_assign(&mut self, rhs: f32) {
129        *self = *self * rhs;
130    }
131}
132impl ops::Mul<Vec3> for f32 {
133    type Output = Vec3;
134
135    fn mul(self, rhs: Vec3) -> Self::Output {
136        rhs * self
137    }
138}
139
140// Component-wise multiplication/division
141impl ops::Mul<Vec3> for Vec3 {
142    type Output = Vec3;
143
144    fn mul(self, rhs: Vec3) -> Self::Output {
145        Vec3::multiply(&self, &rhs)
146    }
147}
148impl ops::MulAssign<Vec3> for Vec3 {
149    fn mul_assign(&mut self, rhs: Vec3) {
150        *self = *self * rhs;
151    }
152}
153impl ops::Div<Vec3> for Vec3 {
154    type Output = Vec3;
155
156    fn div(self, rhs: Vec3) -> Self::Output {
157        Vec3::divide(&self, &rhs)
158    }
159}
160impl ops::DivAssign<Vec3> for Vec3 {
161    fn div_assign(&mut self, rhs: Vec3) {
162        *self = *self / rhs;
163    }
164}