js/src/jit-test/tests/sunspider/check-3d-cube.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 for (; i < NumPix; i++) {
michael@0 58 Num += NumAdd;
michael@0 59 if (Num >= Den) {
michael@0 60 Num -= Den;
michael@0 61 x += IncX1;
michael@0 62 y += IncY1;
michael@0 63 }
michael@0 64 x += IncX2;
michael@0 65 y += IncY2;
michael@0 66 }
michael@0 67 Q.LastPx = NumPix;
michael@0 68 }
michael@0 69
michael@0 70 function CalcCross(V0, V1) {
michael@0 71 var Cross = new Array();
michael@0 72 Cross[0] = V0[1]*V1[2] - V0[2]*V1[1];
michael@0 73 Cross[1] = V0[2]*V1[0] - V0[0]*V1[2];
michael@0 74 Cross[2] = V0[0]*V1[1] - V0[1]*V1[0];
michael@0 75 return Cross;
michael@0 76 }
michael@0 77
michael@0 78 function CalcNormal(V0, V1, V2) {
michael@0 79 var A = new Array(); var B = new Array();
michael@0 80 for (var i = 0; i < 3; i++) {
michael@0 81 A[i] = V0[i] - V1[i];
michael@0 82 B[i] = V2[i] - V1[i];
michael@0 83 }
michael@0 84 A = CalcCross(A, B);
michael@0 85 var Length = Math.sqrt(A[0]*A[0] + A[1]*A[1] + A[2]*A[2]);
michael@0 86 for (var i = 0; i < 3; i++) A[i] = A[i] / Length;
michael@0 87 A[3] = 1;
michael@0 88 return A;
michael@0 89 }
michael@0 90
michael@0 91 function CreateP(X,Y,Z) {
michael@0 92 this.V = [X,Y,Z,1];
michael@0 93 }
michael@0 94
michael@0 95 // multiplies two matrices
michael@0 96 function MMulti(M1, M2) {
michael@0 97 var M = [[],[],[],[]];
michael@0 98 var i = 0;
michael@0 99 var j = 0;
michael@0 100 for (; i < 4; i++) {
michael@0 101 j = 0;
michael@0 102 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 103 }
michael@0 104 return M;
michael@0 105 }
michael@0 106
michael@0 107 //multiplies matrix with vector
michael@0 108 function VMulti(M, V) {
michael@0 109 var Vect = new Array();
michael@0 110 var i = 0;
michael@0 111 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 112 return Vect;
michael@0 113 }
michael@0 114
michael@0 115 function VMulti2(M, V) {
michael@0 116 var Vect = new Array();
michael@0 117 var i = 0;
michael@0 118 for (;i < 3; i++) Vect[i] = M[i][0] * V[0] + M[i][1] * V[1] + M[i][2] * V[2];
michael@0 119 return Vect;
michael@0 120 }
michael@0 121
michael@0 122 // add to matrices
michael@0 123 function MAdd(M1, M2) {
michael@0 124 var M = [[],[],[],[]];
michael@0 125 var i = 0;
michael@0 126 var j = 0;
michael@0 127 for (; i < 4; i++) {
michael@0 128 j = 0;
michael@0 129 for (; j < 4; j++) M[i][j] = M1[i][j] + M2[i][j];
michael@0 130 }
michael@0 131 return M;
michael@0 132 }
michael@0 133
michael@0 134 function Translate(M, Dx, Dy, Dz) {
michael@0 135 var T = [
michael@0 136 [1,0,0,Dx],
michael@0 137 [0,1,0,Dy],
michael@0 138 [0,0,1,Dz],
michael@0 139 [0,0,0,1]
michael@0 140 ];
michael@0 141 return MMulti(T, M);
michael@0 142 }
michael@0 143
michael@0 144 function RotateX(M, Phi) {
michael@0 145 var a = Phi;
michael@0 146 a *= Math.PI / 180;
michael@0 147 var Cos = Math.cos(a);
michael@0 148 var Sin = Math.sin(a);
michael@0 149 var R = [
michael@0 150 [1,0,0,0],
michael@0 151 [0,Cos,-Sin,0],
michael@0 152 [0,Sin,Cos,0],
michael@0 153 [0,0,0,1]
michael@0 154 ];
michael@0 155 return MMulti(R, M);
michael@0 156 }
michael@0 157
michael@0 158 function RotateY(M, Phi) {
michael@0 159 var a = Phi;
michael@0 160 a *= Math.PI / 180;
michael@0 161 var Cos = Math.cos(a);
michael@0 162 var Sin = Math.sin(a);
michael@0 163 var R = [
michael@0 164 [Cos,0,Sin,0],
michael@0 165 [0,1,0,0],
michael@0 166 [-Sin,0,Cos,0],
michael@0 167 [0,0,0,1]
michael@0 168 ];
michael@0 169 return MMulti(R, M);
michael@0 170 }
michael@0 171
michael@0 172 function RotateZ(M, Phi) {
michael@0 173 var a = Phi;
michael@0 174 a *= Math.PI / 180;
michael@0 175 var Cos = Math.cos(a);
michael@0 176 var Sin = Math.sin(a);
michael@0 177 var R = [
michael@0 178 [Cos,-Sin,0,0],
michael@0 179 [Sin,Cos,0,0],
michael@0 180 [0,0,1,0],
michael@0 181 [0,0,0,1]
michael@0 182 ];
michael@0 183 return MMulti(R, M);
michael@0 184 }
michael@0 185
michael@0 186 function DrawQube() {
michael@0 187 // calc current normals
michael@0 188 var CurN = new Array();
michael@0 189 var i = 5;
michael@0 190 Q.LastPx = 0;
michael@0 191 for (; i > -1; i--) CurN[i] = VMulti2(MQube, Q.Normal[i]);
michael@0 192 if (CurN[0][2] < 0) {
michael@0 193 if (!Q.Line[0]) { DrawLine(Q[0], Q[1]); Q.Line[0] = true; };
michael@0 194 if (!Q.Line[1]) { DrawLine(Q[1], Q[2]); Q.Line[1] = true; };
michael@0 195 if (!Q.Line[2]) { DrawLine(Q[2], Q[3]); Q.Line[2] = true; };
michael@0 196 if (!Q.Line[3]) { DrawLine(Q[3], Q[0]); Q.Line[3] = true; };
michael@0 197 }
michael@0 198 if (CurN[1][2] < 0) {
michael@0 199 if (!Q.Line[2]) { DrawLine(Q[3], Q[2]); Q.Line[2] = true; };
michael@0 200 if (!Q.Line[9]) { DrawLine(Q[2], Q[6]); Q.Line[9] = true; };
michael@0 201 if (!Q.Line[6]) { DrawLine(Q[6], Q[7]); Q.Line[6] = true; };
michael@0 202 if (!Q.Line[10]) { DrawLine(Q[7], Q[3]); Q.Line[10] = true; };
michael@0 203 }
michael@0 204 if (CurN[2][2] < 0) {
michael@0 205 if (!Q.Line[4]) { DrawLine(Q[4], Q[5]); Q.Line[4] = true; };
michael@0 206 if (!Q.Line[5]) { DrawLine(Q[5], Q[6]); Q.Line[5] = true; };
michael@0 207 if (!Q.Line[6]) { DrawLine(Q[6], Q[7]); Q.Line[6] = true; };
michael@0 208 if (!Q.Line[7]) { DrawLine(Q[7], Q[4]); Q.Line[7] = true; };
michael@0 209 }
michael@0 210 if (CurN[3][2] < 0) {
michael@0 211 if (!Q.Line[4]) { DrawLine(Q[4], Q[5]); Q.Line[4] = true; };
michael@0 212 if (!Q.Line[8]) { DrawLine(Q[5], Q[1]); Q.Line[8] = true; };
michael@0 213 if (!Q.Line[0]) { DrawLine(Q[1], Q[0]); Q.Line[0] = true; };
michael@0 214 if (!Q.Line[11]) { DrawLine(Q[0], Q[4]); Q.Line[11] = true; };
michael@0 215 }
michael@0 216 if (CurN[4][2] < 0) {
michael@0 217 if (!Q.Line[11]) { DrawLine(Q[4], Q[0]); Q.Line[11] = true; };
michael@0 218 if (!Q.Line[3]) { DrawLine(Q[0], Q[3]); Q.Line[3] = true; };
michael@0 219 if (!Q.Line[10]) { DrawLine(Q[3], Q[7]); Q.Line[10] = true; };
michael@0 220 if (!Q.Line[7]) { DrawLine(Q[7], Q[4]); Q.Line[7] = true; };
michael@0 221 }
michael@0 222 if (CurN[5][2] < 0) {
michael@0 223 if (!Q.Line[8]) { DrawLine(Q[1], Q[5]); Q.Line[8] = true; };
michael@0 224 if (!Q.Line[5]) { DrawLine(Q[5], Q[6]); Q.Line[5] = true; };
michael@0 225 if (!Q.Line[9]) { DrawLine(Q[6], Q[2]); Q.Line[9] = true; };
michael@0 226 if (!Q.Line[1]) { DrawLine(Q[2], Q[1]); Q.Line[1] = true; };
michael@0 227 }
michael@0 228 Q.Line = [false,false,false,false,false,false,false,false,false,false,false,false];
michael@0 229 Q.LastPx = 0;
michael@0 230 }
michael@0 231
michael@0 232 function Loop() {
michael@0 233 if (Testing.LoopCount > Testing.LoopMax) return;
michael@0 234 var TestingStr = String(Testing.LoopCount);
michael@0 235 while (TestingStr.length < 3) TestingStr = "0" + TestingStr;
michael@0 236 MTrans = Translate(I, -Q[8].V[0], -Q[8].V[1], -Q[8].V[2]);
michael@0 237 MTrans = RotateX(MTrans, 1);
michael@0 238 MTrans = RotateY(MTrans, 3);
michael@0 239 MTrans = RotateZ(MTrans, 5);
michael@0 240 MTrans = Translate(MTrans, Q[8].V[0], Q[8].V[1], Q[8].V[2]);
michael@0 241 MQube = MMulti(MTrans, MQube);
michael@0 242 var i = 8;
michael@0 243 for (; i > -1; i--) {
michael@0 244 Q[i].V = VMulti(MTrans, Q[i].V);
michael@0 245 }
michael@0 246 DrawQube();
michael@0 247 Testing.LoopCount++;
michael@0 248 Loop();
michael@0 249 }
michael@0 250
michael@0 251 function Init(CubeSize) {
michael@0 252 // init/reset vars
michael@0 253 Origin.V = [150,150,20,1];
michael@0 254 Testing.LoopCount = 0;
michael@0 255 Testing.LoopMax = 50;
michael@0 256 Testing.TimeMax = 0;
michael@0 257 Testing.TimeAvg = 0;
michael@0 258 Testing.TimeMin = 0;
michael@0 259 Testing.TimeTemp = 0;
michael@0 260 Testing.TimeTotal = 0;
michael@0 261 Testing.Init = false;
michael@0 262
michael@0 263 // transformation matrix
michael@0 264 MTrans = [
michael@0 265 [1,0,0,0],
michael@0 266 [0,1,0,0],
michael@0 267 [0,0,1,0],
michael@0 268 [0,0,0,1]
michael@0 269 ];
michael@0 270
michael@0 271 // position information of qube
michael@0 272 MQube = [
michael@0 273 [1,0,0,0],
michael@0 274 [0,1,0,0],
michael@0 275 [0,0,1,0],
michael@0 276 [0,0,0,1]
michael@0 277 ];
michael@0 278
michael@0 279 // entity matrix
michael@0 280 I = [
michael@0 281 [1,0,0,0],
michael@0 282 [0,1,0,0],
michael@0 283 [0,0,1,0],
michael@0 284 [0,0,0,1]
michael@0 285 ];
michael@0 286
michael@0 287 // create qube
michael@0 288 Q[0] = new CreateP(-CubeSize,-CubeSize, CubeSize);
michael@0 289 Q[1] = new CreateP(-CubeSize, CubeSize, CubeSize);
michael@0 290 Q[2] = new CreateP( CubeSize, CubeSize, CubeSize);
michael@0 291 Q[3] = new CreateP( CubeSize,-CubeSize, CubeSize);
michael@0 292 Q[4] = new CreateP(-CubeSize,-CubeSize,-CubeSize);
michael@0 293 Q[5] = new CreateP(-CubeSize, CubeSize,-CubeSize);
michael@0 294 Q[6] = new CreateP( CubeSize, CubeSize,-CubeSize);
michael@0 295 Q[7] = new CreateP( CubeSize,-CubeSize,-CubeSize);
michael@0 296
michael@0 297 // center of gravity
michael@0 298 Q[8] = new CreateP(0, 0, 0);
michael@0 299
michael@0 300 // anti-clockwise edge check
michael@0 301 Q.Edge = [[0,1,2],[3,2,6],[7,6,5],[4,5,1],[4,0,3],[1,5,6]];
michael@0 302
michael@0 303 // calculate squad normals
michael@0 304 Q.Normal = new Array();
michael@0 305 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 306
michael@0 307 // line drawn ?
michael@0 308 Q.Line = [false,false,false,false,false,false,false,false,false,false,false,false];
michael@0 309
michael@0 310 // create line pixels
michael@0 311 Q.NumPx = 9 * 2 * CubeSize;
michael@0 312 for (var i = 0; i < Q.NumPx; i++) CreateP(0,0,0);
michael@0 313
michael@0 314 MTrans = Translate(MTrans, Origin.V[0], Origin.V[1], Origin.V[2]);
michael@0 315 MQube = MMulti(MTrans, MQube);
michael@0 316
michael@0 317 var i = 0;
michael@0 318 for (; i < 9; i++) {
michael@0 319 Q[i].V = VMulti(MTrans, Q[i].V);
michael@0 320 }
michael@0 321 DrawQube();
michael@0 322 Testing.Init = true;
michael@0 323 Loop();
michael@0 324 }
michael@0 325
michael@0 326 for ( var i = 20; i <= 160; i *= 2 ) {
michael@0 327 Init(i);
michael@0 328 }
michael@0 329
michael@0 330 var actual = '';
michael@0 331 for (var i = 0; i < Q.length; ++i) {
michael@0 332 actual += Q[i].V + ';';
michael@0 333 }
michael@0 334 var expected = "-116.618229186398,212.51135212951073,62.5094191967962,1;127.83701023614447,417.11611179082263,90.41153816299942,1;293.9570894432935,196.58093046570656,252.17789153139591,1;49.501850020750915,-8.02382919560505,224.275772565193,1;6.042910556709444,103.41906953429206,-212.1778915313964,1;250.49814997925202,308.02382919560387,-184.27577256519325,1;416.61822918640064,87.48864787048812,-22.509419196796493,1;172.1629897638581,-117.1161117908236,-50.41153816299975,1;150.0000000000007,149.99999999999952,20,1;";
michael@0 335 assertEq(actual, expected);
michael@0 336
michael@0 337 Q = null;
michael@0 338 MTrans = null;
michael@0 339 MQube = null;
michael@0 340 I = null;
michael@0 341 Origin = null;
michael@0 342 Testing = null;
michael@0 343 LoopTime = null;
michael@0 344 DisplArea = null;
michael@0 345

mercurial