Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

quaternion.cc

Go to the documentation of this file.
00001 /*
00002   svas_server -- virtual World Server of Svas
00003   Copyright (c) 2001, 2002 David Moreno Montero
00004  
00005  
00006   This program is free software; you can redistribute it and/or modify
00007   it under the terms of the GNU General Public License as published by
00008   the Free Software Foundation; either version 2 of the License, or
00009   (at your option) any later version.
00010  
00011   This program is distributed in the hope that it will be useful, but
00012   WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   General Public License for more details.
00015  
00016   You should have received a copy of the GNU General Public License
00017   along with this program; if not, write to the Free Software
00018   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00019   02111-1307, USA.  
00020 
00021 */
00022 
00023 #include "quaternion.h"
00024 #include <stdio.h>
00025 #include <math.h>
00026 #include "log.h"
00027 
00028 inline Quaternion::Quaternion(){
00029   u=x=y=z=0;
00030 }
00031 
00032 inline Quaternion::Quaternion(double _u,double _x,double _y,double _z){
00033   u=_u;  
00034   x=_x; 
00035   y=_y;
00036   z=_z;
00037 }
00038 
00039 inline Quaternion::Quaternion(const Vector3d &_V, double _a){
00040   u=cos(_a/2);
00041   Vector3d V=_V;
00042   V=V.normalized()*sin(_a/2);
00043   x=V.X();
00044   y=V.Y();
00045   z=V.Z();
00046 }
00047 
00048 inline string Quaternion::toString(){
00049   char s[256];
00050   sprintf(s,"[%f,(%f,%f,%f)]",u,x,y,z);
00051   return string(s);
00052 }
00053 
00054 inline double Quaternion::module(){
00055   return sqrt(u*u+x*x+y*y+z*z);
00056 }
00057 
00058 inline Quaternion operator+(const Quaternion &a,const Quaternion &b){
00059   return Quaternion(a.u+b.u, a.x+b.x, a.y+b.y, a.z+b.z);
00060 }
00061 
00062 inline Quaternion Quaternion::operator+=(const Quaternion &a){
00063   u+=a.u;
00064   x+=a.x;
00065   y+=a.y;
00066   z+=a.z;
00067   return *this;
00068 }
00069 
00070 inline Quaternion operator-(const Quaternion &a,const Quaternion &b){
00071   return Quaternion(a.u-b.u, a.x-b.x, a.y-b.y, a.z-b.z);
00072 }
00073 
00074 inline Quaternion Quaternion::operator-=(const Quaternion &a){
00075   u-=a.u;
00076   x-=a.x;
00077   y-=a.y;
00078   z-=a.z;
00079   return *this;
00080 }
00081 
00082 inline Quaternion operator*(const Quaternion &a, double s){
00083   return Quaternion(a.u*s, a.x*s, a.y*s, a.z*s);
00084 }
00085 
00086 inline Quaternion Quaternion::operator*=(double s){
00087   u*=s;
00088   x*=s;
00089   y*=s;
00090   z*=s;
00091   return *this;
00092 }
00093 
00094 inline Quaternion operator/(const Quaternion &a, double s){
00095   return Quaternion(a.u/s, a.x/s, a.y/s, a.z/s);
00096 }
00097 
00098 inline Quaternion Quaternion::operator/=(double s){
00099   u/=s;
00100   x/=s;
00101   y/=s;
00102   z/=s;
00103   return *this;
00104 }
00105 
00106 inline Quaternion operator~(const Quaternion &a){
00107   return Quaternion(a.u, -a.x, -a.y, -a.z);
00108 }
00109 
00110 inline Quaternion operator*(const Quaternion &a, const Quaternion &b){
00111   return Quaternion(a.u*b.u - a.x*b.x - a.y*b.y - a.z*b.z,
00112             a.u*b.x + a.x*b.u + a.y*b.z - a.z*b.y,
00113             a.u*b.y + a.y*b.u + a.z*b.x - a.x*b.z,
00114             a.u*b.z + a.z*b.u + a.x*b.y - a.y*b.x);
00115 }
00116 
00117 inline Quaternion operator*(const Quaternion &q,const Vector3d &v){
00118   return Quaternion( -(q.x*v.x + q.y*v.y + q.z*v.z),
00119              q.u*v.x + q.y*v.z - q.z*v.y,
00120              q.u*v.y + q.z*v.x - q.x*v.z,
00121              q.u*v.z + q.x*v.y - q.y*v.x);
00122 }
00123 
00124 inline Quaternion operator*(const Vector3d &v,const Quaternion &q){
00125   return Quaternion( -(q.x*v.x + q.y*v.y + q.z*v.z),
00126              q.u*v.x + q.z*v.y - q.y*v.z,
00127              q.u*v.y + q.x*v.z - q.z*v.x,
00128              q.u*v.z + q.y*v.x - q.x*v.y);
00129 }
00130 
00131 inline Quaternion Quaternion::operator=(const Vector3d &_v){
00132   Vector3d v(_v);
00133   u=0; 
00134   x=v.X(); y=v.Y(); z=v.Z();
00135   return *this;
00136 }
00137 
00138 inline Vector3d Quaternion::getAxis(void){
00139   return Vector3d(x,y,z);
00140 }
00141 
00142 inline Quaternion Quaternion::rotate(const Vector3d &v){
00143   Quaternion q=(*this)*v*(~(*this));
00144   return q;
00145 }
00146 
00147 /**
00148  * This method rotates the given quaternion over ME.
00149  */
00150 inline Quaternion Quaternion::rotate(const Quaternion &q){
00151   Quaternion rq=(*this)*q*(~(*this));
00152   return rq;
00153 }
00154 
00155 inline Quaternion Quaternion::rotate(const Vector3d &axis, double angle){
00156   log <<"Check this!"<<endl; // FIXME
00157   return Quaternion(axis,angle).rotate(*this);
00158 }
00159 
00160 /**
00161  * Just a rotation
00162  */
00163 inline Vector3d Quaternion::toWorld(const Vector3d &v){
00164   return ((*this)*v*(~(*this))).getAxis();
00165 }
00166 
00167 /**
00168  * Just a rotation vs the conjugate
00169  */
00170 inline Vector3d Quaternion::toBody(const Vector3d &v){
00171   return ((~(*this))*v*(*this)).getAxis();
00172 }

Generated on Mon Jun 17 19:53:44 2002 for Svas Server by doxygen1.2.16