graphene/
vec4.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, Vec2, Vec3, Vec4};
8
9impl Vec4 {
10    /// Initializes a [`Vec4`][crate::Vec4] 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    /// ## `w`
20    /// the W field of the vector
21    ///
22    /// # Returns
23    ///
24    /// a pointer to the initialized
25    ///  vector
26    #[doc(alias = "graphene_vec4_init")]
27    pub fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
28        assert_initialized_main_thread!();
29        unsafe {
30            let mut vec = Self::uninitialized();
31            ffi::graphene_vec4_init(vec.to_glib_none_mut().0, x, y, z, w);
32            vec
33        }
34    }
35
36    /// Initializes a [`Vec4`][crate::Vec4] using the components of a
37    /// [`Vec2`][crate::Vec2] and the values of `z` and `w`.
38    /// ## `src`
39    /// a [`Vec2`][crate::Vec2]
40    /// ## `z`
41    /// the value for the third component of `self`
42    /// ## `w`
43    /// the value for the fourth component of `self`
44    ///
45    /// # Returns
46    ///
47    /// the initialized vector
48    #[doc(alias = "graphene_vec4_init_from_vec2")]
49    #[doc(alias = "init_from_vec2")]
50    pub fn from_vec2(src: &Vec2, z: f32, w: f32) -> Self {
51        assert_initialized_main_thread!();
52        unsafe {
53            let mut vec = Self::uninitialized();
54            ffi::graphene_vec4_init_from_vec2(vec.to_glib_none_mut().0, src.to_glib_none().0, z, w);
55            vec
56        }
57    }
58
59    /// Initializes a [`Vec4`][crate::Vec4] using the components of a
60    /// [`Vec3`][crate::Vec3] and the value of `w`.
61    /// ## `src`
62    /// a [`Vec3`][crate::Vec3]
63    /// ## `w`
64    /// the value for the fourth component of `self`
65    ///
66    /// # Returns
67    ///
68    /// the initialized vector
69    #[doc(alias = "graphene_vec4_init_from_vec3")]
70    #[doc(alias = "init_from_vec3")]
71    pub fn from_vec3(src: &Vec3, w: f32) -> Self {
72        assert_initialized_main_thread!();
73        unsafe {
74            let mut vec = Self::uninitialized();
75            ffi::graphene_vec4_init_from_vec3(vec.to_glib_none_mut().0, src.to_glib_none().0, w);
76            vec
77        }
78    }
79
80    /// Initializes a [`Vec4`][crate::Vec4] with the values inside the given array.
81    /// ## `src`
82    /// an array of four floating point values
83    ///
84    /// # Returns
85    ///
86    /// the initialized vector
87    #[doc(alias = "graphene_vec4_init_from_float")]
88    #[doc(alias = "init_from_float")]
89    pub fn from_float(src: [f32; 4]) -> Self {
90        assert_initialized_main_thread!();
91        unsafe {
92            let mut vec = Self::uninitialized();
93            ffi::graphene_vec4_init_from_float(vec.to_glib_none_mut().0, src.as_ptr() as *const _);
94            vec
95        }
96    }
97
98    /// Stores the components of the given [`Vec4`][crate::Vec4] into an array
99    /// of floating point values.
100    ///
101    /// # Returns
102    ///
103    ///
104    /// ## `dest`
105    /// return location for
106    ///  an array of floating point values
107    #[doc(alias = "graphene_vec4_to_float")]
108    pub fn to_float(&self) -> [f32; 4] {
109        unsafe {
110            let mut out = std::mem::MaybeUninit::uninit();
111            ffi::graphene_vec4_to_float(self.to_glib_none().0, out.as_mut_ptr());
112            out.assume_init()
113        }
114    }
115}
116
117impl fmt::Debug for Vec4 {
118    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119        f.debug_struct("Vec4")
120            .field("x", &self.x())
121            .field("y", &self.y())
122            .field("z", &self.z())
123            .field("w", &self.w())
124            .finish()
125    }
126}
127
128impl Default for Vec4 {
129    fn default() -> Self {
130        Self::zero()
131    }
132}
133
134// addition/subtraction
135impl ops::Add<Vec4> for Vec4 {
136    type Output = Vec4;
137
138    fn add(self, rhs: Vec4) -> Self::Output {
139        Vec4::add(&self, &rhs)
140    }
141}
142impl ops::AddAssign<Vec4> for Vec4 {
143    fn add_assign(&mut self, rhs: Vec4) {
144        *self = *self + rhs;
145    }
146}
147impl ops::Sub<Vec4> for Vec4 {
148    type Output = Vec4;
149
150    fn sub(self, rhs: Vec4) -> Self::Output {
151        Vec4::subtract(&self, &rhs)
152    }
153}
154impl ops::SubAssign<Vec4> for Vec4 {
155    fn sub_assign(&mut self, rhs: Vec4) {
156        *self = *self - rhs;
157    }
158}
159impl ops::Neg for Vec4 {
160    type Output = Vec4;
161
162    fn neg(self) -> Self::Output {
163        Vec4::negate(&self)
164    }
165}
166
167// scalar multiplication
168impl ops::Mul<f32> for Vec4 {
169    type Output = Vec4;
170
171    fn mul(self, rhs: f32) -> Self::Output {
172        Vec4::scale(&self, rhs)
173    }
174}
175impl ops::MulAssign<f32> for Vec4 {
176    fn mul_assign(&mut self, rhs: f32) {
177        *self = *self * rhs;
178    }
179}
180impl ops::Mul<Vec4> for f32 {
181    type Output = Vec4;
182
183    fn mul(self, rhs: Vec4) -> Self::Output {
184        rhs * self
185    }
186}
187
188// Component-wise multiplication/division
189impl ops::Mul<Vec4> for Vec4 {
190    type Output = Vec4;
191
192    fn mul(self, rhs: Vec4) -> Self::Output {
193        Vec4::multiply(&self, &rhs)
194    }
195}
196impl ops::MulAssign<Vec4> for Vec4 {
197    fn mul_assign(&mut self, rhs: Vec4) {
198        *self = *self * rhs;
199    }
200}
201impl ops::Div<Vec4> for Vec4 {
202    type Output = Vec4;
203
204    fn div(self, rhs: Vec4) -> Self::Output {
205        Vec4::divide(&self, &rhs)
206    }
207}
208impl ops::DivAssign<Vec4> for Vec4 {
209    fn div_assign(&mut self, rhs: Vec4) {
210        *self = *self / rhs;
211    }
212}