667 |
khor |
1 |
/* ----------------------------------------------------------------------- *
|
|
|
2 |
* This file is part of GEL, http://www.imm.dtu.dk/GEL
|
|
|
3 |
* Copyright (C) the authors and DTU Informatics
|
|
|
4 |
* For license and list of authors, see ../../doc/intro.pdf
|
|
|
5 |
* ----------------------------------------------------------------------- */
|
|
|
6 |
|
|
|
7 |
/** @file Quatd.h
|
|
|
8 |
* @brief Double based quaternion class
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
#ifndef __CGLA_QUATD_H__
|
|
|
12 |
#define __CGLA_QUATD_H__
|
|
|
13 |
|
|
|
14 |
#include "ArithQuat.h"
|
|
|
15 |
#include "Vec3d.h"
|
|
|
16 |
#include "Vec4d.h"
|
|
|
17 |
#include "Mat3x3d.h"
|
|
|
18 |
#include "Mat4x4d.h"
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
namespace CGLA {
|
|
|
22 |
|
|
|
23 |
/** \brief A float based Quaterinion class.
|
|
|
24 |
|
|
|
25 |
Quaternions are algebraic entities useful for rotation. */
|
|
|
26 |
|
|
|
27 |
class Quatd : public ArithQuat<double,Vec3d,Quatd>
|
|
|
28 |
{
|
|
|
29 |
public:
|
|
|
30 |
Quatd() : ArithQuat<double, Vec3d, Quatd>() {}
|
|
|
31 |
|
|
|
32 |
/// Construct quaternion from vector and scalar
|
|
|
33 |
Quatd(const Vec3d& imaginary, double real = 1.0) : ArithQuat<double, Vec3d, Quatd>(imaginary, real) {}
|
|
|
34 |
|
|
|
35 |
/// Construct quaternion from four scalars
|
|
|
36 |
Quatd(double x, double y, double z, double _qw) : ArithQuat<double, Vec3d, Quatd>(x,y,z,_qw) {}
|
|
|
37 |
|
|
|
38 |
/// Construct quaternion from a 4D vector
|
|
|
39 |
explicit Quatd(const Vec4d& v) : ArithQuat<double, Vec3d, Quatd>(v[0], v[1], v[2], v[3]) {}
|
|
|
40 |
|
|
|
41 |
|
|
|
42 |
/// Get a 3x3 rotation matrix from a quaternion
|
|
|
43 |
Mat3x3d get_Mat3x3d() const
|
|
|
44 |
{
|
|
|
45 |
double s = 2.0/norm();
|
|
|
46 |
// note that the all q_*q_ are used twice (optimize)
|
|
|
47 |
return Mat3x3d(Vec3d(1.0 - s*(qv[1]*qv[1] + qv[2]*qv[2]),
|
|
|
48 |
s*(qv[0]*qv[1] - qw*qv[2]),
|
|
|
49 |
s*(qv[0]*qv[2] + qw*qv[1])),
|
|
|
50 |
Vec3d( s*(qv[0]*qv[1] + qw*qv[2]),
|
|
|
51 |
1.0 - s*(qv[0]*qv[0] + qv[2]*qv[2]),
|
|
|
52 |
s*(qv[1]*qv[2] - qw*qv[0])),
|
|
|
53 |
Vec3d( s*(qv[0]*qv[2] - qw*qv[1]),
|
|
|
54 |
s*(qv[1]*qv[2] + qw*qv[0]),
|
|
|
55 |
1.0 - s*(qv[0]*qv[0] + qv[1]*qv[1])));
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/// Get a 4x4 rotation matrix from a quaternion
|
|
|
59 |
Mat4x4d get_Mat4x4d() const
|
|
|
60 |
{
|
|
|
61 |
double s = 2/norm();
|
|
|
62 |
// note that the all q_*q_ are used twice (optimize?)
|
|
|
63 |
return Mat4x4d(Vec4d(1.0 - s*(qv[1]*qv[1] + qv[2]*qv[2]),
|
|
|
64 |
s*(qv[0]*qv[1] - qw*qv[2]),
|
|
|
65 |
s*(qv[0]*qv[2] + qw*qv[1]),
|
|
|
66 |
0.0),
|
|
|
67 |
Vec4d( s*(qv[0]*qv[1] + qw*qv[2]),
|
|
|
68 |
1.0 - s*(qv[0]*qv[0] + qv[2]*qv[2]),
|
|
|
69 |
s*(qv[1]*qv[2] - qw*qv[0]),
|
|
|
70 |
0.0),
|
|
|
71 |
Vec4d( s*(qv[0]*qv[2] - qw*qv[1]),
|
|
|
72 |
s*(qv[1]*qv[2] + qw*qv[0]),
|
|
|
73 |
1.0 - s*(qv[0]*qv[0] + qv[1]*qv[1]),
|
|
|
74 |
0.0),
|
|
|
75 |
Vec4d(0.0, 0.0, 0.0, 1.0));
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/// Create an identity quaternion
|
|
|
79 |
inline Quatd identity_Quatd()
|
|
|
80 |
{
|
|
|
81 |
return Quatd(Vec3d(0.0));
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
};
|
|
|
85 |
|
|
|
86 |
}
|
|
|
87 |
#endif
|