-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMatrix2x3Plugin.js
460 lines (392 loc) · 11.8 KB
/
Matrix2x3Plugin.js
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/* Smalltalk from Squeak4.5 with VMMaker 4.13.6 translated as JS source on 3 November 2014 1:52:21 pm */
/* Automatically generated by
JSPluginCodeGenerator VMMakerJS-bf.15 uuid: fd4e10f2-3773-4e80-8bb5-c4b471a014e5
from
Matrix2x3Plugin VMMaker-bf.353 uuid: 8ae25e7e-8d2c-451e-8277-598b30e9c002
*/
module("users.bert.SqueakJS.plugins.Matrix2x3Plugin").requires("users.bert.SqueakJS.vm").toRun(function() {
"use strict";
var VM_PROXY_MAJOR = 1;
var VM_PROXY_MINOR = 11;
/*** Functions ***/
function CLASSOF(obj) { return typeof obj === "number" ? interpreterProxy.classSmallInteger() : obj.sqClass }
function SIZEOF(obj) { return obj.pointers ? obj.pointers.length : obj.words ? obj.words.length : obj.bytes ? obj.bytes.length : 0 }
function BYTESIZEOF(obj) { return obj.bytes ? obj.bytes.length : obj.words ? obj.words.length * 4 : 0 }
function DIV(a, b) { return Math.floor(a / b) | 0; } // integer division
function MOD(a, b) { return a - DIV(a, b) * b | 0; } // signed modulus
function SHL(a, b) { return b > 31 ? 0 : a << b; } // fix JS shift
function SHR(a, b) { return b > 31 ? 0 : a >>> b; } // fix JS shift
function SHIFT(a, b) { return b < 0 ? (b < -31 ? 0 : a >>> (0-b) ) : (b > 31 ? 0 : a << b); }
/*** Variables ***/
var interpreterProxy = null;
var m23ArgX = 0;
var m23ArgY = 0;
var m23ResultX = 0;
var m23ResultY = 0;
var moduleName = "Matrix2x3Plugin 3 November 2014 (e)";
/* Note: This is hardcoded so it can be run from Squeak.
The module name is used for validating a module *after*
it is loaded to check if it does really contain the module
we're thinking it contains. This is important! */
function getModuleName() {
return moduleName;
}
function halt() {
;
}
/* Load the argument matrix */
function loadArgumentMatrix(matrix) {
if (interpreterProxy.failed()) {
return null;
}
if (!(interpreterProxy.isWords(matrix) && (SIZEOF(matrix) === 6))) {
interpreterProxy.primitiveFail();
return null;
}
return matrix.wordsAsFloat32Array();
}
/* Load the argument point into m23ArgX and m23ArgY */
function loadArgumentPoint(point) {
var isInt;
var oop;
if (interpreterProxy.failed()) {
return null;
}
if (CLASSOF(point) !== interpreterProxy.classPoint()) {
return interpreterProxy.primitiveFail();
}
oop = interpreterProxy.fetchPointerofObject(0, point);
isInt = typeof oop === "number";
if (!(isInt || (oop.isFloat))) {
return interpreterProxy.primitiveFail();
}
if (isInt) {
m23ArgX = oop;
} else {
m23ArgX = interpreterProxy.floatValueOf(oop);
}
oop = interpreterProxy.fetchPointerofObject(1, point);
isInt = typeof oop === "number";
if (!(isInt || (oop.isFloat))) {
return interpreterProxy.primitiveFail();
}
if (isInt) {
m23ArgY = oop;
} else {
m23ArgY = interpreterProxy.floatValueOf(oop);
}
}
/* Multiply matrix m1 with m2 and store the result into m3. */
function matrix2x3ComposeMatrixwithinto(m1, m2, m3) {
var a11;
var a12;
var a13;
var a21;
var a22;
var a23;
a11 = (m1[0] * m2[0]) + (m1[1] * m2[3]);
a12 = (m1[0] * m2[1]) + (m1[1] * m2[4]);
a13 = ((m1[0] * m2[2]) + (m1[1] * m2[5])) + m1[2];
a21 = (m1[3] * m2[0]) + (m1[4] * m2[3]);
a22 = (m1[3] * m2[1]) + (m1[4] * m2[4]);
a23 = ((m1[3] * m2[2]) + (m1[4] * m2[5])) + m1[5];
m3[0] = a11;
m3[1] = a12;
m3[2] = a13;
m3[3] = a21;
m3[4] = a22;
m3[5] = a23;
}
/* Invert the pre-loaded argument point by the given matrix */
function matrix2x3InvertPoint(m) {
var det;
var detX;
var detY;
var x;
var y;
x = m23ArgX - m[2];
y = m23ArgY - m[5];
det = (m[0] * m[4]) - (m[1] * m[3]);
if (det === 0.0) {
return interpreterProxy.primitiveFail();
}
det = 1.0 / det;
detX = (x * m[4]) - (m[1] * y);
detY = (m[0] * y) - (x * m[3]);
m23ResultX = detX * det;
m23ResultY = detY * det;
}
/* Transform the pre-loaded argument point by the given matrix */
function matrix2x3TransformPoint(m) {
m23ResultX = ((m23ArgX * m[0]) + (m23ArgY * m[1])) + m[2];
m23ResultY = ((m23ArgX * m[3]) + (m23ArgY * m[4])) + m[5];
}
function okayIntValue(value) {
return (value >= -1073741824) && (m23ResultX <= 1073741823);
}
function primitiveComposeMatrix() {
var m1;
var m2;
var m3;
var result;
;
m3 = loadArgumentMatrix((result = interpreterProxy.stackObjectValue(0)));
m2 = loadArgumentMatrix(interpreterProxy.stackObjectValue(1));
m1 = loadArgumentMatrix(interpreterProxy.stackObjectValue(2));
if (interpreterProxy.failed()) {
return null;
}
matrix2x3ComposeMatrixwithinto(m1, m2, m3);
interpreterProxy.popthenPush(3, result);
}
function primitiveInvertPoint() {
var matrix;
loadArgumentPoint(interpreterProxy.stackObjectValue(0));
matrix = loadArgumentMatrix(interpreterProxy.stackObjectValue(1));
if (interpreterProxy.failed()) {
return null;
}
matrix2x3InvertPoint(matrix);
if (!interpreterProxy.failed()) {
roundAndStoreResultPoint(2);
}
}
function primitiveInvertRectInto() {
var cornerX;
var cornerY;
var dstOop;
var matrix;
var maxX;
var maxY;
var minX;
var minY;
var originX;
var originY;
var srcOop;
dstOop = interpreterProxy.stackObjectValue(0);
srcOop = interpreterProxy.stackObjectValue(1);
matrix = loadArgumentMatrix(interpreterProxy.stackObjectValue(2));
if (interpreterProxy.failed()) {
return null;
}
if (CLASSOF(srcOop) !== CLASSOF(dstOop)) {
return interpreterProxy.primitiveFail();
}
if (!interpreterProxy.isPointers(srcOop)) {
return interpreterProxy.primitiveFail();
}
if (SIZEOF(srcOop) !== 2) {
return interpreterProxy.primitiveFail();
}
loadArgumentPoint(interpreterProxy.fetchPointerofObject(0, srcOop));
if (interpreterProxy.failed()) {
return null;
}
originX = m23ArgX;
originY = m23ArgY;
matrix2x3InvertPoint(matrix);
minX = (maxX = m23ResultX);
/* Load bottom-right point */
minY = (maxY = m23ResultY);
loadArgumentPoint(interpreterProxy.fetchPointerofObject(1, srcOop));
if (interpreterProxy.failed()) {
return null;
}
cornerX = m23ArgX;
cornerY = m23ArgY;
matrix2x3InvertPoint(matrix);
minX = Math.min(minX, m23ResultX);
maxX = Math.max(maxX, m23ResultX);
minY = Math.min(minY, m23ResultY);
/* Load top-right point */
maxY = Math.max(maxY, m23ResultY);
m23ArgX = cornerX;
m23ArgY = originY;
matrix2x3InvertPoint(matrix);
minX = Math.min(minX, m23ResultX);
maxX = Math.max(maxX, m23ResultX);
minY = Math.min(minY, m23ResultY);
/* Load bottom-left point */
maxY = Math.max(maxY, m23ResultY);
m23ArgX = originX;
m23ArgY = cornerY;
matrix2x3InvertPoint(matrix);
minX = Math.min(minX, m23ResultX);
maxX = Math.max(maxX, m23ResultX);
minY = Math.min(minY, m23ResultY);
maxY = Math.max(maxY, m23ResultY);
if (!interpreterProxy.failed()) {
dstOop = roundAndStoreResultRectx0y0x1y1(dstOop, minX, minY, maxX, maxY);
}
if (!interpreterProxy.failed()) {
interpreterProxy.popthenPush(3, dstOop);
}
}
function primitiveIsIdentity() {
var matrix;
matrix = loadArgumentMatrix(interpreterProxy.stackObjectValue(0));
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.pop(1);
interpreterProxy.pushBool((((((matrix[0] === 1.0) && (matrix[1] === 0.0)) && (matrix[2] === 0.0)) && (matrix[3] === 0.0)) && (matrix[4] === 1.0)) && (matrix[5] === 0.0));
}
function primitiveIsPureTranslation() {
var matrix;
matrix = loadArgumentMatrix(interpreterProxy.stackObjectValue(0));
if (interpreterProxy.failed()) {
return null;
}
interpreterProxy.pop(1);
interpreterProxy.pushBool((((matrix[0] === 1.0) && (matrix[1] === 0.0)) && (matrix[3] === 0.0)) && (matrix[4] === 1.0));
}
function primitiveTransformPoint() {
var matrix;
loadArgumentPoint(interpreterProxy.stackObjectValue(0));
matrix = loadArgumentMatrix(interpreterProxy.stackObjectValue(1));
if (interpreterProxy.failed()) {
return null;
}
matrix2x3TransformPoint(matrix);
roundAndStoreResultPoint(2);
}
function primitiveTransformRectInto() {
var cornerX;
var cornerY;
var dstOop;
var matrix;
var maxX;
var maxY;
var minX;
var minY;
var originX;
var originY;
var srcOop;
dstOop = interpreterProxy.stackObjectValue(0);
srcOop = interpreterProxy.stackObjectValue(1);
matrix = loadArgumentMatrix(interpreterProxy.stackObjectValue(2));
if (interpreterProxy.failed()) {
return null;
}
if (CLASSOF(srcOop) !== CLASSOF(dstOop)) {
return interpreterProxy.primitiveFail();
}
if (!interpreterProxy.isPointers(srcOop)) {
return interpreterProxy.primitiveFail();
}
if (SIZEOF(srcOop) !== 2) {
return interpreterProxy.primitiveFail();
}
loadArgumentPoint(interpreterProxy.fetchPointerofObject(0, srcOop));
if (interpreterProxy.failed()) {
return null;
}
originX = m23ArgX;
originY = m23ArgY;
matrix2x3TransformPoint(matrix);
minX = (maxX = m23ResultX);
/* Load bottom-right point */
minY = (maxY = m23ResultY);
loadArgumentPoint(interpreterProxy.fetchPointerofObject(1, srcOop));
if (interpreterProxy.failed()) {
return null;
}
cornerX = m23ArgX;
cornerY = m23ArgY;
matrix2x3TransformPoint(matrix);
minX = Math.min(minX, m23ResultX);
maxX = Math.max(maxX, m23ResultX);
minY = Math.min(minY, m23ResultY);
/* Load top-right point */
maxY = Math.max(maxY, m23ResultY);
m23ArgX = cornerX;
m23ArgY = originY;
matrix2x3TransformPoint(matrix);
minX = Math.min(minX, m23ResultX);
maxX = Math.max(maxX, m23ResultX);
minY = Math.min(minY, m23ResultY);
/* Load bottom-left point */
maxY = Math.max(maxY, m23ResultY);
m23ArgX = originX;
m23ArgY = cornerY;
matrix2x3TransformPoint(matrix);
minX = Math.min(minX, m23ResultX);
maxX = Math.max(maxX, m23ResultX);
minY = Math.min(minY, m23ResultY);
maxY = Math.max(maxY, m23ResultY);
dstOop = roundAndStoreResultRectx0y0x1y1(dstOop, minX, minY, maxX, maxY);
if (!interpreterProxy.failed()) {
interpreterProxy.popthenPush(3, dstOop);
}
}
/* Store the result of a previous operation.
Fail if we cannot represent the result as SmallInteger */
function roundAndStoreResultPoint(nItemsToPop) {
m23ResultX += 0.5;
m23ResultY += 0.5;
if (!okayIntValue(m23ResultX)) {
return interpreterProxy.primitiveFail();
}
if (!okayIntValue(m23ResultY)) {
return interpreterProxy.primitiveFail();
}
interpreterProxy.popthenPush(nItemsToPop, interpreterProxy.makePointwithxValueyValue((m23ResultX|0), (m23ResultY|0)));
}
/* Check, round and store the result of a rectangle operation */
function roundAndStoreResultRectx0y0x1y1(dstOop, x0, y0, x1, y1) {
var cornerOop;
var maxX;
var maxY;
var minX;
var minY;
var originOop;
var rectOop;
minX = x0 + 0.5;
if (!okayIntValue(minX)) {
return interpreterProxy.primitiveFail();
}
maxX = x1 + 0.5;
if (!okayIntValue(maxX)) {
return interpreterProxy.primitiveFail();
}
minY = y0 + 0.5;
if (!okayIntValue(minY)) {
return interpreterProxy.primitiveFail();
}
maxY = y1 + 0.5;
if (!okayIntValue(maxY)) {
return interpreterProxy.primitiveFail();
}
interpreterProxy.pushRemappableOop(dstOop);
originOop = interpreterProxy.makePointwithxValueyValue((minX|0), (minY|0));
interpreterProxy.pushRemappableOop(originOop);
cornerOop = interpreterProxy.makePointwithxValueyValue((maxX|0), (maxY|0));
originOop = interpreterProxy.popRemappableOop();
rectOop = interpreterProxy.popRemappableOop();
interpreterProxy.storePointerofObjectwithValue(0, rectOop, originOop);
interpreterProxy.storePointerofObjectwithValue(1, rectOop, cornerOop);
return rectOop;
}
/* Note: This is coded so that is can be run from Squeak. */
function setInterpreter(anInterpreter) {
var ok;
interpreterProxy = anInterpreter;
ok = interpreterProxy.majorVersion() == VM_PROXY_MAJOR;
if (ok === false) {
return false;
}
ok = interpreterProxy.minorVersion() >= VM_PROXY_MINOR;
return ok;
}
Squeak.registerExternalModule("Matrix2x3Plugin", {
primitiveInvertPoint: primitiveInvertPoint,
primitiveInvertRectInto: primitiveInvertRectInto,
primitiveIsIdentity: primitiveIsIdentity,
primitiveComposeMatrix: primitiveComposeMatrix,
setInterpreter: setInterpreter,
primitiveTransformRectInto: primitiveTransformRectInto,
primitiveIsPureTranslation: primitiveIsPureTranslation,
getModuleName: getModuleName,
primitiveTransformPoint: primitiveTransformPoint,
});
}); // end of module