1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::scalar::*;
use crate::vector::*;
use crate::matrix::*;
#[derive(Debug, Clone, Copy)]
pub enum BasisPlane {
YZ,
ZX,
XY,
}
impl BasisPlane {
pub fn to_id(&self) -> usize {
match self {
BasisPlane::YZ => 0,
BasisPlane::ZX => 1,
BasisPlane::XY => 2,
}
}
pub fn of_id(id: usize) -> Self {
match id {
0 => BasisPlane::YZ,
1 => BasisPlane::ZX,
2 => BasisPlane::XY,
_ => panic!("invalid id")
}
}
}
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct Basis<T: Scalar> {
x_axis : Vector3<T>,
y_axis : Vector3<T>,
z_axis : Vector3<T>,
center : Vector3<T>,
}
impl<T: Scalar> Basis<T> {
pub fn center(&self) -> &Vector3<T> {
&self.center
}
pub fn center_mut(&mut self) -> &mut Vector3<T> {
&mut self.center
}
pub fn to_mat4(&self) -> Matrix4<T> {
Matrix4::new(
self.x_axis.x, self.x_axis.y, self.x_axis.z, T::zero(),
self.y_axis.x, self.y_axis.y, self.y_axis.z, T::zero(),
self.z_axis.x, self.z_axis.y, self.z_axis.z, T::zero(),
self.center.x, self.center.y, self.center.z, T::one())
}
pub fn of_mat4(mat: &Matrix4<T>) -> Self {
let col0 = mat.col[0];
let col1 = mat.col[1];
let col2 = mat.col[2];
let col3 = mat.col[3];
Self {
center : col3.xyz(),
x_axis : col0.xyz(),
y_axis : col1.xyz(),
z_axis : col2.xyz(),
}
}
pub fn default() -> Self {
Self {
center : Vector3::new(T::zero(), T::zero(), T::zero()),
x_axis : Vector3::new(T::one() , T::zero(), T::zero()),
y_axis : Vector3::new(T::zero(), T::one() , T::zero()),
z_axis : Vector3::new(T::zero(), T::zero(), T::one() ),
}
}
pub fn default_with_center(center: &Vector3<T>) -> Self {
Self {
center : *center,
x_axis : Vector3::new(T::one() , T::zero(), T::zero()),
y_axis : Vector3::new(T::zero(), T::one() , T::zero()),
z_axis : Vector3::new(T::zero(), T::zero(), T::one() ),
}
}
pub fn plane_axis(&self, plane: BasisPlane) -> (&Vector3<T>, &Vector3<T>) {
match plane {
BasisPlane::YZ => (&self.y_axis, &self.z_axis),
BasisPlane::ZX => (&self.z_axis, &self.x_axis),
BasisPlane::XY => (&self.x_axis, &self.y_axis),
}
}
pub fn plane_axis_mut(&mut self, plane: BasisPlane) -> (&mut Vector3<T>, &mut Vector3<T>) {
match plane {
BasisPlane::YZ => (&mut self.y_axis, &mut self.z_axis),
BasisPlane::ZX => (&mut self.z_axis, &mut self.x_axis),
BasisPlane::XY => (&mut self.x_axis, &mut self.y_axis),
}
}
}