-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrame.cpp
More file actions
209 lines (169 loc) · 4.21 KB
/
Copy pathFrame.cpp
File metadata and controls
209 lines (169 loc) · 4.21 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "Frame.h"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtc/epsilon.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/matrix_decompose.hpp>
#include <glm/gtx/quaternion.hpp>
#include <cassert>
namespace Vkx
{
//! The frame's transformation is computed as M = S * R * T
//!
//! @param translation Translation component as a 3D vector
//! @param rotation Rotation component as a quaternion
//! @param scale Scale component as a 3D vector
Frame::Frame(glm::vec3 const & translation,
glm::quat const & rotation,
glm::vec3 const & scale /* = glm::vec3(1.0f)*/)
: m_(compose(translation, glm::toMat4(rotation), scale))
{
}
//! M' = T * M
//!
//! @param t translation (in local space)
Frame & Frame::translate(glm::vec3 const & t)
{
m_ = glm::translate(glm::identity<glm::mat4x4>(), t) * m_;
return *this;
}
//! M' = R * M
//!
//! @param r rotation (in local space)
Frame & Frame::rotate(glm::quat const & r)
{
m_ = glm::toMat4(r) * m_;
return *this;
}
//! M' = S * M
//!
//! @param s scale (in local space)
Frame & Frame::scale(glm::vec3 const & s)
{
assert(glm::all(glm::epsilonNotEqual(s, glm::vec3(0.0f), std::numeric_limits<float>::epsilon() * glm::length(s))));
m_ = glm::scale(glm::identity<glm::mat4x4>(), s) * m_;
return *this;
}
//! M' = T * M
//!
//! @param t translation
void Frame::setTranslation(glm::vec3 const & t)
{
glm::vec3 s;
glm::quat r;
glm::vec3 old_t;
glm::vec3 k;
glm::vec4 p;
glm::decompose(m_, s, r, old_t, k, p);
// Replace translation
m_ = compose(t, glm::toMat4(r), s);
}
glm::vec3 Frame::translation() const
{
glm::vec3 s;
glm::quat r;
glm::vec3 t;
glm::vec3 k;
glm::vec4 p;
glm::decompose(m_, s, r, t, k, p);
return t;
}
//!
//! @param r rotation
void Frame::setOrientation(glm::mat3x3 const & r)
{
glm::vec3 s;
glm::quat old_r;
glm::vec3 t;
glm::vec3 k;
glm::vec4 p;
glm::decompose(m_, s, old_r, t, k, p);
// Replace orientation
m_ = compose(t, glm::mat4x4(r), s);
}
//!
//! @param r Orientation value.
void Frame::setOrientation(glm::quat const & r)
{
glm::vec3 s;
glm::quat old_r;
glm::vec3 t;
glm::vec3 k;
glm::vec4 p;
glm::decompose(m_, s, old_r, t, k, p);
// Replace orientation
m_ = compose(t, glm::toMat4(r), s);
}
glm::quat Frame::orientation() const
{
glm::vec3 s;
glm::quat r;
glm::vec3 t;
glm::vec3 k;
glm::vec4 p;
glm::decompose(m_, s, r, t, k, p);
return r;
}
glm::mat3x3 Frame::orientationMatrix() const
{
return glm::toMat3(orientation());
}
//!
//! @param s scale
void Frame::setScale(glm::vec3 const & s)
{
assert(glm::all(glm::epsilonNotEqual(s, glm::vec3(0.0f), std::numeric_limits<float>::epsilon() * glm::length(s))));
glm::vec3 old_s;
glm::quat r;
glm::vec3 t;
glm::vec3 k;
glm::vec4 p;
glm::decompose(m_, old_s, r, t, k, p);
// Replace scale
m_ = compose(t, glm::toMat4(r), s);
}
glm::vec3 Frame::scale() const
{
glm::vec3 s;
glm::quat r;
glm::vec3 t;
glm::vec3 k;
glm::vec4 p;
glm::decompose(m_, s, r, t, k, p);
return s;
}
glm::mat4x4 Frame::transformation() const
{
return m_;
}
//!
//! @param m Value to set the transformation matrix to.
void Frame::setTransformation(glm::mat4x4 const & m)
{
// Make sure that none of the scales are 0
// assert( !Math::IsCloseToZero( Vector3d( m[0][0], m[0][1], m[0][2] ).Length2() ) );
// assert( !Math::IsCloseToZero( Vector3d( m[1][0], m[1][1], m[1][2] ).Length2() ) );
// assert( !Math::IsCloseToZero( Vector3d( m[2][0], m[2][1], m[2][2] ).Length2() ) );
// assert( m[0][2] == 0.f );
// assert( m[1][2] == 0.f );
// assert( m[2][2] == 0.f );
// assert( m[3][2] == 1.f );
m_ = m;
}
glm::vec3 Frame::xAxis() const
{
return glm::normalize(glm::vec3(m_[0]));
}
glm::vec3 Frame::yAxis() const
{
return glm::normalize(glm::vec3(m_[1]));
}
glm::vec3 Frame::zAxis() const
{
return glm::normalize(glm::vec3(m_[2]));
}
glm::mat4x4 Frame::compose(glm::vec3 const & t, glm::mat4x4 const & r, glm::vec3 const & s)
{
return glm::scale(glm::translate(glm::identity<glm::mat4x4>(), t) * r, s);
}
} // namespace Vkx