1use bevy_math::*;
4
5pub trait RecipOrZero {
7 fn recip_or_zero(self) -> Self;
10}
11
12impl RecipOrZero for f32 {
13 #[inline]
14 fn recip_or_zero(self) -> Self {
15 if self != 0.0 && self.is_finite() {
16 self.recip()
17 } else {
18 0.0
19 }
20 }
21}
22
23impl RecipOrZero for f64 {
24 #[inline]
25 fn recip_or_zero(self) -> Self {
26 if self != 0.0 && self.is_finite() {
27 self.recip()
28 } else {
29 0.0
30 }
31 }
32}
33
34impl RecipOrZero for Vec2 {
35 #[inline]
36 fn recip_or_zero(self) -> Self {
37 Self::new(self.x.recip_or_zero(), self.y.recip_or_zero())
38 }
39}
40
41impl RecipOrZero for Vec3 {
42 #[inline]
43 fn recip_or_zero(self) -> Self {
44 Self::new(
45 self.x.recip_or_zero(),
46 self.y.recip_or_zero(),
47 self.z.recip_or_zero(),
48 )
49 }
50}
51
52impl RecipOrZero for DVec2 {
53 #[inline]
54 fn recip_or_zero(self) -> Self {
55 Self::new(self.x.recip_or_zero(), self.y.recip_or_zero())
56 }
57}
58
59impl RecipOrZero for DVec3 {
60 #[inline]
61 fn recip_or_zero(self) -> Self {
62 Self::new(
63 self.x.recip_or_zero(),
64 self.y.recip_or_zero(),
65 self.z.recip_or_zero(),
66 )
67 }
68}
69
70pub trait MatExt {
72 fn inverse_or_zero(self) -> Self;
75}
76
77impl MatExt for Mat2 {
78 #[inline]
79 fn inverse_or_zero(self) -> Self {
80 if self.determinant() == 0.0 {
81 Self::ZERO
82 } else {
83 self.inverse()
84 }
85 }
86}
87
88impl MatExt for DMat2 {
89 #[inline]
90 fn inverse_or_zero(self) -> Self {
91 if self.determinant() == 0.0 {
92 Self::ZERO
93 } else {
94 self.inverse()
95 }
96 }
97}
98
99impl MatExt for Mat3 {
100 #[inline]
101 fn inverse_or_zero(self) -> Self {
102 if self.determinant() == 0.0 {
103 Self::ZERO
104 } else {
105 self.inverse()
106 }
107 }
108}
109
110impl MatExt for DMat3 {
111 #[inline]
112 fn inverse_or_zero(self) -> Self {
113 if self.determinant() == 0.0 {
114 Self::ZERO
115 } else {
116 self.inverse()
117 }
118 }
119}