1use num::Float;
5
6use crate::v::TVector;
7use crate::m::TMatrix;
8
9pub type Vector3<F> = [F; 3];
11
12impl<F: Float + std::fmt::Debug + std::iter::Sum> TVector<F> for Vector3<F> {
14 fn new(v: &Vec<F>) -> Self {
16 (0..3).into_iter().map(|i| v[i]).collect::<Vec<_>>().try_into().unwrap()
17 }
18 fn prec_eq(&self, e: F, v: &impl TVector<F>) -> bool {
20 for i in 0..3 {
21 if (self[i] - v.me()[i]).abs() >= e { return false; }
22 }
23 true
24 }
25 fn to_vec(&self) -> Vec<F> {
27 self.iter().map(|&f| f).collect::<Vec<_>>()
28 }
29 fn me(&self) -> &[F] {
31 self
32 }
33 fn dot(&self, a: &impl TVector<F>) -> F {
35 let a = a.me();
36 (0..3).into_iter().map(|i| a[i] * self[i]).sum::<F>()
37 }
38 fn dot_mv(&self, m: &impl TMatrix<F>) -> Self {
40 Self::new(&(0..3).into_iter().map(|j| self.dot(&m.rowv3(j))).collect())
41 }
42 fn cross(&self, b: &impl TVector<F>) -> Self {
44 let a = self.me();
45 let b = b.me();
46 Self::new(&vec![
47 a[1] * b[2] - a[2] * b[1],
48 a[2] * b[0] - a[0] * b[2],
49 a[0] * b[1] - a[1] * b[0]])
50 }
51}