660 |
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 Quatf.h
|
|
|
8 |
* @brief float based quaternion class
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
#ifndef __CGLA_QUATF_H__
|
|
|
12 |
#define __CGLA_QUATF_H__
|
|
|
13 |
|
|
|
14 |
#include "ArithQuat.h"
|
|
|
15 |
#include "Vec3f.h"
|
|
|
16 |
#include "Vec4f.h"
|
|
|
17 |
#include "Mat3x3f.h"
|
|
|
18 |
#include "Mat4x4f.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 Quatf : public ArithQuat<float,Vec3f,Quatf>
|
|
|
28 |
{
|
|
|
29 |
public:
|
|
|
30 |
Quatf() : ArithQuat<float, Vec3f, Quatf>() {}
|
|
|
31 |
|
|
|
32 |
/// Construct quaternion from vector and scalar
|
|
|
33 |
Quatf(const Vec3f& imaginary, float real = 1.0f) : ArithQuat<float, Vec3f, Quatf>(imaginary, real) {}
|
|
|
34 |
|
|
|
35 |
/// Construct quaternion from four scalars
|
|
|
36 |
Quatf(float x, float y, float z, float _qw) : ArithQuat<float, Vec3f, Quatf>(x,y,z,_qw) {}
|
|
|
37 |
|
|
|
38 |
/// Construct quaternion from a 4D vector
|
|
|
39 |
explicit Quatf(const Vec4f& v) : ArithQuat<float, Vec3f, Quatf>(v[0], v[1], v[2], v[3]) {}
|
|
|
40 |
|
|
|
41 |
|
|
|
42 |
/// Get a 3x3 rotation matrix from a quaternion
|
|
|
43 |
Mat3x3f get_Mat3x3f() const
|
|
|
44 |
{
|
|
|
45 |
float s = 2/norm();
|
|
|
46 |
// note that the all q_*q_ are used twice (optimize)
|
|
|
47 |
return Mat3x3f(Vec3f(1.0f - 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 |
Vec3f( s*(qv[0]*qv[1] + qw*qv[2]),
|
|
|
51 |
1.0f - s*(qv[0]*qv[0] + qv[2]*qv[2]),
|
|
|
52 |
s*(qv[1]*qv[2] - qw*qv[0])),
|
|
|
53 |
Vec3f( s*(qv[0]*qv[2] - qw*qv[1]),
|
|
|
54 |
s*(qv[1]*qv[2] + qw*qv[0]),
|
|
|
55 |
1.0f - s*(qv[0]*qv[0] + qv[1]*qv[1])));
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/// Get a 4x4 rotation matrix from a quaternion
|
|
|
59 |
Mat4x4f get_Mat4x4f() const
|
|
|
60 |
{
|
|
|
61 |
float s = 2.0f/norm();
|
|
|
62 |
// note that the all q_*q_ are used twice (optimize?)
|
|
|
63 |
return Mat4x4f(Vec4f(1.0f - 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.0f),
|
|
|
67 |
Vec4f( s*(qv[0]*qv[1] + qw*qv[2]),
|
|
|
68 |
1.0f - s*(qv[0]*qv[0] + qv[2]*qv[2]),
|
|
|
69 |
s*(qv[1]*qv[2] - qw*qv[0]),
|
|
|
70 |
0.0f),
|
|
|
71 |
Vec4f( s*(qv[0]*qv[2] - qw*qv[1]),
|
|
|
72 |
s*(qv[1]*qv[2] + qw*qv[0]),
|
|
|
73 |
1.0f - s*(qv[0]*qv[0] + qv[1]*qv[1]),
|
|
|
74 |
0.0f),
|
|
|
75 |
Vec4f(0.0f, 0.0f, 0.0f, 1.0f));
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/// Create an identity quaternion
|
|
|
79 |
inline Quatf identity_Quatf()
|
|
|
80 |
{
|
|
|
81 |
return Quatf(Vec3f(0.0f));
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
};
|
|
|
85 |
|
|
|
86 |
}
|
|
|
87 |
#endif
|