MechSys  1.0
Computing library for simulations in continuum and discrete mechanics
/home/dorival/mechsys/lib/dem/quaternion.h
Go to the documentation of this file.
00001 /************************************************************************
00002  * MechSys - Open Library for Mechanical Systems                        *
00003  * Copyright (C) 2005 Dorival M. Pedroso, Raul Durand                   *
00004  * Copyright (C) 2009 Sergio Galindo                                    *
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 3 of the License, or    *
00009  * any later version.                                                   *
00010  *                                                                      *
00011  * This program is distributed in the hope that it will be useful,      *
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         *
00014  * GNU 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, see <http://www.gnu.org/licenses/>  *
00018  ************************************************************************/
00019 
00020 #ifndef MECHSYS_DEM_QUATERNION_H
00021 #define MECHSYS_DEM_QUATERNION_H
00022 
00023 // Std Lib
00024 #include <cmath>
00025 
00026 // Blitz++
00027 #include <blitz/tinyvec-et.h>
00028 #include <blitz/tinymat.h>
00029 
00030 // MechSys
00031 #include <mechsys/linalg/matvec.h>
00032 
00033 typedef blitz::TinyVector<double,4> Quaternion_t;
00034 
00035 inline void NormalizeRotation (double Theta, Vec3_t const & Axis, Quaternion_t & C)
00036 {
00037     Vec3_t A = Axis/norm(Axis);
00038     C(0)     = cos(Theta/2.0);
00039     C(1)     = A(0)*sin(Theta/2.0);
00040     C(2)     = A(1)*sin(Theta/2.0);
00041     C(3)     = A(2)*sin(Theta/2.0);
00042 }
00043 
00044 inline void Conjugate (Quaternion_t const & A, Quaternion_t & C)
00045 {
00046     C(0) =  A(0);
00047     C(1) = -A(1);
00048     C(2) = -A(2);
00049     C(3) = -A(3);
00050 }
00051 
00052 inline void GetVector (Quaternion_t const & A, Vec3_t & C)
00053 {
00054     C(0) = A(1);
00055     C(1) = A(2);
00056     C(2) = A(3);
00057 }
00058 
00059 inline void SetQuaternion (double Scalar, Vec3_t const & A, Quaternion_t & C)
00060 {
00061     C(0) = Scalar;
00062     C(1) = A(0);
00063     C(2) = A(1);
00064     C(3) = A(2);
00065 }
00066 
00067 inline void QuaternionProduct (Quaternion_t const & A, Quaternion_t const & B, Quaternion_t & C)
00068 {
00069     Vec3_t t1,t2;
00070     GetVector (A,t1);
00071     GetVector (B,t2);
00072     double scalar = A(0)*B(0) - dot(t1,t2);
00073     Vec3_t vector = A(0)*t2 + B(0)*t1 + cross(t1,t2);
00074     SetQuaternion (scalar,vector,C);
00075 }
00076 
00077 inline void Rotation (Vec3_t const & A, Quaternion_t const & B, Vec3_t & C)
00078 {
00079     Quaternion_t t1,t2,t3;
00080     SetQuaternion     (0.0,A,t1);
00081     QuaternionProduct (B,t1,t2);
00082     Conjugate         (B,t3);
00083     QuaternionProduct (t2,t3,t1);
00084     GetVector         (t1,C);
00085 }
00086 
00087 #endif // MECHSYS_DEM_QUATERNION_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines