forked from Kaynato/klein
-
Notifications
You must be signed in to change notification settings - Fork 0
/
klein.glsl
229 lines (191 loc) · 4.83 KB
/
klein.glsl
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#ifndef KLEIN_GUARD
#define KLEIN_GUARD
// p0 -> (e0, e1, e2, e3)
// p1 -> (1, e23, e31, e12)
// p2 -> (e0123, e01, e02, e03)
// p3 -> (e123, e032, e013, e021)
struct kln_plane
{
vec4 p0;
};
struct kln_line
{
vec4 p1;
vec4 p2;
};
// If integrating this library with other code, remember that the point layout
// here has the homogeneous component in p3[0] and not p3[3]. The swizzle to
// get the vec3 Cartesian representation is p3.yzw
struct kln_point
{
vec4 p3;
};
struct kln_rotor
{
vec4 p1;
};
struct kln_translator
{
vec4 p2;
};
struct kln_motor
{
vec4 p1;
vec4 p2;
};
kln_rotor kln_mul(in kln_rotor a, in kln_rotor b)
{
kln_rotor c;
c.p1 = a.p1.x * b.p1;
c.p1 -= a.p1.yzwy * b.p1.ywyz;
vec4 t = a.p1.zyzw * b.p1.zxxx;
t += a.p1.wwyz * b.p1.wzwy;
t.x = -t.x;
c.p1 += t;
return c;
}
kln_translator kln_mul(in kln_translator a, in kln_translator b)
{
// (1 + a.p2) * (1 + b.p2) = 1 + a.p2 + b.p2
kln_translator c;
c.p2 = a.p2 + b.p2;
return c;
}
kln_motor kln_mul(in kln_motor a, in kln_motor b)
{
kln_motor c;
vec4 a_zyzw = a.p1.zyzw;
vec4 a_ywyz = a.p1.ywyz;
vec4 a_wzwy = a.p1.wzwy;
vec4 c_wwyz = b.p1.wwyz;
vec4 c_yzwy = b.p1.yzwy;
c.p1 = a.p1.x * b.p1;
vec4 t = a_ywyz * c_yzwy;
t += a_zyzw * b.p1.zxxx;
t.x = -t.x;
c.p1 += t;
c.p1 -= a_wzwy * c_wwyz;
c.p2 = a.p1.x * b.p2;
c.p2 += a.p2 * b.p1.x;
c.p2 += a_ywyz * b.p2.yzwy;
c.p2 += a.p2.ywyz * c_yzwy;
t = a_zyzw * b.p2.zxxx;
t += a_wzwy * b.p2.wwyz;
t += a.p2.zxxx * b.p1.zyzw;
t += a.p2.wzwy * c_wwyz;
t.x = -t.x;
c.p2 -= t;
return c;
}
kln_plane kln_apply(in kln_rotor r, in kln_plane p)
{
vec4 dc_scale = vec4(1.0, 2.0, 2.0, 2.0);
vec4 neg_low = vec4(-1.0, 1.0, 1.0, 1.0);
vec4 t1 = r.p1.zxxx * r.p1.zwyz;
t1 += r.p1.yzwy * r.p1.yyzw;
t1 *= dc_scale;
vec4 t2 = r.p1 * r.p1.xwyz;
t2 -= (r.p1.wxxx * r.p1.wzwy) * neg_low;
t2 *= dc_scale;
vec4 t3 = r.p1 * r.p1;
t3 -= r.p1.xwyz * r.p1.xwyz;
t3 += r.p1.xxxx * r.p1.xxxx;
t3 -= r.p1.xzwy * r.p1.xzwy;
// TODO: provide variadic rotor-plane application
kln_plane q;
q.p0 = t1 * p.p0.xzwy;
q.p0 += t2 * p.p0.xwyz;
q.p0 += t3 * p.p0;
return q;
}
kln_plane kln_apply(in kln_motor m, in kln_plane p)
{
vec4 dc_scale = vec4(1.0, 2.0, 2.0, 2.0);
vec4 neg_low = vec4(-1.0, 1.0, 1.0, 1.0);
vec4 t1 = m.p1.zxxx * m.p1.zwyz;
t1 += m.p1.yzwy * m.p1.yyzw;
t1 *= dc_scale;
vec4 t2 = m.p1 * m.p1.xwyz;
t2 -= (m.p1.wxxx * m.p1.wzwy) * neg_low;
t2 *= dc_scale;
vec4 t3 = m.p1 * m.p1;
t3 -= m.p1.xwyz * m.p1.xwyz;
t3 += m.p1.xxxx * m.p1.xxxx;
t3 -= m.p1.xzwy * m.p1.xzwy;
vec4 t4 = m.p1.x * m.p2;
t4 += m.p1.xzwy * m.p2.xwyz;
t4 += m.p1 * m.p2.x;
t4 -= m.p1.xwyz * m.p2.xzwy;
t4 *= vec4(0.0, 2.0, 2.0, 2.0);
// TODO: provide variadic motor-plane application
kln_plane q;
q.p0 = t1 * p.p0.xzwy;
q.p0 += t2 * p.p0.xwyz;
q.p0 += t3 * p.p0;
q.p0 += vec4(dot(t4, p.p0), 0.0, 0.0, 0.0);
return q;
}
kln_point kln_apply(in kln_rotor r, in kln_point p)
{
vec4 scale = vec4(0, 2, 2, 2);
vec4 t1 = r.p1 * r.p1.xwyz;
t1 -= r.p1.x * r.p1.xzwy;
t1 *= scale;
vec4 t2 = r.p1.x * r.p1.xwyz;
t2 += r.p1.xzwy * r.p1;
t2 *= scale;
vec4 t3 = r.p1 * r.p1;
t3 += r.p1.yxxx * r.p1.yxxx;
vec4 t4 = r.p1.zwyz * r.p1.zwyz;
t4 += r.p1.wzwy * r.p1.wzwy;
t3 -= t4 * vec4(-1.0, 1.0, 1.0, 1.0);
// TODO: provide variadic rotor-point application
kln_point q;
q.p3 = t1 * p.p3.xwyz;
q.p3 += t2 * p.p3.xzwy;
q.p3 += t3 * p.p3;
return q;
}
kln_point kln_apply(in kln_motor m, in kln_point p)
{
vec4 scale = vec4(0, 2, 2, 2);
vec4 t1 = m.p1 * m.p1.xwyz;
t1 -= m.p1.x * m.p1.xzwy;
t1 *= scale;
vec4 t2 = m.p1.x * m.p1.xwyz;
t2 += m.p1.xzwy * m.p1;
t2 *= scale;
vec4 t3 = m.p1 * m.p1;
t3 += m.p1.yxxx * m.p1.yxxx;
vec4 t4 = m.p1.zwyz * m.p1.zwyz;
t4 += m.p1.wzwy * m.p1.wzwy;
t3 -= t4 * vec4(-1.0, 1.0, 1.0, 1.0);
t4 = m.p1.xzwy * m.p2.xwyz;
t4 -= m.p1.x * m.p2;
t4 -= m.p1.xwyz * m.p2.xzwy;
t4 -= m.p1 * m.p2.x;
t4 *= scale;
// TODO: provide variadic motor-point application
kln_point q;
q.p3 = t1 * p.p3.xwyz;
q.p3 += t2 * p.p3.xzwy;
q.p3 += t3 * p.p3;
q.p3 += t4 * p.p3.x;
return q;
}
// If no entity is provided as the second argument, the motor is
// applied to the origin.
// NOTE: The motor MUST be normalized for the result of this operation to be
// well defined.
kln_point kln_apply(in kln_motor m)
{
kln_point p;
p.p3 = m.p1 * m.p2.x;
p.p3 += m.p1.x * m.p2;
p.p3 += m.p1.xwyz * m.p2.xzwy;
p.p3 = m.p1.xzwy * m.p2.xwyz - p.p3;
p.p3 *= vec4(0.0, 2.0, 2.0, 2.0);
p.p3.x = 1.0;
return p;
}
#endif // KLEIN_GUARD