js/src/devtools/jint/sunspider/3d-cube.js

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 // 3D Cube Rotation
michael@0 2 // http://www.speich.net/computer/moztesting/3d.htm
michael@0 3 // Created by Simon Speich
michael@0 4
michael@0 5 var Q = new Array();
michael@0 6 var MTrans = new Array(); // transformation matrix
michael@0 7 var MQube = new Array(); // position information of qube
michael@0 8 var I = new Array(); // entity matrix
michael@0 9 var Origin = new Object();
michael@0 10 var Testing = new Object();
michael@0 11 var LoopTimer;
michael@0 12
michael@0 13 var DisplArea = new Object();
michael@0 14 DisplArea.Width = 300;
michael@0 15 DisplArea.Height = 300;
michael@0 16
michael@0 17 function DrawLine(From, To) {
michael@0 18 var x1 = From.V[0];
michael@0 19 var x2 = To.V[0];
michael@0 20 var y1 = From.V[1];
michael@0 21 var y2 = To.V[1];
michael@0 22 var dx = Math.abs(x2 - x1);
michael@0 23 var dy = Math.abs(y2 - y1);
michael@0 24 var x = x1;
michael@0 25 var y = y1;
michael@0 26 var IncX1, IncY1;
michael@0 27 var IncX2, IncY2;
michael@0 28 var Den;
michael@0 29 var Num;
michael@0 30 var NumAdd;
michael@0 31 var NumPix;
michael@0 32
michael@0 33 if (x2 >= x1) { IncX1 = 1; IncX2 = 1; }
michael@0 34 else { IncX1 = -1; IncX2 = -1; }
michael@0 35 if (y2 >= y1) { IncY1 = 1; IncY2 = 1; }
michael@0 36 else { IncY1 = -1; IncY2 = -1; }
michael@0 37 if (dx >= dy) {
michael@0 38 IncX1 = 0;
michael@0 39 IncY2 = 0;
michael@0 40 Den = dx;
michael@0 41 Num = dx / 2;
michael@0 42 NumAdd = dy;
michael@0 43 NumPix = dx;
michael@0 44 }
michael@0 45 else {
michael@0 46 IncX2 = 0;
michael@0 47 IncY1 = 0;
michael@0 48 Den = dy;
michael@0 49 Num = dy / 2;
michael@0 50 NumAdd = dx;
michael@0 51 NumPix = dy;
michael@0 52 }
michael@0 53
michael@0 54 NumPix = Math.round(Q.LastPx + NumPix);
michael@0 55
michael@0 56 var i = Q.LastPx;
michael@0 57 /* BEGIN LOOP */
michael@0 58 for (; i < NumPix; i++) {
michael@0 59 Num += NumAdd;
michael@0 60 if (Num >= Den) {
michael@0 61 Num -= Den;
michael@0 62 x += IncX1;
michael@0 63 y += IncY1;
michael@0 64 }
michael@0 65 x += IncX2;
michael@0 66 y += IncY2;
michael@0 67 }
michael@0 68 /* END LOOP */
michael@0 69 Q.LastPx = NumPix;
michael@0 70 }
michael@0 71
michael@0 72 function CalcCross(V0, V1) {
michael@0 73 var Cross = new Array();
michael@0 74 Cross[0] = V0[1]*V1[2] - V0[2]*V1[1];
michael@0 75 Cross[1] = V0[2]*V1[0] - V0[0]*V1[2];
michael@0 76 Cross[2] = V0[0]*V1[1] - V0[1]*V1[0];
michael@0 77 return Cross;
michael@0 78 }
michael@0 79
michael@0 80 function CalcNormal(V0, V1, V2) {
michael@0 81 var A = new Array(); var B = new Array();
michael@0 82 /* BEGIN LOOP */
michael@0 83 for (var i = 0; i < 3; i++) {
michael@0 84 A[i] = V0[i] - V1[i];
michael@0 85 B[i] = V2[i] - V1[i];
michael@0 86 }
michael@0 87 /* END LOOP */
michael@0 88 A = CalcCross(A, B);
michael@0 89 var Length = Math.sqrt(A[0]*A[0] + A[1]*A[1] + A[2]*A[2]);
michael@0 90 /* BEGIN LOOP */
michael@0 91 for (var i = 0; i < 3; i++) A[i] = A[i] / Length;
michael@0 92 /* END LOOP */
michael@0 93 A[3] = 1;
michael@0 94 return A;
michael@0 95 }
michael@0 96
michael@0 97 function CreateP(X,Y,Z) {
michael@0 98 this.V = [X,Y,Z,1];
michael@0 99 }
michael@0 100
michael@0 101 // multiplies two matrices
michael@0 102 function MMulti(M1, M2) {
michael@0 103 var M = [[],[],[],[]];
michael@0 104 var i = 0;
michael@0 105 var j = 0;
michael@0 106 /* BEGIN LOOP */
michael@0 107 for (; i < 4; i++) {
michael@0 108 j = 0;
michael@0 109 /* BEGIN LOOP */
michael@0 110 for (; j < 4; j++) M[i][j] = M1[i][0] * M2[0][j] + M1[i][1] * M2[1][j] + M1[i][2] * M2[2][j] + M1[i][3] * M2[3][j];
michael@0 111 /* END LOOP */
michael@0 112 }
michael@0 113 /* END LOOP */
michael@0 114 return M;
michael@0 115 }
michael@0 116
michael@0 117 //multiplies matrix with vector
michael@0 118 function VMulti(M, V) {
michael@0 119 var Vect = new Array();
michael@0 120 var i = 0;
michael@0 121 /* BEGIN LOOP */
michael@0 122 for (;i < 4; i++) Vect[i] = M[i][0] * V[0] + M[i][1] * V[1] + M[i][2] * V[2] + M[i][3] * V[3];
michael@0 123 /* END LOOP */
michael@0 124 return Vect;
michael@0 125 }
michael@0 126
michael@0 127 function VMulti2(M, V) {
michael@0 128 var Vect = new Array();
michael@0 129 var i = 0;
michael@0 130 /* BEGIN LOOP */
michael@0 131 for (;i < 3; i++) Vect[i] = M[i][0] * V[0] + M[i][1] * V[1] + M[i][2] * V[2];
michael@0 132 /* END LOOP */
michael@0 133 return Vect;
michael@0 134 }
michael@0 135
michael@0 136 // add to matrices
michael@0 137 function MAdd(M1, M2) {
michael@0 138 var M = [[],[],[],[]];
michael@0 139 var i = 0;
michael@0 140 var j = 0;
michael@0 141 /* BEGIN LOOP */
michael@0 142 for (; i < 4; i++) {
michael@0 143 j = 0;
michael@0 144 /* BEGIN LOOP */
michael@0 145 for (; j < 4; j++) M[i][j] = M1[i][j] + M2[i][j];
michael@0 146 /* END LOOP */
michael@0 147 }
michael@0 148 /* END LOOP */
michael@0 149 return M;
michael@0 150 }
michael@0 151
michael@0 152 function Translate(M, Dx, Dy, Dz) {
michael@0 153 var T = [
michael@0 154 [1,0,0,Dx],
michael@0 155 [0,1,0,Dy],
michael@0 156 [0,0,1,Dz],
michael@0 157 [0,0,0,1]
michael@0 158 ];
michael@0 159 return MMulti(T, M);
michael@0 160 }
michael@0 161
michael@0 162 function RotateX(M, Phi) {
michael@0 163 var a = Phi;
michael@0 164 a *= Math.PI / 180;
michael@0 165 var Cos = Math.cos(a);
michael@0 166 var Sin = Math.sin(a);
michael@0 167 var R = [
michael@0 168 [1,0,0,0],
michael@0 169 [0,Cos,-Sin,0],
michael@0 170 [0,Sin,Cos,0],
michael@0 171 [0,0,0,1]
michael@0 172 ];
michael@0 173 return MMulti(R, M);
michael@0 174 }
michael@0 175
michael@0 176 function RotateY(M, Phi) {
michael@0 177 var a = Phi;
michael@0 178 a *= Math.PI / 180;
michael@0 179 var Cos = Math.cos(a);
michael@0 180 var Sin = Math.sin(a);
michael@0 181 var R = [
michael@0 182 [Cos,0,Sin,0],
michael@0 183 [0,1,0,0],
michael@0 184 [-Sin,0,Cos,0],
michael@0 185 [0,0,0,1]
michael@0 186 ];
michael@0 187 return MMulti(R, M);
michael@0 188 }
michael@0 189
michael@0 190 function RotateZ(M, Phi) {
michael@0 191 var a = Phi;
michael@0 192 a *= Math.PI / 180;
michael@0 193 var Cos = Math.cos(a);
michael@0 194 var Sin = Math.sin(a);
michael@0 195 var R = [
michael@0 196 [Cos,-Sin,0,0],
michael@0 197 [Sin,Cos,0,0],
michael@0 198 [0,0,1,0],
michael@0 199 [0,0,0,1]
michael@0 200 ];
michael@0 201 return MMulti(R, M);
michael@0 202 }
michael@0 203
michael@0 204 function DrawQube() {
michael@0 205 // calc current normals
michael@0 206 var CurN = new Array();
michael@0 207 var i = 5;
michael@0 208 Q.LastPx = 0;
michael@0 209 /* BEGIN LOOP */
michael@0 210 for (; i > -1; i--) CurN[i] = VMulti2(MQube, Q.Normal[i]);
michael@0 211 /* END LOOP */
michael@0 212 if (CurN[0][2] < 0) {
michael@0 213 if (!Q.Line[0]) { DrawLine(Q[0], Q[1]); Q.Line[0] = true; };
michael@0 214 if (!Q.Line[1]) { DrawLine(Q[1], Q[2]); Q.Line[1] = true; };
michael@0 215 if (!Q.Line[2]) { DrawLine(Q[2], Q[3]); Q.Line[2] = true; };
michael@0 216 if (!Q.Line[3]) { DrawLine(Q[3], Q[0]); Q.Line[3] = true; };
michael@0 217 }
michael@0 218 if (CurN[1][2] < 0) {
michael@0 219 if (!Q.Line[2]) { DrawLine(Q[3], Q[2]); Q.Line[2] = true; };
michael@0 220 if (!Q.Line[9]) { DrawLine(Q[2], Q[6]); Q.Line[9] = true; };
michael@0 221 if (!Q.Line[6]) { DrawLine(Q[6], Q[7]); Q.Line[6] = true; };
michael@0 222 if (!Q.Line[10]) { DrawLine(Q[7], Q[3]); Q.Line[10] = true; };
michael@0 223 }
michael@0 224 if (CurN[2][2] < 0) {
michael@0 225 if (!Q.Line[4]) { DrawLine(Q[4], Q[5]); Q.Line[4] = true; };
michael@0 226 if (!Q.Line[5]) { DrawLine(Q[5], Q[6]); Q.Line[5] = true; };
michael@0 227 if (!Q.Line[6]) { DrawLine(Q[6], Q[7]); Q.Line[6] = true; };
michael@0 228 if (!Q.Line[7]) { DrawLine(Q[7], Q[4]); Q.Line[7] = true; };
michael@0 229 }
michael@0 230 if (CurN[3][2] < 0) {
michael@0 231 if (!Q.Line[4]) { DrawLine(Q[4], Q[5]); Q.Line[4] = true; };
michael@0 232 if (!Q.Line[8]) { DrawLine(Q[5], Q[1]); Q.Line[8] = true; };
michael@0 233 if (!Q.Line[0]) { DrawLine(Q[1], Q[0]); Q.Line[0] = true; };
michael@0 234 if (!Q.Line[11]) { DrawLine(Q[0], Q[4]); Q.Line[11] = true; };
michael@0 235 }
michael@0 236 if (CurN[4][2] < 0) {
michael@0 237 if (!Q.Line[11]) { DrawLine(Q[4], Q[0]); Q.Line[11] = true; };
michael@0 238 if (!Q.Line[3]) { DrawLine(Q[0], Q[3]); Q.Line[3] = true; };
michael@0 239 if (!Q.Line[10]) { DrawLine(Q[3], Q[7]); Q.Line[10] = true; };
michael@0 240 if (!Q.Line[7]) { DrawLine(Q[7], Q[4]); Q.Line[7] = true; };
michael@0 241 }
michael@0 242 if (CurN[5][2] < 0) {
michael@0 243 if (!Q.Line[8]) { DrawLine(Q[1], Q[5]); Q.Line[8] = true; };
michael@0 244 if (!Q.Line[5]) { DrawLine(Q[5], Q[6]); Q.Line[5] = true; };
michael@0 245 if (!Q.Line[9]) { DrawLine(Q[6], Q[2]); Q.Line[9] = true; };
michael@0 246 if (!Q.Line[1]) { DrawLine(Q[2], Q[1]); Q.Line[1] = true; };
michael@0 247 }
michael@0 248 Q.Line = [false,false,false,false,false,false,false,false,false,false,false,false];
michael@0 249 Q.LastPx = 0;
michael@0 250 }
michael@0 251
michael@0 252 function Loop() {
michael@0 253 if (Testing.LoopCount > Testing.LoopMax) return;
michael@0 254 var TestingStr = String(Testing.LoopCount);
michael@0 255 /* BEGIN LOOP */
michael@0 256 while (TestingStr.length < 3) TestingStr = "0" + TestingStr;
michael@0 257 /* END LOOP */
michael@0 258 MTrans = Translate(I, -Q[8].V[0], -Q[8].V[1], -Q[8].V[2]);
michael@0 259 MTrans = RotateX(MTrans, 1);
michael@0 260 MTrans = RotateY(MTrans, 3);
michael@0 261 MTrans = RotateZ(MTrans, 5);
michael@0 262 MTrans = Translate(MTrans, Q[8].V[0], Q[8].V[1], Q[8].V[2]);
michael@0 263 MQube = MMulti(MTrans, MQube);
michael@0 264 var i = 8;
michael@0 265 /* BEGIN LOOP */
michael@0 266 for (; i > -1; i--) {
michael@0 267 Q[i].V = VMulti(MTrans, Q[i].V);
michael@0 268 }
michael@0 269 /* END LOOP */
michael@0 270 DrawQube();
michael@0 271 Testing.LoopCount++;
michael@0 272 Loop();
michael@0 273 }
michael@0 274
michael@0 275 function Init(CubeSize) {
michael@0 276 // init/reset vars
michael@0 277 Origin.V = [150,150,20,1];
michael@0 278 Testing.LoopCount = 0;
michael@0 279 Testing.LoopMax = 50;
michael@0 280 Testing.TimeMax = 0;
michael@0 281 Testing.TimeAvg = 0;
michael@0 282 Testing.TimeMin = 0;
michael@0 283 Testing.TimeTemp = 0;
michael@0 284 Testing.TimeTotal = 0;
michael@0 285 Testing.Init = false;
michael@0 286
michael@0 287 // transformation matrix
michael@0 288 MTrans = [
michael@0 289 [1,0,0,0],
michael@0 290 [0,1,0,0],
michael@0 291 [0,0,1,0],
michael@0 292 [0,0,0,1]
michael@0 293 ];
michael@0 294
michael@0 295 // position information of qube
michael@0 296 MQube = [
michael@0 297 [1,0,0,0],
michael@0 298 [0,1,0,0],
michael@0 299 [0,0,1,0],
michael@0 300 [0,0,0,1]
michael@0 301 ];
michael@0 302
michael@0 303 // entity matrix
michael@0 304 I = [
michael@0 305 [1,0,0,0],
michael@0 306 [0,1,0,0],
michael@0 307 [0,0,1,0],
michael@0 308 [0,0,0,1]
michael@0 309 ];
michael@0 310
michael@0 311 // create qube
michael@0 312 Q[0] = new CreateP(-CubeSize,-CubeSize, CubeSize);
michael@0 313 Q[1] = new CreateP(-CubeSize, CubeSize, CubeSize);
michael@0 314 Q[2] = new CreateP( CubeSize, CubeSize, CubeSize);
michael@0 315 Q[3] = new CreateP( CubeSize,-CubeSize, CubeSize);
michael@0 316 Q[4] = new CreateP(-CubeSize,-CubeSize,-CubeSize);
michael@0 317 Q[5] = new CreateP(-CubeSize, CubeSize,-CubeSize);
michael@0 318 Q[6] = new CreateP( CubeSize, CubeSize,-CubeSize);
michael@0 319 Q[7] = new CreateP( CubeSize,-CubeSize,-CubeSize);
michael@0 320
michael@0 321 // center of gravity
michael@0 322 Q[8] = new CreateP(0, 0, 0);
michael@0 323
michael@0 324 // anti-clockwise edge check
michael@0 325 Q.Edge = [[0,1,2],[3,2,6],[7,6,5],[4,5,1],[4,0,3],[1,5,6]];
michael@0 326
michael@0 327 // calculate squad normals
michael@0 328 Q.Normal = new Array();
michael@0 329 /* BEGIN LOOP */
michael@0 330 for (var i = 0; i < Q.Edge.length; i++) Q.Normal[i] = CalcNormal(Q[Q.Edge[i][0]].V, Q[Q.Edge[i][1]].V, Q[Q.Edge[i][2]].V);
michael@0 331 /* END LOOP */
michael@0 332
michael@0 333 // line drawn ?
michael@0 334 Q.Line = [false,false,false,false,false,false,false,false,false,false,false,false];
michael@0 335
michael@0 336 // create line pixels
michael@0 337 Q.NumPx = 9 * 2 * CubeSize;
michael@0 338 /* BEGIN LOOP */
michael@0 339 for (var i = 0; i < Q.NumPx; i++) CreateP(0,0,0);
michael@0 340 /* END LOOP */
michael@0 341
michael@0 342 MTrans = Translate(MTrans, Origin.V[0], Origin.V[1], Origin.V[2]);
michael@0 343 MQube = MMulti(MTrans, MQube);
michael@0 344
michael@0 345 var i = 0;
michael@0 346 /* BEGIN LOOP */
michael@0 347 for (; i < 9; i++) {
michael@0 348 Q[i].V = VMulti(MTrans, Q[i].V);
michael@0 349 }
michael@0 350 /* END LOOP */
michael@0 351 DrawQube();
michael@0 352 Testing.Init = true;
michael@0 353 Loop();
michael@0 354 }
michael@0 355
michael@0 356 /* BEGIN LOOP */
michael@0 357 for ( var i = 20; i <= 160; i *= 2 ) {
michael@0 358 Init(i);
michael@0 359 }
michael@0 360 /* END LOOP */
michael@0 361
michael@0 362 Q = null;
michael@0 363 MTrans = null;
michael@0 364 MQube = null;
michael@0 365 I = null;
michael@0 366 Origin = null;
michael@0 367 Testing = null;
michael@0 368 LoopTime = null;
michael@0 369 DisplArea = null;

mercurial