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    #[doc(alias = "graphene_vec4_init")]
11    pub fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
12        assert_initialized_main_thread!();
13        unsafe {
14            let mut vec = Self::uninitialized();
15            ffi::graphene_vec4_init(vec.to_glib_none_mut().0, x, y, z, w);
16            vec
17        }
18    }
19
20    #[doc(alias = "graphene_vec4_init_from_vec2")]
21    #[doc(alias = "init_from_vec2")]
22    pub fn from_vec2(src: &Vec2, z: f32, w: f32) -> Self {
23        assert_initialized_main_thread!();
24        unsafe {
25            let mut vec = Self::uninitialized();
26            ffi::graphene_vec4_init_from_vec2(vec.to_glib_none_mut().0, src.to_glib_none().0, z, w);
27            vec
28        }
29    }
30
31    #[doc(alias = "graphene_vec4_init_from_vec3")]
32    #[doc(alias = "init_from_vec3")]
33    pub fn from_vec3(src: &Vec3, w: f32) -> Self {
34        assert_initialized_main_thread!();
35        unsafe {
36            let mut vec = Self::uninitialized();
37            ffi::graphene_vec4_init_from_vec3(vec.to_glib_none_mut().0, src.to_glib_none().0, w);
38            vec
39        }
40    }
41
42    #[doc(alias = "graphene_vec4_init_from_float")]
43    #[doc(alias = "init_from_float")]
44    pub fn from_float(src: [f32; 4]) -> Self {
45        assert_initialized_main_thread!();
46        unsafe {
47            let mut vec = Self::uninitialized();
48            ffi::graphene_vec4_init_from_float(vec.to_glib_none_mut().0, src.as_ptr() as *const _);
49            vec
50        }
51    }
52
53    #[doc(alias = "graphene_vec4_to_float")]
54    pub fn to_float(&self) -> [f32; 4] {
55        unsafe {
56            let mut out = std::mem::MaybeUninit::uninit();
57            ffi::graphene_vec4_to_float(self.to_glib_none().0, out.as_mut_ptr());
58            out.assume_init()
59        }
60    }
61}
62
63impl fmt::Debug for Vec4 {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        f.debug_struct("Vec4")
66            .field("x", &self.x())
67            .field("y", &self.y())
68            .field("z", &self.z())
69            .field("w", &self.w())
70            .finish()
71    }
72}
73
74impl Default for Vec4 {
75    fn default() -> Self {
76        Self::zero()
77    }
78}
79
80// addition/subtraction
81impl ops::Add<Vec4> for Vec4 {
82    type Output = Vec4;
83
84    fn add(self, rhs: Vec4) -> Self::Output {
85        Vec4::add(&self, &rhs)
86    }
87}
88impl ops::AddAssign<Vec4> for Vec4 {
89    fn add_assign(&mut self, rhs: Vec4) {
90        *self = *self + rhs;
91    }
92}
93impl ops::Sub<Vec4> for Vec4 {
94    type Output = Vec4;
95
96    fn sub(self, rhs: Vec4) -> Self::Output {
97        Vec4::subtract(&self, &rhs)
98    }
99}
100impl ops::SubAssign<Vec4> for Vec4 {
101    fn sub_assign(&mut self, rhs: Vec4) {
102        *self = *self - rhs;
103    }
104}
105impl ops::Neg for Vec4 {
106    type Output = Vec4;
107
108    fn neg(self) -> Self::Output {
109        Vec4::negate(&self)
110    }
111}
112
113// scalar multiplication
114impl ops::Mul<f32> for Vec4 {
115    type Output = Vec4;
116
117    fn mul(self, rhs: f32) -> Self::Output {
118        Vec4::scale(&self, rhs)
119    }
120}
121impl ops::MulAssign<f32> for Vec4 {
122    fn mul_assign(&mut self, rhs: f32) {
123        *self = *self * rhs;
124    }
125}
126impl ops::Mul<Vec4> for f32 {
127    type Output = Vec4;
128
129    fn mul(self, rhs: Vec4) -> Self::Output {
130        rhs * self
131    }
132}
133
134// Component-wise multiplication/division
135impl ops::Mul<Vec4> for Vec4 {
136    type Output = Vec4;
137
138    fn mul(self, rhs: Vec4) -> Self::Output {
139        Vec4::multiply(&self, &rhs)
140    }
141}
142impl ops::MulAssign<Vec4> for Vec4 {
143    fn mul_assign(&mut self, rhs: Vec4) {
144        *self = *self * rhs;
145    }
146}
147impl ops::Div<Vec4> for Vec4 {
148    type Output = Vec4;
149
150    fn div(self, rhs: Vec4) -> Self::Output {
151        Vec4::divide(&self, &rhs)
152    }
153}
154impl ops::DivAssign<Vec4> for Vec4 {
155    fn div_assign(&mut self, rhs: Vec4) {
156        *self = *self / rhs;
157    }
158}