1 #![allow(dead_code)]
2 #[path = "support/macros.rs"]
3 #[macro_use]
4 mod macros;
5
6 #[cfg(target_arch = "wasm32")]
7 wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
8
9 use glam::{
10 DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4, Mat2, Mat3, Mat3A, Mat4, Quat, Vec2, Vec3,
11 Vec3A, Vec4,
12 };
13
14 pub trait Deg {
to_radians(self) -> Self15 fn to_radians(self) -> Self;
16 }
17
18 impl Deg for f32 {
to_radians(self) -> f3219 fn to_radians(self) -> f32 {
20 f32::to_radians(self)
21 }
22 }
23
24 impl Deg for f64 {
to_radians(self) -> f6425 fn to_radians(self) -> f64 {
26 f64::to_radians(self)
27 }
28 }
29
30 /// Helper function for migrating away from `glam::angle::deg`.
31 #[allow(dead_code)]
32 #[inline]
deg<T: Deg>(angle: T) -> T33 pub fn deg<T: Deg>(angle: T) -> T {
34 angle.to_radians()
35 }
36
37 /// Helper function for migrating away from `glam::angle::rad`.
38 #[allow(dead_code)]
39 #[inline]
rad<T>(angle: T) -> T40 pub fn rad<T>(angle: T) -> T {
41 angle
42 }
43
44 /// Trait used by the `assert_approx_eq` macro for floating point comparisons.
45 pub trait FloatCompare<Rhs: ?Sized = Self> {
46 /// Return true if the absolute difference between `self` and `other` is
47 /// less then or equal to `max_abs_diff`.
approx_eq(&self, other: &Rhs, max_abs_diff: f32) -> bool48 fn approx_eq(&self, other: &Rhs, max_abs_diff: f32) -> bool;
49 /// Returns the absolute difference of `self` and `other` which is printed
50 /// if `assert_approx_eq` fails.
abs_diff(&self, other: &Rhs) -> Rhs51 fn abs_diff(&self, other: &Rhs) -> Rhs;
52 }
53
54 impl FloatCompare for f32 {
55 #[inline]
approx_eq(&self, other: &f32, max_abs_diff: f32) -> bool56 fn approx_eq(&self, other: &f32, max_abs_diff: f32) -> bool {
57 (self - other).abs() <= max_abs_diff
58 }
59 #[inline]
abs_diff(&self, other: &f32) -> f3260 fn abs_diff(&self, other: &f32) -> f32 {
61 (self - other).abs()
62 }
63 }
64
65 impl FloatCompare for f64 {
66 #[inline]
approx_eq(&self, other: &f64, max_abs_diff: f32) -> bool67 fn approx_eq(&self, other: &f64, max_abs_diff: f32) -> bool {
68 (self - other).abs() <= max_abs_diff as f64
69 }
70 #[inline]
abs_diff(&self, other: &f64) -> f6471 fn abs_diff(&self, other: &f64) -> f64 {
72 (self - other).abs()
73 }
74 }
75
76 impl FloatCompare for Mat2 {
77 #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool78 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
79 self.abs_diff_eq(*other, max_abs_diff)
80 }
81 #[inline]
abs_diff(&self, other: &Self) -> Self82 fn abs_diff(&self, other: &Self) -> Self {
83 Self::from_cols(
84 (self.x_axis - other.x_axis).abs(),
85 (self.y_axis - other.y_axis).abs(),
86 )
87 }
88 }
89
90 impl FloatCompare for DMat2 {
91 #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool92 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
93 self.abs_diff_eq(*other, max_abs_diff as f64)
94 }
95 #[inline]
abs_diff(&self, other: &Self) -> Self96 fn abs_diff(&self, other: &Self) -> Self {
97 Self::from_cols(
98 (self.x_axis - other.x_axis).abs(),
99 (self.y_axis - other.y_axis).abs(),
100 )
101 }
102 }
103
104 impl FloatCompare for Mat3 {
105 #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool106 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
107 self.abs_diff_eq(*other, max_abs_diff)
108 }
109 #[inline]
abs_diff(&self, other: &Self) -> Self110 fn abs_diff(&self, other: &Self) -> Self {
111 Self::from_cols(
112 (self.x_axis - other.x_axis).abs(),
113 (self.y_axis - other.y_axis).abs(),
114 (self.z_axis - other.z_axis).abs(),
115 )
116 }
117 }
118
119 impl FloatCompare for Mat3A {
120 #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool121 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
122 self.abs_diff_eq(*other, max_abs_diff)
123 }
124 #[inline]
abs_diff(&self, other: &Self) -> Self125 fn abs_diff(&self, other: &Self) -> Self {
126 Self::from_cols(
127 (self.x_axis - other.x_axis).abs(),
128 (self.y_axis - other.y_axis).abs(),
129 (self.z_axis - other.z_axis).abs(),
130 )
131 }
132 }
133
134 impl FloatCompare for DMat3 {
135 #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool136 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
137 self.abs_diff_eq(*other, max_abs_diff as f64)
138 }
139 #[inline]
abs_diff(&self, other: &Self) -> Self140 fn abs_diff(&self, other: &Self) -> Self {
141 Self::from_cols(
142 (self.x_axis - other.x_axis).abs(),
143 (self.y_axis - other.y_axis).abs(),
144 (self.z_axis - other.z_axis).abs(),
145 )
146 }
147 }
148
149 impl FloatCompare for DMat4 {
150 #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool151 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
152 self.abs_diff_eq(*other, max_abs_diff as f64)
153 }
154 #[inline]
abs_diff(&self, other: &Self) -> Self155 fn abs_diff(&self, other: &Self) -> Self {
156 Self::from_cols(
157 (self.x_axis - other.x_axis).abs(),
158 (self.y_axis - other.y_axis).abs(),
159 (self.z_axis - other.z_axis).abs(),
160 (self.w_axis - other.w_axis).abs(),
161 )
162 }
163 }
164
165 impl FloatCompare for Mat4 {
166 #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool167 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
168 self.abs_diff_eq(*other, max_abs_diff)
169 }
170 #[inline]
abs_diff(&self, other: &Self) -> Self171 fn abs_diff(&self, other: &Self) -> Self {
172 Self::from_cols(
173 (self.x_axis - other.x_axis).abs(),
174 (self.y_axis - other.y_axis).abs(),
175 (self.z_axis - other.z_axis).abs(),
176 (self.w_axis - other.w_axis).abs(),
177 )
178 }
179 }
180
181 impl FloatCompare for Quat {
182 #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool183 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
184 self.abs_diff_eq(*other, max_abs_diff)
185 }
186 #[inline]
abs_diff(&self, other: &Self) -> Self187 fn abs_diff(&self, other: &Self) -> Self {
188 let a: Vec4 = (*self).into();
189 let b: Vec4 = (*other).into();
190 Quat::from_vec4((a - b).abs())
191 }
192 }
193
194 impl FloatCompare for Vec2 {
195 #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool196 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
197 self.abs_diff_eq(*other, max_abs_diff)
198 }
199 #[inline]
abs_diff(&self, other: &Self) -> Self200 fn abs_diff(&self, other: &Self) -> Self {
201 (*self - *other).abs()
202 }
203 }
204
205 impl FloatCompare for Vec3 {
206 #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool207 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
208 self.abs_diff_eq(*other, max_abs_diff)
209 }
210 #[inline]
abs_diff(&self, other: &Self) -> Self211 fn abs_diff(&self, other: &Self) -> Self {
212 (*self - *other).abs()
213 }
214 }
215
216 impl FloatCompare for Vec3A {
217 #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool218 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
219 self.abs_diff_eq(*other, max_abs_diff)
220 }
221 #[inline]
abs_diff(&self, other: &Self) -> Self222 fn abs_diff(&self, other: &Self) -> Self {
223 (*self - *other).abs()
224 }
225 }
226
227 impl FloatCompare for Vec4 {
228 #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool229 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
230 self.abs_diff_eq(*other, max_abs_diff)
231 }
232 #[inline]
abs_diff(&self, other: &Self) -> Self233 fn abs_diff(&self, other: &Self) -> Self {
234 (*self - *other).abs()
235 }
236 }
237
238 impl FloatCompare for DQuat {
239 #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool240 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
241 self.abs_diff_eq(*other, max_abs_diff as f64)
242 }
243 #[inline]
abs_diff(&self, other: &Self) -> Self244 fn abs_diff(&self, other: &Self) -> Self {
245 let a: DVec4 = (*self).into();
246 let b: DVec4 = (*other).into();
247 DQuat::from_vec4((a - b).abs())
248 }
249 }
250
251 impl FloatCompare for DVec2 {
252 #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool253 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
254 self.abs_diff_eq(*other, max_abs_diff as f64)
255 }
256 #[inline]
abs_diff(&self, other: &Self) -> Self257 fn abs_diff(&self, other: &Self) -> Self {
258 (*self - *other).abs()
259 }
260 }
261
262 impl FloatCompare for DVec3 {
263 #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool264 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
265 self.abs_diff_eq(*other, max_abs_diff as f64)
266 }
267 #[inline]
abs_diff(&self, other: &Self) -> Self268 fn abs_diff(&self, other: &Self) -> Self {
269 (*self - *other).abs()
270 }
271 }
272
273 impl FloatCompare for DVec4 {
274 #[inline]
approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool275 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
276 self.abs_diff_eq(*other, max_abs_diff as f64)
277 }
278 #[inline]
abs_diff(&self, other: &Self) -> Self279 fn abs_diff(&self, other: &Self) -> Self {
280 (*self - *other).abs()
281 }
282 }
283