-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transform.cpp
380 lines (341 loc) · 11.2 KB
/
Transform.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include "transform.hpp"
const Transform Transform::IDENTITY;
// Set the initial matrix to identity matrix
// Translate to (0.0,0.0,0.0) and scale to (1.0,1.0,1.0)
Transform::Transform ()
:
mHMatrix(1.0f),
mInvHMatrix(1.0f),
mMatrix(1.0f),
mTranslate(0.0f),
mScale(1.0f),
mIsIdentity(true),
mIsRSMatrix(true),
mIsUniformScale(true),
mInverseNeedsUpdate(false)
{
}
Transform::~Transform ()
{
}
void Transform::MakeIdentity ()
{
mHMatrix=glm::mat4(1.0f);
mInvHMatrix=glm::mat4(1.0f);
mMatrix = glm::mat3(1.0f);
mTranslate = glm::vec3(0.0f);
mScale = glm::vec3(1.0f);
mIsIdentity = true;
mIsRSMatrix = true;
mIsUniformScale = true;
mInverseNeedsUpdate=false;
}
void Transform::MakeUnitScale ()
{
assertion(mIsRSMatrix, "Matrix is not a rotation\n");
mScale = glm::vec3(1.0f);
mIsUniformScale = true;
UpdateHMatrix();
}
void Transform::SetRotate (const glm::mat3& rotate)
{
mMatrix = rotate;
mIsIdentity = false;
mIsRSMatrix = true;
UpdateHMatrix();
}
void Transform::SetMatrix (const glm::mat3& matrix)
{
mMatrix = matrix;
mIsIdentity = false;
mIsRSMatrix = false;
mIsUniformScale = false;
UpdateHMatrix();
}
void Transform::SetTranslate (const glm::vec3& translate)
{
mTranslate = translate;
mIsIdentity = false;
UpdateHMatrix();
}
void Transform::SetScale (const glm::vec3& scale)
{
assertion(mIsRSMatrix, "Matrix is not a rotation\n");
assertion(scale[0] != 0.0f && scale[1] != 0.0f && scale[2] != 0.0f,
"Scales must be nonzero\n");
mScale = scale;
mIsIdentity = false;
mIsUniformScale = false;
UpdateHMatrix();
}
void Transform::SetUniformScale (float scale)
{
assertion(mIsRSMatrix, "Matrix is not a rotation\n");
assertion(scale != 0.0f, "Scale must be nonzero\n");
mScale = glm::vec3(scale);
mIsIdentity = false;
mIsUniformScale = true;
UpdateHMatrix();
}
//----------------------------------------------------------------------------
float Transform::GetNorm () const
{
if (mIsRSMatrix)
{
float maxValue = glm::abs(mScale[0]);
float curValue = glm::abs(mScale[1]);
if (curValue > maxValue)
{
maxValue = curValue;
}
curValue = glm::abs(mScale[2]);
if (curValue > maxValue)
{
maxValue = curValue;
}
return maxValue;
}
// A general matrix. Use the max-row-sum matrix norm. The spectral
// norm (the maximum absolute value of the eigenvalues) is smaller or
// equal to this norm. Therefore, this function returns an approximation
// to the maximum scale.
// Because glm use column major matrix, change the code
float maxRowSum =
glm::abs(mMatrix[0][0]) +
glm::abs(mMatrix[1][0]) +
glm::abs(mMatrix[2][0]);
float rowSum =
glm::abs(mMatrix[0][1]) +
glm::abs(mMatrix[1][1]) +
glm::abs(mMatrix[2][1]);
if (rowSum > maxRowSum)
{
maxRowSum = rowSum;
}
rowSum =
glm::abs(mMatrix[0][2]) +
glm::abs(mMatrix[1][2]) +
glm::abs(mMatrix[2][2]);
if (rowSum > maxRowSum)
{
maxRowSum = rowSum;
}
return maxRowSum;
}
//----------------------------------------------------------------------------
Transform Transform::operator* (const Transform& transform) const
{
if (IsIdentity())
{
return transform;
}
if (transform.IsIdentity())
{
return *this;
}
Transform product;
if (mIsRSMatrix && transform.mIsRSMatrix)
{
if (mIsUniformScale)
{
product.SetRotate(mMatrix*transform.mMatrix);
product.SetTranslate(GetUniformScale()*(
mMatrix*transform.mTranslate) + mTranslate);
if (transform.IsUniformScale())
{
product.SetUniformScale(
GetUniformScale()*transform.GetUniformScale());
}
else
{
product.SetScale(GetUniformScale()*transform.GetScale());
}
return product;
}
}
// In all remaining cases, the matrix cannot be written as R*S*X+T.
glm::mat3 matMA = (mIsRSMatrix ?
glm::mat3(mMatrix[0]*mScale[0],mMatrix[1]*mScale[1],
mMatrix[2]*mScale[2]): mMatrix);
glm::mat3 matMB = (transform.mIsRSMatrix ?
glm::mat3(transform.mMatrix[0]*transform.mScale[0],
transform.mMatrix[1]*transform.mScale[1],
transform.mMatrix[2]*transform.mScale[2]): mMatrix);
product.SetMatrix(matMA*matMB);
product.SetTranslate(matMA*transform.mTranslate + mTranslate);
return product;
}
//----------------------------------------------------------------------------
const glm::mat4& Transform::Inverse () const
{
if (mInverseNeedsUpdate)
{
if (mIsIdentity)
{
mInvHMatrix = glm::mat4(1.0);
}
else
{
if (mIsRSMatrix)
{
if (mIsUniformScale)
{
float invScale = 1.0f/mScale[0];
/*
mInvHMatrix[0][0] = invScale*mMatrix[0][0];
mInvHMatrix[0][1] = invScale*mMatrix[1][0];
mInvHMatrix[0][2] = invScale*mMatrix[2][0];
mInvHMatrix[1][0] = invScale*mMatrix[0][1];
mInvHMatrix[1][1] = invScale*mMatrix[1][1];
mInvHMatrix[1][2] = invScale*mMatrix[2][1];
mInvHMatrix[2][0] = invScale*mMatrix[0][2];
mInvHMatrix[2][1] = invScale*mMatrix[1][2];
mInvHMatrix[2][2] = invScale*mMatrix[2][2];*/
mInvHMatrix=glm::mat4(invScale*glm::transpose(mMatrix));
}
else
{
// Replace 3 reciprocals by 6 multiplies and 1 reciprocal.
float s01 = mScale[0]*mScale[1];
float s02 = mScale[0]*mScale[2];
float s12 = mScale[1]*mScale[2];
float invs012 = 1.0f/(s01*mScale[2]);
float invS0 = s12*invs012;
float invS1 = s02*invs012;
float invS2 = s01*invs012;
/*mInvHMatrix[0][0] = invS0*mMatrix[0][0];
mInvHMatrix[0][1] = invS0*mMatrix[1][0];
mInvHMatrix[0][2] = invS0*mMatrix[2][0];
mInvHMatrix[1][0] = invS1*mMatrix[0][1];
mInvHMatrix[1][1] = invS1*mMatrix[1][1];
mInvHMatrix[1][2] = invS1*mMatrix[2][1];
mInvHMatrix[2][0] = invS2*mMatrix[0][2];
mInvHMatrix[2][1] = invS2*mMatrix[1][2];
mInvHMatrix[2][2] = invS2*mMatrix[2][2];*/
mInvHMatrix[0][0] = invS0*mMatrix[0][0];
mInvHMatrix[0][1] = invS1*mMatrix[1][0];
mInvHMatrix[0][2] = invS2*mMatrix[2][0];
mInvHMatrix[1][0] = invS0*mMatrix[0][1];
mInvHMatrix[1][1] = invS1*mMatrix[1][1];
mInvHMatrix[1][2] = invS2*mMatrix[2][1];
mInvHMatrix[2][0] = invS0*mMatrix[0][2];
mInvHMatrix[2][1] = invS1*mMatrix[1][2];
mInvHMatrix[2][2] = invS2*mMatrix[2][2];
}
}
else
{
// mInvHMatrix=glm::inverse(mHMatrix);
// mInverseNeedsUpdate = false;
// return mInvHMatrix;
mInvHMatrix=glm::mat4(glm::inverse(glm::mat3(mHMatrix)));
}
//todo
/*mInvHMatrix[3][0] = -(
mInvHMatrix[0][0]*mTranslate[0] +
mInvHMatrix[0][1]*mTranslate[1] +
mInvHMatrix[0][2]*mTranslate[2]
);
mInvHMatrix[3][1] = -(
mInvHMatrix[1][0]*mTranslate[0] +
mInvHMatrix[1][1]*mTranslate[1] +
mInvHMatrix[1][2]*mTranslate[2]
);
mInvHMatrix[3][2] = -(
mInvHMatrix[2][0]*mTranslate[0] +
mInvHMatrix[2][1]*mTranslate[1] +
mInvHMatrix[2][2]*mTranslate[2]
);*/
mInvHMatrix[3]=mInvHMatrix*glm::vec4(mTranslate,0.0f);
// The last row of mHMatrix is always (0,0,0,1) for an affine
// transformation, so it is set once in the constructor. It is
// not necessary to reset it here.
}
mInverseNeedsUpdate = false;
}
return mInvHMatrix;
}
//----------------------------------------------------------------------------
Transform Transform::InverseTransform () const
{
if (mIsIdentity)
{
return IDENTITY;
}
Transform inverse;
glm::vec3 invTrn;
if (mIsRSMatrix)
{
glm::mat3 invRot = glm::transpose(mMatrix);
inverse.SetRotate(invRot);
if (mIsUniformScale)
{
float invScale = 1.0f/mScale[0];
inverse.SetUniformScale(invScale);
invTrn = -invScale*(invRot*mTranslate);
}
else
{
glm::vec3 invScale(1.0f/mScale[0], 1.0f/mScale[1], 1.0f/mScale[2]);
inverse.SetScale(invScale);
invTrn = invRot*mTranslate;
invTrn[0] *= -invScale[0];
invTrn[1] *= -invScale[1];
invTrn[2] *= -invScale[2];
}
}
else
{
glm::mat3 invMat=glm::inverse(mMatrix);
inverse.SetMatrix(invMat);
invTrn = -(invMat*mTranslate);
}
inverse.SetTranslate(invTrn);
return inverse;
}
//----------------------------------------------------------------------------
void Transform::UpdateHMatrix ()
{
if (mIsIdentity)
{
mHMatrix = glm::mat4(1.0f);
}
else
{
if (mIsRSMatrix)
{
/*mHMatrix[0][0] = mMatrix[0][0]*mScale[0];
mHMatrix[0][1] = mMatrix[0][1]*mScale[1];
mHMatrix[0][2] = mMatrix[0][2]*mScale[2];
mHMatrix[1][0] = mMatrix[1][0]*mScale[0];
mHMatrix[1][1] = mMatrix[1][1]*mScale[1];
mHMatrix[1][2] = mMatrix[1][2]*mScale[2];
mHMatrix[2][0] = mMatrix[2][0]*mScale[0];
mHMatrix[2][1] = mMatrix[2][1]*mScale[1];
mHMatrix[2][2] = mMatrix[2][2]*mScale[2];*/
mHMatrix[0]=mHMatrix[0]*mScale[0];
mHMatrix[1]=mHMatrix[1]*mScale[1];
mHMatrix[2]=mHMatrix[2]*mScale[2];
}
else
{
/*mHMatrix[0][0] = mMatrix[0][0];
mHMatrix[0][1] = mMatrix[0][1];
mHMatrix[0][2] = mMatrix[0][2];
mHMatrix[1][0] = mMatrix[1][0];
mHMatrix[1][1] = mMatrix[1][1];
mHMatrix[1][2] = mMatrix[1][2];
mHMatrix[2][0] = mMatrix[2][0];
mHMatrix[2][1] = mMatrix[2][1];
mHMatrix[2][2] = mMatrix[2][2];*/
mHMatrix=glm::mat4(mMatrix);
}
mHMatrix[3][0] = mTranslate[0];
mHMatrix[3][1] = mTranslate[1];
mHMatrix[3][2] = mTranslate[2];
// The last row of mHMatrix is always (0,0,0,1) for an affine
// transformation, so it is set once in the constructor. It is not
// necessary to reset it here.
}
mInverseNeedsUpdate = true;
}