Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
Matrix4.hpp
1// INFINITY_API_PUBLIC
2
3#pragma once
4
5#include <Infinity/Types/Core/Value.hpp>
6#include <Infinity/Types/Containers/Array.hpp>
7#include <Infinity/Types/Math/Vector4.hpp>
8
9#include <cmath>
10
12{
13
91 template<typename T>
92 struct INFINITY_API_TEMPLATE t_Matrix4
93 {
94 public:
102 t_Vector4<T> value[4];
103
115 constexpr t_Matrix4() :
116 value{{1, 0, 0, 0},
117 {0, 1, 0, 0},
118 {0, 0, 1, 0},
119 {0, 0, 0, 1}} {}
120
138 constexpr explicit t_Matrix4(T v) :
139 value{{v, 0, 0, 0},
140 {0, v, 0, 0},
141 {0, 0, v, 0},
142 {0, 0, 0, v}} {}
143
162 constexpr t_Matrix4(T v0, T v1, T v2, T v3,
163 T v4, T v5, T v6, T v7,
164 T v8, T v9, T v10, T v11,
165 T v12, T v13, T v14, T v15) :
166 value{{v0, v1, v2, v3},
167 {v4, v5, v6, v7},
168 {v8, v9, v10, v11},
169 {v12, v13, v14, v15}}
170 {
171 }
172
185 constexpr explicit t_Matrix4(T v[16]) :
186 value{{v[0], v[1], v[2], v[3]},
187 {v[4], v[5], v[6], v[7]},
188 {v[8], v[9], v[10], v[11]},
189 {v[12], v[13], v[14], v[15]}} {}
190
209 constexpr t_Matrix4(const t_Vector4<T>& c0,
210 const t_Vector4<T>& c1,
211 const t_Vector4<T>& c2,
212 const t_Vector4<T>& c3) :
213 value{c0, c1, c2, c3}
214 {
215 }
216
225 template<typename R>
226 explicit t_Matrix4(const t_Matrix4<R>& rhs)
227 {
228 value[0] = rhs[0];
229 value[1] = rhs[1];
230 value[2] = rhs[2];
231 value[3] = rhs[3];
232 }
233
239 constexpr size_t size() const { return 16; }
240
246 constexpr size_t columns() const { return 4; }
247
253 constexpr size_t rows() const { return 4; }
254
268 t_Vector4<T>& operator[](size_t c) { return value[c]; }
269
278 const t_Vector4<T>& operator[](size_t c) const { return value[c]; }
279
294 T& operator()(size_t c, size_t r) { return value[c][r]; }
295
305 T operator()(size_t c, size_t r) const { return value[c][r]; }
306
316 template<typename R>
318 {
319 value[0] = rhs[0];
320 value[1] = rhs[1];
321 value[2] = rhs[2];
322 value[3] = rhs[3];
323 return *this;
324 }
325
333 void set(T v0, T v1, T v2, T v3,
334 T v4, T v5, T v6, T v7,
335 T v8, T v9, T v10, T v11,
336 T v12, T v13, T v14, T v15)
337 {
338 value[0].set(v0, v1, v2, v3);
339 value[1].set(v4, v5, v6, v7);
340 value[2].set(v8, v9, v10, v11);
341 value[3].set(v12, v13, v14, v15);
342 }
343
350 template<typename R>
351 void set(const t_Matrix4<R>& rhs)
352 {
353 value[0] = rhs[0];
354 value[1] = rhs[1];
355 value[2] = rhs[2];
356 value[3] = rhs[3];
357 }
358
368 T* data() { return value[0].data(); }
369
375 const T* data() const { return value[0].data(); }
376
390 t_Matrix4 operator*(T scalar) const
391 {
392 return t_Matrix4(
393 value[0].x * scalar, value[0].y * scalar, value[0].z * scalar, value[0].w * scalar,
394 value[1].x * scalar, value[1].y * scalar, value[1].z * scalar, value[1].w * scalar,
395 value[2].x * scalar, value[2].y * scalar, value[2].z * scalar, value[2].w * scalar,
396 value[3].x * scalar, value[3].y * scalar, value[3].z * scalar, value[3].w * scalar
397 );
398 }
399
409 {
410 value[0] *= scalar;
411 value[1] *= scalar;
412 value[2] *= scalar;
413 value[3] *= scalar;
414 return *this;
415 }
416
435 t_Matrix4 operator*(const t_Matrix4& rhs) const
436 {
437 return t_Matrix4(
438 value[0].x * rhs.value[0].x + value[0].y * rhs.value[1].x + value[0].z * rhs.value[2].x + value[0].w * rhs.value[3].x,
439 value[0].x * rhs.value[0].y + value[0].y * rhs.value[1].y + value[0].z * rhs.value[2].y + value[0].w * rhs.value[3].y,
440 value[0].x * rhs.value[0].z + value[0].y * rhs.value[1].z + value[0].z * rhs.value[2].z + value[0].w * rhs.value[3].z,
441 value[0].x * rhs.value[0].w + value[0].y * rhs.value[1].w + value[0].z * rhs.value[2].w + value[0].w * rhs.value[3].w,
442
443 value[1].x * rhs.value[0].x + value[1].y * rhs.value[1].x + value[1].z * rhs.value[2].x + value[1].w * rhs.value[3].x,
444 value[1].x * rhs.value[0].y + value[1].y * rhs.value[1].y + value[1].z * rhs.value[2].y + value[1].w * rhs.value[3].y,
445 value[1].x * rhs.value[0].z + value[1].y * rhs.value[1].z + value[1].z * rhs.value[2].z + value[1].w * rhs.value[3].z,
446 value[1].x * rhs.value[0].w + value[1].y * rhs.value[1].w + value[1].z * rhs.value[2].w + value[1].w * rhs.value[3].w,
447
448 value[2].x * rhs.value[0].x + value[2].y * rhs.value[1].x + value[2].z * rhs.value[2].x + value[2].w * rhs.value[3].x,
449 value[2].x * rhs.value[0].y + value[2].y * rhs.value[1].y + value[2].z * rhs.value[2].y + value[2].w * rhs.value[3].y,
450 value[2].x * rhs.value[0].z + value[2].y * rhs.value[1].z + value[2].z * rhs.value[2].z + value[2].w * rhs.value[3].z,
451 value[2].x * rhs.value[0].w + value[2].y * rhs.value[1].w + value[2].z * rhs.value[2].w + value[2].w * rhs.value[3].w,
452
453 value[3].x * rhs.value[0].x + value[3].y * rhs.value[1].x + value[3].z * rhs.value[2].x + value[3].w * rhs.value[3].x,
454 value[3].x * rhs.value[0].y + value[3].y * rhs.value[1].y + value[3].z * rhs.value[2].y + value[3].w * rhs.value[3].y,
455 value[3].x * rhs.value[0].z + value[3].y * rhs.value[1].z + value[3].z * rhs.value[2].z + value[3].w * rhs.value[3].z,
456 value[3].x * rhs.value[0].w + value[3].y * rhs.value[1].w + value[3].z * rhs.value[2].w + value[3].w * rhs.value[3].w
457 );
458 }
459
469 {
470 *this = *this * rhs;
471 return *this;
472 }
473
497 {
498 return t_Vector4<T>(
499 value[0].x * v.x + value[0].y * v.y + value[0].z * v.z,
500 value[1].x * v.x + value[1].y * v.y + value[1].z * v.z,
501 value[2].x * v.x + value[2].y * v.y + value[2].z * v.z,
502 value[3].x * v.x + value[3].y * v.y + value[3].z * v.z
503 );
504 }
505
514 t_Matrix4 operator+(const t_Matrix4& rhs) const
515 {
516 return t_Matrix4(
517 value[0].x + rhs.value[0].x, value[0].y + rhs.value[0].y, value[0].z + rhs.value[0].z, value[0].w + rhs.value[0].w,
518 value[1].x + rhs.value[1].x, value[1].y + rhs.value[1].y, value[1].z + rhs.value[1].z, value[1].w + rhs.value[1].w,
519 value[2].x + rhs.value[2].x, value[2].y + rhs.value[2].y, value[2].z + rhs.value[2].z, value[2].w + rhs.value[2].w,
520 value[3].x + rhs.value[3].x, value[3].y + rhs.value[3].y, value[3].z + rhs.value[3].z, value[3].w + rhs.value[3].w
521 );
522 }
523
533 {
534 value[0] += rhs.value[0];
535 value[1] += rhs.value[1];
536 value[2] += rhs.value[2];
537 value[3] += rhs.value[3];
538 return *this;
539 }
540
549 t_Matrix4 operator-(const t_Matrix4& rhs) const
550 {
551 return t_Matrix4(
552 value[0].x - rhs.value[0].x, value[0].y - rhs.value[0].y, value[0].z - rhs.value[0].z, value[0].w - rhs.value[0].w,
553 value[1].x - rhs.value[1].x, value[1].y - rhs.value[1].y, value[1].z - rhs.value[1].z, value[1].w - rhs.value[1].w,
554 value[2].x - rhs.value[2].x, value[2].y - rhs.value[2].y, value[2].z - rhs.value[2].z, value[2].w - rhs.value[2].w,
555 value[3].x - rhs.value[3].x, value[3].y - rhs.value[3].y, value[3].z - rhs.value[3].z, value[3].w - rhs.value[3].w
556 );
557 }
558
568 {
569 value[0] -= rhs.value[0];
570 value[1] -= rhs.value[1];
571 value[2] -= rhs.value[2];
572 value[3] -= rhs.value[3];
573 return *this;
574 }
575
587 t_Matrix4 operator/(T scalar) const
588 {
589 if constexpr (std::is_floating_point_v<T>)
590 {
591 T inv = static_cast<T>(1.0) / scalar;
592 return t_Matrix4(
593 value[0].x * inv, value[0].y * inv, value[0].z * inv, value[0].w * inv,
594 value[1].x * inv, value[1].y * inv, value[1].z * inv, value[1].w * inv,
595 value[2].x * inv, value[2].y * inv, value[2].z * inv, value[2].w * inv,
596 value[3].x * inv, value[3].y * inv, value[3].z * inv, value[3].w * inv
597 );
598 }
599 else
600 {
601 return t_Matrix4(
602 value[0].x / scalar, value[0].y / scalar, value[0].z / scalar, value[0].w / scalar,
603 value[1].x / scalar, value[1].y / scalar, value[1].z / scalar, value[1].w / scalar,
604 value[2].x / scalar, value[2].y / scalar, value[2].z / scalar, value[2].w / scalar,
605 value[3].x / scalar, value[3].y / scalar, value[3].z / scalar, value[3].w / scalar
606 );
607 }
608 }
609
621 {
622 if constexpr (std::is_floating_point_v<T>)
623 {
624 T inv = static_cast<T>(1.0) / scalar;
625 value[0] *= inv;
626 value[1] *= inv;
627 value[2] *= inv;
628 value[3] *= inv;
629 }
630 else
631 {
632 value[0] /= scalar;
633 value[1] /= scalar;
634 value[2] /= scalar;
635 value[3] /= scalar;
636 }
637 return *this;
638 }
639
648 bool operator==(const t_Matrix4& rhs) const
649 {
650 return value[0] == rhs.value[0] &&
651 value[1] == rhs.value[1] &&
652 value[2] == rhs.value[2] &&
653 value[3] == rhs.value[3];
654 }
655
662 bool operator!=(const t_Matrix4& rhs) const
663 {
664 return !(*this == rhs);
665 }
666
676 bool operator<(const t_Matrix4& rhs) const
677 {
678 if (value[0] < rhs.value[0]) return true;
679 if (value[0] > rhs.value[0]) return false;
680 if (value[1] < rhs.value[1]) return true;
681 if (value[1] > rhs.value[1]) return false;
682 if (value[2] < rhs.value[2]) return true;
683 if (value[2] > rhs.value[2]) return false;
684 return value[3] < rhs.value[3];
685 }
686
706 {
707 return t_Matrix4(
708 value[0].x, value[1].x, value[2].x, value[3].x,
709 value[0].y, value[1].y, value[2].y, value[3].y,
710 value[0].z, value[1].z, value[2].z, value[3].z,
711 value[0].w, value[1].w, value[2].w, value[3].w
712 );
713 }
714
723 {
724 return t_Matrix4(
725 -value[0].x, -value[0].y, -value[0].z,
726 -value[1].x, -value[1].y, -value[1].z,
727 -value[2].x, -value[2].y, -value[2].z,
728 -value[3].x, -value[3].y, -value[3].z
729 );
730 }
731
739 explicit operator bool() const noexcept
740 {
741 return value[0] != t_Vector4<T>(0, 0, 0, 0) ||
742 value[1] != t_Vector4<T>(0, 0, 0, 0) ||
743 value[2] != t_Vector4<T>(0, 0, 0, 0) ||
744 value[3] != t_Vector4<T>(0, 0, 0, 0);
745 }
746 };
747
756
766
777 template<typename T>
778 inline std::ostream& operator<<(std::ostream& out, const t_Matrix4<T>& m)
779 {
780 out << m.value[0] << " " << m.value[1] << " " << m.value[2] << " " << m.value[3];
781 return out;
782 }
783
794 template<typename T>
795 inline std::istream& operator>>(std::istream& in, t_Matrix4<T>& m)
796 {
797 in >> m.value[0] >> m.value[1] >> m.value[2] >> m.value[3];
798 return in;
799 }
800
801}
802
803// Register Value<Matrix4> specializations in the type system
806
807// Register Array<Matrix4> specializations in the type system
Dynamic contiguous container for homogeneous elements in the Infinity type system.
Definition Array.hpp:77
Template wrapper for primitive types to integrate with the Infinity type system.
Definition Value.hpp:89
Definition Math.hpp:9
Template structure representing a 4x4 matrix.
Definition Matrix4.hpp:93
t_Vector4< T > & operator[](size_t c)
Column access operator (mutable).
Definition Matrix4.hpp:268
constexpr t_Matrix4(T v)
Constructs a diagonal matrix with the same value on the diagonal.
Definition Matrix4.hpp:138
t_Matrix4 operator-() const
Unary negation operator.
Definition Matrix4.hpp:722
constexpr t_Matrix4(T v[16])
Constructs a matrix from an array of sixteen elements.
Definition Matrix4.hpp:185
t_Matrix4 & operator=(const t_Matrix4< R > &rhs)
Type-converting assignment operator.
Definition Matrix4.hpp:317
void set(const t_Matrix4< R > &rhs)
Sets this matrix equal to another matrix.
Definition Matrix4.hpp:351
bool operator==(const t_Matrix4 &rhs) const
Equality comparison operator.
Definition Matrix4.hpp:648
const T * data() const
Gets a const pointer to the underlying data.
Definition Matrix4.hpp:375
t_Vector4< T > value[4]
Column vectors comprising the matrix.
Definition Matrix4.hpp:102
t_Matrix4 & operator*=(T scalar)
Scalar multiplication assignment operator.
Definition Matrix4.hpp:408
void set(T v0, T v1, T v2, T v3, T v4, T v5, T v6, T v7, T v8, T v9, T v10, T v11, T v12, T v13, T v14, T v15)
Sets all sixteen matrix elements.
Definition Matrix4.hpp:333
t_Matrix4 operator*(T scalar) const
Scalar multiplication operator.
Definition Matrix4.hpp:390
t_Matrix4 & operator+=(const t_Matrix4 &rhs)
Matrix addition assignment operator.
Definition Matrix4.hpp:532
t_Matrix4 & operator/=(T scalar)
Scalar division assignment operator.
Definition Matrix4.hpp:620
t_Matrix4 & operator*=(const t_Matrix4 &rhs)
Matrix multiplication assignment operator.
Definition Matrix4.hpp:468
t_Matrix4(const t_Matrix4< R > &rhs)
Type-converting constructor.
Definition Matrix4.hpp:226
constexpr t_Matrix4(T v0, T v1, T v2, T v3, T v4, T v5, T v6, T v7, T v8, T v9, T v10, T v11, T v12, T v13, T v14, T v15)
Constructs a matrix from sixteen individual elements.
Definition Matrix4.hpp:162
const t_Vector4< T > & operator[](size_t c) const
Column access operator (const).
Definition Matrix4.hpp:278
t_Vector4< T > operator*(const t_Vector4< T > &v) const
Matrix-vector multiplication operator.
Definition Matrix4.hpp:496
t_Matrix4 operator+(const t_Matrix4 &rhs) const
Matrix addition operator.
Definition Matrix4.hpp:514
constexpr t_Matrix4(const t_Vector4< T > &c0, const t_Vector4< T > &c1, const t_Vector4< T > &c2, const t_Vector4< T > &c3)
Constructs a matrix from four column vectors.
Definition Matrix4.hpp:209
bool operator<(const t_Matrix4 &rhs) const
Less-than comparison operator for ordering.
Definition Matrix4.hpp:676
t_Matrix4 & operator-=(const t_Matrix4 &rhs)
Matrix subtraction assignment operator.
Definition Matrix4.hpp:567
bool operator!=(const t_Matrix4 &rhs) const
Inequality comparison operator.
Definition Matrix4.hpp:662
T & operator()(size_t c, size_t r)
Element access operator (mutable).
Definition Matrix4.hpp:294
constexpr t_Matrix4()
Default constructor.
Definition Matrix4.hpp:115
constexpr size_t rows() const
Gets the number of rows in the matrix.
Definition Matrix4.hpp:253
t_Matrix4 transpose() const
Computes the transpose of this matrix.
Definition Matrix4.hpp:705
t_Matrix4 operator*(const t_Matrix4 &rhs) const
Matrix multiplication operator.
Definition Matrix4.hpp:435
constexpr size_t columns() const
Gets the number of columns in the matrix.
Definition Matrix4.hpp:246
t_Matrix4 operator-(const t_Matrix4 &rhs) const
Matrix subtraction operator.
Definition Matrix4.hpp:549
T operator()(size_t c, size_t r) const
Element access operator (const).
Definition Matrix4.hpp:305
t_Matrix4 operator/(T scalar) const
Scalar division operator.
Definition Matrix4.hpp:587
T * data()
Gets a pointer to the underlying data.
Definition Matrix4.hpp:368
constexpr size_t size() const
Gets the total number of elements in the matrix.
Definition Matrix4.hpp:239
Template structure representing a 4-component vector.
Definition Vector4.hpp:85
T y
Definition Vector4.hpp:100
T x
Definition Vector4.hpp:100
T z
Definition Vector4.hpp:100