Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
Matrix3.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/Vector3.hpp>
8
9#include <cmath>
10
12{
13
81 template<typename T>
82 struct INFINITY_API_TEMPLATE t_Matrix3
83 {
84 public:
91 t_Vector3<T> value[3];
92
103 constexpr t_Matrix3() :
104 value{{1, 0, 0},
105 {0, 1, 0},
106 {0, 0, 1}} {}
107
124 constexpr explicit t_Matrix3(T v) :
125 value{{v, 0, 0},
126 {0, v, 0},
127 {0, 0, v}} {}
128
154 constexpr t_Matrix3(T v0, T v1, T v2,
155 T v3, T v4, T v5,
156 T v6, T v7, T v8) :
157 value{{v0, v1, v2},
158 {v3, v4, v5},
159 {v6, v7, v8}}
160 {
161 }
162
175 constexpr explicit t_Matrix3(T v[9]) :
176 value{{v[0], v[1], v[2]},
177 {v[3], v[4], v[5]},
178 {v[6], v[7], v[8]}} {}
179
196 constexpr t_Matrix3(const t_Vector3<T>& c0,
197 const t_Vector3<T>& c1,
198 const t_Vector3<T>& c2) :
199 value{c0, c1, c2}
200 {
201 }
202
211 template<typename R>
212 explicit t_Matrix3(const t_Matrix3<R>& rhs)
213 {
214 value[0] = rhs[0];
215 value[1] = rhs[1];
216 value[2] = rhs[2];
217 }
218
224 constexpr size_t size() const { return 9; }
225
231 constexpr size_t columns() const { return 3; }
232
238 constexpr size_t rows() const { return 3; }
239
253 t_Vector3<T>& operator[](size_t c) { return value[c]; }
254
263 const t_Vector3<T>& operator[](size_t c) const { return value[c]; }
264
279 T& operator()(size_t c, size_t r) { return value[c][r]; }
280
290 T operator()(size_t c, size_t r) const { return value[c][r]; }
291
301 template<typename R>
303 {
304 value[0] = rhs[0];
305 value[1] = rhs[1];
306 value[2] = rhs[2];
307 return *this;
308 }
309
317 void set(T v0, T v1, T v2,
318 T v3, T v4, T v5,
319 T v6, T v7, T v8)
320 {
321 value[0].set(v0, v1, v2);
322 value[1].set(v3, v4, v5);
323 value[2].set(v6, v7, v8);
324 }
325
332 template<typename R>
333 void set(const t_Matrix3<R>& rhs)
334 {
335 value[0] = rhs[0];
336 value[1] = rhs[1];
337 value[2] = rhs[2];
338 }
339
348 T* data() { return value[0].data(); }
349
355 const T* data() const { return value[0].data(); }
356
370 t_Matrix3 operator*(const t_Matrix3& rhs) const
371 {
372 return t_Matrix3(
373 value[0].x * rhs.value[0].x + value[0].y * rhs.value[1].x + value[0].z * rhs.value[2].x,
374 value[0].x * rhs.value[0].y + value[0].y * rhs.value[1].y + value[0].z * rhs.value[2].y,
375 value[0].x * rhs.value[0].z + value[0].y * rhs.value[1].z + value[0].z * rhs.value[2].z,
376
377 value[1].x * rhs.value[0].x + value[1].y * rhs.value[1].x + value[1].z * rhs.value[2].x,
378 value[1].x * rhs.value[0].y + value[1].y * rhs.value[1].y + value[1].z * rhs.value[2].y,
379 value[1].x * rhs.value[0].z + value[1].y * rhs.value[1].z + value[1].z * rhs.value[2].z,
380
381 value[2].x * rhs.value[0].x + value[2].y * rhs.value[1].x + value[2].z * rhs.value[2].x,
382 value[2].x * rhs.value[0].y + value[2].y * rhs.value[1].y + value[2].z * rhs.value[2].y,
383 value[2].x * rhs.value[0].z + value[2].y * rhs.value[1].z + value[2].z * rhs.value[2].z
384 );
385 }
386
396 {
397 *this = *this * rhs;
398 return *this;
399 }
400
414 t_Matrix3 operator*(T scalar) const
415 {
416 return t_Matrix3(
417 value[0].x * scalar, value[0].y * scalar, value[0].z * scalar,
418 value[1].x * scalar, value[1].y * scalar, value[1].z * scalar,
419 value[2].x * scalar, value[2].y * scalar, value[2].z * scalar
420 );
421 }
422
432 {
433 value[0] *= scalar;
434 value[1] *= scalar;
435 value[2] *= scalar;
436 return *this;
437 }
438
454 {
455 return t_Vector3<T>(
456 value[0].x * v.x + value[0].y * v.y + value[0].z * v.z,
457 value[1].x * v.x + value[1].y * v.y + value[1].z * v.z,
458 value[2].x * v.x + value[2].y * v.y + value[2].z * v.z
459 );
460 }
461
470 t_Matrix3 operator+(const t_Matrix3& rhs) const
471 {
472 return t_Matrix3(
473 value[0].x + rhs.value[0].x, value[0].y + rhs.value[0].y, value[0].z + rhs.value[0].z,
474 value[1].x + rhs.value[1].x, value[1].y + rhs.value[1].y, value[1].z + rhs.value[1].z,
475 value[2].x + rhs.value[2].x, value[2].y + rhs.value[2].y, value[2].z + rhs.value[2].z
476 );
477 }
478
488 {
489 value[0] += rhs.value[0];
490 value[1] += rhs.value[1];
491 value[2] += rhs.value[2];
492 return *this;
493 }
494
503 t_Matrix3 operator-(const t_Matrix3& rhs) const
504 {
505 return t_Matrix3(
506 value[0].x - rhs.value[0].x, value[0].y - rhs.value[0].y, value[0].z - rhs.value[0].z,
507 value[1].x - rhs.value[1].x, value[1].y - rhs.value[1].y, value[1].z - rhs.value[1].z,
508 value[2].x - rhs.value[2].x, value[2].y - rhs.value[2].y, value[2].z - rhs.value[2].z
509 );
510 }
511
521 {
522 value[0] -= rhs.value[0];
523 value[1] -= rhs.value[1];
524 value[2] -= rhs.value[2];
525 return *this;
526 }
527
539 t_Matrix3 operator/(T scalar) const
540 {
541 if constexpr (std::is_floating_point_v<T>)
542 {
543 T inv = static_cast<T>(1.0) / scalar;
544 return t_Matrix3(
545 value[0].x * inv, value[0].y * inv, value[0].z * inv,
546 value[1].x * inv, value[1].y * inv, value[1].z * inv,
547 value[2].x * inv, value[2].y * inv, value[2].z * inv
548 );
549 }
550 else
551 {
552 return t_Matrix3(
553 value[0].x / scalar, value[0].y / scalar, value[0].z / scalar,
554 value[1].x / scalar, value[1].y / scalar, value[1].z / scalar,
555 value[2].x / scalar, value[2].y / scalar, value[2].z / scalar
556 );
557 }
558 }
559
571 {
572 if constexpr (std::is_floating_point_v<T>)
573 {
574 T inv = static_cast<T>(1.0) / scalar;
575 value[0] *= inv;
576 value[1] *= inv;
577 value[2] *= inv;
578 }
579 else
580 {
581 value[0] /= scalar;
582 value[1] /= scalar;
583 value[2] /= scalar;
584 }
585 return *this;
586 }
587
596 bool operator==(const t_Matrix3& rhs) const
597 {
598 return value[0] == rhs.value[0] &&
599 value[1] == rhs.value[1] &&
600 value[2] == rhs.value[2];
601 }
602
609 bool operator!=(const t_Matrix3& rhs) const
610 {
611 return !(*this == rhs);
612 }
613
623 bool operator<(const t_Matrix3& rhs) const
624 {
625 if (value[0] < rhs.value[0]) return true;
626 if (value[0] > rhs.value[0]) return false;
627 if (value[1] < rhs.value[1]) return true;
628 if (value[1] > rhs.value[1]) return false;
629 return value[2] < rhs.value[2];
630 }
631
647 {
648 return t_Matrix3(
649 value[0].x, value[1].x, value[2].x,
650 value[0].y, value[1].y, value[2].y,
651 value[0].z, value[1].z, value[2].z
652 );
653 }
654
663 {
664 return t_Matrix3(
665 -value[0].x, -value[0].y, -value[0].z,
666 -value[1].x, -value[1].y, -value[1].z,
667 -value[2].x, -value[2].y, -value[2].z
668 );
669 }
670
678 explicit operator bool() const noexcept
679 {
680 return value[0] != t_Vector3<T>(0, 0, 0) ||
681 value[1] != t_Vector3<T>(0, 0, 0) ||
682 value[2] != t_Vector3<T>(0, 0, 0);
683 }
684 };
685
691
697
708 template<typename T>
709 inline std::ostream& operator<<(std::ostream& out, const t_Matrix3<T>& m)
710 {
711 out << m.value[0] << " " << m.value[1] << " " << m.value[2];
712 return out;
713 }
714
725 template<typename T>
726 inline std::istream& operator>>(std::istream& in, t_Matrix3<T>& m)
727 {
728 in >> m.value[0] >> m.value[1] >> m.value[2];
729 return in;
730 }
731}
732
733// Register Value<Matrix3> specializations in the type system
736
737// Register Array<Matrix3> 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 3x3 matrix.
Definition Matrix3.hpp:83
constexpr size_t columns() const
Gets the number of columns in the matrix.
Definition Matrix3.hpp:231
T & operator()(size_t c, size_t r)
Element access operator (mutable).
Definition Matrix3.hpp:279
t_Vector3< T > operator*(const t_Vector3< T > &v) const
Matrix-vector multiplication operator.
Definition Matrix3.hpp:453
void set(T v0, T v1, T v2, T v3, T v4, T v5, T v6, T v7, T v8)
Sets all nine matrix elements.
Definition Matrix3.hpp:317
t_Matrix3 & operator/=(T scalar)
Scalar division assignment operator.
Definition Matrix3.hpp:570
const t_Vector3< T > & operator[](size_t c) const
Column access operator (const).
Definition Matrix3.hpp:263
bool operator!=(const t_Matrix3 &rhs) const
Inequality comparison operator.
Definition Matrix3.hpp:609
t_Matrix3 operator-(const t_Matrix3 &rhs) const
Matrix subtraction operator.
Definition Matrix3.hpp:503
constexpr t_Matrix3(T v[9])
Constructs a matrix from an array of nine elements.
Definition Matrix3.hpp:175
t_Matrix3 operator*(const t_Matrix3 &rhs) const
Matrix multiplication operator.
Definition Matrix3.hpp:370
t_Matrix3 operator/(T scalar) const
Scalar division operator.
Definition Matrix3.hpp:539
const T * data() const
Gets a const pointer to the underlying data.
Definition Matrix3.hpp:355
t_Matrix3 & operator=(const t_Matrix3< R > &rhs)
Type-converting assignment operator.
Definition Matrix3.hpp:302
constexpr t_Matrix3(T v0, T v1, T v2, T v3, T v4, T v5, T v6, T v7, T v8)
Constructs a matrix from nine individual elements.
Definition Matrix3.hpp:154
constexpr size_t rows() const
Gets the number of rows in the matrix.
Definition Matrix3.hpp:238
T operator()(size_t c, size_t r) const
Element access operator (const).
Definition Matrix3.hpp:290
constexpr t_Matrix3(const t_Vector3< T > &c0, const t_Vector3< T > &c1, const t_Vector3< T > &c2)
Constructs a matrix from three column vectors.
Definition Matrix3.hpp:196
t_Matrix3 operator-() const
Unary negation operator.
Definition Matrix3.hpp:662
t_Matrix3 & operator*=(T scalar)
Scalar multiplication assignment operator.
Definition Matrix3.hpp:431
constexpr t_Matrix3()
Default constructor.
Definition Matrix3.hpp:103
t_Matrix3(const t_Matrix3< R > &rhs)
Type-converting constructor.
Definition Matrix3.hpp:212
void set(const t_Matrix3< R > &rhs)
Sets this matrix equal to another matrix.
Definition Matrix3.hpp:333
constexpr size_t size() const
Gets the total number of elements in the matrix.
Definition Matrix3.hpp:224
t_Vector3< T > & operator[](size_t c)
Column access operator (mutable).
Definition Matrix3.hpp:253
bool operator<(const t_Matrix3 &rhs) const
Less-than comparison operator for ordering.
Definition Matrix3.hpp:623
t_Matrix3 operator+(const t_Matrix3 &rhs) const
Matrix addition operator.
Definition Matrix3.hpp:470
constexpr t_Matrix3(T v)
Constructs a diagonal matrix with the same value on the diagonal.
Definition Matrix3.hpp:124
t_Matrix3 & operator+=(const t_Matrix3 &rhs)
Matrix addition assignment operator.
Definition Matrix3.hpp:487
t_Vector3< T > value[3]
Column vectors comprising the matrix.
Definition Matrix3.hpp:91
T * data()
Gets a pointer to the underlying data.
Definition Matrix3.hpp:348
t_Matrix3 transpose() const
Computes the transpose of this matrix.
Definition Matrix3.hpp:646
bool operator==(const t_Matrix3 &rhs) const
Equality comparison operator.
Definition Matrix3.hpp:596
t_Matrix3 & operator*=(const t_Matrix3 &rhs)
Matrix multiplication assignment operator.
Definition Matrix3.hpp:395
t_Matrix3 operator*(T scalar) const
Scalar multiplication operator.
Definition Matrix3.hpp:414
t_Matrix3 & operator-=(const t_Matrix3 &rhs)
Matrix subtraction assignment operator.
Definition Matrix3.hpp:520
Template structure representing a 3-component vector.
Definition Vector3.hpp:82
T x
Definition Vector3.hpp:97
T z
Cartesian coordinate interpretation.
Definition Vector3.hpp:97
T y
Definition Vector3.hpp:97