1 #ifndef SDK_MATRIX_4X4_H_
11 #define SDK_MATRIX_4X4_H_
34 template <
class _SDKMatrix4X4>
38 typedef _SDKMatrix4X4 SDKMatrix4X4;
39 typedef typename SDKMatrix4X4::coordinate_type coordinate_type;
48 explicit Matrix4X4T(
const float* values) noexcept {
49 for (
int row = 0; row < 4; ++row) {
50 for (
int col = 0; col < 4; ++col) {
51 this->_m[row][col] = coordinate_type(values[row * 4 + col]);
55 explicit Matrix4X4T(
const double* values) noexcept {
56 for (
int row = 0; row < 4; ++row) {
57 for (
int col = 0; col < 4; ++col) {
58 this->_m[row][col] = coordinate_type(values[row * 4 + col]);
63 this->_11 = coordinate_type(m._11), this->_12 = coordinate_type(m._12), this->_13 = 0, this->_14 = 0;
64 this->_21 = coordinate_type(m._21), this->_22 = coordinate_type(m._22), this->_23 = 0, this->_24 = 0;
65 this->_31 = coordinate_type(m._31), this->_32 = coordinate_type(m._32), this->_33 = 1, this->_34 = 0;
66 this->_41 = 0, this->_42 = 0, this->_43 = 0, this->_44 = 1;
69 this->_11 = coordinate_type(m._11), this->_12 = coordinate_type(m._12), this->_13 = 0, this->_14 = 0;
70 this->_21 = coordinate_type(m._21), this->_22 = coordinate_type(m._22), this->_23 = 0, this->_24 = 0;
71 this->_31 = coordinate_type(m._31), this->_32 = coordinate_type(m._32), this->_33 = 1, this->_34 = 0;
72 this->_41 = 0, this->_42 = 0, this->_43 = 0, this->_44 = 1;
75 this->_11 = coordinate_type(m._11), this->_12 = coordinate_type(m._12), this->_13 = coordinate_type(m._13), this->_14 = 0;
76 this->_21 = coordinate_type(m._21), this->_22 = coordinate_type(m._22), this->_23 = coordinate_type(m._23), this->_24 = 0;
77 this->_31 = coordinate_type(m._31), this->_32 = coordinate_type(m._32), this->_33 = coordinate_type(m._33), this->_34 = 0;
78 this->_41 = 0, this->_42 = 0, this->_43 = 0, this->_44 = 1;
81 this->_11 = coordinate_type(m._11), this->_12 = coordinate_type(m._12), this->_13 = coordinate_type(m._13), this->_14 = 0;
82 this->_21 = coordinate_type(m._21), this->_22 = coordinate_type(m._22), this->_23 = coordinate_type(m._23), this->_24 = 0;
83 this->_31 = coordinate_type(m._31), this->_32 = coordinate_type(m._32), this->_33 = coordinate_type(m._33), this->_34 = 0;
84 this->_41 = 0, this->_42 = 0, this->_43 = 0, this->_44 = 1;
96 for (
int row = 0; row < 4; ++row) {
97 this->_m[row][0] = coordinate_type(m._m[row][0]);
98 this->_m[row][1] = coordinate_type(m._m[row][1]);
99 this->_m[row][2] = coordinate_type(m._m[row][2]);
100 this->_m[row][3] = coordinate_type(m._m[row][3]);
107 for (
int row = 0; row < 4; ++row) {
108 this->_m[row][0] = coordinate_type(m._m[row][0]);
109 this->_m[row][1] = coordinate_type(m._m[row][1]);
110 this->_m[row][2] = coordinate_type(m._m[row][2]);
111 this->_m[row][3] = coordinate_type(m._m[row][3]);
118 const SDKMatrix4X4& get()
const {
return *
this; }
119 SDKMatrix4X4& get() {
return *
this; }
138 this->_11 = this->_22 = this->_33 = this->_44 = 1;
139 this->_12 = this->_13 = this->_14 = this->_21 = 0;
140 this->_23 = this->_24 = this->_31 = this->_32 = 0;
141 this->_34 = this->_41 = this->_42 = this->_43 = 0;
145 _MyType& Transpose() noexcept
148 v = this->_12; this->_12 = this->_21; this->_21 = coordinate_type(v);
149 v = this->_13; this->_13 = this->_31; this->_31 = coordinate_type(v);
150 v = this->_14; this->_14 = this->_41; this->_41 = coordinate_type(v);
151 v = this->_23; this->_23 = this->_32; this->_32 = coordinate_type(v);
152 v = this->_24; this->_24 = this->_42; this->_42 = coordinate_type(v);
153 v = this->_34; this->_34 = this->_43; this->_43 = coordinate_type(v);
165 const PointF3D& camera_up) noexcept
169 auto dot = [](
const PointF3D& a,
const PointF3D& b) ->
float
170 {
return a.x * b.x + a.y * b.y + a.z * b.z; };
172 auto normalize = [](
const PointF3D& v) -> PointF3D
174 const float len = sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
176 ? PointF3D(0.0f, 0.0f, 0.0f)
177 : PointF3D(v.x / len, v.y / len, v.z / len);
179 auto cross = [](
const PointF3D& a,
const PointF3D& b) -> PointF3D
182 a.y * b.z - b.y * a.z,
183 a.z * b.x - b.z * a.x,
184 a.x * b.y - b.x * a.y);
187 const PointF3D f = normalize(PointF3D(
188 look_pos.x - camera_pos.x,
189 look_pos.y - camera_pos.y,
190 look_pos.z - camera_pos.z));
191 const PointF3D s = normalize(cross(f, camera_up));
192 const PointF3D u = cross(s, f);
203 this->_41 = -dot(s, camera_pos);
204 this->_42 = -dot(u, camera_pos);
205 this->_43 = dot(f, camera_pos);
209 inline _MyType&
LookAt(
const PointF3D& camera_pos,
const PointF3D& look_pos) noexcept
210 {
return LookAt(camera_pos, look_pos, PointF3D(0.0f, 0.0f, 1.0f)); }
217 return RotateAroundZ(angle_deg);
220 _MyType& RotateAroundX(
const coordinate_type angle_deg) noexcept
224 double angle_rad = (angle_deg / 180.0) *
kPI;
227 const double s = sin(angle_rad);
228 const double c = cos(angle_rad);
230 this->_22 = coordinate_type(c);
231 this->_23 = coordinate_type(s);
232 this->_32 = coordinate_type(-s);
233 this->_33 = coordinate_type(c);
238 _MyType& RotateAroundY(
const coordinate_type angle_deg) noexcept
242 double angle_rad = (angle_deg / 180.0) *
kPI;
245 const double s = sin(angle_rad);
246 const double c = cos(angle_rad);
248 this->_33 = coordinate_type(c);
249 this->_31 = coordinate_type(s);
250 this->_13 = coordinate_type(-s);
251 this->_11 = coordinate_type(c);
256 _MyType& RotateAroundZ(
const coordinate_type angle_deg) noexcept
260 double angle_rad = (angle_deg / 180.0) *
kPI;
263 const double s = sin(angle_rad);
264 const double c = cos(angle_rad);
266 this->_11 = coordinate_type(c);
267 this->_12 = coordinate_type(s);
268 this->_21 = coordinate_type(-s);
269 this->_22 = coordinate_type(c);
276 const coordinate_type scale_x,
277 const coordinate_type scale_y,
278 const coordinate_type scale_z = 1) noexcept
289 const coordinate_type x,
290 const coordinate_type y,
291 const coordinate_type z = 0) noexcept
303 *
this = Multiply(*
this, m2);
307 _MyType operator*(
const SDKMatrix4X4& m2)
const
309 return Multiply(*
this, m2);
315 PointD3D vec_3d(point.x, point.y, 0.0);
317 return PointD2D(vec_3d.x, vec_3d.y);
322 PointF3D vec_3d(point.x, point.y, 0.0f);
324 return PointF2D(vec_3d.x, vec_3d.y);
329 PointD3D vec_3d(point.x, point.y, 0);
332 return PointI2D(SDKInt32(floor(res_d.x + 0.5)),
333 SDKInt32(floor(res_d.y + 0.5)));
339 double x = (point.x * this->_11 + point.y * this->_21 + point.z * this->_31 + this->_41);
340 double y = (point.x * this->_12 + point.y * this->_22 + point.z * this->_32 + this->_42);
341 double z = (point.x * this->_13 + point.y * this->_23 + point.z * this->_33 + this->_43);
342 double w = (point.x * this->_14 + point.y * this->_24 + point.z * this->_34 + this->_44);
348 return PointD3D(x, y, z);
353 PointD3D point_d(point);
355 return PointF3D(
float(res_d.x),
361 static _MyType Multiply(
362 const SDKMatrix4X4& m1,
const SDKMatrix4X4& m2) noexcept;
367 return this->
Invert(*
this);
375 inverted_matrix = inv;
380 template <
class _SDKMatrixD4X4>
385 * (double(this->_22) * this->_33 * this->_44
386 - double(this->_22) * this->_34 * this->_43
387 - double(this->_32) * this->_23 * this->_44
388 + double(this->_32) * this->_24 * this->_43
389 + double(this->_42) * this->_23 * this->_34
390 - double(this->_42) * this->_24 * this->_33)
392 * (double(this->_21) * this->_33 * this->_44
393 + double(this->_21) * this->_34 * this->_43
394 + double(this->_31) * this->_23 * this->_44
395 + double(this->_31) * this->_24 * this->_43
396 - double(this->_41) * this->_23 * this->_34
397 + double(this->_41) * this->_24 * this->_33)
399 * (double(this->_21) * this->_32 * this->_44
400 - double(this->_21) * this->_34 * this->_42
401 - double(this->_31) * this->_22 * this->_44
402 + double(this->_31) * this->_24 * this->_42
403 + double(this->_41) * this->_22 * this->_34
404 - double(this->_41) * this->_24 * this->_32)
406 * (double(this->_21) * this->_32 * this->_43
407 + double(this->_21) * this->_33 * this->_42
408 + double(this->_31) * this->_22 * this->_43
409 + double(this->_31) * this->_23 * this->_42
410 - double(this->_41) * this->_22 * this->_33
411 + double(this->_41) * this->_23 * this->_32);
415 template <
class _SDKMatrixD4X4>
417 const SDKMatrix4X4& m1,
const SDKMatrix4X4& m2) noexcept
422 template <
class _SDKMatrixD4X4>
428 + double(this->_22) * this->_33 * this->_44
429 - double(this->_22) * this->_34 * this->_43
430 - double(this->_32) * this->_23 * this->_44
431 + double(this->_32) * this->_24 * this->_43
432 + double(this->_42) * this->_23 * this->_34
433 - double(this->_42) * this->_24 * this->_33;
435 - double(this->_12) * this->_33 * this->_44
436 + double(this->_12) * this->_34 * this->_43
437 + double(this->_32) * this->_13 * this->_44
438 - double(this->_32) * this->_14 * this->_43
439 - double(this->_42) * this->_13 * this->_34
440 + double(this->_42) * this->_14 * this->_33;
442 + double(this->_12) * this->_23 * this->_44
443 - double(this->_12) * this->_24 * this->_43
444 - double(this->_22) * this->_13 * this->_44
445 + double(this->_22) * this->_14 * this->_43
446 + double(this->_42) * this->_13 * this->_24
447 - double(this->_42) * this->_14 * this->_23;
449 - double(this->_12) * this->_23 * this->_34
450 + double(this->_12) * this->_24 * this->_33
451 + double(this->_22) * this->_13 * this->_34
452 - double(this->_22) * this->_14 * this->_33
453 - double(this->_32) * this->_13 * this->_24
454 + double(this->_32) * this->_14 * this->_23;
461 + this->_11 * inv._11 + this->_21 * inv._12
462 + this->_31 * inv._13 + this->_41 * inv._14;
468 - double(this->_21) * this->_33 * this->_44
469 + double(this->_21) * this->_34 * this->_43
470 + double(this->_31) * this->_23 * this->_44
471 - double(this->_31) * this->_24 * this->_43
472 - double(this->_41) * this->_23 * this->_34
473 + double(this->_41) * this->_24 * this->_33;
475 + double(this->_11) * this->_33 * this->_44
476 - double(this->_11) * this->_34 * this->_43
477 - double(this->_31) * this->_13 * this->_44
478 + double(this->_31) * this->_14 * this->_43
479 + double(this->_41) * this->_13 * this->_34
480 - double(this->_41) * this->_14 * this->_33;
482 - double(this->_11) * this->_23 * this->_44
483 + double(this->_11) * this->_24 * this->_43
484 + double(this->_21) * this->_13 * this->_44
485 - double(this->_21) * this->_14 * this->_43
486 - double(this->_41) * this->_13 * this->_24
487 + double(this->_41) * this->_14 * this->_23;
489 + double(this->_11) * this->_23 * this->_34
490 - double(this->_11) * this->_24 * this->_33
491 - double(this->_21) * this->_13 * this->_34
492 + double(this->_21) * this->_14 * this->_33
493 + double(this->_31) * this->_13 * this->_24
494 - double(this->_31) * this->_14 * this->_23;
497 + double(this->_21) * this->_32 * this->_44
498 - double(this->_21) * this->_34 * this->_42
499 - double(this->_31) * this->_22 * this->_44
500 + double(this->_31) * this->_24 * this->_42
501 + double(this->_41) * this->_22 * this->_34
502 - double(this->_41) * this->_24 * this->_32;
504 - double(this->_11) * this->_32 * this->_44
505 + double(this->_11) * this->_34 * this->_42
506 + double(this->_31) * this->_12 * this->_44
507 - double(this->_31) * this->_14 * this->_42
508 - double(this->_41) * this->_12 * this->_34
509 + double(this->_41) * this->_14 * this->_32;
511 + double(this->_11) * this->_22 * this->_44
512 - double(this->_11) * this->_24 * this->_42
513 - double(this->_21) * this->_12 * this->_44
514 + double(this->_21) * this->_14 * this->_42
515 + double(this->_41) * this->_12 * this->_24
516 - double(this->_41) * this->_14 * this->_22;
518 - double(this->_11) * this->_22 * this->_34
519 + double(this->_11) * this->_24 * this->_32
520 + double(this->_21) * this->_12 * this->_34
521 - double(this->_21) * this->_14 * this->_32
522 - double(this->_31) * this->_12 * this->_24
523 + double(this->_31) * this->_14 * this->_22;
526 - double(this->_21) * this->_32 * this->_43
527 + double(this->_21) * this->_33 * this->_42
528 + double(this->_31) * this->_22 * this->_43
529 - double(this->_31) * this->_23 * this->_42
530 - double(this->_41) * this->_22 * this->_33
531 + double(this->_41) * this->_23 * this->_32;
533 + double(this->_11) * this->_32 * this->_43
534 - double(this->_11) * this->_33 * this->_42
535 - double(this->_31) * this->_12 * this->_43
536 + double(this->_31) * this->_13 * this->_42
537 + double(this->_41) * this->_12 * this->_33
538 - double(this->_41) * this->_13 * this->_32;
540 - double(this->_11) * this->_22 * this->_43
541 + double(this->_11) * this->_23 * this->_42
542 + double(this->_21) * this->_12 * this->_43
543 - double(this->_21) * this->_13 * this->_42
544 - double(this->_41) * this->_12 * this->_23
545 + double(this->_41) * this->_13 * this->_22;
547 + double(this->_11) * this->_22 * this->_33
548 - double(this->_11) * this->_23 * this->_32
549 - double(this->_21) * this->_12 * this->_33
550 + double(this->_21) * this->_13 * this->_32
551 + double(this->_31) * this->_12 * this->_23
552 - double(this->_31) * this->_13 * this->_22;
554 inverted_matrix._11 = inv._11 * det;
555 inverted_matrix._12 = inv._12 * det;
556 inverted_matrix._13 = inv._13 * det;
557 inverted_matrix._14 = inv._14 * det;
559 inverted_matrix._21 = inv._21 * det;
560 inverted_matrix._22 = inv._22 * det;
561 inverted_matrix._23 = inv._23 * det;
562 inverted_matrix._24 = inv._24 * det;
564 inverted_matrix._31 = inv._31 * det;
565 inverted_matrix._32 = inv._32 * det;
566 inverted_matrix._33 = inv._33 * det;
567 inverted_matrix._34 = inv._34 * det;
569 inverted_matrix._41 = inv._41 * det;
570 inverted_matrix._42 = inv._42 * det;
571 inverted_matrix._43 = inv._43 * det;
572 inverted_matrix._44 = inv._44 * det;
577 typedef Matrix4X4T<SDKMatrix4X4> Matrix4X4, CMatrix4X4;
578 typedef Matrix4X4T<SDKMatrixD4X4> MatrixD4X4, CMatrixD4X4;
579 typedef Matrix4X4T<SDKMatrixF4X4> MatrixF4X4, CMatrixF4X4;
582 #endif // SDK_MATRIX_4X4_H_