Newer
Older
#ifndef QUATERNION_HH
#define QUATERNION_HH
#include <dune/common/fvector.hh>
#include <dune/common/exceptions.hh>
template <class T>
class Quaternion : public Dune::FieldVector<T,4>
{
public:
/** \brief Default constructor */
/** \brief Copy constructor */
Quaternion(const Dune::FieldVector<T,4>& other) : Dune::FieldVector<T,4>(other) {}
/** \brief Constructor with rotation axis and angle */
Quaternion(Dune::FieldVector<T,3> axis, T angle) {
axis /= axis.two_norm();
axis *= std::sin(angle/2);
(*this)[0] = axis[0];
(*this)[1] = axis[1];
(*this)[2] = axis[2];
(*this)[3] = std::cos(angle/2);
}
/** \brief Return the identity element */
static Quaternion<T> identity() {
Quaternion<T> id;
id[0] = 0;
id[1] = 0;
id[2] = 0;
id[3] = 1;
return id;
}
/** \brief The exponential map from \f$ \mathfrak{so}(3) \f$ to \f$ SO(3) \f$
*/
static Quaternion<T> exp(const T& v0, const T& v1, const T& v2) {
Quaternion<T> q;
T normV = std::sqrt(v0*v0 + v1*v1 + v2*v2);
// Stabilization for small |v| due Grassia
T sin = (normV < 1e-4) ? 0.5 * (normV*normV/48) : std::sin(normV/2)/normV;
q[0] = sin * v0;
q[1] = sin * v1;
q[2] = sin * v2;
q[3] = std::cos(normV/2);
return q;
}
/** \brief Right quaternion multiplication */
Quaternion<T> mult(const Quaternion<T>& other) const {
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
Quaternion<T> q;
q[0] = (*this)[3]*other[0] - (*this)[2]*other[1] + (*this)[1]*other[2] + (*this)[0]*other[3];
q[1] = (*this)[2]*other[0] + (*this)[3]*other[1] - (*this)[0]*other[2] + (*this)[1]*other[3];
q[2] = - (*this)[1]*other[0] + (*this)[0]*other[1] + (*this)[3]*other[2] + (*this)[2]*other[3];
q[3] = - (*this)[0]*other[0] - (*this)[1]*other[1] - (*this)[2]*other[2] + (*this)[3]*other[3];
return q;
}
/** \brief Return the tripel of director vectors represented by a unit quaternion
The formulas are taken from Dichmann, Li, Maddocks, (2.6.4), (2.6.5), (2.6.6)
*/
Dune::FieldVector<T,3> director(int i) const {
Dune::FieldVector<T,3> d;
const Dune::FieldVector<T,4>& q = *this; // simpler notation
if (i==0) {
d[0] = q[0]*q[0] - q[1]*q[1] - q[2]*q[2] + q[3]*q[3];
d[1] = 2 * (q[0]*q[1] + q[2]*q[3]);
d[2] = 2 * (q[0]*q[2] - q[1]*q[3]);
} else if (i==1) {
d[0] = 2 * (q[0]*q[1] - q[2]*q[3]);
d[1] = -q[0]*q[0] + q[1]*q[1] - q[2]*q[2] + q[3]*q[3];
d[2] = 2 * (q[1]*q[2] + q[0]*q[3]);
} else if (i==2) {
d[0] = 2 * (q[0]*q[2] + q[1]*q[3]);
d[1] = 2 * (q[1]*q[2] - q[0]*q[3]);
d[2] = -q[0]*q[0] - q[1]*q[1] + q[2]*q[2] + q[3]*q[3];
} else
DUNE_THROW(Dune::Exception, "Nonexisting director " << i << " requested!");
return d;
}
/** \brief Turn quaternion into a unit quaternion by dividing by its Euclidean norm */
void normalize() {
(*this) /= this->two_norm();
}
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
Dune::FieldVector<double,3> rotate(const Dune::FieldVector<double,3>& v) const {
Dune::FieldVector<double,3> result;
Dune::FieldVector<double,3> d0 = director(0);
Dune::FieldVector<double,3> d1 = director(1);
Dune::FieldVector<double,3> d2 = director(2);
for (int i=0; i<3; i++)
result[i] = v[0]*d0[i] + v[1]*d1[i] + v[2]*d2[i];
return result;
}
/** \brief Interpolate between two rotations */
static Quaternion<T> interpolate(const Quaternion<T>& a, const Quaternion<T>& b, double omega) {
Quaternion<T> result;
for (int i=0; i<4; i++)
result[i] = a[i]*(1-omega) + b[i]*omega;
result.normalize();
return result;
}