qm/v/
v3.rs

1//! vector3
2//!
3
4use num::Float;
5
6use crate::v::TVector;
7use crate::m::TMatrix;
8
9/// Vector3
10pub type Vector3<F> = [F; 3];
11
12/// TVector for Vector3
13impl<F: Float + std::fmt::Debug + std::iter::Sum> TVector<F> for Vector3<F> {
14  /// constructor
15  fn new(v: &Vec<F>) -> Self {
16    (0..3).into_iter().map(|i| v[i]).collect::<Vec<_>>().try_into().unwrap()
17  }
18  /// check equal with precision
19  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  /// to_vec
26  fn to_vec(&self) -> Vec<F> {
27    self.iter().map(|&f| f).collect::<Vec<_>>()
28  }
29  /// like as slice
30  fn me(&self) -> &[F] {
31    self
32  }
33  /// a dot self
34  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  /// m dot self
39  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  /// self cross b
43  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}