Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

quaternion.inl

Go to the documentation of this file.
00001 /*
00002     Sheep - A Rigid Body Dynamics Engine
00003     Copyright (C) 2001-2004 Francois Beaune
00004     Contact: http://toxicengine.sourceforge.net/
00005 
00006     This file is part of Sheep.
00007 
00008     Sheep is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     Sheep is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with Sheep; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 inline
00024 Quaternion::Quaternion() {}
00025 
00026 inline
00027 Quaternion::Quaternion(const Quaternion &q) :
00028     m_s(q.m_s), m_v(q.m_v) {}
00029 
00030 inline
00031 Quaternion::Quaternion(Real s, const Vector3 &v) :
00032     m_s(s), m_v(v) {}
00033 
00034 inline
00035 Quaternion Quaternion::Identity() {
00036     return Quaternion(1.0, 0.0);
00037 }
00038 
00039 inline
00040 Quaternion Quaternion::Rotation(Real angle, const Vector3 &axis) {
00041     const Real half_angle = 0.5 * angle;
00042 
00043     //!\todo Assume that axis is already a normalized vector.
00044     return Quaternion(
00045         cos(half_angle),
00046         sin(half_angle) * axis.Normalized());
00047 }
00048 
00049 inline
00050 Quaternion Quaternion::Rotation(const Vector3 &from, const Vector3 &to) {
00051     return Quaternion(from * to, from ^ to);
00052 }
00053 
00054 inline
00055 Quaternion &Quaternion::operator=(const Quaternion &q) {
00056     m_s = q.m_s;
00057     m_v = q.m_v;
00058 
00059     return *this;
00060 }
00061 
00062 inline
00063 Quaternion Quaternion::operator+(const Quaternion &q) const {
00064     return Quaternion(m_s + q.m_s, m_v + q.m_v);
00065 }
00066 
00067 inline
00068 Quaternion Quaternion::operator-(const Quaternion &q) const {
00069     return Quaternion(m_s - q.m_s, m_v - q.m_v);
00070 }
00071 
00072 inline
00073 Quaternion Quaternion::operator-() const {
00074     return Quaternion(-m_s, -m_v);
00075 }
00076 
00077 inline
00078 Quaternion Quaternion::operator*(Real r) const {
00079     return Quaternion(m_s * r, m_v * r);
00080 }
00081 
00082 inline
00083 Quaternion Quaternion::operator/(Real r) const {
00084     const Real inv_r = 1.0 / r;
00085 
00086     return Quaternion(m_s * inv_r, m_v * inv_r);
00087 }
00088 
00089 inline
00090 Quaternion &Quaternion::operator+=(const Quaternion &q) {
00091     m_s += q.m_s;
00092     m_v += q.m_v;
00093 
00094     return *this;
00095 }
00096 
00097 inline
00098 Quaternion &Quaternion::operator-=(const Quaternion &q) {
00099     m_s -= q.m_s;
00100     m_v -= q.m_v;
00101 
00102     return *this;
00103 }
00104 
00105 inline
00106 Quaternion &Quaternion::operator*=(Real r) {
00107     m_s *= r;
00108     m_v *= r;
00109 
00110     return *this;
00111 }
00112 
00113 inline
00114 Quaternion &Quaternion::operator/=(Real r) {
00115     Real inv_r = 1.0 / r;
00116 
00117     m_s *= inv_r;
00118     m_v *= inv_r;
00119 
00120     return *this;
00121 }
00122 
00123 inline
00124 Quaternion Quaternion::operator*(const Quaternion &q) const {
00125     return Quaternion(
00126         m_s * q.m_s - m_v * q.m_v,
00127         m_s * q.m_v + q.m_s * m_v + (m_v ^ q.m_v));
00128 }
00129 
00130 inline
00131 Quaternion &Quaternion::operator*=(const Quaternion &q) {
00132     const Real s1 = m_s * q.m_s - m_v * q.m_v;
00133 
00134     m_v = m_s * q.m_v + q.m_s * m_v + (m_v ^ q.m_v);
00135     m_s = s1;
00136 
00137     return *this;
00138 }
00139 
00140 inline
00141 Quaternion Quaternion::Conjugate() const {
00142     return Quaternion(m_s, -m_v);
00143 }
00144 
00145 inline
00146 Quaternion Quaternion::Inverse() const {
00147     return Conjugate() / SquareNorm();
00148 }
00149 
00150 inline
00151 Real Quaternion::SquareNorm() const {
00152     return m_s * m_s + m_v * m_v;
00153 }
00154 
00155 inline
00156 Real Quaternion::Norm() const {
00157     return sqrt(SquareNorm());
00158 }
00159 
00160 inline
00161 Quaternion Quaternion::Normalized() const {
00162     const Real n = Norm();
00163     return n == 0.0 ? Identity() : *this / n;
00164 }
00165 
00166 inline
00167 Quaternion operator*(Real r, const Quaternion &q) {
00168     return q * r;
00169 }

Generated on Tue May 11 01:31:51 2004 for toxic by doxygen 1.3.6