Wildvine Engine
Referencia Doxygen del codigo propio de Wildvine Engine.
Cargando...
Buscando...
Nada coincide
Matrix3x3.h
Ir a la documentación de este archivo.
1
6/*
7 * MIT License
8 *
9 * Copyright (c) 2024 Roberto Charreton
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in all
19 * copies or substantial portions of the Software.
20 *
21 * In addition, any project or software that uses this library or class must include
22 * the following acknowledgment in the credits:
23 *
24 * "This project uses software developed by Roberto Charreton and Attribute Overload."
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33*/
34#pragma once
35namespace EU {
42 class Matrix3x3 {
43 public:
44 float m[3][3];
52 m[0][0] = 1; m[0][1] = 0; m[0][2] = 0;
53 m[1][0] = 0; m[1][1] = 1; m[1][2] = 0;
54 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1;
55 }
56
72 Matrix3x3(float a11, float a12, float a13, float a21, float a22, float a23, float a31, float a32, float a33) {
73 m[0][0] = a11; m[0][1] = a12; m[0][2] = a13;
74 m[1][0] = a21; m[1][1] = a22; m[1][2] = a23;
75 m[2][0] = a31; m[2][1] = a32; m[2][2] = a33;
76 }
77
84 Matrix3x3 operator+(const Matrix3x3& other) const {
85 return Matrix3x3(
86 m[0][0] + other.m[0][0], m[0][1] + other.m[0][1], m[0][2] + other.m[0][2],
87 m[1][0] + other.m[1][0], m[1][1] + other.m[1][1], m[1][2] + other.m[1][2],
88 m[2][0] + other.m[2][0], m[2][1] + other.m[2][1], m[2][2] + other.m[2][2]
89 );
90 }
91
98 Matrix3x3 operator-(const Matrix3x3& other) const {
99 return Matrix3x3(
100 m[0][0] - other.m[0][0], m[0][1] - other.m[0][1], m[0][2] - other.m[0][2],
101 m[1][0] - other.m[1][0], m[1][1] - other.m[1][1], m[1][2] - other.m[1][2],
102 m[2][0] - other.m[2][0], m[2][1] - other.m[2][1], m[2][2] - other.m[2][2]
103 );
104 }
105
112 Matrix3x3 operator*(const Matrix3x3& other) const {
113 return Matrix3x3(
114 m[0][0] * other.m[0][0] + m[0][1] * other.m[1][0] + m[0][2] * other.m[2][0], m[0][0] * other.m[0][1] + m[0][1] * other.m[1][1] + m[0][2] * other.m[2][1], m[0][0] * other.m[0][2] + m[0][1] * other.m[1][2] + m[0][2] * other.m[2][2],
115 m[1][0] * other.m[0][0] + m[1][1] * other.m[1][0] + m[1][2] * other.m[2][0], m[1][0] * other.m[0][1] + m[1][1] * other.m[1][1] + m[1][2] * other.m[2][1], m[1][0] * other.m[0][2] + m[1][1] * other.m[1][2] + m[1][2] * other.m[2][2],
116 m[2][0] * other.m[0][0] + m[2][1] * other.m[1][0] + m[2][2] * other.m[2][0], m[2][0] * other.m[0][1] + m[2][1] * other.m[1][1] + m[2][2] * other.m[2][1], m[2][0] * other.m[0][2] + m[2][1] * other.m[1][2] + m[2][2] * other.m[2][2]
117 );
118 }
119
126 Matrix3x3 operator*(float scalar) const {
127 return Matrix3x3(
128 m[0][0] * scalar, m[0][1] * scalar, m[0][2] * scalar,
129 m[1][0] * scalar, m[1][1] * scalar, m[1][2] * scalar,
130 m[2][0] * scalar, m[2][1] * scalar, m[2][2] * scalar
131 );
132 }
133
139 float determinant() const {
140 return m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1])
141 - m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0])
142 + m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]);
143 }
144
151 float det = determinant();
152 if (det == 0) {
153 // Handle non-invertible matrix gracefully.
154 return Matrix3x3();
155 }
156 float invDet = 1.0f / det;
157
158 return Matrix3x3(
159 (m[1][1] * m[2][2] - m[1][2] * m[2][1]) * invDet,
160 (m[0][2] * m[2][1] - m[0][1] * m[2][2]) * invDet,
161 (m[0][1] * m[1][2] - m[0][2] * m[1][1]) * invDet,
162
163 (m[1][2] * m[2][0] - m[1][0] * m[2][2]) * invDet,
164 (m[0][0] * m[2][2] - m[0][2] * m[2][0]) * invDet,
165 (m[0][2] * m[1][0] - m[0][0] * m[1][2]) * invDet,
166
167 (m[1][0] * m[2][1] - m[1][1] * m[2][0]) * invDet,
168 (m[0][1] * m[2][0] - m[0][0] * m[2][1]) * invDet,
169 (m[0][0] * m[1][1] - m[0][1] * m[1][0]) * invDet
170 );
171 }
172 };
173}
174
A 3x3 matrix class.
Definition Matrix3x3.h:42
Matrix3x3 operator-(const Matrix3x3 &other) const
Subtracts another matrix from this matrix.
Definition Matrix3x3.h:98
float m[3][3]
The elements of the matrix.
Definition Matrix3x3.h:44
Matrix3x3 inverse() const
Calculates the inverse of the matrix.
Definition Matrix3x3.h:150
float determinant() const
Calculates the determinant of the matrix.
Definition Matrix3x3.h:139
Matrix3x3 operator+(const Matrix3x3 &other) const
Adds another matrix to this matrix.
Definition Matrix3x3.h:84
Matrix3x3 operator*(float scalar) const
Multiplies this matrix by a scalar.
Definition Matrix3x3.h:126
Matrix3x3 operator*(const Matrix3x3 &other) const
Multiplies this matrix by another matrix.
Definition Matrix3x3.h:112
Matrix3x3()
Default constructor.
Definition Matrix3x3.h:51
Matrix3x3(float a11, float a12, float a13, float a21, float a22, float a23, float a31, float a32, float a33)
Parameterized constructor.
Definition Matrix3x3.h:72
Definition Matrix2x2.h:35