source: flair-src/trunk/lib/FlairCore/src/Quaternion.cpp@ 179

Last change on this file since 179 was 167, checked in by Sanahuja Guillaume, 8 years ago

modifs pour template vectors

File size: 5.2 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5// created: 2016/02/03
6// filename: Quaternion.cpp
7//
8// author: Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: Class defining a quaternion
14//
15//
16/*********************************************************************/
17
18#include "Quaternion.h"
19#include "Euler.h"
20#include "RotationMatrix.h"
21#include <math.h>
22#include <stdio.h>
23
24namespace flair {
25namespace core {
26
27Quaternion::Quaternion(float inQ0, float inQ1, float inQ2, float inQ3)
28 : q0(inQ0), q1(inQ1), q2(inQ2), q3(inQ3) {}
29
30Quaternion::~Quaternion() {}
31
32Quaternion &Quaternion::operator=(const Quaternion &quaternion) {
33 q0 = quaternion.q0;
34 q1 = quaternion.q1;
35 q2 = quaternion.q2;
36 q3 = quaternion.q3;
37 return (*this);
38}
39
40float Quaternion::GetNorm(void) const {
41 return sqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3);
42}
43
44void Quaternion::Normalize(void) {
45 float n = GetNorm();
46 if (n != 0) {
47 q0 = q0 / n;
48 q1 = q1 / n;
49 q2 = q2 / n;
50 q3 = q3 / n;
51 }
52}
53
54void Quaternion::Conjugate(void) {
55 q1 = -q1;
56 q2 = -q2;
57 q3 = -q3;
58}
59
60Quaternion Quaternion::GetConjugate(void) {
61 return Quaternion(q0, -q1, -q2, -q3);
62}
63
64void Quaternion::GetLogarithm(Vector3Df &logarithm) {
65 Normalize();
66 float v_norm = sqrtf(q1 * q1 + q2 * q2 + q3 * q3);
67
68 if (v_norm != 0) {
69 float v_arccos = acosf(q0);
70 logarithm.x = (q1 * v_arccos) / v_norm;
71 logarithm.y = (q2 * v_arccos) / v_norm;
72 logarithm.z = (q3 * v_arccos) / v_norm;
73 } else {
74 logarithm.x = 0;
75 logarithm.y = 0;
76 logarithm.z = 0;
77 }
78}
79
80Vector3Df Quaternion::GetLogarithm(void) {
81 Vector3Df vector;
82 GetLogarithm(vector);
83 return vector;
84}
85
86Quaternion Quaternion::GetDerivative(const Vector3Df &angularSpeed) const {
87 const Quaternion Qw(0, angularSpeed.x, angularSpeed.y, angularSpeed.z);
88 return 0.5 * (*this) * Qw;
89}
90
91void Quaternion::Derivate(const Vector3Df &angularSpeed) {
92 Quaternion Q = GetDerivative(angularSpeed);
93 (*this) = Q;
94}
95
96void Quaternion::ToEuler(Euler &euler) const {
97 euler.roll = atan2(2 * (q0 * q1 + q2 * q3), 1 - 2 * (q1 * q1 + q2 * q2));
98 euler.pitch = asin(2 * (q0 * q2 - q1 * q3));
99 euler.yaw = atan2(2 * (q0 * q3 + q1 * q2), 1 - 2 * (q2 * q2 + q3 * q3));
100}
101
102Euler Quaternion::ToEuler(void) const {
103 Euler euler;
104 ToEuler(euler);
105 return euler;
106}
107
108void Quaternion::ToRotationMatrix(RotationMatrix &matrix) const {
109 float aSq = q0 * q0;
110 float bSq = q1 * q1;
111 float cSq = q2 * q2;
112 float dSq = q3 * q3;
113 matrix.m[0][0] = aSq + bSq - cSq - dSq;
114 matrix.m[0][1] = 2.0f * (q1 * q2 - q0 * q3);
115 matrix.m[0][2] = 2.0f * (q0 * q2 + q1 * q3);
116 matrix.m[1][0] = 2.0f * (q1 * q2 + q0 * q3);
117 matrix.m[1][1] = aSq - bSq + cSq - dSq;
118 matrix.m[1][2] = 2.0f * (q2 * q3 - q0 * q1);
119 matrix.m[2][0] = 2.0f * (q1 * q3 - q0 * q2);
120 matrix.m[2][1] = 2.0f * (q0 * q1 + q2 * q3);
121 matrix.m[2][2] = aSq - bSq - cSq + dSq;
122}
123
124Quaternion &Quaternion::operator+=(const Quaternion &quaternion) {
125 q0 += quaternion.q0;
126 q1 += quaternion.q1;
127 q2 += quaternion.q2;
128 q3 += quaternion.q3;
129 return (*this);
130}
131
132Quaternion &Quaternion::operator-=(const Quaternion &quaternion) {
133 q0 -= quaternion.q0;
134 q1 -= quaternion.q1;
135 q2 -= quaternion.q2;
136 q3 -= quaternion.q3;
137 return (*this);
138}
139
140Quaternion operator+(const Quaternion &quaternionA,
141 const Quaternion &quaterniontB) {
142 return Quaternion(
143 quaternionA.q0 + quaterniontB.q0, quaternionA.q1 + quaterniontB.q1,
144 quaternionA.q2 + quaterniontB.q2, quaternionA.q3 + quaterniontB.q3);
145}
146
147Quaternion operator-(const Quaternion &quaterniontA,
148 const Quaternion &quaterniontB) {
149 return Quaternion(
150 quaterniontA.q0 - quaterniontB.q0, quaterniontA.q1 - quaterniontB.q1,
151 quaterniontA.q2 - quaterniontB.q2, quaterniontA.q3 - quaterniontB.q3);
152}
153
154Quaternion operator-(const Quaternion &quaternion) {
155 return Quaternion(-quaternion.q0, -quaternion.q1, -quaternion.q2,
156 -quaternion.q3);
157}
158
159Quaternion operator*(const Quaternion &quaterniontA,
160 const Quaternion &quaterniontB) {
161 return Quaternion(
162 quaterniontA.q0 * quaterniontB.q0 - quaterniontA.q1 * quaterniontB.q1 -
163 quaterniontA.q2 * quaterniontB.q2 - quaterniontA.q3 * quaterniontB.q3,
164 quaterniontA.q0 * quaterniontB.q1 + quaterniontA.q1 * quaterniontB.q0 +
165 quaterniontA.q2 * quaterniontB.q3 - quaterniontA.q3 * quaterniontB.q2,
166 quaterniontA.q0 * quaterniontB.q2 - quaterniontA.q1 * quaterniontB.q3 +
167 quaterniontA.q2 * quaterniontB.q0 + quaterniontA.q3 * quaterniontB.q1,
168 quaterniontA.q0 * quaterniontB.q3 + quaterniontA.q1 * quaterniontB.q2 -
169 quaterniontA.q2 * quaterniontB.q1 + quaterniontA.q3 * quaterniontB.q0);
170}
171
172Quaternion operator*(float coeff, const Quaternion &quaternion) {
173 return Quaternion(coeff * quaternion.q0, coeff * quaternion.q1,
174 coeff * quaternion.q2, coeff * quaternion.q3);
175}
176
177Quaternion operator*(const Quaternion &quaternion, float coeff) {
178 return Quaternion(coeff * quaternion.q0, coeff * quaternion.q1,
179 coeff * quaternion.q2, coeff * quaternion.q3);
180}
181
182} // end namespace core
183} // end namespace flair
Note: See TracBrowser for help on using the repository browser.