1use std::ops::Mul;
2
3use crate::{
4 matrix::Transpose,
5 scalar::{Lerp, MulAdd, Sqrt},
6 utils::EPSILON,
7 vector::Angle,
8 Dot, Matrix, Scalar, Vector, V,
9};
10
11impl Scalar for f32 {
12 type AbsOutput = f32;
13 type SinOutput = f32;
14 type CosOutput = f32;
15
16 fn abs(&self) -> Self::AbsOutput {
17 if *self < 0. {
18 -self
19 } else {
20 self.clone()
21 }
22 }
23
24 fn one() -> Self {
25 1.
26 }
27
28 fn inv(self) -> Self {
29 1. / self
30 }
31
32 type TanOutput = f32;
33 fn tan(self) -> Self::TanOutput {
34 f32::tan(self)
35 }
36
37 fn cos(self) -> Self::CosOutput {
38 f32::cos(self)
39 }
40
41 fn sin(self) -> Self::SinOutput {
42 f32::sin(self)
43 }
44
45 fn is_non_zero(&self) -> bool {
46 self.abs() > EPSILON as f32
47 }
48}
49
50impl MulAdd<f32, f32> for f32 {
51 fn mul_add(self, a: &f32, b: &f32) -> Self {
52 self.mul_add(*a, *b)
53 }
54}
55
56impl Sqrt for f32 {
57 fn sqrt(self: Self) -> Self {
58 self.powf(0.5)
59 }
60}
61
62impl Lerp for f32 {
63 fn lerp(u: Self, v: Self, t: f32) -> Self {
64 match t {
65 0. => u,
66 1. => v,
67 p => u + (v - u) * p,
68 }
69 }
70}
71
72impl Dot<f32> for [f32] {
73 fn dot(&self, v: &Vector<f32>) -> f32 {
74 assert_eq!(v.size(), self.len(), "vectors must be the same size");
75
76 self * v
77 }
78}
79
80impl Dot<f32> for Vector<f32> {
81 fn dot(&self, v: &Vector<f32>) -> f32 {
82 assert_eq!(v.size(), self.size(), "vectors must be the same size");
83
84 self * v
85 }
86}
87
88impl Mul<&Vector<f32>> for &Vector<f32> {
89 type Output = f32;
90
91 fn mul(self, rhs: &Vector<f32>) -> Self::Output {
92 self * &rhs._d
93 }
94}
95
96impl Mul<&Vec<f32>> for &Vector<f32> {
97 type Output = f32;
98
99 fn mul(self, rhs: &Vec<f32>) -> Self::Output {
100 self * &rhs[..]
101 }
102}
103
104impl Mul<&Vector<f32>> for &Vec<f32> {
105 type Output = f32;
106
107 fn mul(self, rhs: &Vector<f32>) -> Self::Output {
108 rhs * self
109 }
110}
111
112impl Mul<&[f32]> for &Vector<f32> {
113 type Output = f32;
114
115 fn mul(self, rhs: &[f32]) -> Self::Output {
116 let mut sum = f32::default();
117
118 for (a, &b) in self._d.iter().zip(rhs) {
119 sum = a.mul_add(b, sum);
120 }
121
122 sum
123 }
124}
125
126impl Mul<&Vector<f32>> for &[f32] {
127 type Output = f32;
128
129 fn mul(self, rhs: &Vector<f32>) -> Self::Output {
130 rhs * self
131 }
132}
133
134impl MulAdd<f32, Vector<f32>> for Vector<f32> {
135 fn mul_add(self, a: &f32, b: &Vector<f32>) -> Self {
136 assert!(self.size() == b.size(), "vectors must be the same size");
137
138 let mut vec = Vec::with_capacity(self.size());
139
140 for i in 0..self.size() {
141 vec.push(self[i].mul_add(*a, b[i]))
142 }
143
144 V!(vec)
145 }
146}
147
148impl Angle for Vector<f32> {
149 type Output = f32;
150 fn angle_cos(u: &Vector<f32>, v: &Vector<f32>) -> Self::Output {
151 u.dot(v) / (u.norm() * v.norm())
152 }
153}
154
155impl Transpose<f32> for Matrix<f32> {
156 fn transpose(&self) -> Matrix<f32> {
157 let mut vec = Vec::with_capacity(self.rows * self.cols);
158 for i in 0..self.cols {
159 for j in 0..self.rows {
160 vec.push(self[j][i]);
161 }
162 }
163
164 Matrix {
165 rows: self.cols,
166 cols: self.rows,
167 _d: vec,
168 }
169 }
170}
171
172impl Lerp for Vector<f32> {
173 fn lerp(u: Self, v: Self, t: f32) -> Self {
174 match t {
175 0. => u,
176 1. => v,
177 p => {
178 let mut vec = Vec::with_capacity(u.size());
179
180 for i in 0..u.size() {
181 vec.push((v[i] - u[i]).mul_add(p, u[i]))
182 }
183
184 V!(vec)
185 }
186 }
187 }
188}
189
190impl Lerp for Matrix<f32> {
191 fn lerp(u: Self, v: Self, t: f32) -> Self {
192 match t {
193 0. => u,
194 1. => v,
195 p => {
196 let mut vec = Vec::with_capacity(u._d.len());
197
198 for i in 0..u._d.len() {
199 vec.push((v._d[i] - u._d[i]).mul_add(p, u._d[i]))
200 }
201
202 Matrix {
203 _d: vec,
204 cols: u.cols,
205 rows: u.rows,
206 }
207 }
208 }
209 }
210}