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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use crate::layout::InstanceModel;
#[derive(Clone, Copy, Default)]
pub struct Position(pub [f32; 3]);
impl From<[f32; 3]> for Position {
fn from(v: [f32; 3]) -> Self {
Self(v)
}
}
#[derive(Clone, Copy)]
pub struct Transform<Q = Identity> {
pub pos: [f32; 3],
pub rot: Q,
pub scl: [f32; 3],
}
impl<Q> Transform<Q>
where
Q: IntoQuat,
{
pub(crate) fn into_model(self) -> InstanceModel {
let Self { pos, rot, scl } = self;
let transform = Transform {
pos,
rot: rot.into_quat(),
scl,
};
transform.into_model_quat()
}
}
impl Transform<Quat> {
fn into_model_quat(self) -> InstanceModel {
fn rotation(Quat([x, y, z, w]): Quat) -> [[f32; 3]; 3] {
let x2 = x + x;
let y2 = y + y;
let z2 = z + z;
let xx = x * x2;
let xy = x * y2;
let xz = x * z2;
let yy = y * y2;
let yz = y * z2;
let zz = z * z2;
let wx = w * x2;
let wy = w * y2;
let wz = w * z2;
[
[1. - yy - zz, xy + wz, xz - wy],
[xy - wz, 1. - xx - zz, yz + wx],
[xz + wy, yz - wx, 1. - xx - yy],
]
}
let [xt, yt, zt] = self.pos;
let [x_axis, y_axis, z_axis] = rotation(self.rot);
let [xs, ys, zs] = self.scl;
let [xx, xy, xz] = x_axis.map(|v| v * xs);
let [yx, yy, yz] = y_axis.map(|v| v * ys);
let [zx, zy, zz] = z_axis.map(|v| v * zs);
InstanceModel {
mat: [
[xx, xy, xz, 0.],
[yx, yy, yz, 0.],
[zx, zy, zz, 0.],
[xt, yt, zt, 1.],
],
}
}
}
impl Default for Transform<Identity> {
fn default() -> Self {
Self {
pos: [0., 0., 0.],
rot: Identity,
scl: [1., 1., 1.],
}
}
}
pub trait IntoTransform {
type IntoQuat;
fn into_transform(self) -> Transform<Self::IntoQuat>;
}
impl IntoTransform for Position {
type IntoQuat = Identity;
fn into_transform(self) -> Transform<Self::IntoQuat> {
Transform {
pos: self.0,
..Default::default()
}
}
}
impl<Q> IntoTransform for Transform<Q> {
type IntoQuat = Q;
fn into_transform(self) -> Transform<Self::IntoQuat> {
self
}
}
pub trait IntoQuat {
fn into_quat(self) -> Quat;
}
#[derive(Default)]
pub struct Identity;
impl IntoQuat for Identity {
fn into_quat(self) -> Quat {
Quat::default()
}
}
pub struct Quat(pub [f32; 4]);
impl Default for Quat {
fn default() -> Self {
Self([0., 0., 0., 1.])
}
}
impl IntoQuat for Quat {
fn into_quat(self) -> Self {
self
}
}
pub struct AxisAngle(pub [f32; 3], pub f32);
impl IntoQuat for AxisAngle {
fn into_quat(self) -> Quat {
let Self(axis, angle) = self;
let (sin, cos) = (angle * 0.5).sin_cos();
let [x, y, z] = axis.map(|v| v * sin);
Quat([x, y, z, cos])
}
}
#[derive(Default)]
pub struct ReverseRotation<Q>(pub Q);
impl<Q> IntoQuat for ReverseRotation<Q>
where
Q: IntoQuat,
{
fn into_quat(self) -> Quat {
let Quat([x, y, z, w]) = self.0.into_quat();
Quat([-x, -y, -z, w])
}
}