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

base/Quat4.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)1996 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: Quat4.cpp 1029 2004-02-11 20:45:54Z jungd $
00019   $Revision: 1.7 $
00020   $Date: 2004-02-11 15:45:54 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #include <base/consts>
00026 #include <base/Quat4>
00027 #include <base/Matrix4>
00028 #include <base/Math>
00029 #include <base/Serializer>
00030 
00031 
00032 using base::Quat4;
00033 using base::Matrix4;
00034 using base::Vector3;
00035 using base::Point4;
00036 using base::Math;
00037 
00038 
00039 
00040 Quat4& Quat4::operator*=(const Quat4& qr)
00041 {
00042   Quat4 ql(*this);
00043 
00044   w   = ql.w*qr.w   - ql.v.x*qr.v.x - ql.v.y*qr.v.y - ql.v.z*qr.v.z;
00045   v.x = ql.w*qr.v.x + ql.v.x*qr.w   + ql.v.y*qr.v.z - ql.v.z*qr.v.y;
00046   v.y = ql.w*qr.v.y + ql.v.y*qr.w   + ql.v.z*qr.v.x - ql.v.x*qr.v.z;
00047   v.z = ql.w*qr.v.z + ql.v.z*qr.w   + ql.v.x*qr.v.y - ql.v.y*qr.v.x;
00048 
00049   return *this;
00050 }
00051 
00052 void Quat4::setRotation(const Vector3& axis, Real angle)
00053 {
00054   Vector3 a(axis);
00055   a.normalize();
00056   v=a*sin(angle/2.0);
00057   w=cos(angle/2.0);
00058 }
00059 
00060 
00061 void Quat4::setRotation(const Matrix4& rotation)
00062 {
00063   // Algorithm is from the Matrix and Quaternion FAQ
00064   //  ( currently http://www.cs.ualberta.ca/~andreas/math/matrfaq_latest.html )
00065   const Matrix4& m = rotation;
00066   Quat4& q = *this;
00067   
00068   Real tr = 1 + m.e(1,1) + m.e(2,2) + m.e(3,3);
00069   if (tr > consts::epsilon) {
00070     Real s = sqrt(tr)*2.0;
00071     q.v.x = (m.e(3,2) - m.e(2,3))/s;
00072     q.v.y = (m.e(1,3) - m.e(3,1))/s;
00073     q.v.z = (m.e(2,1) - m.e(1,2))/s;
00074     q.w = 0.25*s;
00075   }     
00076   else {
00077 
00078     if ((m.e(1,1) > m.e(2,2)) && (m.e(1,1) > m.e(3,3))) {
00079       Real s = sqrt(1+m.e(1,1)-m.e(2,2)-m.e(3,3))*2.0;
00080       q.v.x = 0.25*s;
00081       q.v.y = (m.e(2,1)+m.e(1,2))/s;
00082       q.v.z = (m.e(1,3)+m.e(3,1))/s;
00083       q.w   = (m.e(3,2)-m.e(2,3))/s;
00084     }
00085     else if (m.e(2,2) > m.e(3,3)) {
00086       Real s = sqrt(1+m.e(2,2)-m.e(1,1)-m.e(3,3))*2.0;
00087       q.v.x = (m.e(2,1)+m.e(1,2))/s;
00088       q.v.y = 0.25*s;
00089       q.v.z = (m.e(3,2)+m.e(2,3))/s;
00090       q.w   = (m.e(1,3)-m.e(3,1))/s;
00091     }
00092     else {
00093       Real s = sqrt(1+m.e(3,3)-m.e(1,1)-m.e(2,2))*2.0;
00094       q.v.x = (m.e(1,3)+m.e(3,1))/s;
00095       q.v.y = (m.e(3,2)+m.e(2,3))/s;
00096       q.v.z = 0.25*s;
00097       q.w   = (m.e(2,1)-m.e(1,2))/s;
00098     }
00099   }
00100         
00101   //normalize();
00102 }
00103 
00104 
00105 void Quat4::getRotation(Vector3& axis, Real& angle) const
00106 {
00107   angle = 2.0*acos(w);   /// !!!! this is just a guess, is it correct? (seems to work)
00108   if (!base::equals(angle,0))
00109     axis = v / sin(angle/2.0);
00110   else
00111     axis = Vector3(0,0,1);
00112 
00113   if (base::equals(axis.norm(),0))
00114     angle = 0;
00115 }
00116 
00117 
00118 void Quat4::rotatePoint(Point4& p) const
00119 {
00120   const Quat4& q = *this;
00121   Quat4 r( (q * Quat4(p)) * inverse(q) );
00122   p=Point4(r.v.x,r.v.y,r.v.z,r.w);
00123 }
00124 
00125 
00126 /// Spherical Linear Interpolation
00127 /// As t goes from 0 to 1, the Quat4 object goes from "from" to "to"
00128 /// Reference: Shoemake at SIGGRAPH 89
00129 /// See also
00130 /// http://www.gamasutra.com/features/programming/19980703/quaternions_01.htm
00131 Quat4 Quat4::interpolate(const Quat4& from, const Quat4& to, Real t)
00132 {
00133   const Real linearTolerance = 1e-3;
00134 
00135   //  lifted from OSG code
00136   Real omega, cosomega, sinomega, scale_from, scale_to;
00137 
00138   Quat4 quatTo(to);
00139 
00140   // this is a dot product
00141   cosomega = from.v.dot(to.v)+(from.w*to.w);
00142 
00143   if ( cosomega <0.0 ) {
00144     cosomega = -cosomega;
00145     quatTo.negate();
00146   }
00147 
00148   if( (1.0 - cosomega) > linearTolerance ) {
00149     omega= Math::acos(cosomega) ;  // 0 <= omega <= Pi (see man acos)
00150     sinomega = Math::sin(omega) ;  // this sinomega should always be +ve so
00151     // could try sinomega=sqrt(1-cosomega*cosomega) to avoid a sin()?
00152     scale_from = Math::sin((1.0-t)*omega)/sinomega ;
00153     scale_to = Math::sin(t*omega)/sinomega ;
00154   }
00155   else {
00156     // The ends of the vectors are very close
00157     //  we can use simple linear interpolation - no need
00158     //   to worry about the "spherical" interpolation
00159     scale_from = 1.0 - t ;
00160     scale_to = t ;
00161   }
00162   
00163   Quat4 ret;
00164   ret.v = (from.v*scale_from) + (quatTo.v*scale_to);
00165   ret.w = (from.w*scale_from) + (quatTo.w*scale_to);
00166   return ret;
00167 }
00168 
00169 
00170 Real Quat4::angleBetween(const Quat4& q1, const Quat4& q2)
00171 {
00172   // we calculate the angle a round-about way - there
00173   //  is probably a better one
00174   // transform a unit x-axis vector by each Quat and then use the dot product to get
00175   // and angle between them
00176   Vector3 xaxis(Vector3(1,0,0));  
00177   Vector3 vector1(q1.rotate(xaxis));
00178   Vector3 vector2(q2.rotate(xaxis));
00179   return Math::acos(dot(vector1,vector2));
00180 }
00181 
00182         
00183 
00184 Quat4::operator Matrix4() const
00185 {
00186   Real nq = norm();
00187   Real s = (nq> Real(0))? (Real(2)/base::sqrt(nq)):Real(0);
00188 
00189   Real xs = v.x*s, ys = v.y*s, zs = v.z*s;
00190   Real wx = w*xs,  wy = w*ys,  wz = w*zs;
00191   Real xx = v.x*xs,xy = v.x*ys,xz = v.x*zs;
00192   Real yy = v.y*ys,yz = v.y*zs,zz = v.z*zs;
00193 
00194   Matrix4 m; // identity
00195         
00196   m.e(1,1) = Real(1) - (yy+zz);  m.e(2,1) = xy+wz;             m.e(3,1) = xz-wy;
00197   m.e(1,2) = xy - wz;            m.e(2,2) = Real(1) - (xx+zz); m.e(3,2) = yz+wx;
00198   m.e(1,3) = xz+wy;              m.e(2,3) = yz-wx;             m.e(3,3) = Real(1) - (xx+yy);
00199 
00200   return m;
00201 }
00202 
00203 
00204 void Quat4::serialize(Serializer& s)
00205 {
00206   s(v,"v"); s(w,"w");
00207 }
00208 
00209 
00210 
00211 // output
00212 std::ostream& base::operator<<(std::ostream& out, const Quat4& q)
00213 {
00214   return out << "([" << q.v.x << "," << q.v.y << "," << q.v.z << "]," << q.w << ")";
00215 }

Generated on Thu Jul 29 15:56:11 2004 for OpenSim by doxygen 1.3.6