-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a22c67
commit e15995c
Showing
2 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
var vector = require('../Utilities/vector.js') | ||
|
||
const p = "PASS", f = "FAIL"; | ||
var state; | ||
|
||
function within(target, precision, test) { | ||
return (test < target+precision) && (test > target-precision); | ||
} | ||
|
||
var res = vector.zero; | ||
state = (res && | ||
(typeof res.x != 'undefined' && res.x===0)&& | ||
(typeof res.y != 'undefined' && res.y===0)&& | ||
(typeof res.z != 'undefined' && res.z===0))?p:f; | ||
console.log("zero: "+JSON.stringify(res)+"\t\t\t"+state); | ||
|
||
res = vector.one; | ||
state = (res && | ||
(typeof res.x != 'undefined' && res.x===1)&& | ||
(typeof res.y != 'undefined' && res.y===1)&& | ||
(typeof res.z != 'undefined' && res.z===1))?p:f; | ||
console.log("one: "+JSON.stringify(res)+"\t\t\t"+state); | ||
|
||
res = vector.look; | ||
state = (res && | ||
(typeof res.x != 'undefined' && res.x===0)&& | ||
(typeof res.y != 'undefined' && res.y===0)&& | ||
(typeof res.z != 'undefined' && res.z===1))?p:f; | ||
console.log("look: "+JSON.stringify(res)+"\t\t\t"+state); | ||
|
||
res = vector.right; | ||
state = (res && | ||
(typeof res.x != 'undefined' && res.x===1)&& | ||
(typeof res.y != 'undefined' && res.y===0)&& | ||
(typeof res.z != 'undefined' && res.z===0))?p:f; | ||
console.log("right: "+JSON.stringify(res)+"\t\t\t"+state); | ||
|
||
res = vector.up; | ||
state = (res && | ||
(typeof res.x != 'undefined' && res.x===0)&& | ||
(typeof res.y != 'undefined' && res.y===1)&& | ||
(typeof res.z != 'undefined' && res.z===0))?p:f; | ||
console.log("up: "+JSON.stringify(res)+"\t\t\t"+state); | ||
|
||
res = vector.v3(1,2,3); | ||
state = (res && | ||
(typeof res.x != 'undefined' && res.x===1)&& | ||
(typeof res.y != 'undefined' && res.y===2)&& | ||
(typeof res.z != 'undefined' && res.z===3))?p:f; | ||
console.log("xyz: "+JSON.stringify(res)+"\t\t\t"+state); | ||
|
||
var r3= Math.sqrt(3); | ||
res = vector.v3(1,1,1); | ||
res = vector.mag(res); | ||
state = (r3 === res)?p:f; | ||
console.log("mag(x,y,z): "+res.toFixed(3)+"\t\t\t\t"+state); | ||
|
||
var u = vector.v3(1,2,3); | ||
var v = vector.v3(4,5,6); | ||
res = vector.cross(u,v); | ||
state = (res.x===-3&&res.y===6&&res.z===-3)?p:f; | ||
console.log("cross(u,v): "+JSON.stringify(res)+"\t\t"+state); | ||
|
||
var w = vector.v3(1, 2, 3); | ||
res = vector.unit(w); | ||
state = (res.x===0.2672612419124244&& | ||
res.y===0.5345224838248488&& | ||
res.z===0.8017837257372732)?p:f; | ||
console.log("unit(v): {\"x\":"+res.x.toFixed(2)+",\"y\":"+res.y.toFixed(2)+ | ||
",\"z\":"+res.z.toFixed(2)+"}\t\t"+state); | ||
|
||
res = vector.dot(u,v); | ||
state = (res===32)?p:f; | ||
console.log("dot(u,v): "+res+"\t\t\t\t\t"+state); | ||
|
||
res = vector.negate(vector.one); | ||
state = (res.x===-1&&res.y===-1&&res.z===-1)?p:f; | ||
console.log("negate(v): "+JSON.stringify(res)+"\t\t"+state); | ||
|
||
res = vector.add(u, v); | ||
state = (res.x===5&&res.y===7&&res.z===9)?p:f; | ||
console.log("add(u, v): "+JSON.stringify(res)+"\t\t\t"+state); | ||
|
||
res = vector.scale(vector.one, 2); | ||
state = (res.x===2&&res.y===2&&res.z===2)?p:f; | ||
console.log("scale(v,s): "+JSON.stringify(res)+"\t\t\t"+state); | ||
|
||
res = vector.rotx(vector.up, 0.5*Math.PI); | ||
state = (within(0,0.0001,res.x)&& | ||
within(0,0.0001,res.y)&& | ||
within(1,0.0001,res.z))?p:f; | ||
console.log("rotx(v,theta): {\"x\":"+res.x.toFixed(2)+ | ||
",\"y\":"+res.y.toFixed(2)+",\"z\":"+res.z.toFixed(2)+"}\t"+state); | ||
|
||
res = vector.rotz(vector.up, 0.5*Math.PI); | ||
state = (within(-1,0.0001,res.x)&& | ||
within(0,0.0001,res.y)&& | ||
within(0,0.0001,res.z))?p:f; | ||
console.log("rotz(v,theta): {\"x\":"+res.x.toFixed(2)+ | ||
",\"y\":"+res.y.toFixed(2)+",\"z\":"+res.z.toFixed(2)+"}\t"+state); | ||
|
||
res = vector.roty(vector.look, 0.5*Math.PI); | ||
state = (within(1,0.0001,res.x)&& | ||
within(0,0.0001,res.y)&& | ||
within(0,0.0001,res.z))?p:f; | ||
console.log("roty(v,theta): {\"x\":"+res.x.toFixed(2)+ | ||
",\"y\":"+res.y.toFixed(2)+",\"z\":"+res.z.toFixed(2)+"}\t"+state); | ||
|
||
u = vector.up; | ||
v = vector.look; | ||
res = vector.angle(u,v)*180/Math.PI; | ||
state = within(90,0.0001,res)?p:f; | ||
console.log("angle(u,v): "+res.toFixed(2)+"\t\t\t\t"+state); | ||
|
||
v = vector.rotx(v, -45/180.0*Math.PI); | ||
res = vector.angle(u,v)*180/Math.PI; | ||
state = within(45,0.0001,res)?p:f; | ||
console.log("angle(u,v): "+res.toFixed(2)+"\t\t\t\t"+state); | ||
|
||
var axis = vector.unit(vector.one); | ||
v = vector.v3(1,-1,0); | ||
res = vector.rot(v, axis, Math.PI); | ||
state = (within(-1,0.0001,res.x)&& | ||
within(1,0.0001,res.y)&& | ||
within(0,0.0001,res.z))?p:f; | ||
console.log("rot(v,ax,theta): {\"x\":"+res.x.toFixed(2)+ | ||
",\"y\":"+res.y.toFixed(2)+",\"z\":"+res.z.toFixed(2)+"}\t"+state); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
var mag = function(v) { | ||
return Math.sqrt(v.x*v.x + v.y*v.y + v.z*v.z); | ||
}; | ||
var cross = function(u, v) { | ||
var res = v3(0,0,0); | ||
res.x = (u.y*v.z - u.z*v.y); | ||
res.y = (u.z*v.x - u.x*v.z); | ||
res.z = (u.x*v.y - u.y*v.x); | ||
return res; | ||
}; | ||
var unit = function(v) { | ||
var res = v3(0,0,0); | ||
var vmag = mag(v); | ||
res.x = v.x/vmag; | ||
res.y = v.y/vmag; | ||
res.z = v.z/vmag; | ||
return res; | ||
}; | ||
var negate = function(v) { | ||
var res = v3(0,0,0); | ||
res.x = -v.x; | ||
res.y = -v.y; | ||
res.z = -v.z | ||
return res; | ||
}; | ||
var add = function(u, v) { | ||
var res = v3(0,0,0); | ||
res.x = u.x + v.x; | ||
res.y = u.y + v.y; | ||
res.z = u.z + v.z; | ||
return res; | ||
}; | ||
var scale = function(v,s) { | ||
var res = v3(0,0,0); | ||
res.x = s*v.x; | ||
res.y = s*v.y; | ||
res.z = s*v.z; | ||
return res; | ||
}; | ||
var dot = function(u,v) { | ||
return u.x*v.x + u.y*v.y + u.z*v.z; | ||
}; | ||
var angle = function(u, v) { | ||
var mu = mag(u); | ||
var mv = mag(v); | ||
if(mu === 0 || mv === 0) | ||
return 180; | ||
var q = dot(u,v) / (mu * mv); | ||
return Math.acos(q); | ||
}; | ||
|
||
var rotatex = function(v, theta) { | ||
var res = v3(0,0,0); | ||
res.x = v.x; | ||
res.y = v.y*Math.cos(theta) - v.z*Math.sin(theta); | ||
res.z = v.y*Math.sin(theta) + v.z*Math.cos(theta); | ||
return res; | ||
}; | ||
var rotatey = function(v, theta) { | ||
var res = v3(0,0,0); | ||
res.x = v.x*Math.cos(theta) + v.z*Math.sin(theta); | ||
res.y = v.y; | ||
res.z = -v.x*Math.sin(theta) + v.z*Math.cos(theta); | ||
return res; | ||
}; | ||
var rotatez = function(v, theta) { | ||
var res = v3(0,0,0); | ||
res.x = v.x*Math.cos(theta) - v.y*Math.sin(theta); | ||
res.y = v.x*Math.sin(theta) + v.y*Math.cos(theta); | ||
res.z = v.z; | ||
return res; | ||
}; | ||
var rotate = function(v, axis, theta) { | ||
var res = v3(0,0,0); | ||
a = unit(axis); | ||
|
||
var _c = Math.cos(theta); | ||
var _s = Math.sin(theta); | ||
var _u = dot(a,v)*(1 - _c); | ||
|
||
res.x = a.x*_u + v.x*_c + (-a.z*v.y + a.y*a.z)*_s; | ||
res.y = a.y*_u + v.y*_c + ( a.z*v.x - a.x*v.z)*_s; | ||
res.z = a.z*_u + v.z*_c + (-a.y*v.x + a.x*v.y)*_s; | ||
|
||
return res; | ||
}; | ||
|
||
var v3 = function(x,y,z) { | ||
return {x:x,y:y,z:z}; | ||
} | ||
var print = function(v,f,m) { | ||
if(typeof f == 'undefined') | ||
f = 3; | ||
if(typeof m == 'undefined') | ||
m = ""; | ||
console.log(m+"{x: "+v.x.toFixed(f)+",\ty: "+v.y.toFixed(f)+",\tz: "+v.z.toFixed(f)+"}"); | ||
} | ||
|
||
module.exports = { | ||
mag: mag, | ||
cross: cross, | ||
unit: unit, | ||
dot: dot, | ||
negate: negate, | ||
add: add, | ||
scale: scale, | ||
rotx: rotatex, | ||
roty: rotatey, | ||
rotz: rotatez, | ||
rot: rotate, | ||
angle: angle, | ||
|
||
v3: v3, | ||
zero: v3(0,0,0), | ||
one: v3(1,1,1), | ||
look: v3(0,0,1), | ||
right: v3(1,0,0), | ||
up: v3(0,1,0), | ||
|
||
print: print | ||
}; |