zenilib: zenilib/jni/external/zenilib/zeni/Matrix4f.cpp Source File

zenilib 0.5.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Matrix4f.cpp
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2 * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3 *
4 * zenilib is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * zenilib is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <zeni.h>
19
20 #include <cmath>
21 #include <iostream>
22
23 namespace Zeni {
24
25 Matrix4f::Matrix4f()
26 {
27 memset(&m_matrix, 0, sizeof(m_matrix));
28 }
29
30 Matrix4f::Matrix4f(
31 const float &aw, const float &ax, const float &ay, const float &az,
32 const float &bw, const float &bx, const float &by, const float &bz,
33 const float &cw, const float &cx, const float &cy, const float &cz,
34 const float &dw, const float &dx, const float &dy, const float &dz)
35 {
36 m_matrix[0][0] = aw;
37 m_matrix[1][0] = ax;
38 m_matrix[2][0] = ay;
39 m_matrix[3][0] = az;
40
41 m_matrix[0][1] = bw;
42 m_matrix[1][1] = bx;
43 m_matrix[2][1] = by;
44 m_matrix[3][1] = bz;
45
46 m_matrix[0][2] = cw;
47 m_matrix[1][2] = cx;
48 m_matrix[2][2] = cy;
49 m_matrix[3][2] = cz;
50
51 m_matrix[0][3] = dw;
52 m_matrix[1][3] = dx;
53 m_matrix[2][3] = dy;
54 m_matrix[3][3] = dz;
55 }
56
57 Matrix4f::Matrix4f(const Vector3f &first, const Vector3f &second, const Vector3f &third, const bool &rows)
58 {
59 m_matrix[0][0] = first.i;
60 m_matrix[1][1] = second.j;
61 m_matrix[2][2] = third.k;
62 m_matrix[0][3] = 0.0f;
63 m_matrix[1][3] = 0.0f;
64 m_matrix[2][3] = 0.0f;
65 m_matrix[3][0] = 0.0f;
66 m_matrix[3][1] = 0.0f;
67 m_matrix[3][2] = 0.0f;
68 m_matrix[3][3] = 1.0f;
69
70 if(rows) {
71 m_matrix[1][0] = first.j;
72 m_matrix[2][0] = first.k;
73 m_matrix[0][1] = second.i;
74 m_matrix[2][1] = second.k;
75 m_matrix[0][2] = third.i;
76 m_matrix[1][2] = third.j;
77 }
78 else {
79 m_matrix[1][0] = second.i;
80 m_matrix[2][0] = third.i;
81 m_matrix[0][1] = first.j;
82 m_matrix[2][1] = third.j;
83 m_matrix[0][2] = first.k;
84 m_matrix[1][2] = second.k;
85 }
86 }
87
88 Matrix4f::Matrix4f(const Matrix4f &rhs)
89 {
90 *this = rhs;
91 }
92
93 Matrix4f & Matrix4f::invert()
94 {
95 return *this = inverted();
96 }
97
98 Matrix4f Matrix4f::inverted() const
99 {
100 const float det_M = determinant();
101
102 #define m(i,j) (m_matrix[j][i])
103
104 const float m00 = (m(1,2)*m(2,3)*m(3,1) - m(1,3)*m(2,2)*m(3,1) + m(1,3)*m(2,1)*m(3,2) - m(1,1)*m(2,3)*m(3,2) - m(1,2)*m(2,1)*m(3,3) + m(1,1)*m(2,2)*m(3,3)) / det_M;
105 const float m01 = (m(0,3)*m(2,2)*m(3,1) - m(0,2)*m(2,3)*m(3,1) - m(0,3)*m(2,1)*m(3,2) + m(0,1)*m(2,3)*m(3,2) + m(0,2)*m(2,1)*m(3,3) - m(0,1)*m(2,2)*m(3,3)) / det_M;
106 const float m02 = (m(0,2)*m(1,3)*m(3,1) - m(0,3)*m(1,2)*m(3,1) + m(0,3)*m(1,1)*m(3,2) - m(0,1)*m(1,3)*m(3,2) - m(0,2)*m(1,1)*m(3,3) + m(0,1)*m(1,2)*m(3,3)) / det_M;
107 const float m03 = (m(0,3)*m(1,2)*m(2,1) - m(0,2)*m(1,3)*m(2,1) - m(0,3)*m(1,1)*m(2,2) + m(0,1)*m(1,3)*m(2,2) + m(0,2)*m(1,1)*m(2,3) - m(0,1)*m(1,2)*m(2,3)) / det_M;
108 const float m10 = (m(1,3)*m(2,2)*m(3,0) - m(1,2)*m(2,3)*m(3,0) - m(1,3)*m(2,0)*m(3,2) + m(1,0)*m(2,3)*m(3,2) + m(1,2)*m(2,0)*m(3,3) - m(1,0)*m(2,2)*m(3,3)) / det_M;
109 const float m11 = (m(0,2)*m(2,3)*m(3,0) - m(0,3)*m(2,2)*m(3,0) + m(0,3)*m(2,0)*m(3,2) - m(0,0)*m(2,3)*m(3,2) - m(0,2)*m(2,0)*m(3,3) + m(0,0)*m(2,2)*m(3,3)) / det_M;
110 const float m12 = (m(0,3)*m(1,2)*m(3,0) - m(0,2)*m(1,3)*m(3,0) - m(0,3)*m(1,0)*m(3,2) + m(0,0)*m(1,3)*m(3,2) + m(0,2)*m(1,0)*m(3,3) - m(0,0)*m(1,2)*m(3,3)) / det_M;
111 const float m13 = (m(0,2)*m(1,3)*m(2,0) - m(0,3)*m(1,2)*m(2,0) + m(0,3)*m(1,0)*m(2,2) - m(0,0)*m(1,3)*m(2,2) - m(0,2)*m(1,0)*m(2,3) + m(0,0)*m(1,2)*m(2,3)) / det_M;
112 const float m20 = (m(1,1)*m(2,3)*m(3,0) - m(1,3)*m(2,1)*m(3,0) + m(1,3)*m(2,0)*m(3,1) - m(1,0)*m(2,3)*m(3,1) - m(1,1)*m(2,0)*m(3,3) + m(1,0)*m(2,1)*m(3,3)) / det_M;
113 const float m21 = (m(0,3)*m(2,1)*m(3,0) - m(0,1)*m(2,3)*m(3,0) - m(0,3)*m(2,0)*m(3,1) + m(0,0)*m(2,3)*m(3,1) + m(0,1)*m(2,0)*m(3,3) - m(0,0)*m(2,1)*m(3,3)) / det_M;
114 const float m22 = (m(0,1)*m(1,3)*m(3,0) - m(0,3)*m(1,1)*m(3,0) + m(0,3)*m(1,0)*m(3,1) - m(0,0)*m(1,3)*m(3,1) - m(0,1)*m(1,0)*m(3,3) + m(0,0)*m(1,1)*m(3,3)) / det_M;
115 const float m23 = (m(0,3)*m(1,1)*m(2,0) - m(0,1)*m(1,3)*m(2,0) - m(0,3)*m(1,0)*m(2,1) + m(0,0)*m(1,3)*m(2,1) + m(0,1)*m(1,0)*m(2,3) - m(0,0)*m(1,1)*m(2,3)) / det_M;
116 const float m30 = (m(1,2)*m(2,1)*m(3,0) - m(1,1)*m(2,2)*m(3,0) - m(1,2)*m(2,0)*m(3,1) + m(1,0)*m(2,2)*m(3,1) + m(1,1)*m(2,0)*m(3,2) - m(1,0)*m(2,1)*m(3,2)) / det_M;
117 const float m31 = (m(0,1)*m(2,2)*m(3,0) - m(0,2)*m(2,1)*m(3,0) + m(0,2)*m(2,0)*m(3,1) - m(0,0)*m(2,2)*m(3,1) - m(0,1)*m(2,0)*m(3,2) + m(0,0)*m(2,1)*m(3,2)) / det_M;
118 const float m32 = (m(0,2)*m(1,1)*m(3,0) - m(0,1)*m(1,2)*m(3,0) - m(0,2)*m(1,0)*m(3,1) + m(0,0)*m(1,2)*m(3,1) + m(0,1)*m(1,0)*m(3,2) - m(0,0)*m(1,1)*m(3,2)) / det_M;
119 const float m33 = (m(0,1)*m(1,2)*m(2,0) - m(0,2)*m(1,1)*m(2,0) + m(0,2)*m(1,0)*m(2,1) - m(0,0)*m(1,2)*m(2,1) - m(0,1)*m(1,0)*m(2,2) + m(0,0)*m(1,1)*m(2,2)) / det_M;
120
121 #undef m
122
123 return Matrix4f(
124 m00, m01, m02, m03,
125 m10, m11, m12, m13,
126 m20, m21, m22, m23,
127 m30, m31, m32, m33);
128 }
129
130 Matrix4f & Matrix4f::transpose()
131 {
132 for(int i = 1; i < 4; ++i)
133 for(int j = 0; j < i; ++j) {
134 float temp = m_matrix[i][j];
135 m_matrix[i][j] = m_matrix[j][i];
136 m_matrix[j][i] = temp;
137 }
138
139 return *this;
140 }
141
142 Matrix4f Matrix4f::transposed() const
143 {
144 Matrix4f matrix;
145
146 for(int i = 0; i < 4; ++i)
147 for(int j = 0; j < 4; ++j)
148 matrix.m_matrix[i][j] = m_matrix[j][i];
149
150 return matrix;
151 }
152
153 float Matrix4f::determinant() const
154 {
155 #define m(i,j) m_matrix[j][i]
156 #define term(a,b,c,d) m(0,a)*m(1,b)*m(2,c)*m(3,d)
157
158 return
159 + term(0,1,2,3) - term(0,1,3,2)
160 - term(0,2,1,3) + term(0,2,3,1)
161 + term(0,3,1,2) + term(0,3,2,1)
162
163 - term(1,0,2,3) + term(1,0,3,2)
164 + term(1,2,0,3) - term(1,2,3,0)
165 - term(1,3,0,2) + term(1,3,2,0)
166
167 + term(2,0,1,3) - term(2,0,3,1)
168 - term(2,1,0,3) + term(2,1,3,0)
169 + term(2,3,0,1) - term(2,3,1,0)
170
171 - term(3,0,1,2) + term(3,0,2,1)
172 + term(3,1,0,2) - term(3,1,2,0)
173 - term(3,2,0,1) + term(3,2,1,0);
174
175 #undef term
176 #undef m
177 }
178
179 Vector3f Matrix4f::operator*(const Vector3f &vector) const {
180 return Vector3f(m_matrix[0][0] * vector.i + m_matrix[1][0] * vector.j + m_matrix[2][0] * vector.k + m_matrix[3][0],
181 m_matrix[0][1] * vector.i + m_matrix[1][1] * vector.j + m_matrix[2][1] * vector.k + m_matrix[3][1],
182 m_matrix[0][2] * vector.i + m_matrix[1][2] * vector.j + m_matrix[2][2] * vector.k + m_matrix[3][2]);
183 }
184
185 std::ostream & serialize(std::ostream &os, const Matrix4f &value) {
186 return os.write(reinterpret_cast<const char * const>(&value), 16u * sizeof(float));
187 }
188
189 std::istream & unserialize(std::istream &is, Matrix4f &value) {
190 return is.read(reinterpret_cast<char * const>(&value), 16u * sizeof(float));
191 }
192
193 }
Zeni::Matrix4f::invert
Matrix4f & invert()
Invert the matrix.
Definition: Matrix4f.cpp:93
j
int32_t j
Definition: e_log.c:102
memset
#define memset
Definition: SDL_malloc.c:633
Zeni::unserialize
std::istream & unserialize(std::istream &is, Color &value)
Definition: Color.cpp:72
Zeni::serialize
std::ostream & serialize(std::ostream &os, const Color &value)
Definition: Color.cpp:68
Zeni::Matrix4f::transpose
Matrix4f & transpose()
Transpose the matrix.
Definition: Matrix4f.cpp:130
Zeni::Matrix4f::transposed
Matrix4f transposed() const
Get the transpose.
Definition: Matrix4f.cpp:142
Zeni::Vector3f
A Featureful 3-Space Vector Class.
Definition: Vector3f.h:58
u
ALuint u
Definition: alMain.h:58
first
GLint first
Definition: gl2ext.h:1011
Zeni::Matrix4f::determinant
float determinant() const
Get the determinant.
Definition: Matrix4f.cpp:153
by
GLbyte by
Definition: SDL_opengl.h:8286
Zeni::Matrix4f::operator*
Matrix4f operator*(const Matrix4f &rhs) const
Get the product.
Definition: Matrix4f.hxx:202
Zeni::Vector3f::k
float k
Definition: Vector3f.h:119
matrix
GLuint GLenum matrix
Definition: glew.h:13408
Zeni::Vector3f::j
float j
Definition: Vector3f.h:115
bz
GLbyte GLbyte bz
Definition: SDL_opengl.h:8286
value
EGLSurface EGLint void ** value
Definition: eglext.h:301
Zeni::Matrix4f::Matrix4f
Matrix4f()
Definition: Matrix4f.cpp:25
Zeni::Matrix4f::inverted
Matrix4f inverted() const
Get the inverse.
Definition: Matrix4f.cpp:98
zeni.h
Zeni::Matrix4f
A Featureful 4-Space Matrix Class.
Definition: Matrix4f.h:47
i
int i
Definition: pngrutil.c:1377
Zeni::Vector3f::i
float i
Definition: Vector3f.h:111
m
#define m(i, j)
term
#define term(a, b, c, d)