graphene/euler.rs
1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::fmt;
4
5use glib::translate::*;
6
7use crate::{ffi, Euler, EulerOrder, Matrix, Quaternion, Vec3};
8
9impl Euler {
10 /// Initializes a [`Euler`][crate::Euler] using the given angles.
11 ///
12 /// The order of the rotations is [`EulerOrder::Default`][crate::EulerOrder::Default].
13 /// ## `x`
14 /// rotation angle on the X axis, in degrees
15 /// ## `y`
16 /// rotation angle on the Y axis, in degrees
17 /// ## `z`
18 /// rotation angle on the Z axis, in degrees
19 ///
20 /// # Returns
21 ///
22 /// the initialized [`Euler`][crate::Euler]
23 #[doc(alias = "graphene_euler_init")]
24 pub fn new(x: f32, y: f32, z: f32) -> Self {
25 assert_initialized_main_thread!();
26 unsafe {
27 let mut eul = Self::uninitialized();
28 ffi::graphene_euler_init(eul.to_glib_none_mut().0, x, y, z);
29 eul
30 }
31 }
32
33 /// Initializes a [`Euler`][crate::Euler] using the given rotation matrix.
34 ///
35 /// If the [`Matrix`][crate::Matrix] `m` is [`None`], the [`Euler`][crate::Euler] will
36 /// be initialized with all angles set to 0.
37 /// ## `m`
38 /// a rotation matrix
39 /// ## `order`
40 /// the order used to apply the rotations
41 ///
42 /// # Returns
43 ///
44 /// the initialized [`Euler`][crate::Euler]
45 #[doc(alias = "graphene_euler_init_from_matrix")]
46 #[doc(alias = "init_from_matrix")]
47 pub fn from_matrix(m: Option<&Matrix>, order: EulerOrder) -> Self {
48 assert_initialized_main_thread!();
49 unsafe {
50 let mut eul = Self::uninitialized();
51 ffi::graphene_euler_init_from_matrix(
52 eul.to_glib_none_mut().0,
53 m.to_glib_none().0,
54 order.into_glib(),
55 );
56 eul
57 }
58 }
59
60 /// Initializes a [`Euler`][crate::Euler] using the given normalized quaternion.
61 ///
62 /// If the [`Quaternion`][crate::Quaternion] `q` is [`None`], the [`Euler`][crate::Euler] will
63 /// be initialized with all angles set to 0.
64 /// ## `q`
65 /// a normalized [`Quaternion`][crate::Quaternion]
66 /// ## `order`
67 /// the order used to apply the rotations
68 ///
69 /// # Returns
70 ///
71 /// the initialized [`Euler`][crate::Euler]
72 #[doc(alias = "graphene_euler_init_from_quaternion")]
73 #[doc(alias = "init_from_quaternion")]
74 pub fn from_quaternion(q: Option<&Quaternion>, order: EulerOrder) -> Self {
75 assert_initialized_main_thread!();
76 unsafe {
77 let mut eul = Self::uninitialized();
78 ffi::graphene_euler_init_from_quaternion(
79 eul.to_glib_none_mut().0,
80 q.to_glib_none().0,
81 order.into_glib(),
82 );
83 eul
84 }
85 }
86
87 /// Initializes a [`Euler`][crate::Euler] using the given angles
88 /// and order of rotation.
89 /// ## `x`
90 /// rotation angle on the X axis, in radians
91 /// ## `y`
92 /// rotation angle on the Y axis, in radians
93 /// ## `z`
94 /// rotation angle on the Z axis, in radians
95 /// ## `order`
96 /// order of rotations
97 ///
98 /// # Returns
99 ///
100 /// the initialized [`Euler`][crate::Euler]
101 #[doc(alias = "graphene_euler_init_from_radians")]
102 #[doc(alias = "init_from_radians")]
103 pub fn from_radians(x: f32, y: f32, z: f32, order: EulerOrder) -> Self {
104 unsafe {
105 let mut eul = Self::uninitialized();
106 ffi::graphene_euler_init_from_radians(
107 eul.to_glib_none_mut().0,
108 x,
109 y,
110 z,
111 order.into_glib(),
112 );
113 eul
114 }
115 }
116
117 /// Initializes a [`Euler`][crate::Euler] using the angles contained in a
118 /// [`Vec3`][crate::Vec3].
119 ///
120 /// If the [`Vec3`][crate::Vec3] `v` is [`None`], the [`Euler`][crate::Euler] will be
121 /// initialized with all angles set to 0.
122 /// ## `v`
123 /// a [`Vec3`][crate::Vec3] containing the rotation
124 /// angles in degrees
125 /// ## `order`
126 /// the order used to apply the rotations
127 ///
128 /// # Returns
129 ///
130 /// the initialized [`Euler`][crate::Euler]
131 #[doc(alias = "graphene_euler_init_from_vec3")]
132 #[doc(alias = "init_from_vec3")]
133 pub fn from_vec3(v: Option<&Vec3>, order: EulerOrder) -> Self {
134 assert_initialized_main_thread!();
135 unsafe {
136 let mut eul = Self::uninitialized();
137 ffi::graphene_euler_init_from_vec3(
138 eul.to_glib_none_mut().0,
139 v.to_glib_none().0,
140 order.into_glib(),
141 );
142 eul
143 }
144 }
145
146 /// Initializes a [`Euler`][crate::Euler] with the given angles and `order`.
147 /// ## `x`
148 /// rotation angle on the X axis, in degrees
149 /// ## `y`
150 /// rotation angle on the Y axis, in degrees
151 /// ## `z`
152 /// rotation angle on the Z axis, in degrees
153 /// ## `order`
154 /// the order used to apply the rotations
155 ///
156 /// # Returns
157 ///
158 /// the initialized [`Euler`][crate::Euler]
159 #[doc(alias = "graphene_euler_init_with_order")]
160 #[doc(alias = "init_with_order")]
161 pub fn with_order(x: f32, y: f32, z: f32, order: EulerOrder) -> Self {
162 assert_initialized_main_thread!();
163 unsafe {
164 let mut eul = Self::uninitialized();
165 ffi::graphene_euler_init_with_order(
166 eul.to_glib_none_mut().0,
167 x,
168 y,
169 z,
170 order.into_glib(),
171 );
172 eul
173 }
174 }
175}
176
177impl fmt::Debug for Euler {
178 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
179 f.debug_struct("Euler")
180 .field("order", &self.order())
181 .field("alpha", &self.alpha())
182 .field("beta", &self.beta())
183 .field("gamma", &self.gamma())
184 .finish()
185 }
186}