--- trunk/OOPSE-4/src/math/Quaternion.hpp 2005/04/15 22:03:16 2203 +++ trunk/OOPSE-4/src/math/Quaternion.hpp 2005/04/15 22:04:00 2204 @@ -1,4 +1,4 @@ - /* +/* * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. * * The University of Notre Dame grants you ("Licensee") a @@ -54,234 +54,234 @@ namespace oopse{ namespace oopse{ - /** - * @class Quaternion Quaternion.hpp "math/Quaternion.hpp" - * Quaternion is a sort of a higher-level complex number. - * It is defined as Q = w + x*i + y*j + z*k, - * where w, x, y, and z are numbers of type T (e.g. double), and - * i*i = -1; j*j = -1; k*k = -1; - * i*j = k; j*k = i; k*i = j; - */ - template - class Quaternion : public Vector { - public: - Quaternion() : Vector() {} + /** + * @class Quaternion Quaternion.hpp "math/Quaternion.hpp" + * Quaternion is a sort of a higher-level complex number. + * It is defined as Q = w + x*i + y*j + z*k, + * where w, x, y, and z are numbers of type T (e.g. double), and + * i*i = -1; j*j = -1; k*k = -1; + * i*j = k; j*k = i; k*i = j; + */ + template + class Quaternion : public Vector { + public: + Quaternion() : Vector() {} - /** Constructs and initializes a Quaternion from w, x, y, z values */ - Quaternion(Real w, Real x, Real y, Real z) { - this->data_[0] = w; - this->data_[1] = x; - this->data_[2] = y; - this->data_[3] = z; - } + /** Constructs and initializes a Quaternion from w, x, y, z values */ + Quaternion(Real w, Real x, Real y, Real z) { + this->data_[0] = w; + this->data_[1] = x; + this->data_[2] = y; + this->data_[3] = z; + } - /** Constructs and initializes a Quaternion from a Vector */ - Quaternion(const Vector& v) - : Vector(v){ - } + /** Constructs and initializes a Quaternion from a Vector */ + Quaternion(const Vector& v) + : Vector(v){ + } - /** copy assignment */ - Quaternion& operator =(const Vector& v){ - if (this == & v) - return *this; + /** copy assignment */ + Quaternion& operator =(const Vector& v){ + if (this == & v) + return *this; - Vector::operator=(v); + Vector::operator=(v); - return *this; - } + return *this; + } - /** - * Returns the value of the first element of this quaternion. - * @return the value of the first element of this quaternion - */ - Real w() const { - return this->data_[0]; - } + /** + * Returns the value of the first element of this quaternion. + * @return the value of the first element of this quaternion + */ + Real w() const { + return this->data_[0]; + } - /** - * Returns the reference of the first element of this quaternion. - * @return the reference of the first element of this quaternion - */ - Real& w() { - return this->data_[0]; - } + /** + * Returns the reference of the first element of this quaternion. + * @return the reference of the first element of this quaternion + */ + Real& w() { + return this->data_[0]; + } - /** - * Returns the value of the first element of this quaternion. - * @return the value of the first element of this quaternion - */ - Real x() const { - return this->data_[1]; - } + /** + * Returns the value of the first element of this quaternion. + * @return the value of the first element of this quaternion + */ + Real x() const { + return this->data_[1]; + } - /** - * Returns the reference of the second element of this quaternion. - * @return the reference of the second element of this quaternion - */ - Real& x() { - return this->data_[1]; - } + /** + * Returns the reference of the second element of this quaternion. + * @return the reference of the second element of this quaternion + */ + Real& x() { + return this->data_[1]; + } - /** - * Returns the value of the thirf element of this quaternion. - * @return the value of the third element of this quaternion - */ - Real y() const { - return this->data_[2]; - } + /** + * Returns the value of the thirf element of this quaternion. + * @return the value of the third element of this quaternion + */ + Real y() const { + return this->data_[2]; + } - /** - * Returns the reference of the third element of this quaternion. - * @return the reference of the third element of this quaternion - */ - Real& y() { - return this->data_[2]; - } + /** + * Returns the reference of the third element of this quaternion. + * @return the reference of the third element of this quaternion + */ + Real& y() { + return this->data_[2]; + } - /** - * Returns the value of the fourth element of this quaternion. - * @return the value of the fourth element of this quaternion - */ - Real z() const { - return this->data_[3]; - } - /** - * Returns the reference of the fourth element of this quaternion. - * @return the reference of the fourth element of this quaternion - */ - Real& z() { - return this->data_[3]; - } + /** + * Returns the value of the fourth element of this quaternion. + * @return the value of the fourth element of this quaternion + */ + Real z() const { + return this->data_[3]; + } + /** + * Returns the reference of the fourth element of this quaternion. + * @return the reference of the fourth element of this quaternion + */ + Real& z() { + return this->data_[3]; + } - /** - * Tests if this quaternion is equal to other quaternion - * @return true if equal, otherwise return false - * @param q quaternion to be compared - */ - inline bool operator ==(const Quaternion& q) { + /** + * Tests if this quaternion is equal to other quaternion + * @return true if equal, otherwise return false + * @param q quaternion to be compared + */ + inline bool operator ==(const Quaternion& q) { - for (unsigned int i = 0; i < 4; i ++) { - if (!equal(this->data_[i], q[i])) { - return false; - } - } + for (unsigned int i = 0; i < 4; i ++) { + if (!equal(this->data_[i], q[i])) { + return false; + } + } - return true; - } + return true; + } - /** - * Returns the inverse of this quaternion - * @return inverse - * @note since quaternion is a complex number, the inverse of quaternion - * q = w + xi + yj+ zk is inv_q = (w -xi - yj - zk)/(|q|^2) - */ - Quaternion inverse() { - Quaternion q; - Real d = this->lengthSquare(); + /** + * Returns the inverse of this quaternion + * @return inverse + * @note since quaternion is a complex number, the inverse of quaternion + * q = w + xi + yj+ zk is inv_q = (w -xi - yj - zk)/(|q|^2) + */ + Quaternion inverse() { + Quaternion q; + Real d = this->lengthSquare(); - q.w() = w() / d; - q.x() = -x() / d; - q.y() = -y() / d; - q.z() = -z() / d; + q.w() = w() / d; + q.x() = -x() / d; + q.y() = -y() / d; + q.z() = -z() / d; - return q; - } + return q; + } - /** - * Sets the value to the multiplication of itself and another quaternion - * @param q the other quaternion - */ - void mul(const Quaternion& q) { - Quaternion tmp(*this); + /** + * Sets the value to the multiplication of itself and another quaternion + * @param q the other quaternion + */ + void mul(const Quaternion& q) { + Quaternion tmp(*this); - this->data_[0] = (tmp[0]*q[0]) -(tmp[1]*q[1]) - (tmp[2]*q[2]) - (tmp[3]*q[3]); - this->data_[1] = (tmp[0]*q[1]) + (tmp[1]*q[0]) + (tmp[2]*q[3]) - (tmp[3]*q[2]); - this->data_[2] = (tmp[0]*q[2]) + (tmp[2]*q[0]) + (tmp[3]*q[1]) - (tmp[1]*q[3]); - this->data_[3] = (tmp[0]*q[3]) + (tmp[3]*q[0]) + (tmp[1]*q[2]) - (tmp[2]*q[1]); - } + this->data_[0] = (tmp[0]*q[0]) -(tmp[1]*q[1]) - (tmp[2]*q[2]) - (tmp[3]*q[3]); + this->data_[1] = (tmp[0]*q[1]) + (tmp[1]*q[0]) + (tmp[2]*q[3]) - (tmp[3]*q[2]); + this->data_[2] = (tmp[0]*q[2]) + (tmp[2]*q[0]) + (tmp[3]*q[1]) - (tmp[1]*q[3]); + this->data_[3] = (tmp[0]*q[3]) + (tmp[3]*q[0]) + (tmp[1]*q[2]) - (tmp[2]*q[1]); + } - void mul(const Real& s) { - this->data_[0] *= s; - this->data_[1] *= s; - this->data_[2] *= s; - this->data_[3] *= s; - } + void mul(const Real& s) { + this->data_[0] *= s; + this->data_[1] *= s; + this->data_[2] *= s; + this->data_[3] *= s; + } - /** Set the value of this quaternion to the division of itself by another quaternion */ - void div(Quaternion& q) { - mul(q.inverse()); - } - - void div(const Real& s) { - this->data_[0] /= s; - this->data_[1] /= s; - this->data_[2] /= s; - this->data_[3] /= s; - } + /** Set the value of this quaternion to the division of itself by another quaternion */ + void div(Quaternion& q) { + mul(q.inverse()); + } + + void div(const Real& s) { + this->data_[0] /= s; + this->data_[1] /= s; + this->data_[2] /= s; + this->data_[3] /= s; + } - Quaternion& operator *=(const Quaternion& q) { - mul(q); - return *this; - } + Quaternion& operator *=(const Quaternion& q) { + mul(q); + return *this; + } - Quaternion& operator *=(const Real& s) { - mul(s); - return *this; - } + Quaternion& operator *=(const Real& s) { + mul(s); + return *this; + } - Quaternion& operator /=(Quaternion& q) { - *this *= q.inverse(); - return *this; - } + Quaternion& operator /=(Quaternion& q) { + *this *= q.inverse(); + return *this; + } - Quaternion& operator /=(const Real& s) { - div(s); - return *this; - } - /** - * Returns the conjugate quaternion of this quaternion - * @return the conjugate quaternion of this quaternion - */ - Quaternion conjugate() { - return Quaternion(w(), -x(), -y(), -z()); - } + Quaternion& operator /=(const Real& s) { + div(s); + return *this; + } + /** + * Returns the conjugate quaternion of this quaternion + * @return the conjugate quaternion of this quaternion + */ + Quaternion conjugate() { + return Quaternion(w(), -x(), -y(), -z()); + } - /** - * Returns the corresponding rotation matrix (3x3) - * @return a 3x3 rotation matrix - */ - SquareMatrix toRotationMatrix3() { - SquareMatrix rotMat3; + /** + * Returns the corresponding rotation matrix (3x3) + * @return a 3x3 rotation matrix + */ + SquareMatrix toRotationMatrix3() { + SquareMatrix rotMat3; - Real w2; - Real x2; - Real y2; - Real z2; + Real w2; + Real x2; + Real y2; + Real z2; - if (!this->isNormalized()) - this->normalize(); + if (!this->isNormalized()) + this->normalize(); - w2 = w() * w(); - x2 = x() * x(); - y2 = y() * y(); - z2 = z() * z(); + w2 = w() * w(); + x2 = x() * x(); + y2 = y() * y(); + z2 = z() * z(); - rotMat3(0, 0) = w2 + x2 - y2 - z2; - rotMat3(0, 1) = 2.0 * ( x() * y() + w() * z() ); - rotMat3(0, 2) = 2.0 * ( x() * z() - w() * y() ); + rotMat3(0, 0) = w2 + x2 - y2 - z2; + rotMat3(0, 1) = 2.0 * ( x() * y() + w() * z() ); + rotMat3(0, 2) = 2.0 * ( x() * z() - w() * y() ); - rotMat3(1, 0) = 2.0 * ( x() * y() - w() * z() ); - rotMat3(1, 1) = w2 - x2 + y2 - z2; - rotMat3(1, 2) = 2.0 * ( y() * z() + w() * x() ); + rotMat3(1, 0) = 2.0 * ( x() * y() - w() * z() ); + rotMat3(1, 1) = w2 - x2 + y2 - z2; + rotMat3(1, 2) = 2.0 * ( y() * z() + w() * x() ); - rotMat3(2, 0) = 2.0 * ( x() * z() + w() * y() ); - rotMat3(2, 1) = 2.0 * ( y() * z() - w() * x() ); - rotMat3(2, 2) = w2 - x2 -y2 +z2; + rotMat3(2, 0) = 2.0 * ( x() * z() + w() * y() ); + rotMat3(2, 1) = 2.0 * ( y() * z() - w() * x() ); + rotMat3(2, 2) = w2 - x2 -y2 +z2; - return rotMat3; - } + return rotMat3; + } - };//end Quaternion + };//end Quaternion /** @@ -290,71 +290,71 @@ namespace oopse{ * @param q the source quaternion * @param s the scalar value */ - template - Quaternion operator * ( const Quaternion& q, Real s) { - Quaternion result(q); - result.mul(s); - return result; - } + template + Quaternion operator * ( const Quaternion& q, Real s) { + Quaternion result(q); + result.mul(s); + return result; + } - /** - * Returns the vaule of scalar multiplication of this quaterion q (q * s). - * @return the vaule of scalar multiplication of this vector - * @param s the scalar value - * @param q the source quaternion - */ - template - Quaternion operator * ( const Real& s, const Quaternion& q ) { - Quaternion result(q); - result.mul(s); - return result; - } + /** + * Returns the vaule of scalar multiplication of this quaterion q (q * s). + * @return the vaule of scalar multiplication of this vector + * @param s the scalar value + * @param q the source quaternion + */ + template + Quaternion operator * ( const Real& s, const Quaternion& q ) { + Quaternion result(q); + result.mul(s); + return result; + } - /** - * Returns the multiplication of two quaternion - * @return the multiplication of two quaternion - * @param q1 the first quaternion - * @param q2 the second quaternion - */ - template - inline Quaternion operator *(const Quaternion& q1, const Quaternion& q2) { - Quaternion result(q1); - result *= q2; - return result; - } + /** + * Returns the multiplication of two quaternion + * @return the multiplication of two quaternion + * @param q1 the first quaternion + * @param q2 the second quaternion + */ + template + inline Quaternion operator *(const Quaternion& q1, const Quaternion& q2) { + Quaternion result(q1); + result *= q2; + return result; + } - /** - * Returns the division of two quaternion - * @param q1 divisor - * @param q2 dividen - */ + /** + * Returns the division of two quaternion + * @param q1 divisor + * @param q2 dividen + */ - template - inline Quaternion operator /( Quaternion& q1, Quaternion& q2) { - return q1 * q2.inverse(); - } + template + inline Quaternion operator /( Quaternion& q1, Quaternion& q2) { + return q1 * q2.inverse(); + } - /** - * Returns the value of the division of a scalar by a quaternion - * @return the value of the division of a scalar by a quaternion - * @param s scalar - * @param q quaternion - * @note for a quaternion q, 1/q = q.inverse() - */ - template - Quaternion operator /(const Real& s, Quaternion& q) { + /** + * Returns the value of the division of a scalar by a quaternion + * @return the value of the division of a scalar by a quaternion + * @param s scalar + * @param q quaternion + * @note for a quaternion q, 1/q = q.inverse() + */ + template + Quaternion operator /(const Real& s, Quaternion& q) { - Quaternion x; - x = q.inverse(); - x *= s; - return x; - } + Quaternion x; + x = q.inverse(); + x *= s; + return x; + } - template - inline bool operator==(const Quaternion& lhs, const Quaternion& rhs) { - return equal(lhs[0] ,rhs[0]) && equal(lhs[1] , rhs[1]) && equal(lhs[2], rhs[2]) && equal(lhs[3], rhs[3]); - } + template + inline bool operator==(const Quaternion& lhs, const Quaternion& rhs) { + return equal(lhs[0] ,rhs[0]) && equal(lhs[1] , rhs[1]) && equal(lhs[2], rhs[2]) && equal(lhs[3], rhs[3]); + } - typedef Quaternion Quat4d; + typedef Quaternion Quat4d; } #endif //MATH_QUATERNION_HPP