-
Notifications
You must be signed in to change notification settings - Fork 1
/
maths.js
686 lines (508 loc) · 14.3 KB
/
maths.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//////////////////////////////////////////////////////////////////////////////
//
// Mathematical functions
//
// Ref. Original code from the Angel / Shreiner examples
//
// Additional functions by J. Madeira - Sep./Oct. 2015
//
//////////////////////////////////////////////////////////////////////////////
//----------------------------------------------------------------------------
//
// Helper functions
//
function _argumentsToArray( args )
{
return [].concat.apply( [], Array.prototype.slice.apply(args) );
}
//----------------------------------------------------------------------------
function radians( degrees ) {
return degrees * Math.PI / 180.0;
}
//----------------------------------------------------------------------------
//
// Vector Constructors
//
function vec2()
{
var result = _argumentsToArray( arguments );
switch ( result.length ) {
case 0: result.push( 0.0 );
case 1: result.push( 0.0 );
}
return result.splice( 0, 2 );
}
function vec3()
{
var result = _argumentsToArray( arguments );
switch ( result.length ) {
case 0: result.push( 0.0 );
case 1: result.push( 0.0 );
case 2: result.push( 0.0 );
}
return result.splice( 0, 3 );
}
function vec4()
{
var result = _argumentsToArray( arguments );
switch ( result.length ) {
case 0: result.push( 0.0 );
case 1: result.push( 0.0 );
case 2: result.push( 0.0 );
case 3: result.push( 1.0 );
}
return result.splice( 0, 4 );
}
//----------------------------------------------------------------------------
//
// Matrix Constructors
//
function mat2()
{
var v = _argumentsToArray( arguments );
var m = [];
switch ( v.length ) {
case 0:
v[0] = 1;
case 1:
m = [
vec2( v[0], 0.0 ),
vec2( 0.0, v[0] )
];
break;
default:
m.push( vec2(v) ); v.splice( 0, 2 );
m.push( vec2(v) );
break;
}
m.matrix = true;
return m;
}
//----------------------------------------------------------------------------
function mat3()
{
var v = _argumentsToArray( arguments );
var m = [];
switch ( v.length ) {
case 0:
v[0] = 1;
case 1:
m = [
vec3( v[0], 0.0, 0.0 ),
vec3( 0.0, v[0], 0.0 ),
vec3( 0.0, 0.0, v[0] )
];
break;
default:
m.push( vec3(v) ); v.splice( 0, 3 );
m.push( vec3(v) ); v.splice( 0, 3 );
m.push( vec3(v) );
break;
}
m.matrix = true;
return m;
}
//----------------------------------------------------------------------------
function mat4()
{
var v = _argumentsToArray( arguments );
var m = [];
switch ( v.length ) {
case 0:
v[0] = 1;
case 1:
m = [
vec4( v[0], 0.0, 0.0, 0.0 ),
vec4( 0.0, v[0], 0.0, 0.0 ),
vec4( 0.0, 0.0, v[0], 0.0 ),
vec4( 0.0, 0.0, 0.0, v[0] )
];
break;
default:
m.push( vec4(v) ); v.splice( 0, 4 );
m.push( vec4(v) ); v.splice( 0, 4 );
m.push( vec4(v) ); v.splice( 0, 4 );
m.push( vec4(v) );
break;
}
m.matrix = true;
return m;
}
//----------------------------------------------------------------------------
//
// Generic Mathematical Operations for Vectors and Matrices
//
function equal( u, v )
{
if ( u.length != v.length ) { return false; }
if ( u.matrix && v.matrix ) {
for ( var i = 0; i < u.length; ++i ) {
if ( u[i].length != v[i].length ) { return false; }
for ( var j = 0; j < u[i].length; ++j ) {
if ( u[i][j] !== v[i][j] ) { return false; }
}
}
}
else if ( u.matrix && !v.matrix || !u.matrix && v.matrix ) {
return false;
}
else {
for ( var i = 0; i < u.length; ++i ) {
if ( u[i] !== v[i] ) { return false; }
}
}
return true;
}
//----------------------------------------------------------------------------
function add( u, v )
{
var result = [];
if ( u.matrix && v.matrix ) {
if ( u.length != v.length ) {
throw "add(): trying to add matrices of different dimensions";
}
for ( var i = 0; i < u.length; ++i ) {
if ( u[i].length != v[i].length ) {
throw "add(): trying to add matrices of different dimensions";
}
result.push( [] );
for ( var j = 0; j < u[i].length; ++j ) {
result[i].push( u[i][j] + v[i][j] );
}
}
result.matrix = true;
return result;
}
else if ( u.matrix && !v.matrix || !u.matrix && v.matrix ) {
throw "add(): trying to add matrix and non-matrix variables";
}
else {
if ( u.length != v.length ) {
throw "add(): vectors are not the same dimension";
}
for ( var i = 0; i < u.length; ++i ) {
result.push( u[i] + v[i] );
}
return result;
}
}
//----------------------------------------------------------------------------
function subtract( u, v )
{
var result = [];
if ( u.matrix && v.matrix ) {
if ( u.length != v.length ) {
throw "subtract(): trying to subtract matrices" +
" of different dimensions";
}
for ( var i = 0; i < u.length; ++i ) {
if ( u[i].length != v[i].length ) {
throw "subtract(): trying to subtact matrices" +
" of different dimensions";
}
result.push( [] );
for ( var j = 0; j < u[i].length; ++j ) {
result[i].push( u[i][j] - v[i][j] );
}
}
result.matrix = true;
return result;
}
else if ( u.matrix && !v.matrix || !u.matrix && v.matrix ) {
throw "subtact(): trying to subtact matrix and non-matrix variables";
}
else {
if ( u.length != v.length ) {
throw "subtract(): vectors are not the same length";
}
for ( var i = 0; i < u.length; ++i ) {
result.push( u[i] - v[i] );
}
return result;
}
}
//----------------------------------------------------------------------------
function mult( u, v )
{
var result = [];
if ( u.matrix && v.matrix ) {
if ( u.length != v.length ) {
throw "mult(): trying to add matrices of different dimensions";
}
for ( var i = 0; i < u.length; ++i ) {
if ( u[i].length != v[i].length ) {
throw "mult(): trying to add matrices of different dimensions";
}
}
for ( var i = 0; i < u.length; ++i ) {
result.push( [] );
for ( var j = 0; j < v.length; ++j ) {
var sum = 0.0;
for ( var k = 0; k < u.length; ++k ) {
sum += u[i][k] * v[k][j];
}
result[i].push( sum );
}
}
result.matrix = true;
return result;
}
else {
if ( u.length != v.length ) {
throw "mult(): vectors are not the same dimension";
}
for ( var i = 0; i < u.length; ++i ) {
result.push( u[i] * v[i] );
}
return result;
}
}
//----------------------------------------------------------------------------
//
// Matrix Functions
//
function transpose( m )
{
if ( !m.matrix ) {
return "transpose(): trying to transpose a non-matrix";
}
var result = [];
for ( var i = 0; i < m.length; ++i ) {
result.push( [] );
for ( var j = 0; j < m[i].length; ++j ) {
result[i].push( m[j][i] );
}
}
result.matrix = true;
return result;
}
//----------------------------------------------------------------------------
//
// Helper function: Column-major 1D representation
//
function flatten( v )
{
if ( v.matrix === true ) {
v = transpose( v );
}
var n = v.length;
var elemsAreArrays = false;
if ( Array.isArray(v[0]) ) {
elemsAreArrays = true;
n *= v[0].length;
}
var floats = new Float32Array( n );
if ( elemsAreArrays ) {
var idx = 0;
for ( var i = 0; i < v.length; ++i ) {
for ( var j = 0; j < v[i].length; ++j ) {
floats[idx++] = v[i][j];
}
}
}
else {
for ( var i = 0; i < v.length; ++i ) {
floats[i] = v[i];
}
}
return floats;
}
//----------------------------------------------------------------------------
//
// To get the number of bytes
//
var sizeof = {
'vec2' : new Float32Array( flatten(vec2()) ).byteLength,
'vec3' : new Float32Array( flatten(vec3()) ).byteLength,
'vec4' : new Float32Array( flatten(vec4()) ).byteLength,
'mat2' : new Float32Array( flatten(mat2()) ).byteLength,
'mat3' : new Float32Array( flatten(mat3()) ).byteLength,
'mat4' : new Float32Array( flatten(mat4()) ).byteLength
};
//----------------------------------------------------------------------------
//
// Constructing the 4 x 4 transformation matrices - J. Madeira
//
function rotationXXMatrix( degrees )
{
m = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]];
m.matrix = true;
m[1][1] = Math.cos( radians( degrees ) );
m[1][2] = -Math.sin( radians( degrees ) );
m[2][1] = Math.sin( radians( degrees ) );
m[2][2] = Math.cos( radians( degrees ) )
return m;
}
function rotationYYMatrix( degrees )
{
m = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]];
m.matrix = true;
m[0][0] = Math.cos( radians( degrees ) );
m[0][2] = Math.sin( radians( degrees ) );
m[2][0] = -Math.sin( radians( degrees ) );
m[2][2] = Math.cos( radians( degrees ) )
return m;
}
function rotationZZMatrix( degrees )
{
m = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]];
m.matrix = true;
m[0][0] = Math.cos( radians( degrees ) );
m[0][1] = -Math.sin( radians( degrees ) );
m[1][0] = Math.sin( radians( degrees ) );
m[1][1] = Math.cos( radians( degrees ) )
return m;
}
function scalingMatrix( sx, sy, sz )
{
m = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]];
m.matrix = true;
m[0][0] = sx;
m[1][1] = sy;
m[2][2] = sz;
return m;
}
function translationMatrix( tx, ty, tz )
{
m = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]];
m.matrix = true;
m[0][3] = tx;
m[1][3] = ty;
m[2][3] = tz;
return m;
}
//----------------------------------------------------------------------------
//
// Projection Matrix Generators - Angel / Shreiner
//
function ortho( left, right, bottom, top, near, far )
{
if ( left == right ) { throw "ortho(): left and right are equal"; }
if ( bottom == top ) { throw "ortho(): bottom and top are equal"; }
if ( near == far ) { throw "ortho(): near and far are equal"; }
var w = right - left;
var h = top - bottom;
var d = far - near;
var result = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]];
result.matrix = true;
result[0][0] = 2.0 / w;
result[1][1] = 2.0 / h;
result[2][2] = -2.0 / d;
result[0][3] = -(left + right) / w;
result[1][3] = -(top + bottom) / h;
result[2][3] = -(near + far) / d;
return result;
}
//----------------------------------------------------------------------------
function perspective( fovy, aspect, near, far )
{
var f = 1.0 / Math.tan( radians(fovy) / 2 );
var d = far - near;
var result = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]];
result.matrix = true;
result[0][0] = f / aspect;
result[1][1] = f;
result[2][2] = -(near + far) / d;
result[2][3] = -2 * near * far / d;
result[3][2] = -1;
result[3][3] = 0.0;
return result;
}
//----------------------------------------------------------------------------
//
// Operations for 3D Points and Vectors - J. Madeira
//
function computeMidPoint( p1, p2 )
{
var result = [0,0,0];
for( i = 0; i < 3; i++ ) {
result[i] = ( p1[i] + p2[i] ) / 2.0;
}
return result;
}
function computeCentroid( p1, p2, p3 )
{
var result = [0,0,0];
for( i = 0; i < 3; i++ ) {
result[i] = ( p1[i] + p2[i] + p3[i]) / 3.0;
}
return result;
}
//----------------------------------------------------------------------------
function normalize( v )
{
var squaresSum = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
var norm = Math.sqrt( squaresSum );
v[0] /= norm;
v[1] /= norm;
v[2] /= norm;
}
//----------------------------------------------------------------------------
// NEW --- Symmetric vector
function symmetric( v )
{
var result = [0,0,0];
for( i = 0; i < 3; i++ ) {
result[i] = - v[i];
}
return result;
}
//----------------------------------------------------------------------------
// NEW --- Dot product
function dotProduct( v1, v2 )
{
var result = 0.0;
for( i = 0; i < 3; i++ ) {
result += v1[i] * v2[i];
}
return result;
}
//----------------------------------------------------------------------------
// NEW --- Vector product
function vectorProduct( v1, v2 )
{
var res = [0,0,0];
res[0] = v1[1] * v2[2] - v1[2] * v2[1];
res[1] = - ( v1[0] * v2[2] - v1[2] * v2[0] );
res[2] = v1[0] * v2[1] - v1[1] * v2[0];
return res;
}
//----------------------------------------------------------------------------
// NEW --- Compute unit normal vector to triangle defined by p1, p2 and p3 (CCW)
function computeNormalVector( p0, p1, p2 )
{
var v1 = [0,0,0];
var v2 = [0,0,0];
var result = [0,0,0];
v1[0] = p1[0] - p0[0];
v1[1] = p1[1] - p0[1];
v1[2] = p1[2] - p0[2];
v2[0] = p2[0] - p0[0];
v2[1] = p2[1] - p0[1];
v2[2] = p2[2] - p0[2];
result = vectorProduct( v1, v2 );
normalize( result );
return result;
}
//----------------------------------------------------------------------------
// NEW --- Multiplying using homogeneous coordinates
function multiplyPointByMatrix( m, p )
{
var result = [0,0,0,1];
for( var i = 0; i < 4; i++ ) {
for( var j = 0; j < 4; j++ ) {
result[i] += m[i][j] * p[j];
}
}
return result;
}
function multiplyVectorByMatrix( m, p )
{
var result = [0,0,0,1];
for( var i = 0; i < 4; i++ ) {
for( var j = 0; j < 4; j++ ) { // Can stop earlier; 4th coord is ZERO !!
result[i] += m[i][j] * p[j];
}
}
return result;
}