vector_traits/
glam_impl.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2// Copyright (c) 2023, 2025 lacklustr@protonmail.com https://github.com/eadf
3
4// This file is part of vector-traits.
5
6pub mod mat4a;
7#[cfg(test)]
8mod tests;
9pub mod vec2a;
10
11use crate::{
12    glam,
13    prelude::{
14        glam_ext::{Mat4A, Vec2A},
15        *,
16    },
17};
18
19use approx::{AbsDiffEq, UlpsEq};
20use std::{borrow::Borrow, fmt::Debug};
21
22macro_rules! impl_aabb2 {
23    ($struct_name:ident, $vec_type:ty, $scalar_type:ty) => {
24        #[derive(Debug, Copy, Clone)]
25        pub struct $struct_name {
26            min: $vec_type,
27            max: $vec_type,
28        }
29
30        impl Default for $struct_name {
31            fn default() -> Self {
32                Self {
33                    min: <$vec_type>::splat(<$scalar_type>::INFINITY),
34                    max: <$vec_type>::splat(<$scalar_type>::NEG_INFINITY),
35                }
36            }
37        }
38
39        impl crate::prelude::Aabb2 for $struct_name {
40            type Vector = $vec_type;
41
42            fn from_point(point: Self::Vector) -> Self {
43                Self {
44                    min: point,
45                    max: point,
46                }
47            }
48
49            #[inline(always)]
50            fn from_corners(min: Self::Vector, max: Self::Vector) -> Self {
51                let mut rv = Self { min: max, max };
52                rv.add_point(min);
53                rv
54            }
55
56            #[inline(always)]
57            fn from_center_and_half_extents(
58                center: Self::Vector,
59                half_extents: Self::Vector,
60            ) -> Self {
61                Self {
62                    min: center - half_extents,
63                    max: center + half_extents,
64                }
65            }
66
67            #[inline(always)]
68            fn from_center_and_size(center: Self::Vector, size: Self::Vector) -> Self {
69                let half_extents =
70                    size / <<Self as crate::prelude::Aabb2>::Vector as HasXY>::Scalar::TWO;
71                Self::from_center_and_half_extents(center, half_extents)
72            }
73
74            fn from_points<I>(points: I) -> Self
75            where
76                I: IntoIterator,
77                I::Item: Borrow<Self::Vector>,
78            {
79                let mut aabb = Self::default();
80                for point in points {
81                    let point = *point.borrow();
82                    aabb.min = aabb.min.min(point);
83                    aabb.max = aabb.max.max(point);
84                }
85                aabb
86            }
87
88            #[inline(always)]
89            fn max(&self) -> Self::Vector {
90                self.max
91            }
92            #[inline(always)]
93            fn min(&self) -> Self::Vector {
94                self.min
95            }
96            #[inline(always)]
97            fn add_point(&mut self, pos: Self::Vector) {
98                self.max = self.max.max(pos);
99                self.min = self.min.min(pos);
100            }
101            #[inline(always)]
102            fn is_empty(&self) -> bool {
103                self.max.x < self.min.x
104            }
105            #[inline(always)]
106            fn add_aabb(&mut self, other: &Self) {
107                self.max = self.max.max(other.max);
108                self.min = self.min.min(other.min);
109            }
110            #[inline(always)]
111            fn fast_pad(&mut self, delta: Self::Vector) {
112                self.max += delta;
113                self.min -= delta;
114            }
115            #[inline]
116            fn pad(&mut self, delta: Self::Vector) {
117                let center = self.center();
118                let half_extent: Self::Vector = (self.max - self.min)
119                    / <<Self as crate::prelude::Aabb2>::Vector as HasXY>::Scalar::TWO;
120                let new_half_extent: Self::Vector = (half_extent + delta);
121                let new_half_extent = new_half_extent.max(Self::Vector::ZERO);
122                self.max = center + new_half_extent;
123                self.min = center - new_half_extent;
124            }
125
126            #[inline(always)]
127            fn contains_point_inclusive(&self, point: Self::Vector) -> bool {
128                point.x >= self.min.x
129                    && point.x <= self.max.x
130                    && point.y >= self.min.y
131                    && point.y <= self.max.y
132            }
133            #[inline(always)]
134            fn center(&self) -> Self::Vector {
135                (self.max + self.min) * 0.5
136            }
137            #[inline(always)]
138            fn extents(&self) -> (Self::Vector, Self::Vector, Self::Vector) {
139                (self.min, self.max, self.max - self.min)
140            }
141            #[inline(always)]
142            fn contains_aabb_inclusive(&self, other: &Self) -> bool {
143                self.min.x <= other.min.x
144                    && other.max.x <= self.max.x
145                    && self.min.y <= other.min.y
146                    && other.max.y <= self.max.y
147            }
148            #[inline(always)]
149            fn convex_hull(&self) -> Vec<Self::Vector> {
150                crate::trait_impl::aabb_to_vec::<$struct_name>(*self)
151            }
152            #[inline(always)]
153            fn apply<F>(&mut self, f: F)
154            where
155                F: Fn(Self::Vector) -> Self::Vector,
156            {
157                if !self.is_empty() {
158                    let new_min = f(self.min);
159                    let new_max = f(self.max);
160                    self.min = new_min.min(new_max);
161                    self.max = new_max.max(new_min);
162                }
163            }
164        }
165
166        impl From<$struct_name> for Vec<$vec_type> {
167            #[inline(always)]
168            fn from(other: $struct_name) -> Self {
169                crate::trait_impl::aabb_to_vec(other)
170            }
171        }
172
173        impl PartialEq for $struct_name
174        where
175            $struct_name: crate::prelude::Aabb2,
176        {
177            #[inline(always)]
178            fn eq(&self, other: &Self) -> bool {
179                crate::trait_impl::aabb2_partial_eq(self, other)
180            }
181        }
182    };
183}
184
185impl_aabb2!(DAabb2, glam::DVec2, f64);
186impl_aabb2!(Aabb2A, Vec2A, f32);
187impl_aabb2!(Aabb2, glam::Vec2, f32);
188
189macro_rules! impl_aabb3 {
190    ($struct_name:ident, $vec_type:ty, $scalar_type:ty) => {
191        #[derive(Debug, Copy, Clone)]
192        pub struct $struct_name {
193            min: $vec_type,
194            max: $vec_type,
195        }
196
197        impl Default for $struct_name {
198            fn default() -> Self {
199                Self {
200                    min: <$vec_type>::splat(<$scalar_type>::INFINITY),
201                    max: <$vec_type>::splat(<$scalar_type>::NEG_INFINITY),
202                }
203            }
204        }
205
206        impl crate::prelude::Aabb3 for $struct_name {
207            type Vector = $vec_type;
208
209            fn from_point(point: Self::Vector) -> Self {
210                Self {
211                    min: point,
212                    max: point,
213                }
214            }
215
216            #[inline(always)]
217            fn from_corners(min: Self::Vector, max: Self::Vector) -> Self {
218                let mut rv = Self { min: max, max };
219                rv.add_point(min);
220                rv
221            }
222
223            #[inline(always)]
224            fn from_center_and_half_extents(
225                center: Self::Vector,
226                half_extents: Self::Vector,
227            ) -> Self {
228                Self {
229                    min: center - half_extents,
230                    max: center + half_extents,
231                }
232            }
233
234            #[inline(always)]
235            fn from_center_and_size(center: Self::Vector, size: Self::Vector) -> Self {
236                let half_extents = size / 2.0;
237                Self::from_center_and_half_extents(center, half_extents)
238            }
239
240            fn from_points<I>(points: I) -> Self
241            where
242                I: IntoIterator,
243                I::Item: Borrow<Self::Vector>,
244            {
245                let mut aabb = Self::default();
246                for point in points {
247                    let point = *point.borrow();
248                    aabb.min = aabb.min.min(point);
249                    aabb.max = aabb.max.max(point);
250                }
251                aabb
252            }
253
254            #[inline(always)]
255            fn max(&self) -> Self::Vector {
256                self.max
257            }
258            #[inline(always)]
259            fn min(&self) -> Self::Vector {
260                self.min
261            }
262            #[inline(always)]
263            fn add_point(&mut self, pos: Self::Vector) {
264                self.max = self.max.max(pos);
265                self.min = self.min.min(pos);
266            }
267            #[inline(always)]
268            fn is_empty(&self) -> bool {
269                self.max.x < self.min.x
270            }
271            #[inline(always)]
272            fn add_aabb(&mut self, other: &Self) {
273                self.max = self.max.max(other.max);
274                self.min = self.min.min(other.min);
275            }
276            #[inline(always)]
277            fn fast_pad(&mut self, delta: Self::Vector) {
278                self.max += delta;
279                self.min -= delta;
280            }
281            #[inline]
282            fn pad(&mut self, delta: Self::Vector) {
283                let center = self.center();
284                let half_extent = (self.max - self.min) * 0.5;
285                let new_half_extent = (half_extent + delta).max(Self::Vector::ZERO);
286                self.max = center + new_half_extent;
287                self.min = center - new_half_extent;
288            }
289
290            #[inline(always)]
291            fn contains_point_inclusive(&self, point: Self::Vector) -> bool {
292                point.x >= self.min.x
293                    && point.x <= self.max.x
294                    && point.y >= self.min.y
295                    && point.y <= self.max.y
296                    && point.z >= self.min.z
297                    && point.z <= self.max.z
298            }
299            #[inline(always)]
300            fn center(&self) -> Self::Vector {
301                (self.max + self.min) * 0.5
302            }
303            #[inline(always)]
304            fn extents(&self) -> (Self::Vector, Self::Vector, Self::Vector) {
305                (self.min, self.max, self.max - self.min)
306            }
307            #[inline(always)]
308            fn contains_aabb_inclusive(&self, other: &Self) -> bool {
309                self.min.x <= other.min.x
310                    && other.max.x <= self.max.x
311                    && self.min.y <= other.min.y
312                    && other.max.y <= self.max.y
313                    && self.min.z <= other.min.z
314                    && other.max.z <= self.max.z
315            }
316            #[inline(always)]
317            fn get_plane(&self) -> Option<Plane> {
318                Plane::get_plane::<Self>(self)
319            }
320            #[inline(always)]
321            fn get_plane_relaxed(&self, epsilon: $scalar_type, max_ulps: u32) -> Option<Plane> {
322                Plane::get_plane_relaxed::<$vec_type>(self, epsilon, max_ulps)
323            }
324            #[inline(always)]
325            fn apply<F>(&mut self, f: F)
326            where
327                F: Fn(Self::Vector) -> Self::Vector,
328            {
329                if !self.is_empty() {
330                    let new_min = f(self.min);
331                    let new_max = f(self.max);
332                    self.min = new_min.min(new_max);
333                    self.max = new_max.max(new_min);
334                }
335            }
336        }
337
338        impl PartialEq for $struct_name
339        where
340            $struct_name: crate::prelude::Aabb3,
341        {
342            #[inline(always)]
343            fn eq(&self, other: &Self) -> bool {
344                crate::trait_impl::aabb3_partial_eq(self, other)
345            }
346        }
347    };
348}
349
350impl_aabb3!(DAabb3, glam::DVec3, f64);
351impl_aabb3!(Aabb3A, glam::Vec3A, f32);
352impl_aabb3!(Aabb3, glam::Vec3, f32);
353
354macro_rules! impl_vector2 {
355    ($vec2_type:ty, $scalar_type:ty, $vec3_type:ty, $affine_type:ty, $aabb_type:ty) => {
356        impl HasXY for $vec2_type {
357            type Scalar = $scalar_type;
358
359            #[inline(always)]
360            fn new_2d(x: Self::Scalar, y: Self::Scalar) -> Self {
361                <$vec2_type>::new(x, y)
362            }
363            #[inline(always)]
364            fn x(self) -> Self::Scalar {
365                self.x
366            }
367            #[inline(always)]
368            fn set_x(&mut self, val: Self::Scalar) {
369                self.x = val
370            }
371            #[inline(always)]
372            fn x_mut(&mut self) -> &mut Self::Scalar {
373                &mut self.x
374            }
375            #[inline(always)]
376            fn y(self) -> Self::Scalar {
377                self.y
378            }
379            #[inline(always)]
380            fn set_y(&mut self, val: Self::Scalar) {
381                self.y = val
382            }
383            #[inline(always)]
384            fn y_mut(&mut self) -> &mut Self::Scalar {
385                &mut self.y
386            }
387        }
388
389        impl GenericVector2 for $vec2_type {
390            const ZERO: Self = <$vec2_type>::ZERO;
391            const ONE: Self = <$vec2_type>::ONE;
392
393            type Vector3 = $vec3_type;
394            type Affine = $affine_type;
395            type Aabb = $aabb_type;
396
397            #[inline(always)]
398            fn new(x: Self::Scalar, y: Self::Scalar) -> Self {
399                <$vec2_type>::new(x, y)
400            }
401            #[inline(always)]
402            fn splat(value: Self::Scalar) -> Self {
403                <$vec2_type>::splat(value)
404            }
405            #[inline(always)]
406            fn to_3d(self, z: Self::Scalar) -> Self::Vector3 {
407                <$vec3_type>::new(self.x, self.y, z)
408            }
409            #[inline(always)]
410            fn magnitude(self) -> Self::Scalar {
411                self.length()
412            }
413            #[inline(always)]
414            fn magnitude_sq(self) -> Self::Scalar {
415                self.length_squared()
416            }
417            #[inline(always)]
418            fn dot(self, rhs: Self) -> Self::Scalar {
419                <$vec2_type>::dot(self, rhs)
420            }
421            #[inline(always)]
422            fn perp_dot(self, rhs: Self) -> Self::Scalar {
423                self.perp_dot(rhs)
424            }
425            #[inline(always)]
426            fn normalize(self) -> Self {
427                <$vec2_type>::normalize(self)
428            }
429            #[inline(always)]
430            fn try_normalize(self, epsilon: Self::Scalar) -> Option<Self> {
431                let l_sq = self.length_squared();
432                (l_sq > epsilon * epsilon).then(|| self / l_sq.sqrt())
433            }
434            #[inline(always)]
435            fn distance(self, rhs: Self) -> Self::Scalar {
436                <$vec2_type>::distance(self, rhs)
437            }
438            #[inline(always)]
439            fn distance_sq(self, rhs: Self) -> Self::Scalar {
440                <$vec2_type>::distance_squared(self, rhs)
441            }
442            #[inline(always)]
443            fn min(self, rhs: Self) -> Self {
444                <$vec2_type>::min(self, rhs)
445            }
446            #[inline(always)]
447            fn max(self, rhs: Self) -> Self {
448                <$vec2_type>::max(self, rhs)
449            }
450            #[inline(always)]
451            fn clamp(self, min: Self, max: Self) -> Self {
452                <$vec2_type>::clamp(self, min, max)
453            }
454            #[inline(always)]
455            fn is_finite(self) -> bool {
456                self.x.is_finite() && self.y.is_finite()
457            }
458        }
459    };
460}
461macro_rules! impl_approx2 {
462    ($vec_type:ty) => {
463        impl Approx for $vec_type {
464            #[inline(always)]
465            fn is_ulps_eq(
466                self,
467                other: Self,
468                epsilon: <Self::Scalar as AbsDiffEq>::Epsilon,
469                max_ulps: u32,
470            ) -> bool {
471                self.x().ulps_eq(&other.x(), epsilon, max_ulps)
472                    && self.y().ulps_eq(&other.y(), epsilon, max_ulps)
473            }
474            #[inline(always)]
475            fn is_abs_diff_eq(
476                self,
477                other: Self,
478                epsilon: <Self::Scalar as AbsDiffEq>::Epsilon,
479            ) -> bool {
480                self.x().abs_diff_eq(&other.x(), epsilon)
481                    && self.y().abs_diff_eq(&other.y(), epsilon)
482            }
483        }
484    };
485}
486
487impl_vector2!(glam::Vec2, f32, glam::Vec3, glam::Mat3, Aabb2);
488impl_approx2!(glam::Vec2);
489impl_vector2!(glam::DVec2, f64, glam::DVec3, glam::DMat3, DAabb2);
490impl_approx2!(glam::DVec2);
491
492macro_rules! impl_vector3 {
493    ($vec3_type:ty, $scalar_type:ty, $vec2_type:ty, $affine_type:ty, $aabb_type:ty) => {
494        impl HasXY for $vec3_type {
495            type Scalar = $scalar_type;
496            fn new_2d(x: Self::Scalar, y: Self::Scalar) -> Self {
497                <$vec3_type>::new(x, y, Self::Scalar::ZERO)
498            }
499            #[inline(always)]
500            fn x(self) -> Self::Scalar {
501                self.x
502            }
503            #[inline(always)]
504            fn set_x(&mut self, val: Self::Scalar) {
505                self.x = val
506            }
507            #[inline(always)]
508            fn x_mut(&mut self) -> &mut Self::Scalar {
509                &mut self.x
510            }
511            #[inline(always)]
512            fn y(self) -> Self::Scalar {
513                self.y
514            }
515            #[inline(always)]
516            fn set_y(&mut self, val: Self::Scalar) {
517                self.y = val
518            }
519            #[inline(always)]
520            fn y_mut(&mut self) -> &mut Self::Scalar {
521                &mut self.y
522            }
523        }
524
525        impl HasXYZ for $vec3_type {
526            #[inline(always)]
527            fn new_3d(x: Self::Scalar, y: Self::Scalar, z: Self::Scalar) -> Self {
528                <$vec3_type>::new(x, y, z)
529            }
530            #[inline(always)]
531            fn z(self) -> Self::Scalar {
532                self.z
533            }
534            #[inline(always)]
535            fn set_z(&mut self, val: Self::Scalar) {
536                self.z = val
537            }
538            #[inline(always)]
539            fn z_mut(&mut self) -> &mut Self::Scalar {
540                &mut self.z
541            }
542        }
543
544        impl GenericVector3 for $vec3_type {
545            const ZERO: Self = <$vec3_type>::ZERO;
546            const ONE: Self = <$vec3_type>::ONE;
547
548            type Affine = $affine_type;
549            type Aabb = $aabb_type;
550            type Vector2 = $vec2_type;
551            #[inline(always)]
552            fn new(x: Self::Scalar, y: Self::Scalar, z: Self::Scalar) -> Self {
553                <$vec3_type>::new(x, y, z)
554            }
555            #[inline(always)]
556            fn to_2d(self) -> Self::Vector2 {
557                <$vec2_type>::new(self.x, self.y)
558            }
559            #[inline(always)]
560            fn splat(value: Self::Scalar) -> Self {
561                <$vec3_type>::splat(value)
562            }
563            #[inline(always)]
564            fn magnitude(self) -> Self::Scalar {
565                <$vec3_type>::length(self)
566            }
567            #[inline(always)]
568            fn magnitude_sq(self) -> Self::Scalar {
569                <$vec3_type>::length_squared(self)
570            }
571            #[inline(always)]
572            fn normalize(self) -> Self {
573                <$vec3_type>::normalize(self)
574            }
575            #[inline(always)]
576            fn try_normalize(self, epsilon: Self::Scalar) -> Option<Self> {
577                let l_sq = self.length_squared();
578                (l_sq > epsilon * epsilon).then(|| self / l_sq.sqrt())
579            }
580            #[inline(always)]
581            fn dot(self, rhs: Self) -> Self::Scalar {
582                <$vec3_type>::dot(self, rhs)
583            }
584            #[inline(always)]
585            fn cross(self, rhs: Self) -> Self {
586                <$vec3_type>::cross(self, rhs)
587            }
588            #[inline(always)]
589            fn distance(self, rhs: Self) -> Self::Scalar {
590                <$vec3_type>::distance(self, rhs)
591            }
592            #[inline(always)]
593            fn distance_sq(self, rhs: Self) -> Self::Scalar {
594                <$vec3_type>::distance_squared(self, rhs)
595            }
596            #[inline(always)]
597            fn min(self, rhs: Self) -> Self {
598                <$vec3_type>::min(self, rhs)
599            }
600            #[inline(always)]
601            fn max(self, rhs: Self) -> Self {
602                <$vec3_type>::max(self, rhs)
603            }
604            #[inline(always)]
605            fn clamp(self, min: Self, max: Self) -> Self {
606                <$vec3_type>::clamp(self, min, max)
607            }
608            #[inline(always)]
609            fn is_finite(self) -> bool {
610                self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
611            }
612        }
613    };
614}
615
616macro_rules! impl_approx3 {
617    ($vec_type:ty) => {
618        impl Approx for $vec_type {
619            #[inline(always)]
620            fn is_ulps_eq(
621                self,
622                other: Self,
623                epsilon: <Self::Scalar as AbsDiffEq>::Epsilon,
624                max_ulps: u32,
625            ) -> bool {
626                self.x.ulps_eq(&other.x, epsilon, max_ulps)
627                    && self.y.ulps_eq(&other.y, epsilon, max_ulps)
628                    && self.z.ulps_eq(&other.z, epsilon, max_ulps)
629            }
630            #[inline(always)]
631            fn is_abs_diff_eq(
632                self,
633                other: Self,
634                epsilon: <Self::Scalar as AbsDiffEq>::Epsilon,
635            ) -> bool {
636                self.x.abs_diff_eq(&other.x, epsilon)
637                    && self.y.abs_diff_eq(&other.y, epsilon)
638                    && self.z.abs_diff_eq(&other.z, epsilon)
639            }
640        }
641    };
642}
643
644impl_vector3!(glam::Vec3, f32, glam::Vec2, glam::Mat4, Aabb3);
645impl_approx3!(glam::Vec3);
646impl_vector3!(glam::DVec3, f64, glam::DVec2, glam::DMat4, DAabb3);
647impl_approx3!(glam::DVec3);
648
649impl_approx2!(Vec2A);
650
651impl HasXY for glam::Vec3A {
652    type Scalar = f32;
653    #[inline(always)]
654    fn new_2d(x: Self::Scalar, y: Self::Scalar) -> Self {
655        glam::vec3a(x, y, Self::Scalar::ZERO)
656    }
657
658    #[inline(always)]
659    fn x(self) -> Self::Scalar {
660        self.x
661    }
662
663    #[inline(always)]
664    fn x_mut(&mut self) -> &mut Self::Scalar {
665        &mut self.x
666    }
667
668    #[inline(always)]
669    fn set_x(&mut self, val: Self::Scalar) {
670        self.x = val;
671    }
672
673    #[inline(always)]
674    fn y(self) -> Self::Scalar {
675        self.y
676    }
677
678    #[inline(always)]
679    fn y_mut(&mut self) -> &mut Self::Scalar {
680        &mut self.y
681    }
682
683    #[inline(always)]
684    fn set_y(&mut self, val: Self::Scalar) {
685        self.y = val
686    }
687}
688
689impl HasXYZ for glam::Vec3A {
690    #[inline(always)]
691    fn new_3d(x: Self::Scalar, y: Self::Scalar, z: Self::Scalar) -> Self {
692        glam::vec3a(x, y, z)
693    }
694
695    #[inline(always)]
696    fn z(self) -> Self::Scalar {
697        self.z
698    }
699
700    #[inline(always)]
701    fn z_mut(&mut self) -> &mut Self::Scalar {
702        &mut self.z
703    }
704
705    #[inline(always)]
706    fn set_z(&mut self, val: Self::Scalar) {
707        self.z = val
708    }
709}
710
711impl GenericVector3 for glam::Vec3A {
712    const ZERO: Self = glam::Vec3A::ZERO;
713    const ONE: Self = glam::Vec3A::ONE;
714
715    type Vector2 = Vec2A;
716    type Aabb = Aabb3A;
717    type Affine = Mat4A;
718    #[inline(always)]
719    fn new(x: Self::Scalar, y: Self::Scalar, z: Self::Scalar) -> Self {
720        glam::Vec3A::new(x, y, z)
721    }
722    #[inline(always)]
723    fn splat(value: Self::Scalar) -> Self {
724        Self::new(value, value, value)
725    }
726    #[inline(always)]
727    fn to_2d(self) -> Self::Vector2 {
728        Vec2A(glam::vec2(self.x, self.y))
729    }
730
731    #[inline(always)]
732    fn magnitude(self) -> Self::Scalar {
733        self.length()
734    }
735
736    #[inline(always)]
737    fn magnitude_sq(self) -> Self::Scalar {
738        self.length_squared()
739    }
740
741    #[inline(always)]
742    fn dot(self, other: Self) -> Self::Scalar {
743        self.dot(other)
744    }
745
746    #[inline(always)]
747    fn cross(self, rhs: Self) -> Self {
748        self.cross(rhs)
749    }
750
751    #[inline(always)]
752    fn normalize(self) -> Self {
753        self.normalize()
754    }
755
756    #[inline(always)]
757    fn try_normalize(self, epsilon: Self::Scalar) -> Option<Self> {
758        let l_sq = self.length_squared();
759        (l_sq > epsilon * epsilon).then(|| self / l_sq.sqrt())
760    }
761
762    #[inline(always)]
763    fn distance(self, other: Self) -> Self::Scalar {
764        self.distance(other)
765    }
766
767    #[inline(always)]
768    fn distance_sq(self, rhs: Self) -> Self::Scalar {
769        self.distance_squared(rhs)
770    }
771    #[inline(always)]
772    fn min(self, rhs: Self) -> Self {
773        glam::Vec3A::min(self, rhs)
774    }
775    #[inline(always)]
776    fn max(self, rhs: Self) -> Self {
777        glam::Vec3A::max(self, rhs)
778    }
779    #[inline(always)]
780    fn clamp(self, min: Self, max: Self) -> Self {
781        glam::Vec3A::clamp(self, min, max)
782    }
783    fn is_finite(self) -> bool {
784        self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
785    }
786}
787
788impl_approx3!(glam::Vec3A);
789
790macro_rules! impl_matrix3 {
791    ($mat_type:ty, $vec_type:ty, $vec_type_p1:ty, $scalar_type:ty) => {
792        impl Affine2D for $mat_type
793        where
794            $vec_type: GenericVector2<Scalar = $scalar_type> + HasXY,
795        {
796            type Vector2 = $vec_type;
797
798            #[inline(always)]
799            fn from_cols_array(array: &[$scalar_type; 9]) -> Self {
800                <$mat_type>::from_cols_array(array)
801            }
802
803            #[inline(always)]
804            fn identity() -> Self {
805                <$mat_type>::IDENTITY
806            }
807
808            #[inline(always)]
809            fn transform_vector2(&self, point: $vec_type) -> $vec_type {
810                <$mat_type>::transform_vector2(self, point)
811            }
812
813            #[inline(always)]
814            fn transform_point2(&self, point: $vec_type) -> $vec_type {
815                <$mat_type>::transform_point2(self, point)
816            }
817
818            #[inline(always)]
819            fn try_inverse(&self) -> Option<Self> {
820                let inv = self.inverse();
821                inv.is_finite().then_some(inv)
822            }
823        }
824    };
825}
826
827// For Mat3 with Vec2 and f32
828impl_matrix3!(glam::Mat3, glam::Vec2, glam::Vec3, f32);
829
830// For DMat3 with DVec2 and f64
831impl_matrix3!(glam::DMat3, glam::DVec2, glam::DVec3, f64);
832
833impl Affine2D for glam::Mat3A
834where
835    Vec2A: GenericVector2<Scalar = f32> + HasXY,
836{
837    type Vector2 = Vec2A;
838
839    #[inline(always)]
840    fn from_cols_array(array: &[f32; 9]) -> Self {
841        glam::Mat3A::from_cols_array(array)
842    }
843
844    #[inline(always)]
845    fn identity() -> Self {
846        glam::Mat3A::IDENTITY
847    }
848
849    #[inline(always)]
850    fn transform_vector2(&self, point: Vec2A) -> Vec2A {
851        Vec2A(glam::Mat3A::transform_vector2(self, *point))
852    }
853
854    #[inline(always)]
855    fn transform_point2(&self, point: Vec2A) -> Vec2A {
856        Vec2A(glam::Mat3A::transform_point2(self, *point))
857    }
858
859    #[inline(always)]
860    fn try_inverse(&self) -> Option<Self> {
861        let inverse = glam::Mat3A::inverse(self);
862        inverse.is_finite().then_some(inverse)
863    }
864}
865
866macro_rules! impl_matrix4 {
867    ($mat_type:ty, $vec_type:ty, $scalar_type:ty) => {
868        impl Affine3D for $mat_type
869        where
870            $vec_type: GenericVector3<Scalar = $scalar_type>,
871        {
872            type Vector3 = $vec_type;
873
874            #[inline(always)]
875            fn from_cols_array(array: &[$scalar_type; 16]) -> Self {
876                <$mat_type>::from_cols_array(array)
877            }
878
879            #[inline(always)]
880            fn identity() -> Self {
881                <$mat_type>::IDENTITY
882            }
883
884            #[inline(always)]
885            fn transform_vector3(&self, vec: $vec_type) -> $vec_type {
886                <$mat_type>::transform_vector3(self, vec)
887            }
888
889            #[inline(always)]
890            fn transform_point3(&self, point: $vec_type) -> $vec_type {
891                <$mat_type>::transform_point3(self, point)
892            }
893
894            #[inline(always)]
895            fn try_inverse(&self) -> Option<Self> {
896                let inverse = self.inverse();
897                inverse.is_finite().then_some(inverse)
898            }
899
900            #[inline(always)]
901            fn from_plane_to_xy(source_plane: Plane) -> Self {
902                crate::trait_impl::from_plane_to_xy::<$vec_type>(source_plane)
903            }
904
905            #[inline(always)]
906            fn from_translation(translation: Self::Vector3) -> Self {
907                <$mat_type>::from_translation(translation)
908            }
909
910            #[inline(always)]
911            fn from_scale(scale: Self::Vector3) -> Self {
912                <$mat_type>::from_scale(scale)
913            }
914        }
915    };
916}
917
918// For glam::Mat4 with Vec3 and f32
919impl_matrix4!(glam::Mat4, glam::Vec3, f32);
920// For DMat4 with DVec3 and f64
921impl_matrix4!(glam::DMat4, glam::DVec3, f64);
922
923impl SimdUpgradable for glam::Vec3 {
924    type Simd = glam::Vec3A;
925
926    #[inline(always)]
927    /// Convert to the SIMD-optimized variant
928    fn to_simd(self) -> Self::Simd {
929        self.to_vec3a()
930    }
931
932    #[inline(always)]
933    /// Convert from the SIMD-optimized variant
934    fn from_simd(simd: Self::Simd) -> Self {
935        simd.to_vec3()
936    }
937}
938
939impl SimdUpgradable for glam::DVec3 {
940    type Simd = glam::DVec3;
941
942    #[inline(always)]
943    /// Convert to the SIMD-optimized variant (this particular variant is a NOP)
944    fn to_simd(self) -> Self::Simd {
945        self
946    }
947
948    #[inline(always)]
949    /// Convert from the SIMD-optimized variant (this particular variant is a NOP)
950    fn from_simd(simd: Self::Simd) -> Self {
951        simd
952    }
953}