-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathTransform.cpp
More file actions
487 lines (396 loc) · 19.9 KB
/
Transform.cpp
File metadata and controls
487 lines (396 loc) · 19.9 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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2014 Laurent Gomila ([email protected])
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <cpp3ds/Graphics/Transform.hpp>
#include <cmath>
namespace cpp3ds
{
////////////////////////////////////////////////////////////
const Transform Transform::Identity;
////////////////////////////////////////////////////////////
Transform::Transform()
{
// Identity matrix
m_matrix[0] = 1.f; m_matrix[4] = 0.f; m_matrix[8] = 0.f; m_matrix[12] = 0.f;
m_matrix[1] = 0.f; m_matrix[5] = 1.f; m_matrix[9] = 0.f; m_matrix[13] = 0.f;
m_matrix[2] = 0.f; m_matrix[6] = 0.f; m_matrix[10] = 1.f; m_matrix[14] = 0.f;
m_matrix[3] = 0.f; m_matrix[7] = 0.f; m_matrix[11] = 0.f; m_matrix[15] = 1.f;
}
////////////////////////////////////////////////////////////
Transform::Transform(float a00, float a01, float a02, float a03,
float a10, float a11, float a12, float a13,
float a20, float a21, float a22, float a23,
float a30, float a31, float a32, float a33)
{
m_matrix[0] = a00; m_matrix[4] = a01; m_matrix[8] = a02; m_matrix[12] = a03;
m_matrix[1] = a10; m_matrix[5] = a11; m_matrix[9] = a12; m_matrix[13] = a13;
m_matrix[2] = a20; m_matrix[6] = a21; m_matrix[10] = a22; m_matrix[14] = a23;
m_matrix[3] = a30; m_matrix[7] = a31; m_matrix[11] = a32; m_matrix[15] = a33;
}
////////////////////////////////////////////////////////////
const float* Transform::getMatrix() const
{
return m_matrix;
}
////////////////////////////////////////////////////////////
Transform Transform::getInverse() const
{
// Compute the inverse
float inverted_matrix[16];
inverted_matrix[0] = m_matrix[5] * m_matrix[10] * m_matrix[15] -
m_matrix[5] * m_matrix[11] * m_matrix[14] -
m_matrix[9] * m_matrix[6] * m_matrix[15] +
m_matrix[9] * m_matrix[7] * m_matrix[14] +
m_matrix[13] * m_matrix[6] * m_matrix[11] -
m_matrix[13] * m_matrix[7] * m_matrix[10];
inverted_matrix[1] = -m_matrix[1] * m_matrix[10] * m_matrix[15] +
m_matrix[1] * m_matrix[11] * m_matrix[14] +
m_matrix[9] * m_matrix[2] * m_matrix[15] -
m_matrix[9] * m_matrix[3] * m_matrix[14] -
m_matrix[13] * m_matrix[2] * m_matrix[11] +
m_matrix[13] * m_matrix[3] * m_matrix[10];
inverted_matrix[2] = m_matrix[1] * m_matrix[6] * m_matrix[15] -
m_matrix[1] * m_matrix[7] * m_matrix[14] -
m_matrix[5] * m_matrix[2] * m_matrix[15] +
m_matrix[5] * m_matrix[3] * m_matrix[14] +
m_matrix[13] * m_matrix[2] * m_matrix[7] -
m_matrix[13] * m_matrix[3] * m_matrix[6];
inverted_matrix[3] = -m_matrix[1] * m_matrix[6] * m_matrix[11] +
m_matrix[1] * m_matrix[7] * m_matrix[10] +
m_matrix[5] * m_matrix[2] * m_matrix[11] -
m_matrix[5] * m_matrix[3] * m_matrix[10] -
m_matrix[9] * m_matrix[2] * m_matrix[7] +
m_matrix[9] * m_matrix[3] * m_matrix[6];
inverted_matrix[4] = -m_matrix[4] * m_matrix[10] * m_matrix[15] +
m_matrix[4] * m_matrix[11] * m_matrix[14] +
m_matrix[8] * m_matrix[6] * m_matrix[15] -
m_matrix[8] * m_matrix[7] * m_matrix[14] -
m_matrix[12] * m_matrix[6] * m_matrix[11] +
m_matrix[12] * m_matrix[7] * m_matrix[10];
inverted_matrix[5] = m_matrix[0] * m_matrix[10] * m_matrix[15] -
m_matrix[0] * m_matrix[11] * m_matrix[14] -
m_matrix[8] * m_matrix[2] * m_matrix[15] +
m_matrix[8] * m_matrix[3] * m_matrix[14] +
m_matrix[12] * m_matrix[2] * m_matrix[11] -
m_matrix[12] * m_matrix[3] * m_matrix[10];
inverted_matrix[6] = -m_matrix[0] * m_matrix[6] * m_matrix[15] +
m_matrix[0] * m_matrix[7] * m_matrix[14] +
m_matrix[4] * m_matrix[2] * m_matrix[15] -
m_matrix[4] * m_matrix[3] * m_matrix[14] -
m_matrix[12] * m_matrix[2] * m_matrix[7] +
m_matrix[12] * m_matrix[3] * m_matrix[6];
inverted_matrix[7] = m_matrix[0] * m_matrix[6] * m_matrix[11] -
m_matrix[0] * m_matrix[7] * m_matrix[10] -
m_matrix[4] * m_matrix[2] * m_matrix[11] +
m_matrix[4] * m_matrix[3] * m_matrix[10] +
m_matrix[8] * m_matrix[2] * m_matrix[7] -
m_matrix[8] * m_matrix[3] * m_matrix[6];
inverted_matrix[8] = m_matrix[4] * m_matrix[9] * m_matrix[15] -
m_matrix[4] * m_matrix[11] * m_matrix[13] -
m_matrix[8] * m_matrix[5] * m_matrix[15] +
m_matrix[8] * m_matrix[7] * m_matrix[13] +
m_matrix[12] * m_matrix[5] * m_matrix[11] -
m_matrix[12] * m_matrix[7] * m_matrix[9];
inverted_matrix[9] = -m_matrix[0] * m_matrix[9] * m_matrix[15] +
m_matrix[0] * m_matrix[11] * m_matrix[13] +
m_matrix[8] * m_matrix[1] * m_matrix[15] -
m_matrix[8] * m_matrix[3] * m_matrix[13] -
m_matrix[12] * m_matrix[1] * m_matrix[11] +
m_matrix[12] * m_matrix[3] * m_matrix[9];
inverted_matrix[10] = m_matrix[0] * m_matrix[5] * m_matrix[15] -
m_matrix[0] * m_matrix[7] * m_matrix[13] -
m_matrix[4] * m_matrix[1] * m_matrix[15] +
m_matrix[4] * m_matrix[3] * m_matrix[13] +
m_matrix[12] * m_matrix[1] * m_matrix[7] -
m_matrix[12] * m_matrix[3] * m_matrix[5];
inverted_matrix[11] = -m_matrix[0] * m_matrix[5] * m_matrix[11] +
m_matrix[0] * m_matrix[7] * m_matrix[9] +
m_matrix[4] * m_matrix[1] * m_matrix[11] -
m_matrix[4] * m_matrix[3] * m_matrix[9] -
m_matrix[8] * m_matrix[1] * m_matrix[7] +
m_matrix[8] * m_matrix[3] * m_matrix[5];
inverted_matrix[12] = -m_matrix[4] * m_matrix[9] * m_matrix[14] +
m_matrix[4] * m_matrix[10] * m_matrix[13] +
m_matrix[8] * m_matrix[5] * m_matrix[14] -
m_matrix[8] * m_matrix[6] * m_matrix[13] -
m_matrix[12] * m_matrix[5] * m_matrix[10] +
m_matrix[12] * m_matrix[6] * m_matrix[9];
inverted_matrix[13] = m_matrix[0] * m_matrix[9] * m_matrix[14] -
m_matrix[0] * m_matrix[10] * m_matrix[13] -
m_matrix[8] * m_matrix[1] * m_matrix[14] +
m_matrix[8] * m_matrix[2] * m_matrix[13] +
m_matrix[12] * m_matrix[1] * m_matrix[10] -
m_matrix[12] * m_matrix[2] * m_matrix[9];
inverted_matrix[14] = -m_matrix[0] * m_matrix[5] * m_matrix[14] +
m_matrix[0] * m_matrix[6] * m_matrix[13] +
m_matrix[4] * m_matrix[1] * m_matrix[14] -
m_matrix[4] * m_matrix[2] * m_matrix[13] -
m_matrix[12] * m_matrix[1] * m_matrix[6] +
m_matrix[12] * m_matrix[2] * m_matrix[5];
inverted_matrix[15] = m_matrix[0] * m_matrix[5] * m_matrix[10] -
m_matrix[0] * m_matrix[6] * m_matrix[9] -
m_matrix[4] * m_matrix[1] * m_matrix[10] +
m_matrix[4] * m_matrix[2] * m_matrix[9] +
m_matrix[8] * m_matrix[1] * m_matrix[6] -
m_matrix[8] * m_matrix[2] * m_matrix[5];
// Compute the determinant
float det = m_matrix[0] * inverted_matrix[0] +
m_matrix[1] * inverted_matrix[4] +
m_matrix[2] * inverted_matrix[8] +
m_matrix[3] * inverted_matrix[12];
// Compute the inverse if the determinant is not zero
// (don't use an epsilon because the determinant may *really* be tiny)
if (det != 0.f)
{
det = 1.f / det;
for (int i = 0; i < 16; ++i)
inverted_matrix[i] = inverted_matrix[i] * det;
return Transform(inverted_matrix[0], inverted_matrix[4], inverted_matrix[8], inverted_matrix[12],
inverted_matrix[1], inverted_matrix[5], inverted_matrix[8], inverted_matrix[13],
inverted_matrix[2], inverted_matrix[6], inverted_matrix[10], inverted_matrix[14],
inverted_matrix[3], inverted_matrix[7], inverted_matrix[11], inverted_matrix[15]);
}
else
{
return Identity;
}
}
////////////////////////////////////////////////////////////
Transform Transform::getTranspose() const
{
return Transform(m_matrix[0], m_matrix[1], m_matrix[2], m_matrix[3],
m_matrix[4], m_matrix[5], m_matrix[6], m_matrix[7],
m_matrix[8], m_matrix[9], m_matrix[10], m_matrix[11],
m_matrix[12], m_matrix[13], m_matrix[14], m_matrix[15]);
}
////////////////////////////////////////////////////////////
Vector3f Transform::transformPoint(float x, float y, float z) const
{
return Vector3f(m_matrix[0] * x + m_matrix[4] * y + m_matrix[8] * z + m_matrix[12],
m_matrix[1] * x + m_matrix[5] * y + m_matrix[9] * z + m_matrix[13],
m_matrix[2] * x + m_matrix[6] * y + m_matrix[10] * z + m_matrix[14]);
}
////////////////////////////////////////////////////////////
Vector3f Transform::transformPoint(const Vector3f& point) const
{
return transformPoint(point.x, point.y, point.z);
}
////////////////////////////////////////////////////////////
FloatRect Transform::transformRect(const FloatRect& rectangle) const
{
// Transform the 4 corners of the rectangle
const Vector3f points[] =
{
transformPoint(rectangle.left, rectangle.top),
transformPoint(rectangle.left, rectangle.top + rectangle.height),
transformPoint(rectangle.left + rectangle.width, rectangle.top),
transformPoint(rectangle.left + rectangle.width, rectangle.top + rectangle.height)
};
// Compute the bounding rectangle of the transformed points
float left = points[0].x;
float top = points[0].y;
float right = points[0].x;
float bottom = points[0].y;
for (int i = 1; i < 4; ++i)
{
if (points[i].x < left) left = points[i].x;
else if (points[i].x > right) right = points[i].x;
if (points[i].y < top) top = points[i].y;
else if (points[i].y > bottom) bottom = points[i].y;
}
return FloatRect(left, top, right - left, bottom - top);
}
////////////////////////////////////////////////////////////
FloatBox Transform::transformBox(const FloatBox& box) const
{
// Transform the 8 corners of the box
const Vector3f points[] =
{
transformPoint(box.left, box.top, box.front),
transformPoint(box.left, box.top + box.height, box.front),
transformPoint(box.left + box.width, box.top, box.front),
transformPoint(box.left + box.width, box.top + box.height, box.front),
transformPoint(box.left, box.top, box.front + box.depth),
transformPoint(box.left, box.top + box.height, box.front + box.depth),
transformPoint(box.left + box.width, box.top, box.front + box.depth),
transformPoint(box.left + box.width, box.top + box.height, box.front + box.depth)
};
// Compute the bounding box of the transformed points
float left = points[0].x;
float top = points[0].y;
float front = points[0].z;
float right = points[0].x;
float bottom = points[0].y;
float back = points[0].z;
for (int i = 1; i < 4; ++i)
{
if (points[i].x < left) left = points[i].x;
else if (points[i].x > right) right = points[i].x;
if (points[i].y < top) top = points[i].y;
else if (points[i].y > bottom) bottom = points[i].y;
if (points[i].z < front) front = points[i].z;
else if (points[i].z > back) back = points[i].z;
}
return FloatBox(left, top, front, right - left, bottom - top, back - front);
}
////////////////////////////////////////////////////////////
Transform& Transform::combine(const Transform& transform)
{
const float* a = m_matrix;
const float* b = transform.m_matrix;
*this = Transform(a[0] * b[0] + a[4] * b[1] + a[8] * b[2] + a[12] * b[3],
a[0] * b[4] + a[4] * b[5] + a[8] * b[6] + a[12] * b[7],
a[0] * b[8] + a[4] * b[9] + a[8] * b[10] + a[12] * b[11],
a[0] * b[12] + a[4] * b[13] + a[8] * b[14] + a[12] * b[15],
a[1] * b[0] + a[5] * b[1] + a[9] * b[2] + a[13] * b[3],
a[1] * b[4] + a[5] * b[5] + a[9] * b[6] + a[13] * b[7],
a[1] * b[8] + a[5] * b[9] + a[9] * b[10] + a[13] * b[11],
a[1] * b[12] + a[5] * b[13] + a[9] * b[14] + a[13] * b[15],
a[2] * b[0] + a[6] * b[1] + a[10] * b[2] + a[14] * b[3],
a[2] * b[4] + a[6] * b[5] + a[10] * b[6] + a[14] * b[7],
a[2] * b[8] + a[6] * b[9] + a[10] * b[10] + a[14] * b[11],
a[2] * b[12] + a[6] * b[13] + a[10] * b[14] + a[14] * b[15],
a[3] * b[0] + a[7] * b[1] + a[11] * b[2] + a[15] * b[3],
a[3] * b[4] + a[7] * b[5] + a[11] * b[6] + a[15] * b[7],
a[3] * b[8] + a[7] * b[9] + a[11] * b[10] + a[15] * b[11],
a[3] * b[12] + a[7] * b[13] + a[11] * b[14] + a[15] * b[15]);
return *this;
}
////////////////////////////////////////////////////////////
Transform& Transform::translate(float x, float y, float z)
{
Transform translation(1, 0, 0, x,
0, 1, 0, y,
0, 0, 1, z,
0, 0, 0, 1);
return combine(translation);
}
////////////////////////////////////////////////////////////
Transform& Transform::translate(const Vector3f& offset)
{
return translate(offset.x, offset.y, offset.z);
}
////////////////////////////////////////////////////////////
Transform& Transform::rotate(float angle)
{
float rad = angle * 3.141592654f / 180.f;
float cos = std::cos(rad);
float sin = std::sin(rad);
Transform rotation(cos, -sin, 0, 0,
sin, cos, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
return combine(rotation);
}
////////////////////////////////////////////////////////////
Transform& Transform::rotate(float angle, float centerX, float centerY)
{
float rad = angle * 3.141592654f / 180.f;
float cos = std::cos(rad);
float sin = std::sin(rad);
Transform rotation(cos, -sin, 0, centerX * (1 - cos) + centerY * sin,
sin, cos, 0, centerY * (1 - cos) - centerX * sin,
0, 0, 1, 0,
0, 0, 0, 1);
return combine(rotation);
}
////////////////////////////////////////////////////////////
Transform& Transform::rotate(float angle, const Vector2f& center)
{
return rotate(angle, center.x, center.y);
}
////////////////////////////////////////////////////////////
Transform& Transform::rotate(float angle, const Vector3f& axis)
{
// Normalize axis
float norm = std::sqrt(axis.x * axis.x + axis.y * axis.y + axis.z * axis.z);
if(norm == 0.f)
return *this;
float x = axis.x / norm;
float y = axis.y / norm;
float z = axis.z / norm;
float rad = angle * 3.141592654f / 180.f;
float cos = std::cos(rad);
float sin = std::sin(rad);
Transform rotation(x * x * (1 - cos) + cos, x * y * (1 - cos) - z * sin, x * z * (1 - cos) + y * sin, 0,
y * x * (1 - cos) + z * sin, y * y * (1 - cos) + cos, y * z * (1 - cos) - x * sin, 0,
x * z * (1 - cos) - y * sin, y * z * (1 - cos) + x * sin, z * z * (1 - cos) + cos, 0,
0, 0, 0, 1);
return combine(rotation);
}
////////////////////////////////////////////////////////////
Transform& Transform::scale(float scaleX, float scaleY, float scaleZ)
{
Transform scaling(scaleX, 0, 0, 0,
0, scaleY, 0, 0,
0, 0, scaleZ, 0,
0, 0, 0, 1);
return combine(scaling);
}
////////////////////////////////////////////////////////////
Transform& Transform::scale(float scaleX, float scaleY, float centerX, float centerY)
{
Transform scaling(scaleX, 0, 0, centerX * (1 - scaleX),
0, scaleY, 0, centerY * (1 - scaleY),
0, 0, 1, 0,
0, 0, 0, 1);
return combine(scaling);
}
////////////////////////////////////////////////////////////
Transform& Transform::scale(float scaleX, float scaleY, float scaleZ, float centerX, float centerY, float centerZ)
{
Transform scaling(scaleX, 0, 0, centerX * (1 - scaleX),
0, scaleY, 0, centerY * (1 - scaleY),
0, 0, scaleZ, centerZ * (1 - scaleZ),
0, 0, 0, 1);
return combine(scaling);
}
////////////////////////////////////////////////////////////
Transform& Transform::scale(const Vector3f& factors)
{
return scale(factors.x, factors.y, factors.z);
}
////////////////////////////////////////////////////////////
Transform& Transform::scale(const Vector3f& factors, const Vector3f& center)
{
return scale(factors.x, factors.y, factors.z, center.x, center.y, center.z);
}
////////////////////////////////////////////////////////////
Transform operator *(const Transform& left, const Transform& right)
{
return Transform(left).combine(right);
}
////////////////////////////////////////////////////////////
Transform& operator *=(Transform& left, const Transform& right)
{
return left.combine(right);
}
////////////////////////////////////////////////////////////
Vector3f operator *(const Transform& left, const Vector3f& right)
{
return left.transformPoint(right);
}
} // namespace cpp3ds