-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAxisAngle4.h
More file actions
68 lines (52 loc) · 1.54 KB
/
AxisAngle4.h
File metadata and controls
68 lines (52 loc) · 1.54 KB
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
#ifndef AXISANGLE4_H
#define AXISANGLE4_H
#include "VmUtil.h"
#include "AxisAngle4_.h"
#include "Quat4_.h"
VM_BEGIN_NS
template<class T>
void AxisAngle4<T>::set(const Quat4<T>& q1) {
setFromQuat(q1.x, q1.y, q1.z, q1.w);
}
template<class T>
void AxisAngle4<T>::setFromMat(T m00, T m01, T m02,
T m10, T m11, T m12,
T m20, T m21, T m22) {
// assuming M is normalized.
T cos = (m00 + m11 + m22 - 1.0)*0.5;
x = m21 - m12;
y = m02 - m20;
z = m10 - m01;
T sin = 0.5*VmUtil<T>::sqrt(x*x + y*y + z*z);
angle = VmUtil<T>::atan2(sin, cos);
// no need to normalize
// x /= n;
// y /= n;
// z /= n;
}
template<class T>
void AxisAngle4<T>::setFromQuat(T x, T y, T z, T w) {
// This logic can calculate angle without normalization.
// The direction of (x,y,z) and the sign of rotation cancel
// each other to calculate a right answer.
T sin_a2 = VmUtil<T>::sqrt(x*x + y*y + z*z); // |sin a/2|, w = cos a/2
this->angle = 2.0*VmUtil<T>::atan2(sin_a2, w); // 0 <= angle <= PI , because 0 < sin_a2
this->x = x;
this->y = y;
this->z = z;
}
#ifdef VM_INCLUDE_TOSTRING
template<class T>
std::string AxisAngle4<T>::toString() const {
VM_TOSTRING
}
#endif
#ifdef VM_INCLUDE_IO
template <class T>
std::ostream& operator<<(std::ostream& o, const VM_VECMATH_NS::AxisAngle4<T>& a1) {
return o << "(" << a1.x << "," << a1.y << ","
<< a1.z << "," << a1.angle << ")";
}
#endif
VM_END_NS
#endif /* AXISANGLE4_H */