1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/tilt/test/browser_tilt_math02.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,104 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 +"use strict"; 1.7 + 1.8 +function test() { 1.9 + let v1 = vec3.create(); 1.10 + 1.11 + ok(v1, "Should have created a vector with vec3.create()."); 1.12 + is(v1.length, 3, "A vec3 should have 3 elements."); 1.13 + 1.14 + ok(isApproxVec(v1, [0, 0, 0]), 1.15 + "When created, a vec3 should have the values default to 0."); 1.16 + 1.17 + vec3.set([1, 2, 3], v1); 1.18 + ok(isApproxVec(v1, [1, 2, 3]), 1.19 + "The vec3.set() function didn't set the values correctly."); 1.20 + 1.21 + vec3.zero(v1); 1.22 + ok(isApproxVec(v1, [0, 0, 0]), 1.23 + "The vec3.zero() function didn't set the values correctly."); 1.24 + 1.25 + let v2 = vec3.create([4, 5, 6]); 1.26 + ok(isApproxVec(v2, [4, 5, 6]), 1.27 + "When cloning arrays, a vec3 should have the values copied."); 1.28 + 1.29 + let v3 = vec3.create(v2); 1.30 + ok(isApproxVec(v3, [4, 5, 6]), 1.31 + "When cloning vectors, a vec3 should have the values copied."); 1.32 + 1.33 + vec3.add(v2, v3); 1.34 + ok(isApproxVec(v2, [8, 10, 12]), 1.35 + "The vec3.add() function didn't set the x value correctly."); 1.36 + 1.37 + vec3.subtract(v2, v3); 1.38 + ok(isApproxVec(v2, [4, 5, 6]), 1.39 + "The vec3.subtract() function didn't set the values correctly."); 1.40 + 1.41 + vec3.negate(v2); 1.42 + ok(isApproxVec(v2, [-4, -5, -6]), 1.43 + "The vec3.negate() function didn't set the values correctly."); 1.44 + 1.45 + vec3.scale(v2, -2); 1.46 + ok(isApproxVec(v2, [8, 10, 12]), 1.47 + "The vec3.scale() function didn't set the values correctly."); 1.48 + 1.49 + vec3.normalize(v1); 1.50 + ok(isApproxVec(v1, [0, 0, 0]), 1.51 + "Normalizing a vector with zero length should return [0, 0, 0]."); 1.52 + 1.53 + vec3.normalize(v2); 1.54 + ok(isApproxVec(v2, [ 1.55 + 0.4558423161506653, 0.5698028802871704, 0.6837634444236755 1.56 + ]), "The vec3.normalize() function didn't set the values correctly."); 1.57 + 1.58 + vec3.cross(v2, v3); 1.59 + ok(isApproxVec(v2, [ 1.60 + 5.960464477539063e-8, -1.1920928955078125e-7, 5.960464477539063e-8 1.61 + ]), "The vec3.cross() function didn't set the values correctly."); 1.62 + 1.63 + vec3.dot(v2, v3); 1.64 + ok(isApproxVec(v2, [ 1.65 + 5.960464477539063e-8, -1.1920928955078125e-7, 5.960464477539063e-8 1.66 + ]), "The vec3.dot() function didn't set the values correctly."); 1.67 + 1.68 + ok(isApproxVec([vec3.length(v2)], [1.4600096599955427e-7]), 1.69 + "The vec3.length() function didn't calculate the value correctly."); 1.70 + 1.71 + vec3.direction(v2, v3); 1.72 + ok(isApproxVec(v2, [ 1.73 + -0.4558422863483429, -0.5698028802871704, -0.6837634444236755 1.74 + ]), "The vec3.direction() function didn't set the values correctly."); 1.75 + 1.76 + vec3.lerp(v2, v3, 0.5); 1.77 + ok(isApproxVec(v2, [ 1.78 + 1.7720788717269897, 2.2150986194610596, 2.65811824798584 1.79 + ]), "The vec3.lerp() function didn't set the values correctly."); 1.80 + 1.81 + 1.82 + vec3.project([100, 100, 10], [0, 0, 100, 100], 1.83 + mat4.create(), mat4.perspective(45, 1, 0.1, 100), v1); 1.84 + ok(isApproxVec(v1, [-1157.10693359375, 1257.10693359375, 0]), 1.85 + "The vec3.project() function didn't set the values correctly."); 1.86 + 1.87 + vec3.unproject([100, 100, 1], [0, 0, 100, 100], 1.88 + mat4.create(), mat4.perspective(45, 1, 0.1, 100), v1); 1.89 + ok(isApproxVec(v1, [ 1.90 + 41.420406341552734, -41.420406341552734, -99.99771118164062 1.91 + ]), "The vec3.project() function didn't set the values correctly."); 1.92 + 1.93 + 1.94 + let ray = vec3.createRay([10, 10, 0], [100, 100, 1], [0, 0, 100, 100], 1.95 + mat4.create(), mat4.perspective(45, 1, 0.1, 100)); 1.96 + 1.97 + ok(isApproxVec(ray.origin, [ 1.98 + -0.03313708305358887, 0.03313708305358887, -0.1000000014901161 1.99 + ]), "The vec3.createRay() function didn't create the position correctly."); 1.100 + ok(isApproxVec(ray.direction, [ 1.101 + 0.35788586614428364, -0.35788586614428364, -0.862458934459091 1.102 + ]), "The vec3.createRay() function didn't create the direction correctly."); 1.103 + 1.104 + 1.105 + is(vec3.str([0, 0, 0]), "[0, 0, 0]", 1.106 + "The vec3.str() function didn't work properly."); 1.107 +}