1 #[path = "support/macros.rs"]
2 #[macro_use]
3 mod macros;
4 
5 #[cfg(target_arch = "wasm32")]
6 wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
7 
8 use glam::{
9     DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4, Mat2, Mat3, Mat3A, Mat4, Quat, Vec2, Vec3,
10     Vec3A, Vec4,
11 };
12 
13 pub trait Deg {
to_radians(self) -> Self14     fn to_radians(self) -> Self;
15 }
16 
17 impl Deg for f32 {
to_radians(self) -> f3218     fn to_radians(self) -> f32 {
19         f32::to_radians(self)
20     }
21 }
22 
23 impl Deg for f64 {
to_radians(self) -> f6424     fn to_radians(self) -> f64 {
25         f64::to_radians(self)
26     }
27 }
28 
29 /// Helper function for migrating away from `glam::angle::deg`.
30 #[allow(dead_code)]
31 #[inline]
deg<T: Deg>(angle: T) -> T32 pub fn deg<T: Deg>(angle: T) -> T {
33     angle.to_radians()
34 }
35 
36 /// Helper function for migrating away from `glam::angle::rad`.
37 #[allow(dead_code)]
38 #[inline]
rad<T>(angle: T) -> T39 pub fn rad<T>(angle: T) -> T {
40     angle
41 }
42 
43 /// Trait used by the `assert_approx_eq` macro for floating point comparisons.
44 pub trait FloatCompare<Rhs: ?Sized = Self> {
45     /// Return true if the absolute difference between `self` and `other` is
46     /// less then or equal to `max_abs_diff`.
approx_eq(&self, other: &Rhs, max_abs_diff: f32) -> bool47     fn approx_eq(&self, other: &Rhs, max_abs_diff: f32) -> bool;
48     /// Returns the absolute difference of `self` and `other` which is printed
49     /// if `assert_approx_eq` fails.
abs_diff(&self, other: &Rhs) -> Rhs50     fn abs_diff(&self, other: &Rhs) -> Rhs;
51 }
52 
53 impl FloatCompare for f32 {
54     #[inline]
approx_eq(&self, other: &f32, max_abs_diff: f32) -> bool55     fn approx_eq(&self, other: &f32, max_abs_diff: f32) -> bool {
56         (self - other).abs() <= max_abs_diff
57     }
58     #[inline]
abs_diff(&self, other: &f32) -> f3259     fn abs_diff(&self, other: &f32) -> f32 {
60         (self - other).abs()
61     }
62 }
63 
64 impl FloatCompare for f64 {
65     #[inline]
approx_eq(&self, other: &f64, max_abs_diff: f32) -> bool66     fn approx_eq(&self, other: &f64, max_abs_diff: f32) -> bool {
67         (self - other).abs() <= max_abs_diff as f64
68     }
69     #[inline]
abs_diff(&self, other: &f64) -> f6470     fn abs_diff(&self, other: &f64) -> f64 {
71         (self - other).abs()
72     }
73 }
74 
75 impl FloatCompare for Mat2 {
76     #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool77     fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
78         self.abs_diff_eq(*other, max_abs_diff)
79     }
80     #[inline]
abs_diff(&self, other: &Self) -> Self81     fn abs_diff(&self, other: &Self) -> Self {
82         Self::from_cols(
83             (self.x_axis - other.x_axis).abs(),
84             (self.y_axis - other.y_axis).abs(),
85         )
86     }
87 }
88 
89 impl FloatCompare for DMat2 {
90     #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool91     fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
92         self.abs_diff_eq(*other, max_abs_diff as f64)
93     }
94     #[inline]
abs_diff(&self, other: &Self) -> Self95     fn abs_diff(&self, other: &Self) -> Self {
96         Self::from_cols(
97             (self.x_axis - other.x_axis).abs(),
98             (self.y_axis - other.y_axis).abs(),
99         )
100     }
101 }
102 
103 impl FloatCompare for Mat3 {
104     #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool105     fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
106         self.abs_diff_eq(*other, max_abs_diff)
107     }
108     #[inline]
abs_diff(&self, other: &Self) -> Self109     fn abs_diff(&self, other: &Self) -> Self {
110         Self::from_cols(
111             (self.x_axis - other.x_axis).abs(),
112             (self.y_axis - other.y_axis).abs(),
113             (self.z_axis - other.z_axis).abs(),
114         )
115     }
116 }
117 
118 impl FloatCompare for Mat3A {
119     #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool120     fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
121         self.abs_diff_eq(*other, max_abs_diff)
122     }
123     #[inline]
abs_diff(&self, other: &Self) -> Self124     fn abs_diff(&self, other: &Self) -> Self {
125         Self::from_cols(
126             (self.x_axis - other.x_axis).abs(),
127             (self.y_axis - other.y_axis).abs(),
128             (self.z_axis - other.z_axis).abs(),
129         )
130     }
131 }
132 
133 impl FloatCompare for DMat3 {
134     #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool135     fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
136         self.abs_diff_eq(*other, max_abs_diff as f64)
137     }
138     #[inline]
abs_diff(&self, other: &Self) -> Self139     fn abs_diff(&self, other: &Self) -> Self {
140         Self::from_cols(
141             (self.x_axis - other.x_axis).abs(),
142             (self.y_axis - other.y_axis).abs(),
143             (self.z_axis - other.z_axis).abs(),
144         )
145     }
146 }
147 
148 impl FloatCompare for DMat4 {
149     #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool150     fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
151         self.abs_diff_eq(*other, max_abs_diff as f64)
152     }
153     #[inline]
abs_diff(&self, other: &Self) -> Self154     fn abs_diff(&self, other: &Self) -> Self {
155         Self::from_cols(
156             (self.x_axis - other.x_axis).abs(),
157             (self.y_axis - other.y_axis).abs(),
158             (self.z_axis - other.z_axis).abs(),
159             (self.w_axis - other.w_axis).abs(),
160         )
161     }
162 }
163 
164 impl FloatCompare for Mat4 {
165     #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool166     fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
167         self.abs_diff_eq(*other, max_abs_diff)
168     }
169     #[inline]
abs_diff(&self, other: &Self) -> Self170     fn abs_diff(&self, other: &Self) -> Self {
171         Self::from_cols(
172             (self.x_axis - other.x_axis).abs(),
173             (self.y_axis - other.y_axis).abs(),
174             (self.z_axis - other.z_axis).abs(),
175             (self.w_axis - other.w_axis).abs(),
176         )
177     }
178 }
179 
180 impl FloatCompare for Quat {
181     #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool182     fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
183         self.abs_diff_eq(*other, max_abs_diff)
184     }
185     #[inline]
abs_diff(&self, other: &Self) -> Self186     fn abs_diff(&self, other: &Self) -> Self {
187         let a: Vec4 = (*self).into();
188         let b: Vec4 = (*other).into();
189         Quat::from_vec4((a - b).abs())
190     }
191 }
192 
193 impl FloatCompare for Vec2 {
194     #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool195     fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
196         self.abs_diff_eq(*other, max_abs_diff)
197     }
198     #[inline]
abs_diff(&self, other: &Self) -> Self199     fn abs_diff(&self, other: &Self) -> Self {
200         (*self - *other).abs()
201     }
202 }
203 
204 impl FloatCompare for Vec3 {
205     #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool206     fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
207         self.abs_diff_eq(*other, max_abs_diff)
208     }
209     #[inline]
abs_diff(&self, other: &Self) -> Self210     fn abs_diff(&self, other: &Self) -> Self {
211         (*self - *other).abs()
212     }
213 }
214 
215 impl FloatCompare for Vec3A {
216     #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool217     fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
218         self.abs_diff_eq(*other, max_abs_diff)
219     }
220     #[inline]
abs_diff(&self, other: &Self) -> Self221     fn abs_diff(&self, other: &Self) -> Self {
222         (*self - *other).abs()
223     }
224 }
225 
226 impl FloatCompare for Vec4 {
227     #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool228     fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
229         self.abs_diff_eq(*other, max_abs_diff)
230     }
231     #[inline]
abs_diff(&self, other: &Self) -> Self232     fn abs_diff(&self, other: &Self) -> Self {
233         (*self - *other).abs()
234     }
235 }
236 
237 impl FloatCompare for DQuat {
238     #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool239     fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
240         self.abs_diff_eq(*other, max_abs_diff as f64)
241     }
242     #[inline]
abs_diff(&self, other: &Self) -> Self243     fn abs_diff(&self, other: &Self) -> Self {
244         let a: DVec4 = (*self).into();
245         let b: DVec4 = (*other).into();
246         DQuat::from_vec4((a - b).abs())
247     }
248 }
249 
250 impl FloatCompare for DVec2 {
251     #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool252     fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
253         self.abs_diff_eq(*other, max_abs_diff as f64)
254     }
255     #[inline]
abs_diff(&self, other: &Self) -> Self256     fn abs_diff(&self, other: &Self) -> Self {
257         (*self - *other).abs()
258     }
259 }
260 
261 impl FloatCompare for DVec3 {
262     #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool263     fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
264         self.abs_diff_eq(*other, max_abs_diff as f64)
265     }
266     #[inline]
abs_diff(&self, other: &Self) -> Self267     fn abs_diff(&self, other: &Self) -> Self {
268         (*self - *other).abs()
269     }
270 }
271 
272 impl FloatCompare for DVec4 {
273     #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool274     fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
275         self.abs_diff_eq(*other, max_abs_diff as f64)
276     }
277     #[inline]
abs_diff(&self, other: &Self) -> Self278     fn abs_diff(&self, other: &Self) -> Self {
279         (*self - *other).abs()
280     }
281 }
282