NGL  6.5
The NCCA Graphics Library
Mat3.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 Jon Macey
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU 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  This program 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 General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "Mat3.h"
19 #include "Mat4.h"
20 #include "NGLassert.h"
21 #include "Quaternion.h"
22 #include "Util.h"
23 #include "Vec2.h"
24 #include <iostream>
25 #include <cstring> // for memset
26 //----------------------------------------------------------------------------------------------------------------------
29 //----------------------------------------------------------------------------------------------------------------------
30 
31 
32 namespace ngl
33 {
34 
35 //----------------------------------------------------------------------------------------------------------------------
36 Mat3::Mat3() noexcept
37 {
38  memset(&m_m,0,sizeof(m_m));
39  m_00=1.0f;
40  m_11=1.0f;
41  m_22=1.0f;
42 }
43 
44 Mat3::Mat3( const Mat4 &_m ) noexcept
45 {
46  m_00=_m.m_00;
47  m_01=_m.m_01;
48  m_02=_m.m_02;
49  m_10=_m.m_10;
50  m_11=_m.m_11;
51  m_12=_m.m_12;
52  m_20=_m.m_20;
53  m_21=_m.m_21;
54  m_22=_m.m_22;
55 }
56 
57 Mat3::Mat3(Real _00, Real _01, Real _02, Real _10, Real _11, Real _12,Real _20, Real _21, Real _22) noexcept
58 {
59  m_00=_00;
60  m_01=_01;
61  m_02=_02;
62 
63  m_10=_10;
64  m_11=_11;
65  m_12=_12;
66  m_20=_20;
67  m_21=_21;
68  m_22=_22;
69 
70 }
71 //----------------------------------------------------------------------------------------------------------------------
72 Mat3::Mat3(const Mat3& _m) noexcept
73 {
74  memcpy(m_m,&_m.m_m,sizeof(m_m));
75 }
76 
77 
78 
79 //----------------------------------------------------------------------------------------------------------------------
80 Mat3::Mat3(const Real _m ) noexcept
81 {
82  memset(m_m,0,sizeof(m_m));
83  m_00=_m;
84  m_11=_m;
85  m_22=_m;
86 }
87 
88 
89 
90 
91 
92 //----------------------------------------------------------------------------------------------------------------------
94 void Mat3::setAtXY( GLint _x,GLint _y, Real _equals ) noexcept
95 {
96  m_m[_x][_y]=_equals;
97 }
98 //----------------------------------------------------------------------------------------------------------------------
99 const Mat3& Mat3::null() noexcept
100 {
101  memset(&m_m,0,sizeof(m_m));
102  return *this;
103 }
104 //----------------------------------------------------------------------------------------------------------------------
105 const Mat3& Mat3::identity() noexcept
106 {
107  memset(m_m,0,sizeof(m_m));
108  m_00=1.0f;
109  m_11=1.0f;
110  m_22=1.0f;
111  return *this;
112 }
113 
114 //----------------------------------------------------------------------------------------------------------------------
115 Mat3 Mat3::operator*(const Mat3& _m )const noexcept
116 {
117  Mat3 temp;
118 
119  temp.m_m[0][0] = m_m[0][0] * _m.m_m[0][0] + m_m[0][1] * _m.m_m[1][0] + m_m[0][2] * _m.m_m[2][0] ;
120  temp.m_m[0][1] = m_m[0][0] * _m.m_m[0][1] + m_m[0][1] * _m.m_m[1][1] + m_m[0][2] * _m.m_m[2][1] ;
121  temp.m_m[0][2] = m_m[0][0] * _m.m_m[0][2] + m_m[0][1] * _m.m_m[1][2] + m_m[0][2] * _m.m_m[2][2] ;
122  temp.m_m[1][0] = m_m[1][0] * _m.m_m[0][0] + m_m[1][1] * _m.m_m[1][0] + m_m[1][2] * _m.m_m[2][0] ;
123  temp.m_m[1][1] = m_m[1][0] * _m.m_m[0][1] + m_m[1][1] * _m.m_m[1][1] + m_m[1][2] * _m.m_m[2][1] ;
124  temp.m_m[1][2] = m_m[1][0] * _m.m_m[0][2] + m_m[1][1] * _m.m_m[1][2] + m_m[1][2] * _m.m_m[2][2] ;
125  temp.m_m[2][0] = m_m[2][0] * _m.m_m[0][0] + m_m[2][1] * _m.m_m[1][0] + m_m[2][2] * _m.m_m[2][0] ;
126  temp.m_m[2][1] = m_m[2][0] * _m.m_m[0][1] + m_m[2][1] * _m.m_m[1][1] + m_m[2][2] * _m.m_m[2][1] ;
127  temp.m_m[2][2] = m_m[2][0] * _m.m_m[0][2] + m_m[2][1] * _m.m_m[1][2] + m_m[2][2] * _m.m_m[2][2] ;
128  return temp;
129 }
130 
131 //----------------------------------------------------------------------------------------------------------------------
132 const Mat3& Mat3::operator*= ( const Mat3 &_m ) noexcept
133 {
134  Mat3 temp(*this);
135 
136  // row 0
137  m_00 = temp.m_00 * _m.m_00;
138  m_01 = temp.m_01 * _m.m_00;
139  m_02 = temp.m_02 * _m.m_00;
140 
141  m_00 += temp.m_10 * _m.m_01;
142  m_01 += temp.m_11 * _m.m_01;
143  m_02 += temp.m_12 * _m.m_01;
144 
145  m_00 += temp.m_20 * _m.m_02;
146  m_01 += temp.m_21 * _m.m_02;
147  m_02 += temp.m_22 * _m.m_02;
148 
149 
150  // row 1
151  m_10 = temp.m_00 * _m.m_10;
152  m_11 = temp.m_01 * _m.m_10;
153  m_12 = temp.m_02 * _m.m_10;
154 
155  m_10 += temp.m_10 * _m.m_11;
156  m_11 += temp.m_11 * _m.m_11;
157  m_12 += temp.m_12 * _m.m_11;
158 
159  m_10 += temp.m_20 * _m.m_12;
160  m_11 += temp.m_21 * _m.m_12;
161  m_12 += temp.m_22 * _m.m_12;
162 
163 
164 
165  // row 2
166  m_20 = temp.m_00 * _m.m_20;
167  m_21 = temp.m_01 * _m.m_20;
168  m_22 = temp.m_02 * _m.m_20;
169 
170  m_20 += temp.m_10 * _m.m_21;
171  m_21 += temp.m_11 * _m.m_21;
172  m_22 += temp.m_12 * _m.m_21;
173 
174  m_20 += temp.m_20 * _m.m_22;
175  m_21 += temp.m_21 * _m.m_22;
176  m_22 += temp.m_22 * _m.m_22;
177 
178  return *this;
179 }
180 
181 //----------------------------------------------------------------------------------------------------------------------
182 Mat3 Mat3::operator+(const Mat3 &_m ) const noexcept
183 {
184  Mat3 ret;
185  const Real* iterA = &m_openGL[0];
186  const Real* iterB = &_m.m_openGL[0];
187  Real* iterR = &ret.m_openGL[0];
188  const Real* end = &m_openGL[9];
189 
190  for( ; iterA != end; ++iterA, ++iterB, ++iterR)
191  {
192  *iterR = *iterA + *iterB;
193  }
194  return ret;
195 }
196 //----------------------------------------------------------------------------------------------------------------------
197 const Mat3& Mat3::operator+=( const Mat3 &_m ) noexcept
198 {
199  Real* iterA =&m_openGL[0];
200  const Real* iterB = &_m.m_openGL[0];
201  const Real* end = &m_openGL[9];
202 
203  for( ; iterA != end; ++iterA, ++iterB)
204  {
205  *iterA += *iterB;
206  }
207  return *this;
208 }
209 //----------------------------------------------------------------------------------------------------------------------
210 Mat3 Mat3::operator*( Real _i ) const noexcept
211 {
212  Mat3 ret;
213  const Real* iterA = &m_openGL[0];
214  Real* iterB = &ret.m_openGL[0];
215  const Real* end = &m_openGL[9];
216 
217  for( ; iterA != end; ++iterA, ++iterB)
218  {
219  *iterB = (*iterA) * _i;
220  }
221  return ret;
222 }
223 
224 //----------------------------------------------------------------------------------------------------------------------
225 const Mat3& Mat3::operator*=(Real _i) noexcept
226 {
227  for(int y=0; y<3; ++y)
228  {
229  for(int x=0; x<3; ++x)
230  {
231  m_m[y][x]*=_i;
232  }
233  }
234  return *this;
235 }
236 
237 
238 
239 
240 //----------------------------------------------------------------------------------------------------------------------
241 const Mat3& Mat3::transpose() noexcept
242 {
243  Mat3 tmp(*this);
244 
245  for(int row=0; row<3; ++row)
246  {
247  for(int col=0; col<3; ++col)
248  {
249  m_m[row][col]=tmp.m_m[col][row];
250  }
251  }
252  return *this;
253 }
254 
255 
256 
257 //----------------------------------------------------------------------------------------------------------------------
258 void Mat3::rotateX( Real _deg) noexcept
259 {
260  Real beta=radians(_deg);
261  Real sr = sinf( beta );
262  Real cr = cosf( beta );
263  m_11 = cr;
264  m_21 = -sr;
265  m_12 = sr;
266  m_22 = cr;
267 }
268 
269 //----------------------------------------------------------------------------------------------------------------------
270 void Mat3::rotateY(Real _deg) noexcept
271 {
272  Real beta=radians(_deg);
273  Real sr = sinf( beta );
274  Real cr = cosf( beta );
275  m_00 = cr;
276  m_20 = sr;
277  m_02 = -sr;
278  m_22 = cr;
279 }
280 
281 //----------------------------------------------------------------------------------------------------------------------
282 void Mat3::rotateZ( Real _deg) noexcept
283 {
284  Real beta=radians(_deg);
285  Real sr = sinf( beta );
286  Real cr = cosf( beta );
287  m_00 = cr;
288  m_10 = -sr;
289  m_01 = sr;
290  m_11 = cr;
291 }
292 
293 
294 
295 //----------------------------------------------------------------------------------------------------------------------
296 void Mat3::scale(Real _x, Real _y, Real _z) noexcept
297 {
298  m_00 = _x;
299  m_11 = _y;
300  m_22 = _z;
301 }
302 
303 //----------------------------------------------------------------------------------------------------------------------
304 Vec3 Mat3::operator * (const Vec3 &_v) const noexcept
305 {
306  Vec3 temp;
307 
308  temp.m_x=_v.m_x * m_00 + _v.m_y* m_01 + _v.m_z * m_02;
309  temp.m_y=_v.m_x * m_10 + _v.m_y* m_11 + _v.m_z * m_12;
310  temp.m_z=_v.m_x * m_20 + _v.m_y* m_21 + _v.m_z * m_22;
311 
312  return temp;
313 }
314 
315 
316 
317 
318 
319 //----------------------------------------------------------------------------------------------------------------------
320 void Mat3::euler( Real _angle,Real _x, Real _y, Real _z) noexcept
321 {
322  // Axis and Angle Mat3x3 rotation see
323  // http://en.wikipedia.org/wiki/Rotation_Mat3x3 for more details
324  Real beta=radians(_angle);
325  Real c = cosf(radians(beta));
326  Real s = sinf(radians(beta));
327  Real C=1-c;
328  Real xs = _x*s; Real ys = _y*s; Real zs = _z*s;
329  Real xC = _x*C; Real yC = _y*C; Real zC = _z*C;
330  Real xyC = _x*yC; Real yzC = _y*zC; Real zxC = _z*xC;
331 
332  m_m[0][0]=_x*xC+c; m_m[0][1]= xyC-zs; m_m[0][2]= zxC+ys;
333  m_m[1][0]=xyC+zs; m_m[1][1]=_y*yC+c; m_m[1][2]= yzC-xs;
334  m_m[2][0]=zxC-ys; m_m[2][1]=yzC+xs; m_m[2][2]=_z*zC+c;
335 }
336 
337 
338 Real Mat3::determinant() const noexcept
339 {
340  return +m_00*(m_11*m_22-m_21*m_12)
341  -m_01*(m_10*m_22-m_12*m_20)
342  +m_02*(m_10*m_21-m_11*m_20);
343 }
344 
345 
346 Mat3 Mat3::inverse() noexcept
347 {
348  Real det = determinant();
349  if(det==0.0f)
350  {
351  std::cerr<<"ngl 0 determinant \n";
352  return Mat3();
353 
354  }
355  Real invdet = 1/det;
356 
357  Mat3 tmp;
358  tmp.m_00 = (m_11*m_22-m_21*m_12)*invdet;
359  tmp.m_01 = -(m_10*m_22-m_12*m_20)*invdet;
360  tmp.m_02 = (m_10*m_21-m_20*m_11)*invdet;
361 
362  tmp.m_10 = -(m_01*m_22-m_02*m_21)*invdet;
363  tmp.m_11 = (m_00*m_22-m_02*m_20)*invdet;
364  tmp.m_12 = -(m_00*m_21-m_20*m_01)*invdet;
365 
366  tmp.m_20 = (m_01*m_12-m_02*m_11)*invdet;
367  tmp.m_21 = -(m_00*m_12-m_10*m_02)*invdet;
368  tmp.m_22 = (m_00*m_11-m_10*m_01)*invdet;
369  *this=tmp;
370  return *this;
371 }
372 
373 //----------------------------------------------------------------------------------------------------------------------
374 Vec3 Mat3::getLeftVector() const noexcept
375 {
376  return Vec3(-m_openGL[0],-m_openGL[1],-m_openGL[2]);
377 }
378 //----------------------------------------------------------------------------------------------------------------------
379 Vec3 Mat3::getRightVector() const noexcept
380 {
381  return Vec3( m_openGL[0],m_openGL[1],m_openGL[2]);
382 
383 }
384 //----------------------------------------------------------------------------------------------------------------------
385 Vec3 Mat3::getUpVector() const noexcept
386 {
387  return Vec3(m_openGL[3],m_openGL[4],m_openGL[5]);
388 
389 }
390 
391 //----------------------------------------------------------------------------------------------------------------------
392 Vec3 Mat3::getDownVector() const noexcept
393 {
394  return Vec3(-m_openGL[3],-m_openGL[4],-m_openGL[5]);
395 
396 }
397 //----------------------------------------------------------------------------------------------------------------------
398 Vec3 Mat3::getForwardVector() const noexcept
399 {
400  return Vec3(-m_openGL[6],-m_openGL[7],-m_openGL[8]);
401 }
402 //----------------------------------------------------------------------------------------------------------------------
403 
404 Vec3 Mat3::getBackVector() const noexcept
405 {
406  return Vec3(m_openGL[6],m_openGL[7],m_openGL[8]);
407 
408 }
409 //----------------------------------------------------------------------------------------------------------------------
410 
411 
412 
413 } // end namespace ngl
414 
415 
416 
Real determinant() const noexcept
get the determinant of the matrix
Definition: Mat3.cpp:338
some useful definitions and functions
Vec3 getLeftVector() const noexcept
get the left vector of the matrix (-ve 1st Row)
Definition: Mat3.cpp:374
Mat3 basic 3x3 matrix for certain glsl ops.
Definition: Mat3.h:43
Mat3 inverse() noexcept
set the matrix to be the inverse
Definition: Mat3.cpp:346
Real m_00
individual matrix element maps to m_m[0][0] or m_openGL[0]
Definition: Mat3.h:256
Vec3 getRightVector() const noexcept
get the right vector of the matrix ( 1nd Row)
Definition: Mat3.cpp:379
void rotateX(Real _deg) noexcept
set this matrix to a rotation matrix in the X axis for value _deg note the matrix should be set to id...
Definition: Mat3.cpp:258
Real m_12
individual matrix element maps to m_m[1][2] or m_openGL[6]
Definition: Mat3.h:276
std::array< Real, 9 > m_openGL
The matrix in m_openGL 16 Real array format usefull for OpenGL fv formats mapped to m_m[][] elements ...
Definition: Mat3.h:243
const GLfloat * c
Definition: glew.h:16629
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1255
Real m_m[3][3]
Mat3 element m_m as a 4x4 array mapped by union to m_nn elements and m_openGL.
Definition: Mat3.h:237
simple Vec3 encapsulates a 3 float object like glsl vec3 but not maths use the Vec3 class for maths a...
Definition: Vec3.h:51
implementation files for RibExport class
Definition: AABB.cpp:22
PRECISION Real
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
Definition: Types.h:127
Mat3 operator*(const Mat3 &_m) const noexcept
operator for matrix multiplication
Definition: Mat3.cpp:115
Real m_y
y component
Definition: Vec3.h:311
GLuint GLuint end
Definition: glew.h:1256
a simple 2 tuple container for compatibility with glsl
void euler(Real _angle, Real _x, Real _y, Real _z) noexcept
axis / angle rotation using the Euler method
Definition: Mat3.cpp:320
Vec3 getUpVector() const noexcept
get the up vector of the matrix (2nd Row)
Definition: Mat3.cpp:385
const Mat3 & transpose() noexcept
method to transpose the matrix
Definition: Mat3.cpp:241
Real m_x
x component
Definition: Vec3.h:310
void setAtXY(GLint _x, GLint _y, Real _equals) noexcept
set the value at m_m[_x][_y] to _equals
Definition: Mat3.cpp:94
Defines the class Quaternion based on John Vinces lecture notes basically we have a scalar part and t...
Real m_11
individual matrix element maps to m_m[1][1] or m_openGL[5]
Definition: Mat3.h:272
const Mat3 & null() noexcept
clear the matrix to all 0
Definition: Mat3.cpp:99
Vec3 getDownVector() const noexcept
get the down vector of the matrix ( -ve 2nd Row)
Definition: Mat3.cpp:392
Mat3() noexcept
ctor will always create an identity matrix
Definition: Mat3.cpp:36
a simple 3x3 TX matrix
Vec3 getForwardVector() const noexcept
get the forward vector of the matrix (-ve 3rd Row)
Definition: Mat3.cpp:398
GLint GLint GLint GLint GLint x
Definition: glew.h:1255
Real m_10
individual matrix element maps to m_m[1][0] or m_openGL[4]
Definition: Mat3.h:268
Vec3 getBackVector() const noexcept
get the back vector of the matrix ( 3nd Row)
Definition: Mat3.cpp:404
void rotateY(Real _deg) noexcept
set this matrix to a rotation matrix in the Y axis for value _deg note the matrix should be set to id...
Definition: Mat3.cpp:270
Mat3 operator+(const Mat3 &_m) const noexcept
operator to add two matrices together
Definition: Mat3.cpp:182
Real m_20
individual matrix element maps to m_m[2][0] or m_openGL[8]
Definition: Mat3.h:280
GLenum GLenum void * row
Definition: glew.h:4993
NGL_DLLEXPORT Real radians(const Real _deg)
converts Degrees to Radians
Definition: Util.cpp:89
Real m_21
individual matrix element maps to m_m[2][1] or m_openGL[9]
Definition: Mat3.h:284
Matrix Class to do simple matrix operations included operator overloaded functions for maths and matr...
Definition: Mat4.h:58
const Mat3 & operator*=(const Mat3 &_m) noexcept
operator to mult this matrix by value _m
Definition: Mat3.cpp:132
int GLint
Definition: glew.h:281
void rotateZ(Real _deg) noexcept
set this matrix to a rotation matrix in the Z axis for value _deg note the matrix should be set to id...
Definition: Mat3.cpp:282
Real m_02
individual matrix element maps to m_m[0][2] or m_openGL[2]
Definition: Mat3.h:264
re impliment asserts so we don&#39;t exit on failure
GLdouble s
Definition: glew.h:1393
void scale(Real _x, Real _y, Real _z) noexcept
set the matrix scale values
Definition: Mat3.cpp:296
Real m_01
individual matrix element maps to m_m[0][1] or m_openGL[1]
Definition: Mat3.h:260
Real m_z
z component
Definition: Vec3.h:312
const Mat3 & identity() noexcept
make the matrix m the identity matrix 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
Definition: Mat3.cpp:105
const Mat3 & operator+=(const Mat3 &_m) noexcept
+= operator
Definition: Mat3.cpp:197
GLclampf f
Definition: glew.h:3511
Real m_22
individual matrix element maps to m_m[2][2] or m_openGL[10]
Definition: Mat3.h:288