/
RotationMatrix.cpp
152 lines (135 loc) · 3.25 KB
/
RotationMatrix.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "RotationMatrix.h"
#include "ientity.h"
#include "math/Matrix4.h"
void RotationMatrix::setIdentity()
{
rotation[0] = 1;
rotation[1] = 0;
rotation[2] = 0;
rotation[3] = 0;
rotation[4] = 1;
rotation[5] = 0;
rotation[6] = 0;
rotation[7] = 0;
rotation[8] = 1;
}
void RotationMatrix::readFromString(const std::string& value)
{
std::stringstream strm(value);
strm << std::skipws;
for (int i = 0; i < 9; ++i)
{
strm >> rotation[i];
}
if (!strm)
{
// Parsing failed, fall back to the identity matrix
setIdentity();
}
}
void RotationMatrix::writeToEntity(Entity* entity, const std::string& key) const
{
if (rotation[0] == 1 &&
rotation[1] == 0 &&
rotation[2] == 0 &&
rotation[3] == 0 &&
rotation[4] == 1 &&
rotation[5] == 0 &&
rotation[6] == 0 &&
rotation[7] == 0 &&
rotation[8] == 1)
{
entity->setKeyValue(key, "");
}
else
{
entity->setKeyValue(key, getRotationKeyValue());
}
}
std::string RotationMatrix::getRotationKeyValue() const
{
std::ostringstream value;
value << rotation[0] << ' '
<< rotation[1] << ' '
<< rotation[2] << ' '
<< rotation[3] << ' '
<< rotation[4] << ' '
<< rotation[5] << ' '
<< rotation[6] << ' '
<< rotation[7] << ' '
<< rotation[8];
return value.str();
}
Matrix4 RotationMatrix::getMatrix4() const
{
return Matrix4::byColumns(
rotation[0],
rotation[1],
rotation[2],
0,
rotation[3],
rotation[4],
rotation[5],
0,
rotation[6],
rotation[7],
rotation[8],
0,
0,
0,
0,
1
);
}
void RotationMatrix::setFromMatrix4(const Matrix4& matrix)
{
rotation[0] = static_cast<float>(matrix.xx());
rotation[1] = static_cast<float>(matrix.xy());
rotation[2] = static_cast<float>(matrix.xz());
rotation[3] = static_cast<float>(matrix.yx());
rotation[4] = static_cast<float>(matrix.yy());
rotation[5] = static_cast<float>(matrix.yz());
rotation[6] = static_cast<float>(matrix.zx());
rotation[7] = static_cast<float>(matrix.zy());
rotation[8] = static_cast<float>(matrix.zz());
}
// Assignment operator
const RotationMatrix& RotationMatrix::operator=(const RotationMatrix& other)
{
rotation[0] = other[0];
rotation[1] = other[1];
rotation[2] = other[2];
rotation[3] = other[3];
rotation[4] = other[4];
rotation[5] = other[5];
rotation[6] = other[6];
rotation[7] = other[7];
rotation[8] = other[8];
return *this;
}
void RotationMatrix::rotate(const Quaternion& rotate)
{
setFromMatrix4(
getMatrix4().getPremultipliedBy(Matrix4::getRotationQuantised(rotate))
);
}
void RotationMatrix::setFromAngleString(const std::string& value)
{
// Quick check before trying to parse anything
if (value.empty())
{
setIdentity();
return;
}
try
{
float angle = std::stof(value);
// Cast succeeded
setFromMatrix4(Matrix4::getRotationAboutZ(math::Degrees(angle)));
}
catch (std::invalid_argument&)
{
// Cast failed
setIdentity();
}
}