Neo  0.5.0
Developer Documentation
Matrix4x4.h
Go to the documentation of this file.
1 //========================================================================
2 // Copyright (c) 2003-2011 Anael Seghezzi <www.maratis3d.com>
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 //
8 // Permission is granted to anyone to use this software for any purpose,
9 // including commercial applications, and to alter it and redistribute it
10 // freely, subject to the following restrictions:
11 //
12 // 1. The origin of this software must not be misrepresented; you must not
13 // claim that you wrote the original software. If you use this software
14 // in a product, an acknowledgment in the product documentation would
15 // be appreciated but is not required.
16 //
17 // 2. Altered source versions must be plainly marked as such, and must not
18 // be misrepresented as being the original software.
19 //
20 // 3. This notice may not be removed or altered from any source
21 // distribution.
22 //
23 //========================================================================
24 
25 
26 #ifndef __MATRIX4X4_H
27 #define __MATRIX4X4_H
28 
29 namespace Neo
30 {
31 class NEO_CORE_EXPORT Matrix4x4
32 {
33 public:
34 
35  float entries[16];
36 
37 public:
38 
39  Matrix4x4(void){
40  loadIdentity();
41  }
42 
43  Matrix4x4( float e0, float e1, float e2, float e3,
44  float e4, float e5, float e6, float e7,
45  float e8, float e9, float e10, float e11,
46  float e12, float e13, float e14, float e15);
47 
48  Matrix4x4(const float * value);
49  Matrix4x4(const Matrix4x4 & mat);
50 
51  ~Matrix4x4(void){}
52 
53 public:
54 
55  Matrix4x4 operator + (const Matrix4x4 & mat) const;
56  Matrix4x4 operator - (const Matrix4x4 & mat) const;
57  Matrix4x4 operator * (const Matrix4x4 & mat) const;
58  Matrix4x4 operator * (const float value) const;
59  Matrix4x4 operator / (const float value) const;
60 
61  friend Matrix4x4 operator * (float factor, const Matrix4x4 & mat);
62 
63  void operator += (const Matrix4x4 & mat);
64  void operator -= (const Matrix4x4 & mat);
65  void operator *= (const Matrix4x4 & mat);
66  void operator *= (const float value);
67  void operator /= (const float value);
68 
69  bool operator == (const Matrix4x4 & mat) const;
70  bool operator != (const Matrix4x4 & mat) const;
71 
72  Matrix4x4 operator - (void) const;
73  Matrix4x4 operator + (void) const
74  {
75  return (*this);
76  }
77 
78  Vector4 operator * (const Vector4 mat) const;
79  inline Vector3 operator * (const Vector3 mat) const
80  {
81  return Vector3(
82  entries[0]*mat.x
83  + entries[4]*mat.y
84  + entries[8]*mat.z
85  + entries[12],
86 
87  entries[1]*mat.x
88  + entries[5]*mat.y
89  + entries[9]*mat.z
90  + entries[13],
91 
92  entries[2]*mat.x
93  + entries[6]*mat.y
94  + entries[10]*mat.z
95  + entries[14]
96  );
97  }
98 
99  float & operator () (int col, int row){
100  return entries[row*4+col];
101  }
102 
103  operator float* () const {
104  return (float*) this;
105  }
106 
107  operator const float* () const {
108  return (const float*) this;
109  }
110 
111 public:
112 
113  void loadIdentity(void);
114  void loadZero(void);
115 
116  void invert(void);
117  void transpose(void);
118  void affineInvert(void);
119  void invertTranspose(void);
120  void affineInvertTranspose(void);
121 
122  void scale(const Vector3 & scaleFactor);
123  void rotate(const Vector3 & axis, const float angle);
124  void translate(const Vector3 translate);
125 
126  void setEntry(int position, float value);
127  void setScale(const Vector3 & scaleFactor);
128  void setUniformScale(const float size);
129  void setTranslation(const Vector3 & translation);
130  void setTranslationPart(const Vector3 & translation);
131  void setRotationAxis(const float angle, const Vector3 & axis);
132  void setRotationX(const float angle);
133  void setRotationY(const float angle);
134  void setRotationZ(const float angle);
135  void setRotationEuler(const float angleX, const float angleY, const float angleZ);
136  void setRotationPartEuler(const float angleX, const float angleY, const float angleZ);
137  void setRotationPartEuler(const Vector3 & rotations)
138  {
139  setRotationPartEuler(rotations.x, rotations.y, rotations.z);
140  }
141 
142  float getEntry(int position) const;
143 
144  inline Vector3 getRotatedVector3(const Vector3 & vec) const
145  {
146  return Vector3(
147  entries[0]*vec.x + entries[4]*vec.y + entries[8]*vec.z,
148  entries[1]*vec.x + entries[5]*vec.y + entries[9]*vec.z,
149  entries[2]*vec.x + entries[6]*vec.y + entries[10]*vec.z
150  );
151  }
152 
153  Vector3 getInverseRotatedVector3(const Vector3 & vec) const;
154  Vector3 getTranslatedVector3(const Vector3 & vec) const;
155  Vector3 getInversetranslatedVector3(const Vector3 & vec) const;
156 
157  inline Vector3 getTranslationPart(void) const { return Vector3(entries[12], entries[13], entries[14]); }
158 
159  Vector3 getEulerAngles(void) const;
160 
161  Vector3 getScale(void) const;
162 
163  Vector4 getRow(int position) const;
164  Vector4 getColumn(int position) const;
165 
166  Matrix4x4 getInverse(void) const;
167  Matrix4x4 getTranspose(void) const;
168  Matrix4x4 getInversetranspose(void) const;
169  Matrix4x4 getAffineInverse(void) const;
170  Matrix4x4 getAffineInverseTranspose(void) const;
171 
172  void rotateVector3(Vector3 & vec) const
173  {
174  vec = getRotatedVector3(vec);
175  }
176 
177  void inverseRotateVector3(Vector3 & vec) const
178  {
179  vec = getInverseRotatedVector3(vec);
180  }
181 
182  void translateVector3(Vector3 & vec) const
183  {
184  vec = getTranslatedVector3(vec);
185  }
186 
188  {
189  vec = getInversetranslatedVector3(vec);
190  }
191 };
192 }
193 #endif
Definition: Vector4.h:31
Vector3 getRotatedVector3(const Vector3 &vec) const
Definition: Matrix4x4.h:144
void translateVector3(Vector3 &vec) const
Definition: Matrix4x4.h:182
void setRotationPartEuler(const Vector3 &rotations)
Definition: Matrix4x4.h:137
void rotateVector3(Vector3 &vec) const
Definition: Matrix4x4.h:172
~Matrix4x4(void)
Definition: Matrix4x4.h:51
Definition: Matrix4x4.h:31
Definition: Vector3.h:31
float y
Definition: Vector3.h:36
void inverseRotateVector3(Vector3 &vec) const
Definition: Matrix4x4.h:177
float x
Definition: Vector3.h:35
Matrix4x4(void)
Definition: Matrix4x4.h:39
Definition: Color.h:29
float z
Definition: Vector3.h:37
void inversetranslateVector3(Vector3 &vec) const
Definition: Matrix4x4.h:187
Vector3 getTranslationPart(void) const
Definition: Matrix4x4.h:157