forked from Matway/mpl-sl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quaternion.mpl
executable file
·175 lines (151 loc) · 3.12 KB
/
Quaternion.mpl
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
"Quaternion" module
"control" useModule
Quaternion: [{
QUATERNION: ();
entries: 4 array;
}];
quaternion: [{
QUATERNION: ();
entries: copy;
}];
identityQuaternion: [
entry:;
(0 entry cast 0 entry cast 0 entry cast 1 entry cast) quaternion
];
fieldCount: ["QUATERNION" has] [.@entries fieldCount] pfunc;
@: ["QUATERNION" has] [.@entries @] pfunc;
!: ["QUATERNION" has] [.@entries !] pfunc;
+: [q0:q1:;; q0 "QUATERNION" has q1 "QUATERNION" has and] [
q0:q1:;;
q0.entries q1.entries + quaternion
] pfunc;
*: [value:q:;; q "QUATERNION" has value 0 q @ same and] [
value:q:;;
value q.entries * quaternion
] pfunc;
*: [q:value:;; q "QUATERNION" has value 0 q @ same and] [
q:value:;;
q.entries value * quaternion
] pfunc;
*: [q0:q1:;; q0 "QUATERNION" has q1 "QUATERNION" has and] [
q0:q1:;;
(
0 q0 @ 3 q1 @ * 3 q0 @ 0 q1 @ * + 2 q0 @ 1 q1 @ * + 1 q0 @ 2 q1 @ * -
1 q0 @ 3 q1 @ * 3 q0 @ 1 q1 @ * + 0 q0 @ 2 q1 @ * + 2 q0 @ 0 q1 @ * -
2 q0 @ 3 q1 @ * 3 q0 @ 2 q1 @ * + 1 q0 @ 0 q1 @ * + 0 q0 @ 1 q1 @ * -
3 q0 @ 3 q1 @ * 0 q0 @ 0 q1 @ * - 1 q0 @ 1 q1 @ * - 2 q0 @ 2 q1 @ * -
) quaternion
] pfunc;
squaredLength: ["QUATERNION" has] [
q:;
q q dot
] pfunc;
dot: [
q0: q1:;;
q0 "QUATERNION" has
q1 "QUATERNION" has and
] [
q0: q1:;;
0 q0 @ 0 q1 @ *
1 q0 @ 1 q1 @ * +
2 q0 @ 2 q1 @ * +
3 q0 @ 3 q1 @ * +
] pfunc;
unit: ["QUATERNION" has] [
q:;
length: q squaredLength sqrt;
one: 1 0 q @ cast;
q one length / *
] pfunc;
unitCheckedWithThresold: [
thresold: copy;
q:;
squaredLength: q squaredLength;
squaredLength thresold thresold * < [
thresold identityQuaternion
] [
one: 1 thresold cast;
q one squaredLength sqrt / *
] if
];
unitChecked: [
q:;
q 1.0e-6 0 q @ cast unitCheckedWithThresold
];
nlerp: [
q0: q1: f:; copy;;
q0 q1 dot 0 0 q0 @ cast < [q1.entries neg @q1.!entries] when
q0 q1 f lerp unitChecked
];
conj: ["QUATERNION" has] [
q:;
(
0 q @ copy
1 q @ copy
2 q @ copy
3 q @ neg
) quaternion
] pfunc;
slerpWithEpsilon: [
q0:q1:o:epsilon:;;;;
q2: q1 copy;
c: q0 q2 dot;
c 0 o cast < [
(
0 q2 @ neg
1 q2 @ neg
2 q2 @ neg
3 q2 @ neg
) quaternion !q2
c neg !c
] when
c 1 o cast epsilon - > not [
a: c cos;
sr: 1 o cast a sin /;
a2: a o *;
k0: a a2 - sin sr *;
k2: a2 sin sr *;
q0 k0 * q2 k2 * + !q2
] when
q2
];
slerp: [
o:;
o 1.0e-6 o cast slerpWithEpsilon
];
vector: ["QUATERNION" has] [
.entries copy
] pfunc;
matrix: ["QUATERNION" has] [
q:;
one: 1 0 q @ cast;
x2: 0 q @ 0 q @ +;
y2: 1 q @ 1 q @ +;
z2: 2 q @ 2 q @ +;
omxx2: one x2 0 q @ * -;
xy2: x2 1 q @ *;
xz2: x2 2 q @ *;
xw2: x2 3 q @ *;
yy2: y2 1 q @ *;
yz2: y2 2 q @ *;
yw2: y2 3 q @ *;
zz2: z2 2 q @ *;
zw2: z2 3 q @ *;
(
(one yy2 - zz2 - xy2 zw2 + xz2 yw2 -)
(xy2 zw2 - omxx2 zz2 - yz2 xw2 +)
(xz2 yw2 + yz2 xw2 - omxx2 yy2 -)
)
] pfunc;
axisAngleQuaternion: [
axisAngle:;
third: 3 axisAngle @;
ah: third 0.5 third cast *;
ahs: ah sin;
(
0 axisAngle @ ahs *
1 axisAngle @ ahs *
2 axisAngle @ ahs *
ah cos
) quaternion
];