qm/m/
m3.rs

1//! matrix3
2//!
3
4use num::Float;
5
6use crate::v::{TVector, v3::Vector3};
7use crate::m::TMatrix;
8
9/// Matrix3
10pub type Matrix3<F> = [[F; 3]; 3];
11
12/// TMatrix for Matrix3
13impl<F: Float + std::fmt::Debug + std::iter::Sum> TMatrix<F> for Matrix3<F> {
14  /// constructor col major from v3 (move)
15  fn colmajor3(m: Vec<Vector3<F>>) -> Self where Self: Sized {
16    (0..3).into_iter().map(|i|
17      (0..3).into_iter().map(|j|
18        m[j][i]
19      ).collect::<Vec<_>>().try_into().unwrap()
20    ).collect::<Vec<_>>().try_into().unwrap()
21  }
22  /// constructor row major from v3 (move)
23  fn rowmajor3(m: Vec<Vector3<F>>) -> Self where Self: Sized {
24    (0..3).into_iter().map(|j|
25      m[j]
26    ).collect::<Vec<_>>().try_into().unwrap()
27  }
28  /// constructor col major
29  fn col_major(m: &Vec<Vec<F>>) -> Self {
30    (0..3).into_iter().map(|i|
31      (0..3).into_iter().map(|j|
32        m[j][i]
33      ).collect::<Vec<_>>().try_into().unwrap()
34    ).collect::<Vec<_>>().try_into().unwrap()
35  }
36  /// constructor row major
37  fn row_major(m: &Vec<Vec<F>>) -> Self {
38    (0..3).into_iter().map(|j|
39      (0..3).into_iter().map(|i|
40        m[j][i]
41      ).collect::<Vec<_>>().try_into().unwrap()
42    ).collect::<Vec<_>>().try_into().unwrap()
43  }
44  /// constructor row major
45  fn new(m: &Vec<Vec<F>>) -> Self {
46    Self::row_major(m)
47  }
48  /// constructor
49  fn identity() -> Self {
50    let o = <F>::from(0).unwrap();
51    let l = <F>::from(1).unwrap();
52    Self::new(&vec![
53      vec![l, o, o],
54      vec![o, l, o],
55      vec![o, o, l]])
56  }
57  /// check equal with precision
58  fn prec_eq(&self, e: F, m: &impl TMatrix<F>) -> bool {
59    let m = m.mev3();
60    for j in 0..3 {
61      for i in 0..3 {
62        if (self[j][i] - m[j][i]).abs() >= e { return false; }
63      }
64    }
65    true
66  }
67  /// like as slice v3
68  fn mev3(&self) -> &[Vector3<F>] {
69    self
70  }
71  /// m dot self
72  fn dot_m(&self, m: &impl TMatrix<F>) -> Self {
73    Matrix3::<F>::colmajor3((0..3).into_iter().map(|i| {
74      self.colv3(i).dot_mv(m) // m dot self.col
75    }).collect())
76  }
77  /// row to v3
78  fn rowv3(&self, j: usize) -> Vector3<F> {
79    self[j]
80//    Vector3::<F>::new(&(0..3).into_iter().map(|i| self[j][i]).collect())
81  }
82  /// col to v3
83  fn colv3(&self, i: usize) -> Vector3<F> {
84    Vector3::<F>::new(&(0..3).into_iter().map(|j| self[j][i]).collect())
85  }
86  /// to_vec
87  fn to_vec(&self) -> Vec<Vec<F>> {
88    self.iter().map(|r| r.to_vec()).collect::<Vec<_>>()
89  }
90}