aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/3D/math-utils.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/helper-classes/3D/math-utils.js')
-rwxr-xr-xjs/helper-classes/3D/math-utils.js87
1 files changed, 53 insertions, 34 deletions
diff --git a/js/helper-classes/3D/math-utils.js b/js/helper-classes/3D/math-utils.js
index f084cf9f..2f0283a9 100755
--- a/js/helper-classes/3D/math-utils.js
+++ b/js/helper-classes/3D/math-utils.js
@@ -86,7 +86,7 @@ var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, {
86 if (b.length < n) n = b.length; 86 if (b.length < n) n = b.length;
87 if (n > 0) 87 if (n > 0)
88 { 88 {
89 rtnVec = Vector.create([0]); 89 rtnVec = [0];
90 for (var i=0; i<n; i++) 90 for (var i=0; i<n; i++)
91 rtnVec[i] = a[i] - b[i]; 91 rtnVec[i] = a[i] - b[i];
92 } 92 }
@@ -103,7 +103,7 @@ var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, {
103 if (b.length < n) n = b.length; 103 if (b.length < n) n = b.length;
104 if (n > 0) 104 if (n > 0)
105 { 105 {
106 rtnVec = Vector.create([0]); 106 rtnVec = [0];
107 for (var i=0; i<n; i++) 107 for (var i=0; i<n; i++)
108 rtnVec[i] = a[i] + b[i]; 108 rtnVec[i] = a[i] + b[i];
109 } 109 }
@@ -145,9 +145,7 @@ var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, {
145 { 145 {
146 var x0 = pt0[0], y0 = pt0[1], z0 = pt0[2]; 146 var x0 = pt0[0], y0 = pt0[1], z0 = pt0[2];
147 var dx = vec[0], dy = vec[1], dz = vec[2]; 147 var dx = vec[0], dy = vec[1], dz = vec[2];
148 rtnPt = Vector.create( [x0 + t*dx, 148 rtnPt = [x0 + t*dx, y0 + t*dy, z0 + t*dz] ;
149 y0 + t*dy,
150 z0 + t*dz] );
151 } 149 }
152 150
153 return rtnPt; 151 return rtnPt;
@@ -267,11 +265,7 @@ var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, {
267 { 265 {
268 var x0 = pt0[0], y0 = pt0[1], z0 = pt0[2], 266 var x0 = pt0[0], y0 = pt0[1], z0 = pt0[2],
269 x1 = pt1[0], y1 = pt1[1], z1 = pt1[2]; 267 x1 = pt1[0], y1 = pt1[1], z1 = pt1[2];
270 var pt = Vector.create( [ 268 var pt = [ x0 + t*(x1 - x0), y0 + t*(y1 - y0), z0 + t*(z1 - z0) ];
271 x0 + t*(x1 - x0),
272 y0 + t*(y1 - y0),
273 z0 + t*(z1 - z0)
274 ] );
275 269
276 return pt; 270 return pt;
277 } 271 }
@@ -359,8 +353,7 @@ var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, {
359 return null; //no intersection unless the the intersection point lies on both segments 353 return null; //no intersection unless the the intersection point lies on both segments
360 } 354 }
361 355
362 var intPt = Vector.create([seg0Start[0] + paramSeg0 * (seg0End[0] - seg0Start[0]), 356 var intPt = [seg0Start[0] + paramSeg0 * (seg0End[0] - seg0Start[0]), seg0Start[1] + paramSeg0 * (seg0End[1] - seg0Start[1])];
363 seg0Start[1] + paramSeg0 * (seg0End[1] - seg0Start[1])]);
364 357
365 return intPt; 358 return intPt;
366 } 359 }
@@ -383,10 +376,7 @@ var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, {
383 return this.vecDist(rayOrig, pt); 376 return this.vecDist(rayOrig, pt);
384 }//if( U < 0.0) { 377 }//if( U < 0.0) {
385 378
386 var intersection = Vector.create([ 379 var intersection = [ rayOrig[0] + U * (rayDir[0]), rayOrig[1] + U * (rayDir[1]), rayOrig[2] + U * (rayDir[2])];
387 rayOrig[0] + U * (rayDir[0]),
388 rayOrig[1] + U * (rayDir[1]),
389 rayOrig[2] + U * (rayDir[2])]);
390 380
391 return this.vecDist(intersection, pt); 381 return this.vecDist(intersection, pt);
392 } 382 }
@@ -424,10 +414,7 @@ var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, {
424 } 414 }
425 }//if( U < 0.0 || U > 1.0 ) { 415 }//if( U < 0.0 || U > 1.0 ) {
426 416
427 var intersection = Vector.create([ 417 var intersection = [ segP0[0] + U * (segP1[0] - segP0[0]), segP0[1] + U * (segP1[1] - segP0[1]), segP0[2] + U * (segP1[2] - segP0[2])];
428 segP0[0] + U * (segP1[0] - segP0[0]),
429 segP0[1] + U * (segP1[1] - segP0[1]),
430 segP0[2] + U * (segP1[2] - segP0[2])]);
431 418
432 return this.vecDist(intersection, pt); 419 return this.vecDist(intersection, pt);
433 } 420 }
@@ -538,6 +525,39 @@ var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, {
538 } 525 }
539 }, 526 },
540 527
528 isIdentityMatrix: {
529 value: function( mat )
530 {
531 if(!mat)
532 {
533 return false;
534 }
535 else
536 {
537 if(mat[0] !== 1) return false;
538 if(mat[1] !== 0) return false;
539 if(mat[2] !== 0) return false;
540 if(mat[3] !== 0) return false;
541
542 if(mat[4] !== 0) return false;
543 if(mat[5] !== 1) return false;
544 if(mat[6] !== 0) return false;
545 if(mat[7] !== 0) return false;
546
547 if(mat[8] !== 0) return false;
548 if(mat[9] !== 0) return false;
549 if(mat[10] !== 1) return false;
550 if(mat[11] !== 0) return false;
551
552 if(mat[12] !== 0) return false;
553 if(mat[13] !== 0) return false;
554 if(mat[14] !== 0) return false;
555 if(mat[15] !== 1) return false;
556 }
557 return true;
558 }
559 },
560
541 rectsOverlap: 561 rectsOverlap:
542 { 562 {
543 value: function( pt, width, height, elt ) 563 value: function( pt, width, height, elt )
@@ -704,7 +724,7 @@ var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, {
704 yNrm += (zPts[i] - zPts[j]) * (xPts[i] + xPts[j]); 724 yNrm += (zPts[i] - zPts[j]) * (xPts[i] + xPts[j]);
705 zNrm += (xPts[i] - xPts[j]) * (yPts[i] + yPts[j]); 725 zNrm += (xPts[i] - xPts[j]) * (yPts[i] + yPts[j]);
706 } 726 }
707 var normal = Vector.create( [xNrm, yNrm, zNrm] ); 727 var normal = [xNrm, yNrm, zNrm];
708 728
709 // the area of the polygon is the length of the normal 729 // the area of the polygon is the length of the normal
710 var area = VecUtils.vecMag(3, normal ); 730 var area = VecUtils.vecMag(3, normal );
@@ -730,7 +750,7 @@ var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, {
730 yNrm += (b[i][2] - b[j][2]) * (b[i][0] + b[j][0]); 750 yNrm += (b[i][2] - b[j][2]) * (b[i][0] + b[j][0]);
731 zNrm += (b[i][0] - b[j][0]) * (b[i][1] + b[j][1]); 751 zNrm += (b[i][0] - b[j][0]) * (b[i][1] + b[j][1]);
732 } 752 }
733 var normal = Vector.create( [xNrm, yNrm, zNrm] ); 753 var normal = [xNrm, yNrm, zNrm];
734 754
735 // the area of the polygon is the length of the normal 755 // the area of the polygon is the length of the normal
736 var area = VecUtils.vecMag(3, normal ); 756 var area = VecUtils.vecMag(3, normal );
@@ -870,7 +890,7 @@ var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, {
870// var v0 = pt0.slice(0); 890// var v0 = pt0.slice(0);
871// var v1 = pt1.slice(0); 891// var v1 = pt1.slice(0);
872// 892//
873// var origin = Vector.create([0, 0]); 893// var origin = [0, 0];
874// 894//
875// if(origin) 895// if(origin)
876// { 896// {
@@ -934,8 +954,7 @@ var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, {
934 if(!localPt) 954 if(!localPt)
935 { 955 {
936 return null; 956 return null;
937 return Vector.create( [1, 1]); 957// return [1, 1];
938 return [1, 1];
939 } 958 }
940 localPt = this.transformPoint(localPt, matL); 959 localPt = this.transformPoint(localPt, matL);
941 960
@@ -1026,12 +1045,12 @@ var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, {
1026 perspectiveMatrix, 1045 perspectiveMatrix,
1027 inversePerspectiveMatrix, 1046 inversePerspectiveMatrix,
1028 transposedInversePerspectiveMatrix, 1047 transposedInversePerspectiveMatrix,
1029 perspective = Vector.create([0,0,0,0]), 1048 perspective = [0,0,0,0],
1030 translate = Vector.create([0,0,0]), 1049 translate = [0,0,0],
1031 scale = Vector.create([0,0,0]), 1050 scale = [0,0,0],
1032 skew = Vector.create([0,0,0]), 1051 skew = [0,0,0],
1033 rotate = Vector.create([0,0,0]), 1052 rotate = [0,0,0],
1034 rightHandSide = Vector.create([0,0,0,0]); 1053 rightHandSide = [0,0,0,0];
1035 // Normalize the matrix. 1054 // Normalize the matrix.
1036 if (matrix[15] === 0) 1055 if (matrix[15] === 0)
1037 { 1056 {
@@ -1109,9 +1128,9 @@ var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, {
1109 } 1128 }
1110 1129
1111 // Compute X scale factor and normalize first row. 1130 // Compute X scale factor and normalize first row.
1112 var rowX = Vector.create([row[0], row[0+4], row[0+8]]); 1131 var rowX = [row[0], row[0+4], row[0+8]];
1113 var rowY = Vector.create([row[1], row[1+4], row[1+8]]); 1132 var rowY = [row[1], row[1+4], row[1+8]];
1114 var rowZ = Vector.create([row[2], row[2+4], row[2+8]]); 1133 var rowZ = [row[2], row[2+4], row[2+8]];
1115 scale[0] = VecUtils.vecMag(3, rowX); 1134 scale[0] = VecUtils.vecMag(3, rowX);
1116 rowX = VecUtils.vecNormalize(3, rowX); 1135 rowX = VecUtils.vecNormalize(3, rowX);
1117 row[0] = rowX[0]; 1136 row[0] = rowX[0];