Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
Matrix3x3.cpp
1/*
2Copyright (C) 2014-2019,2025 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#include <cmath>
19#include "aeongames/Vector3.hpp"
20#include "3DMath.h"
21
22namespace AeonGames
23{
24 static_assert ( sizeof ( Matrix3x3 ) == sizeof ( float ) * 9, "Size of Matrix3x3 is not the same as float[9]." );
25 Matrix3x3::Matrix3x3() : mMatrix{1, 0, 0, 0, 1, 0, 0, 0, 1} {}
26
27 Matrix3x3::Matrix3x3 ( const Transform& aTransform ) : Matrix3x3{ aTransform.GetScaleRotationMatrix() }
28 {
29 }
30
31 Matrix3x3::Matrix3x3 ( const float* const aMatrix3x3 )
32 {
33 memcpy ( mMatrix, aMatrix3x3, sizeof ( float ) * 9 );
34 }
35
36 Matrix3x3::Matrix3x3 ( const std::initializer_list<const float> aList )
37 {
38 const float* scalar = aList.begin();
39 for ( size_t i = 0; i < 9; ++i )
40 {
41 mMatrix[i] = ( scalar != aList.end() ) ? *scalar++ : ( i % 4 ) ? 0.0f : 1.0f;
42 }
43 }
44
45 const float* const Matrix3x3::GetMatrix3x3() const
46 {
47 return mMatrix;
48 }
49
51 {
53 return *this;
54 }
55
57 {
58 return Matrix3x3 ( *this ).Invert();
59 }
60
61 Matrix3x3& Matrix3x3::Rotate ( float angle, float x, float y, float z )
62 {
63 /*
64 This replicates how glRotatef works.
65 */
66 return *this *= GetRotationMatrix ( angle, x, y, z );
67 }
68
70 {
71 // Item 22 from MEC++
72 float local[9]
73 {
74 mMatrix[0], mMatrix[1], mMatrix[2],
75 mMatrix[3], mMatrix[4], mMatrix[5],
76 mMatrix[6], mMatrix[7], mMatrix[8]
77 };
78
79 // Column mayor (OpenGL way)
80 mMatrix[0] = local[0] * aRight.mMatrix[0] + local[3] * aRight.mMatrix[1] + local[6] * aRight.mMatrix[2];
81 mMatrix[1] = local[1] * aRight.mMatrix[0] + local[4] * aRight.mMatrix[1] + local[7] * aRight.mMatrix[2];
82 mMatrix[2] = local[2] * aRight.mMatrix[0] + local[5] * aRight.mMatrix[1] + local[8] * aRight.mMatrix[2];
83
84 mMatrix[3] = local[0] * aRight.mMatrix[3] + local[3] * aRight.mMatrix[4] + local[6] * aRight.mMatrix[5];
85 mMatrix[4] = local[1] * aRight.mMatrix[3] + local[4] * aRight.mMatrix[4] + local[7] * aRight.mMatrix[5];
86 mMatrix[5] = local[2] * aRight.mMatrix[3] + local[5] * aRight.mMatrix[4] + local[8] * aRight.mMatrix[5];
87
88 mMatrix[6] = local[0] * aRight.mMatrix[6] + local[3] * aRight.mMatrix[7] + local[6] * aRight.mMatrix[8];
89 mMatrix[7] = local[1] * aRight.mMatrix[6] + local[4] * aRight.mMatrix[7] + local[7] * aRight.mMatrix[8];
90 mMatrix[8] = local[2] * aRight.mMatrix[6] + local[5] * aRight.mMatrix[7] + local[8] * aRight.mMatrix[8];
91
92 return *this;
93 }
94
95 const float Matrix3x3::operator[] ( size_t aIndex ) const
96 {
97 assert ( aIndex < 9 );
98 return mMatrix[aIndex];
99 }
100
101 const Matrix3x3 Matrix3x3::GetRotationMatrix ( float angle, float x, float y, float z )
102 {
103 auto radians = float ( ( angle / 180.0f ) * static_cast<float> ( M_PI ) );
104 float c = cosf ( radians );
105 float s = sinf ( radians );
106 return Matrix3x3
107 {
108 x * x * ( 1 - c ) + c,
109 x * y * ( 1 - c ) - z * s,
110 x * z * ( 1 - c ) + y * s,
111 y * x * ( 1 - c ) + z * s,
112 y * y * ( 1 - c ) + c,
113 y * z * ( 1 - c ) - x * s,
114 x * z * ( 1 - c ) - y * s,
115 y * z * ( 1 - c ) + x * s,
116 z * z * ( 1 - c ) + c,
117 };
118 }
119
120 const Matrix3x3 operator* ( const Matrix3x3& lhs, const Matrix3x3& rhs )
121 {
122 /* Here Matrix3x3(lhs) *MAY* mean cast away constness
123 rather tan create a temporary object in some compilers,
124 we want the temporary, NOT the cast.*/
125 return Matrix3x3 ( lhs ) *= rhs;
126 }
127
128 const Vector3 operator* ( const Matrix3x3 & lhs, const Vector3 & rhs )
129 {
130 return Vector3
131 {
132 rhs[0] * lhs[0] + rhs[1] * lhs[3] + rhs[2] * lhs[6],
133 rhs[0] * lhs[1] + rhs[1] * lhs[4] + rhs[2] * lhs[7],
134 rhs[0] * lhs[2] + rhs[1] * lhs[5] + rhs[2] * lhs[8]
135 };
136 }
137
138 const bool operator== ( const Matrix3x3& lhs, const Matrix3x3& rhs )
139 {
140 return memcmp ( lhs.GetMatrix3x3(), rhs.GetMatrix3x3(), sizeof ( float ) * 9 ) == 0;
141 }
142
143 const Matrix3x3 Abs ( const Matrix3x3& aMatrix3x3 )
144 {
145 return Matrix3x3
146 {
147 std::abs ( aMatrix3x3[0] ),
148 std::abs ( aMatrix3x3[1] ),
149 std::abs ( aMatrix3x3[2] ),
150 std::abs ( aMatrix3x3[3] ),
151 std::abs ( aMatrix3x3[4] ),
152 std::abs ( aMatrix3x3[5] ),
153 std::abs ( aMatrix3x3[6] ),
154 std::abs ( aMatrix3x3[7] ),
155 std::abs ( aMatrix3x3[8] )
156 };
157 }
158}
Inline functions related to 3D Math.
float * Invert3x3Matrix(const float *src, float *dst)
Inverts a 3x3 Matrix.
Definition 3DMath.h:530
Header for 3x3 matrix class.
Header for Transform component class.
Header for the 3D vector class.
3 by 3 matrix in colum mayor order.
Definition Matrix3x3.hpp:33
DLL const Matrix3x3 GetInvertedMatrix3x3()
Get the inverted matrix.
Definition Matrix3x3.cpp:56
DLL Matrix3x3 & operator*=(const Matrix3x3 &lhs)
Multiply this matrix by another matrix.
Definition Matrix3x3.cpp:69
DLL Matrix3x3()
Default constructor.
Definition Matrix3x3.cpp:25
DLL const float *const GetMatrix3x3() const
Get a pointer to the internal matrix data.
Definition Matrix3x3.cpp:45
static DLL const Matrix3x3 GetRotationMatrix(float angle, float x, float y, float z)
Constructs the rotation matrix defined by the axis-angle provided.
DLL const float operator[](size_t aIndex) const
Access a matrix element by index.
Definition Matrix3x3.cpp:95
float mMatrix[9]
Lineal row mayor matrix.
Definition Matrix3x3.hpp:89
DLL Matrix3x3 & Invert()
Invert this matrix in place.
Definition Matrix3x3.cpp:50
DLL Matrix3x3 & Rotate(float angle, float x, float y, float z)
Apply a rotation to this matrix.
Definition Matrix3x3.cpp:61
Component class for any object that requires space transformations.
Definition Transform.hpp:34
3D vector class.
Definition Vector3.hpp:32
<- This is here just for the literals
Definition AABB.hpp:31
DLL const Matrix3x3 operator*(const Matrix3x3 &lhs, const Matrix3x3 &rhs)
Multiplies two 3x3 matrices. Multiplies two 3x3 matrices.
DLL const Matrix3x3 Abs(const Matrix3x3 &aMatrix3x3)
Compute the element-wise absolute value of a matrix.
DLL const bool operator==(const Matrix3x3 &lhs, const Matrix3x3 &rhs)
Compare two 3x3 matrices for equality.