1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#pragma once
4
5#include "Prerequisites/BsPrerequisitesUtil.h"
6#include "Math/BsVector3.h"
7
8namespace bs
9{
10 /** @addtogroup Math
11 * @{
12 */
13
14 /**
15 * Class representing a NxM matrix.
16 *
17 * @note If you need to use matrices for more than just data storage then
18 * it is suggested you use specialized Matrix3 or Matrix4 classes
19 * as they provide a wide range of functionality.
20 */
21 template<int N, int M>
22 class MatrixNxM
23 {
24 public:
25 MatrixNxM() = default;
26 MatrixNxM(const MatrixNxM&) = default;
27 MatrixNxM& operator=(const MatrixNxM&) = default;
28
29 explicit MatrixNxM(float data[N*M])
30 {
31 memcpy(m, data, N*M * sizeof(float));
32 }
33
34 /** Returns a transpose of the matrix (switched columns and rows). */
35 MatrixNxM<M, N> transpose() const
36 {
37 MatrixNxM<M, N> matTranspose;
38 for (UINT32 row = 0; row < N; row++)
39 {
40 for (UINT32 col = 0; col < M; col++)
41 matTranspose[col][row] = m[row][col];
42 }
43
44 return matTranspose;
45 }
46
47 /** Returns a row of the matrix. */
48 float* operator[] (UINT32 row) const
49 {
50 assert(row < N);
51
52 return (float*)m[row];
53 }
54
55 bool operator== (const MatrixNxM& rhs) const
56 {
57 for (UINT32 row = 0; row < N; row++)
58 {
59 for (UINT32 col = 0; col < M; col++)
60 {
61 if (m[row][col] != rhs.m[row][col])
62 return false;
63 }
64 }
65
66 return true;
67 }
68
69 bool operator!= (const MatrixNxM& rhs) const
70 {
71 return !operator==(rhs);
72 }
73
74 float m[N][M];
75 };
76
77 typedef MatrixNxM<2, 2> Matrix2;
78 typedef MatrixNxM<2, 3> Matrix2x3;
79 typedef MatrixNxM<2, 4> Matrix2x4;
80 typedef MatrixNxM<3, 2> Matrix3x2;
81 typedef MatrixNxM<3, 4> Matrix3x4;
82 typedef MatrixNxM<4, 2> Matrix4x2;
83 typedef MatrixNxM<4, 3> Matrix4x3;
84
85 /** @} */
86}
87