/
Quat.ts
325 lines (292 loc) 路 8.33 KB
/
Quat.ts
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* tslint:disable no-bitwise */
/* tslint:disable prefer-for-of */ // TODO
// We need a quaternion class. We use this to represent rotations,
// planes, and points.
const eps = 1e-9; // TODO: Deduplicate with `PuzzleGeometry`?
export function centermassface(face: Quat[]): Quat {
// calculate a center of a face by averaging points
let s = new Quat(0, 0, 0, 0);
for (let i = 0; i < face.length; i++) {
s = s.sum(face[i]);
}
return s.smul(1.0 / face.length);
}
export function solvethreeplanes(
p1: number,
p2: number,
p3: number,
planes: Quat[],
): any {
// find intersection of three planes but only if interior
// Takes three indices into a plane array, and returns the point at the
// intersection of all three, but only if it is internal to all planes.
const p = planes[p1].intersect3(planes[p2], planes[p3]);
if (!p) {
return p;
}
for (let i = 0; i < planes.length; i++) {
if (i !== p1 && i !== p2 && i !== p3) {
const dt = planes[i].b * p.b + planes[i].c * p.c + planes[i].d * p.d;
if (
(planes[i].a > 0 && dt > planes[i].a) ||
(planes[i].a < 0 && dt < planes[i].a)
) {
return false;
}
}
}
return p;
}
export class Quat {
constructor(
public a: number,
public b: number,
public c: number,
public d: number,
) {}
public mul(q: Quat): Quat {
// Quaternion multiplication
return new Quat(
this.a * q.a - this.b * q.b - this.c * q.c - this.d * q.d,
this.a * q.b + this.b * q.a + this.c * q.d - this.d * q.c,
this.a * q.c - this.b * q.d + this.c * q.a + this.d * q.b,
this.a * q.d + this.b * q.c - this.c * q.b + this.d * q.a,
);
}
public toString(): string {
return `Q[${this.a},${this.b},${this.c},${this.d}]`;
}
public dist(q: Quat): number {
// Euclidean distance
return Math.hypot(this.a - q.a, this.b - q.b, this.c - q.c, this.d - q.d);
}
public len(): number {
// Euclidean length
return Math.hypot(this.a, this.b, this.c, this.d);
}
public cross(q: Quat): Quat {
// cross product
return new Quat(
0,
this.c * q.d - this.d * q.c,
this.d * q.b - this.b * q.d,
this.b * q.c - this.c * q.b,
);
}
public dot(q: Quat): number {
// dot product of two quaternions
return this.b * q.b + this.c * q.c + this.d * q.d;
}
public normalize(): Quat {
// make the magnitude be 1
const d = Math.sqrt(this.dot(this));
return new Quat(this.a / d, this.b / d, this.c / d, this.d / d);
}
public makenormal(): Quat {
// make a normal vector from a plane or quat or point
return new Quat(0, this.b, this.c, this.d).normalize();
}
public normalizeplane(): Quat {
// normalize a plane
const d = Math.hypot(this.b, this.c, this.d);
return new Quat(this.a / d, this.b / d, this.c / d, this.d / d);
}
public smul(m: number): Quat {
// scalar multiplication
return new Quat(this.a * m, this.b * m, this.c * m, this.d * m);
}
public sum(q: Quat): Quat {
// quaternion sum
return new Quat(this.a + q.a, this.b + q.b, this.c + q.c, this.d + q.d);
}
public sub(q: Quat): Quat {
// difference
return new Quat(this.a - q.a, this.b - q.b, this.c - q.c, this.d - q.d);
}
public angle(): number {
// quaternion angle
return 2 * Math.acos(this.a);
}
public invrot(): Quat {
// quaternion inverse rotation
return new Quat(this.a, -this.b, -this.c, -this.d);
}
public det3x3(
a00: number,
a01: number,
a02: number,
a10: number,
a11: number,
a12: number,
a20: number,
a21: number,
a22: number,
): number {
// 3x3 determinant
return (
a00 * (a11 * a22 - a12 * a21) +
a01 * (a12 * a20 - a10 * a22) +
a02 * (a10 * a21 - a11 * a20)
);
}
public rotateplane(q: Quat): Quat {
// rotate a plane using a quaternion
const t = q.mul(new Quat(0, this.b, this.c, this.d)).mul(q.invrot());
t.a = this.a;
return t;
}
// return any vector orthogonal to the given one. Find the smallest
// component (in absolute value) and return the cross product of that
// axis with the given vector.
public orthogonal(): Quat {
const ab = Math.abs(this.b);
const ac = Math.abs(this.c);
const ad = Math.abs(this.d);
if (ab < ac && ab < ad) {
return this.cross(new Quat(0, 1, 0, 0)).normalize();
} else if (ac < ab && ac < ad) {
return this.cross(new Quat(0, 0, 1, 0)).normalize();
} else {
return this.cross(new Quat(0, 0, 0, 1)).normalize();
}
}
// return the Quaternion that will rotate the this vector
// to the b vector through rotatepoint.
public pointrotation(b: Quat): Quat {
const a = this.normalize();
b = b.normalize();
if (a.sub(b).len() < eps) {
return new Quat(1, 0, 0, 0);
}
let h = a.sum(b);
if (h.len() < eps) {
h = h.orthogonal();
} else {
h = h.normalize();
}
const r = a.cross(h);
r.a = a.dot(h);
return r;
}
// given two vectors, return the portion of the first that
// is not in the direction of the second.
public unproject(b: Quat): Quat {
return this.sum(b.smul(-this.dot(b) / (this.len() * b.len())));
}
public rotatepoint(q: Quat): Quat {
// rotate a point
return q.mul(this).mul(q.invrot());
}
public rotateface(face: Quat[]): Quat[] {
// rotate a face by this Q.
return face.map((_: Quat) => _.rotatepoint(this));
}
public intersect3(p2: Quat, p3: Quat): Quat | false {
// intersect three planes if there is one
const det = this.det3x3(
this.b,
this.c,
this.d,
p2.b,
p2.c,
p2.d,
p3.b,
p3.c,
p3.d,
);
if (Math.abs(det) < eps) {
return false; // TODO: Change to `null` or `undefined`?
}
return new Quat(
0,
this.det3x3(this.a, this.c, this.d, p2.a, p2.c, p2.d, p3.a, p3.c, p3.d) /
det,
this.det3x3(this.b, this.a, this.d, p2.b, p2.a, p2.d, p3.b, p3.a, p3.d) /
det,
this.det3x3(this.b, this.c, this.a, p2.b, p2.c, p2.a, p3.b, p3.c, p3.a) /
det,
);
}
public side(x: number): number {
// is this point close to the origin, or on one or the other side?
if (x > eps) {
return 1;
}
if (x < -eps) {
return -1;
}
return 0;
}
/**
* Cuts a face by this plane, or returns null if there
* is no intersection.
* @param face The face to cut.
*/
public cutface(face: Quat[]): Quat[][] | null {
const d = this.a;
let seen = 0;
let r = null;
for (let i = 0; i < face.length; i++) {
seen |= 1 << (this.side(face[i].dot(this) - d) + 1);
}
if ((seen & 5) === 5) {
r = [];
// saw both sides
const inout = face.map((_: Quat) => this.side(_.dot(this) - d));
for (let s = -1; s <= 1; s += 2) {
const nface = [];
for (let k = 0; k < face.length; k++) {
if (inout[k] === s || inout[k] === 0) {
nface.push(face[k]);
}
const kk = (k + 1) % face.length;
if (inout[k] + inout[kk] === 0 && inout[k] !== 0) {
const vk = face[k].dot(this) - d;
const vkk = face[kk].dot(this) - d;
const r = vk / (vk - vkk);
const pt = face[k].smul(1 - r).sum(face[kk].smul(r));
nface.push(pt);
}
}
r.push(nface);
}
}
return r;
}
public cutfaces(faces: Quat[][]): Quat[][] {
// Cut a set of faces by a plane and return new set
const nfaces = [];
for (let j = 0; j < faces.length; j++) {
const face = faces[j];
const t = this.cutface(face);
if (t) {
nfaces.push(t[0]);
nfaces.push(t[1]);
} else {
nfaces.push(face);
}
}
return nfaces;
}
public faceside(face: Quat[]): number {
// which side of a plane is a face on?
const d = this.a;
for (let i = 0; i < face.length; i++) {
const s = this.side(face[i].dot(this) - d);
if (s !== 0) {
return s;
}
}
throw new Error("Could not determine side of plane in faceside");
}
public sameplane(p: Quat): boolean {
// are two planes the same?
const a = this.normalize();
const b = p.normalize();
return a.dist(b) < eps || a.dist(b.smul(-1)) < eps;
}
public makecut(r: number): Quat {
// make a cut from a normal vector
return new Quat(r, this.b, this.c, this.d);
}
}