Repository navigation
Expand file tree
/
Copy pathMatrix.ts
More file actions
402 lines (347 loc) · 10.8 KB
/
Copy pathMatrix.ts
File metadata and controls
402 lines (347 loc) · 10.8 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/**
* Matrix Polyfill for AS3.
* Author: Jim Chen
* Part of the CCLScripter
*/
module Display {
export class Point {
public x:number;
public y:number;
constructor(x:number = 0, y:number = 0) {
this.x = x;
this.y = y;
}
set length(l:number){
__trace("Point.length is read-only","err");
}
get length():number{
return Math.sqrt(this.x * this.x + this.y * this.y)
}
public add(p:Point):Point{
return new Point(p.x + this.x, p.y + this.y);
}
public subtract(p:Point):Point{
return new Point(this.x - p.x, this.y - p.y);
}
public static interpolate(a:Point, b:Point, f:number):Point{
return new Point((b.x - a.x) * f + a.x, (b.y - a.y) * f + a.y);
}
public offset(dx:number, dy:number):void{
this.x+= dx;
this.y+= dy;
}
public normalize(thickness:number):void{
var ratio:number = thickness/this.length;
this.x *= ratio;
this.y *= ratio;
}
public static polar(r:number, theta:number):Point{
return new Point(r * Math.cos(theta), r * Math.sin(theta));
}
public setTo(x:number, y:number):void{
this.x = x;
this.y = y;
}
public equals(p:Point):boolean{
if(p.x === this.x && p.y === this.y)
return true;
return false;
}
public toString():string{
return "(x=" + this.x + ", y=" + this.y + ")";
}
public clone():Point{
return new Point(this.x, this.y);
}
}
export class Matrix implements Display.ISerializable {
private _data:Array<number>;
constructor(a:number = 1, b:number = 0, c:number = 0, d:number = 1, tx:number = 0, ty:number = 0) {
this._data = [a, c, tx, b, d, ty, 0, 0, 1];
}
private dotProduct(o:Array<number>):Array<number> {
if (o.length < 9) {
throw new Error("Matrix dot product expects a matrix");
}
var res:Array<number> = [0, 0, 0, 0, 0, 0, 0, 0, 0];
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
for (var k = 0; k < 3; k++) {
res[i * 3 + j] += this._data[i * 3 + k] * o[k * 3 + j];
}
}
}
return res;
}
public setTo(a:number = 1, b:number = 0, c:number = 0, d:number = 1, tx:number = 0, ty:number = 0):void {
this._data = [a, c, tx, b, d, ty, 0, 0, 1];
}
public translate(tX:number, tY:number):void {
this._data[2] += tX;
this._data[5] += tY;
}
public rotate(q:number):void {
this._data = this.dotProduct([
Math.cos(q), -Math.sin(q), 0,
Math.sin(q), Math.cos(q), 0,
0, 0, 1
]);
}
public scale(sx:number, sy:number):void {
this._data = this.dotProduct([
sx, 0, 0,
0, sy, 0,
0, 0, 1
]);
}
public identity():void {
this.setTo(1, 0, 0, 1, 0, 0);
}
public createGradientBox(width:number, height:number, rotation:number, tX:number, tY:number):void {
this.createBox(width, height, rotation, tX, tY);
}
public createBox(sX:number, sY:number, q:number, tX:number, tY:number):void {
this.identity();
this.rotate(q);
this.scale(sX, sY);
this.translate(tX, tY);
}
public clone():Matrix {
var a:number = this._data[0],
b:number = this._data[3],
c:number = this._data[1],
d:number = this._data[4],
tx:number = this._data[2],
ty:number = this._data[5];
return new Matrix(a, b, c, d, tx, ty);
}
public serialize():Object {
return this._data;
}
}
export class Matrix3D implements Display.ISerializable {
private _data:Array<number>;
constructor(iv:Array<number> = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]) {
if (iv.length === 16) {
this._data = iv;
} else if (iv.length === 0){
this.identity();
}else {
__trace("Matrix3D initialization vector invalid", "warn");
this.identity();
}
}
private dotProduct(a:Array<number>, b:Array<number>):Array<number> {
if (a.length !== 16 || b.length !== 16) {
throw new Error("Matrix3D dot product expects a matrix3d");
}
var res:Array<number> = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
for (var k = 0; k < 4; k++) {
res[i * 4 + j] += a[i * 4 + k] * b[k * 4 + j];
}
}
}
return res;
}
private rotationMatrix(angle:number, axis:Vector3D):Array<number> {
var sT:number = Math.sin(angle), cT:number = Math.cos(angle);
return [
cT + axis.x * axis.x * (1 - cT), axis.x * axis.y * (1 - cT) - axis.z * sT, axis.x * axis.z * (1 - cT) + axis.y * sT, 0,
axis.x * axis.y * (1 - cT) + axis.z * sT, cT + axis.y * axis.y * (1 - cT), axis.y * axis.z * (1 - cT) - axis.x * sT, 0,
axis.z * axis.x * (1 - cT) - axis.y * sT, axis.z * axis.y * (1 - cT) + axis.x * sT, cT + axis.z * axis.z * (1 - cT), 0,
0, 0, 0, 1
];
}
public identity():void {
this._data = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
}
public append(lhs:Matrix3D):void {
this._data = this.dotProduct(lhs._data, this._data);
}
public appendRotation(degrees:number, axis:Vector3D, pivotPoint:Vector3D = null):void {
if (pivotPoint !== null) {
this.appendTranslation(pivotPoint.x, pivotPoint.y, pivotPoint.z);
}
this._data = this.dotProduct(this.rotationMatrix(degrees * Math.PI / 180, axis), this._data);
if (pivotPoint !== null) {
this.appendTranslation(-pivotPoint.x, -pivotPoint.y, -pivotPoint.z);
}
}
public appendTranslation(x:number, y:number, z:number):void {
this._data = this.dotProduct([
1, 0, 0, x,
0, 1, 0, y,
0, 0, 1, z,
0, 0, 0, 1
],this._data);
}
public appendScale(sX:number = 1, sY:number = 1, sZ:number = 1):void{
this._data = this.dotProduct([
sX, 0, 0, 0,
0, sY, 0, 0,
0, 0, sZ, 0,
0, 0, 0, 1
],this._data);
}
public prepend(rhs:Matrix3D):void {
this._data = this.dotProduct(this._data, rhs._data);
}
public prependRotation(degrees:number, axis:Vector3D, pivotPoint:Vector3D = null):void {
if (pivotPoint !== null) {
this.prependTranslation(pivotPoint.x, pivotPoint.y, pivotPoint.z);
}
this._data = this.dotProduct(this._data, this.rotationMatrix(degrees * Math.PI / 180, axis));
if (pivotPoint !== null) {
this.prependTranslation(-pivotPoint.x, -pivotPoint.y, -pivotPoint.z);
}
}
public prependTranslation(x:number, y:number, z:number):void {
this._data = this.dotProduct(this._data, [
1, 0, 0, x,
0, 1, 0, y,
0, 0, 1, z,
0, 0, 0, 1
]);
}
public prependScale(sX:number, sY:number, sZ:number):void{
this._data = this.dotProduct(this._data, [
sX, 0, 0, 0,
0, sY, 0, 0,
0, 0, sZ, 0,
0, 0, 0, 1
]);
}
public transformVector(v:Vector3D):Vector3D {
var rx = this._data[0] * v.x + this._data[1] * v.y + this._data[2] * v.z + this._data[3] * v.w;
var ry = this._data[4] * v.x + this._data[5] * v.y + this._data[6] * v.z + this._data[7] * v.w;
var rz = this._data[8] * v.x + this._data[9] * v.y + this._data[10] * v.z + this._data[11] * v.w;
var rw = this._data[12] * v.x + this._data[13] * v.y + this._data[14] * v.z + this._data[15] * v.w;
return new Vector3D(rx, ry, rz, rw);
}
/**
* Given an array of numbers representing vectors, postMultiply them to the current matrix.
* @param vin - input (x,y,z)
* @param vout - output (x,y,z)
*/
public transformVectors(vin:Array<number>, vout:Array<number>):void {
if (vin.length % 3 !== 0) {
__trace("Matrix3D.transformVectors expects input size to be multiple of 3.", "err");
return;
}
for (var i = 0; i < vin.length / 3; i++) {
var x = vin[i * 3], y = vin[i * 3 + 1], z = vin[i * 3 + 2];
var rx = this._data[0] * x + this._data[1] * y + this._data[2] * z;
var ry = this._data[4] * x + this._data[5] * y + this._data[6] * z;
var rz = this._data[8] * x + this._data[9] * y + this._data[10] * z;
vout.push(rx, ry, rz);
}
}
public transpose():void {
this._data = [
this._data[0], this._data[4], this._data[8], this._data[12],
this._data[1], this._data[5], this._data[9], this._data[13],
this._data[2], this._data[6], this._data[10], this._data[14],
this._data[3], this._data[7], this._data[11], this._data[15]
];
}
public clone():Matrix3D {
return new Matrix3D(this._data);
}
public serialize():Object {
return this._data;
}
}
export class Vector3D {
public static X_AXIS:Vector3D = new Vector3D(1, 0, 0);
public static Y_AXIS:Vector3D = new Vector3D(0, 1, 0);
public static Z_AXIS:Vector3D = new Vector3D(0, 0, 1);
public x:number;
public y:number;
public z:number;
public w:number;
constructor(x:number = 0, y:number = 0, z:number = 0, w:number = 0) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public toString():string {
return "(x=" + this.x + ", y=" + this.y + ", z=" + this.z + ", w=" + this.w + ")";
}
}
export function createMatrix(a:number,b:number,c:number,d:number,tx:number,ty:number):any {
return new Matrix(a,b,c,d,tx,ty);
}
export function createMatrix3D(iv:Array<number>):any {
return new Matrix3D(iv);
}
export function createColorTransform():any {
return null;
}
export function createGradientBox(width:number, height:number, rotation:number, tX:number, tY:number):any {
var m:Matrix = new Matrix();
m.createGradientBox(width, height, rotation, tX, tY);
return m;
}
export function createVector3D(x:number = 0, y:number = 0, z:number = 0, w:number = 0):any {
return new Vector3D(x, y, z, w);
}
export function projectVector(matrix:Matrix3D, vector:Vector3D):any {
return matrix.transformVector(vector);
}
export function projectVectors(matrix:Matrix3D, verts:Array<number>, projectedVerts:Array<number>, uvts:Array<number>):void {
/** Clear projected Verts **/
while (projectedVerts.length > 0) {
projectedVerts.pop();
}
if (verts.length % 3 !== 0) {
__trace("Display.projectVectors input vertex Vector must be a multiple of 3.", "err");
return;
}
var transformed:Array<number> = [];
matrix.transformVectors(verts, transformed);
for (var i = 0; i < transformed.length / 3; i++) {
var x = transformed[i * 3], y = transformed[i * 3 + 1];
projectedVerts.push(x, y);
}
}
export function createPoint(x:number = 0, y:number = 0):any {
return new Point(x,y);
}
/**
* Transforms a JS Array into an AS3 Vector<int>.
* Nothing is actually done since the methods are very
* similar across both.
* @param array - Array
* @returns {Array<number>} - AS3 Integer Vector
*/
export function toIntVector(array:Array<number>):Array<number> {
Object.defineProperty(array, 'as3Type', {
get: function () {
return "Vector<int>";
},
set: function (value) {
}
});
return array;
}
/**
* Transforms a JS Array into an AS3 Vector<number>.
* Nothing is actually done since the methods are very
* similar across both.
* @param array - Array
* @returns {Array<number>} - AS3 Number Vector
*/
export function toNumberVector(array:Array<number>):Array<number> {
Object.defineProperty(array, 'as3Type', {
get: function () {
return "Vector<number>";
},
set: function (value) {
}
});
return array;
}
}