-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquat.cpp
More file actions
131 lines (124 loc) · 3.74 KB
/
Copy pathquat.cpp
File metadata and controls
131 lines (124 loc) · 3.74 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
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
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
127
128
129
130
131
#include "PAZ_Math"
paz::Mat paz::to_mat(const MatRef& q)
{
if(q.rows() != 4 || q.cols() != 1)
{
throw std::runtime_error("Not a quaternion.");
}
const auto xx = q(0)*q(0);
const auto yy = q(1)*q(1);
const auto zz = q(2)*q(2);
const auto xy = q(0)*q(1);
const auto zw = q(2)*q(3);
const auto xz = q(0)*q(2);
const auto yw = q(1)*q(3);
const auto yz = q(1)*q(2);
const auto xw = q(0)*q(3);
return {{1. - 2.*(yy + zz), 2.*(xy + zw), 2.*(xz - yw)},
{ 2.*(xy - zw), 1. - 2.*(xx + zz), 2.*(yz + xw)},
{ 2.*(xz + yw), 2.*(yz - xw), 1. - 2.*(xx + yy)}};
}
paz::Vec paz::to_quat(const MatRef& m)
{
if(m.rows() != 3 || m.cols() != 3)
{
throw std::runtime_error("Not a rotation matrix.");
}
const auto fourXSqMinus1 = m(0, 0) - m(1, 1) - m(2, 2);
const auto fourYSqMinus1 = m(1, 1) - m(0, 0) - m(2, 2);
const auto fourZSqMinus1 = m(2, 2) - m(0, 0) - m(1, 1);
const auto fourWSqMinus1 = m(0, 0) + m(1, 1) + m(2, 2);
auto maxVal = fourWSqMinus1;
int maxIdx = 0;
if(fourXSqMinus1 > maxVal)
{
maxVal = fourXSqMinus1;
maxIdx = 1;
}
if(fourYSqMinus1 > maxVal)
{
maxVal = fourYSqMinus1;
maxIdx = 2;
}
if(fourZSqMinus1 > maxVal)
{
maxVal = fourZSqMinus1;
maxIdx = 3;
}
maxVal = 0.5*std::sqrt(maxVal + 1.);
const auto mult = 0.25/maxVal;
switch(maxIdx)
{
case 0:
return {{(m(1, 2) - m(2, 1))*mult,
(m(2, 0) - m(0, 2))*mult,
(m(0, 1) - m(1, 0))*mult,
maxVal}};
case 1:
return {{ maxVal,
(m(0, 1) + m(1, 0))*mult,
(m(2, 0) + m(0, 2))*mult,
(m(1, 2) - m(2, 1))*mult}};
case 2:
return {{(m(0, 1) + m(1, 0))*mult,
maxVal,
(m(1, 2) + m(2, 1))*mult,
(m(2, 0) - m(0, 2))*mult}};
default:
return {{(m(2, 0) + m(0, 2))*mult,
(m(1, 2) + m(2, 1))*mult,
maxVal,
(m(0, 1) - m(1, 0))*mult}};
}
}
paz::Vec paz::qinv(const MatRef& q)
{
if(q.rows() != 4 || q.cols() != 1)
{
throw std::runtime_error("Not a quaternion.");
}
return {{-q(0), -q(1), -q(2), q(3)}};
}
paz::Mat paz::xi(const MatRef& q)
{
if(q.rows() != 4 || q.cols() != 1)
{
throw std::runtime_error("Not a quaternion.");
}
return {{ q(3), -q(2), q(1)},
{ q(2), q(3), -q(0)},
{-q(1), q(0), q(3)},
{-q(0), -q(1), -q(2)}};
}
paz::Vec paz::qmult(const MatRef& p, const MatRef& q)
{
if(p.rows() != 4 || p.cols() != 1 || q.rows() != 4 || q.cols() != 1)
{
throw std::runtime_error("Not a quaternion.");
}
const Vec pVec = p.block(0, 0, 3, 1);
const Vec qVec = q.block(0, 0, 3, 1);
const Vec rVec = q(3)*pVec + p(3)*qVec - pVec.cross(qVec);
return {{rVec(0), rVec(1), rVec(2), p(3)*q(3) - pVec.dot(qVec)}};
}
paz::Vec paz::axis_angle(const MatRef& axis, double angle)
{
if(axis.rows() != 3 || axis.cols() != 1)
{
throw std::runtime_error("Not a 3-vector.");
}
const double s = std::sin(0.5*angle);
return Vec{{s*axis(0), s*axis(1), s*axis(2), std::cos(0.5*angle)}};
}
paz::Vec paz::nlerp(const MatRef& p, const MatRef& q, double k)
{
if(p.rows() != 4 || p.cols() != 1 || q.rows() != 4 || q.cols() != 1)
{
throw std::runtime_error("Not a quaternion.");
}
if(p.dot(q) < 0.)
{
return mix(p, -q, k).normalized();
}
return mix(p, q, k).normalized();
}