1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #!/usr/bin/node
function CartesianPoint(x,y,z){
this.x = x;
this.y = y;
this.z = z;
}
function SphericalPoint(pho, theta, phi){
this.pho = pho;
this.theta = theta;
this.phi = phi;
}
function cartesianToSpheric(p){
var pho = Math.sqrt(p.x*p.x + p.y*p.y +p.z*p.z);
var theta = Math.atan2(p.y, p.x);
var phi = Math.acos(p.z/pho);
return new SphericalPoint(pho, theta, phi);
}
function sphericToCartesian(s){
var x = s.pho*Math.sin(s.phi)*Math.cos(s.theta);
var y = s.pho*Math.sin(s.phi)*Math.sin(s.theta);
var z = s.pho*Math.cos(s.phi);
return new CartesianPoint(x,y,z);
}
function sphericOffset(s, theta, phi){
return new SphericalPoint(s.pho, s.theta+theta, s.phi+phi);
}
function transform(p, theta, phi){
var s = cartesianToSpheric(p);
//console.log(s)
var s2 = sphericOffset(s, theta, phi);
//console.log(s2)
var p2 = sphericToCartesian(s2);
//console.log(p2)
return p2;
}
function testShift(beg, theta, phi, end, label){
var p = new CartesianPoint(beg[0], beg[1], beg[2]);
p2 = transform(p, theta, phi);
var eps = 0.00001;
if(Math.abs(end[0]-p2.x)> eps || Math.abs(end[1]-p2.y)> eps || Math.abs(end[2]-p2.z)> eps){
console.error(label+' ',p2,' expected ',end)
}
}
function test(){
testShift([0,0,1], Math.PI, 0, [0,0,1], 'no rot on z');
testShift([0,0,1], Math.PI/2, 0, [0,0,1], 'no rot2 on z');
testShift([1,0,0], 0, 0, [1,0,0], 'identity');
testShift([1,0,0], 0, Math.PI, [-1,0,0], 'rot on z');
testShift([1,0,0], Math.PI, -Math.PI/2, [0,0,1], 'rot on theta and z');
var theta = Math.PI/2;
var phi = -Math.PI/3;
//u is rotated [1,0,0] by angle theta
ux = 1*Math.cos(theta)
uy = 1*Math.sin(theta)
vx = 1*Math.cos(phi); //colinear to u
vz = 1*Math.sin(phi); //colinear to -z
//v = vx e_u + vz e_z
//v = vx (ux e_x + uy e_y) - vz e_z
v = [vx*ux, vx*uy, -vz]
testShift([1,0,0], theta, phi, v, 'rot on theta and z2');
}
test() |
Partager