/* * ----------------------------------------------------- * Class: QuaternionClass.as * ----------------------------------------------------- * Version: 1.0 * ----------------------------------------------------- * Latest update: August 1, 2003 * ----------------------------------------------------- * Dependencies: * Vector3DClass.as * Matrix3x3Class.as * ----------------------------------------------------- * Developer: Richard Wright * [wisolutions2002@shaw.ca] * ----------------------------------------------------- * Copyright: (c) 2003, Brandon Williams * [brandon@plotdev.com] * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of this software nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ----------------------------------------------------- * Functions: * 1. Quaternion(w,x,y,z) * Quaternion.prototype. * 2. toString() * 3. reset_components(w,x,y,z) * 4. copy_components(q) * 5. magnitude() * 6. normalize() * 7. multiply(q) * 8. axis_angle(theta,A) * 9. rotation_matrix() * ------------------------------------------------------ * Errata: * copy_components * should get x,y,z thru q.v not q * * magnitude * should get x,y,z thru this.v not this * * multiply * should get x,y,z thru this.v not this and q.v not q * * rotation_matrix * should get w thru this not this.v * ----------------------------------------------------- * Updates may be available at: * http://members.shaw.ca/flashmath101/as/ * ----------------------------------------------------- */ // check if this file has already been included if (!_global._QUATERNIONCLASS_AS) { // create the variable that keeps track of whether or not this file has been included _global._QUATERNIONCLASS_AS = true; // dependencies #include "Vector3DClass.as" #include "Matrix3x3Class.as" // 1. class for quaternion datatype Quaternion = function(w,x,y,z) { // scalar component of quaternion this.w = w; // vector component of quaternion this.v = new Vector3D (x, y, z); }; // 2. make quaternion trace-able Quaternion.prototype.toString = function() { return ("Quaternion = ( "+this.w+" , "+this.v.x+" , "+this.v.y+" , "+this.v.z+" )"); }; // 3. resets the components of the quaternion Quaternion.prototype.reset_components = function(w,x,y,z) { this.w = w; this.v = new Vector3D(x,y,z); }; // 4. copies the components of the quaternion 'q' into this quaternion Quaternion.prototype.copy_components = function(q) { this.w = q.w; this.v = new Vector3D(q.x,q.y,q.z); }; // 5. returns the magnitude of the quaternion Quaternion.prototype.magnitude = function() { return (Math.sqrt (this.w*this.w + this.x*this.x + this.y*this.y + this.z*this.z)); }; // 6. normalizes the quaternion Quaternion.prototype.normalize = function() { var mag = this.magnitude(); this.w /= mag; this.v.scale(1.0/mag); }; // 7. returns the multiplication of this quaternion with 'q' Quaternion.prototype.multiply = function(q) { return (new Quaternion ((this.w*q.w-this.x*q.x-this.y*q.y-this.z*q.z), (this.w*q.x+this.x*q.w+this.y*q.z-this.z*q.y), (this.w*q.y+this.x*q.z+this.y*q.w-this.z*q.x), (this.w*q.z+this.x*q.y+this.y*q.x-this.z*q.w))); }; // 8. sets the elements of the quaternion to represent a //rotation around the unit vector 'A' by an angle of theta Quaternion.prototype.axis_angle = function(theta,A) { this.w = Math.cos(theta/2.0); this.v.copy_components(A); this.v.scale(Math.sin(theta/2.0)); }; // 9. returns the rotation matrix of the quaternion Quaternion.prototype.rotation_matrix = function() { // rotation matrix that will be returned var M = new Matrix3x3(); // values that are repeatedly used and can be pre-calculated to save computations var xx = this.v.x*this.v.x; var xy = this.v.x*this.v.y; var xz = this.v.x*this.v.z; var xw = this.v.x*this.v.w; var yy = this.v.y*this.v.y; var yz = this.v.y*this.v.z; var yw = this.v.y*this.v.w; var zz = this.v.z*this.v.z; var zw = this.v.z*this.v.w; // calculate the elements of the rotation matrix M.e[0][0] = 1-2*(yy+zz); M.e[0][1] = 2*(xy-zw); M.e[0][2] = 2*(xz+yw); M.e[1][0] = 2*(xy+zw); M.e[1][1] = 1-2*(xx+zz); M.e[1][2] = 2*(yz-xw); M.e[2][0] = 2*(xz-yw); M.e[2][1] = 2*(yz+xw); M.e[2][2] = 1-2*(xx+yy); return (M); }; } // closes the 'if' statement that checked if this file had already been included