/usr/include/sofa/helper/Quater.h is in libsofa1-dev 1.0~beta4-12.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | /******************************************************************************
* SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4 *
* (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
*******************************************************************************
* SOFA :: Framework *
* *
* Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
* H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza, *
* M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#ifndef SOFA_HELPER_QUATER_H
#define SOFA_HELPER_QUATER_H
#include <sofa/defaulttype/Vec.h>
#include <sofa/defaulttype/Mat.h>
#include <math.h>
#include <assert.h>
#include <iostream>
#include <sofa/helper/helper.h>
namespace sofa
{
namespace helper
{
template<class Real>
class SOFA_HELPER_API Quater
{
private:
Real _q[4];
public:
Quater();
virtual ~Quater();
Quater(Real x, Real y, Real z, Real w);
template<class Real2>
Quater(const Real2 q[]) { for (int i=0; i<4; i++) _q[i] = (Real)q[i]; }
template<class Real2>
Quater(const Quater<Real2>& q) { for (int i=0; i<4; i++) _q[i] = (Real)q[i]; }
Quater( const defaulttype::Vec<3,Real>& axis, Real angle );
static Quater identity() {
return Quater(0,0,0,1);
}
/// Cast into a standard C array of elements.
const Real* ptr() const
{
return this->_q;
}
/// Cast into a standard C array of elements.
Real* ptr()
{
return this->_q;
}
/// Normalize a quaternion
void normalize();
void clear()
{
_q[0]=0.0;
_q[1]=0.0;
_q[2]=0.0;
_q[3]=1.0;
}
void fromMatrix(const defaulttype::Matrix3 &m);
template<class Mat33>
void toMatrix(Mat33 &m) const
{
m[0][0] = (typename Mat33::Real) (1.0f - 2.0f * (_q[1] * _q[1] + _q[2] * _q[2]));
m[0][1] = (typename Mat33::Real) (2.0f * (_q[0] * _q[1] - _q[2] * _q[3]));
m[0][2] = (typename Mat33::Real) (2.0f * (_q[2] * _q[0] + _q[1] * _q[3]));
m[1][0] = (typename Mat33::Real) (2.0f * (_q[0] * _q[1] + _q[2] * _q[3]));
m[1][1] = (typename Mat33::Real) (1.0f - 2.0f * (_q[2] * _q[2] + _q[0] * _q[0]));
m[1][2] = (typename Mat33::Real) (2.0f * (_q[1] * _q[2] - _q[0] * _q[3]));
m[2][0] = (typename Mat33::Real) (2.0f * (_q[2] * _q[0] - _q[1] * _q[3]));
m[2][1] = (typename Mat33::Real) (2.0f * (_q[1] * _q[2] + _q[0] * _q[3]));
m[2][2] = (typename Mat33::Real) (1.0f - 2.0f * (_q[1] * _q[1] + _q[0] * _q[0]));
}
/// Apply the rotation to a given vector
template<class Vec>
Vec rotate( const Vec& v ) const
{
return Vec(
(typename Vec::value_type)((1.0f - 2.0f * (_q[1] * _q[1] + _q[2] * _q[2]))*v[0] + (2.0f * (_q[0] * _q[1] - _q[2] * _q[3])) * v[1] + (2.0f * (_q[2] * _q[0] + _q[1] * _q[3])) * v[2]),
(typename Vec::value_type)((2.0f * (_q[0] * _q[1] + _q[2] * _q[3]))*v[0] + (1.0f - 2.0f * (_q[2] * _q[2] + _q[0] * _q[0]))*v[1] + (2.0f * (_q[1] * _q[2] - _q[0] * _q[3]))*v[2]),
(typename Vec::value_type)((2.0f * (_q[2] * _q[0] - _q[1] * _q[3]))*v[0] + (2.0f * (_q[1] * _q[2] + _q[0] * _q[3]))*v[1] + (1.0f - 2.0f * (_q[1] * _q[1] + _q[0] * _q[0]))*v[2])
);
}
/// Apply the inverse rotation to a given vector
template<class Vec>
Vec inverseRotate( const Vec& v ) const
{
return Vec(
(typename Vec::value_type)((1.0f - 2.0f * (_q[1] * _q[1] + _q[2] * _q[2]))*v[0] + (2.0f * (_q[0] * _q[1] + _q[2] * _q[3])) * v[1] + (2.0f * (_q[2] * _q[0] - _q[1] * _q[3])) * v[2]),
(typename Vec::value_type)((2.0f * (_q[0] * _q[1] - _q[2] * _q[3]))*v[0] + (1.0f - 2.0f * (_q[2] * _q[2] + _q[0] * _q[0]))*v[1] + (2.0f * (_q[1] * _q[2] + _q[0] * _q[3]))*v[2]),
(typename Vec::value_type)((2.0f * (_q[2] * _q[0] + _q[1] * _q[3]))*v[0] + (2.0f * (_q[1] * _q[2] - _q[0] * _q[3]))*v[1] + (1.0f - 2.0f * (_q[1] * _q[1] + _q[0] * _q[0]))*v[2])
);
}
/// Given two quaternions, add them together to get a third quaternion.
/// Adding quaternions to get a compound rotation is analagous to adding
/// translations to get a compound translation.
//template <class T>
//friend Quater<T> operator+(Quater<T> q1, Quater<T> q2);
Quater<Real> operator+(const Quater<Real> &q1) const;
Quater<Real> operator*(const Quater<Real> &q1) const;
Quater<Real> operator*(const Real &r) const;
Quater<Real> operator/(const Real &r) const;
void operator*=(const Real &r);
void operator/=(const Real &r);
/// Given two Quaters, multiply them together to get a third quaternion.
//template <class T>
//friend Quater<T> operator*(const Quater<T>& q1, const Quater<T>& q2);
Quater quatVectMult(const defaulttype::Vec<3,Real>& vect);
Quater vectQuatMult(const defaulttype::Vec<3,Real>& vect);
Real& operator[](int index)
{
assert(index >= 0 && index < 4);
return _q[index];
}
const Real& operator[](int index) const
{
assert(index >= 0 && index < 4);
return _q[index];
}
Quater inverse() const;
defaulttype::Vec<3,Real> toEulerVector() const;
// A useful function, builds a rotation matrix in Matrix based on
// given quaternion.
void buildRotationMatrix(Real m[4][4]) const;
void writeOpenGlMatrix( double* m ) const;
void writeOpenGlMatrix( float* m ) const;
//void buildRotationMatrix(MATRIX4x4 m);
//void buildRotationMatrix(Matrix &m);
// This function computes a quaternion based on an axis (defined by
// the given vector) and an angle about which to rotate. The angle is
// expressed in radians.
Quater axisToQuat(defaulttype::Vec<3,Real> a, Real phi);
/// Create using rotation vector (axis*angle) given in parent coordinates
template<class V>
static Quater createFromRotationVector(const V& a)
{
Real phi = (Real)sqrt(a*a);
if( phi < 1.0e-5 )
return Quater(0,0,0,1);
else {
Real nor = 1/phi;
Real s = (Real)sin(phi/2);
return Quater( a[0]*s*nor, a[1]*s*nor,a[2]*s*nor, (Real)cos(phi/2) );
}
}
/// Create a quaternion from Euler
static Quater createQuaterFromEuler( defaulttype::Vec<3,Real> v) {
Real quat[4]; Real a0 = v.elems[0];
Real a1 = v.elems[1];
Real a2 = v.elems[2];
quat[3] = cos(a0/2)*cos(a1/2)*cos(a2/2) + sin(a0/2)*sin(a1/2)*sin(a2/2);
quat[0] = sin(a0/2)*cos(a1/2)*cos(a2/2) - cos(a0/2)*sin(a1/2)*sin(a2/2);
quat[1] = cos(a0/2)*sin(a1/2)*cos(a2/2) + sin(a0/2)*cos(a1/2)*sin(a2/2);
quat[2] = cos(a0/2)*cos(a1/2)*sin(a2/2) - sin(a0/2)*sin(a1/2)*cos(a2/2);
Quater quatResult( quat[0], quat[1], quat[2], quat[3] );
return quatResult;
}
/// Create using the entries of a rotation vector (axis*angle) given in parent coordinates
template<class T>
static Quater createFromRotationVector(T a0, T a1, T a2 )
{
Real phi = (Real)sqrt((Real)(a0*a0+a1*a1+a2*a2));
if( phi < 1.0e-5 )
return Quater(0,0,0,1);
else {
Real nor = 1/phi;
Real s = (Real)sin(phi/2.0);
return Quater( a0*s*nor, a1*s*nor,a2*s*nor, (Real)cos(phi/2.0) );
}
}
/// Create using rotation vector (axis*angle) given in parent coordinates
template<class V>
static Quater set(const V& a){ return createFromRotationVector(a); }
/// Create using using the entries of a rotation vector (axis*angle) given in parent coordinates
template<class T>
static Quater set(T a0, T a1, T a2){ return createFromRotationVector(a0,a1,a2); }
// Print the quaternion
// inline friend std::ostream& operator<<(std::ostream& out, Quater Q)
// {
// return (out << "(" << Q._q[0] << "," << Q._q[1] << "," << Q._q[2] << ","
// << Q._q[3] << ")");
// }
// Print the quaternion (C style)
void print();
void operator+=(const Quater& q2);
void operator*=(const Quater& q2);
bool operator==(const Quater& q) const
{
for (int i=0;i<4;i++)
if ( fabs( _q[i] - q._q[i] ) > EQUALITY_THRESHOLD ) return false;
return true;
}
bool operator!=(const Quater& q) const
{
for (int i=0;i<4;i++)
if ( fabs( _q[i] - q._q[i] ) > EQUALITY_THRESHOLD ) return true;
return false;
}
/// write to an output stream
inline friend std::ostream& operator << ( std::ostream& out, const Quater& v ){
out<<v._q[0]<<" "<<v._q[1]<<" "<<v._q[2]<<" "<<v._q[3];
return out;
}
/// read from an input stream
inline friend std::istream& operator >> ( std::istream& in, Quater& v ){
in>>v._q[0]>>v._q[1]>>v._q[2]>>v._q[3];
return in;
}
static unsigned int size(){return 4;};
};
//typedef Quater<double> Quat; ///< alias
//typedef Quater<float> Quatf; ///< alias
//typedef Quater<double> Quaternion; ///< alias
} // namespace helper
} // namespace sofa
#endif
|