/* ** Copyright (C) 2005 Antonin Stefanutti ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License as published by ** the Free Software Foundation; either version 2 of the License, or ** (at your option) any later version. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this program; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ ///////////////////////////////////////////////////////////////////////////////////////// //an instance of this class can be considered either a 3D point or a 3D vector class Point3D { //coordinates public var x:Number; public var y:Number; public var z:Number; ///////////////////////////////////////////////////////////////////////////////////////// //constructor function Point3D(x0:Number, y0:Number, z0:Number) { x = x0; y = y0; z = z0; } ///////////////////////////////////////////////////////////////////////////////////////// //rotate this point around the 3 reference axis (i,j,k) by angles (a,b,c) public function rotEx0(a:Number, b:Number, c:Number):Void { var l1:Number = Math.cos(c); var l2:Number = Math.sin(c); var h:Number = x*l1 - y*l2; y = x*l2 + y*l1; x = h; l1 = Math.cos(b); l2 = Math.sin(b); h = x*l1 - z*l2; z = x*l2 + z*l1; x = h; l1 = Math.cos(a); l2 = Math.sin(a); h = y*l1 - z*l2; z = y*l2 + z*l1; y = h; } ///////////////////////////////////////////////////////////////////////////////////////// //rotate this point around the 3 reference axis (i,j,k) by angles (a,b,c) defined //by previously computed cos and sin of each angle public function rotmEx0(ca:Number, sa:Number, cb:Number, sb:Number, cc:Number, sc:Number):Void { var h:Number = x*cc - y*sc; y = x*sc + y*cc; x = h; h = x*cb - z*sb; z = x*sb + z*cb; x = h; h = y*ca - z*sa; z = y*sa + z*ca; y = h; } ///////////////////////////////////////////////////////////////////////////////////////// //spin this point of angle w (radian) around affine axis defined by point(0) + vector(q) public function rotQx0(w:Number, q:Point3D):Void { var sw:Number = Math.sin(w/2); var cw:Number = Math.cos(w/2); var cx:Number = sw*q.x; var cy:Number = sw*q.y; var cz:Number = sw*q.z; var r1:Number = x; var r2:Number = y; var r3:Number = z; x = r1*(1-2*(cy*cy+cz*cz)) + r2*(2*cx*cy-2*cw*cz) + r3*(2*cw*cy+2*cx*cz); y = r1*(2*cx*cy+2*cw*cz)+r2*(1-2*(cx*cx + cz*cz)) + r3*((-2)*cw*cx+2*cy*cz); z = r1*((-2)*cw*cy + 2*cx*cz) + r2*(2*cw*cx+2*cy*cz) + r3*(1-2*(cx*cx + cy*cy)); } ///////////////////////////////////////////////////////////////////////////////////////// //spin this point around affine axis defined by point(0) + vector(q) //by passing prevously computed variables depending on vector(q) //and the rotation angle (radian) public function rotmQx0(sw:Number, cw:Number, cx:Number, cy:Number, cz:Number):Void { var r1:Number = x; var r2:Number = y; var r3:Number = z; x = r1*(1-2*(cy*cy+cz*cz)) + r2*(2*cx*cy-2*cw*cz) + r3*(2*cw*cy+2*cx*cz); y = r1*(2*cx*cy+2*cw*cz)+r2*(1-2*(cx*cx + cz*cz)) + r3*((-2)*cw*cx+2*cy*cz); z = r1*((-2)*cw*cy + 2*cx*cz) + r2*(2*cw*cx+2*cy*cz) + r3*(1-2*(cx*cx + cy*cy)); } ///////////////////////////////////////////////////////////////////////////////////////// //spin this point of angle w (radian) around affine axis defined by point(p) + vector(q) public function rotQxP(w:Number, q:Point3D, p:Point3D):Void { var sw:Number = Math.sin(w/2); var cw:Number = Math.cos(w/2); var cx:Number = sw*q.x; var cy:Number = sw*q.y; var cz:Number = sw*q.z; var r1:Number = x - p.x; var r2:Number = y - p.y; var r3:Number = z - p.z; x = r1*(1-2*(cy*cy+cz*cz)) + r2*(2*cx*cy-2*cw*cz) + r3*(2*cw*cy+2*cx*cz) + p.x; y = r1*(2*cx*cy+2*cw*cz)+r2*(1-2*(cx*cx + cz*cz)) + r3*((-2)*cw*cx+2*cy*cz) + p.y; z = r1*((-2)*cw*cy + 2*cx*cz) + r2*(2*cw*cx+2*cy*cz) + r3*(1-2*(cx*cx + cy*cy)) + p.z; } ///////////////////////////////////////////////////////////////////////////////////////// //set the coordinates of this point with those of argument point p public function setP(p:Point3D):Void { x = p.x; y = p.y; z = p.z; } ///////////////////////////////////////////////////////////////////////////////////////// //set the coordinates of this point with the triplet (x0,y0,z0) public function set(x0:Number, y0:Number, z0:Number):Void { x = x0; y = y0; z = z0; } ///////////////////////////////////////////////////////////////////////////////////////// //return the euclidian norm of this 3D vector public function norm():Number { return Math.sqrt(x*x + y*y + z*z); } ///////////////////////////////////////////////////////////////////////////////////////// //return the euclidian distance between this point and point p public function dist(p:Point3D):Number { var dx:Number = x-p.x; var dy:Number = y-p.y; var dz:Number = z-p.z; return Math.sqrt(dx*dx + dy*dy + dz*dz); } ///////////////////////////////////////////////////////////////////////////////////////// }