matrix4.hpp
Go to the documentation of this file.
1 /*
2 ** TP CPE Lyon
3 ** Copyright (C) 2014 Damien Rohmer
4 **
5 ** This program is free software: you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation, either version 3 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
20 #pragma once
21 
22 #ifndef MATRIX4_HPP
23 #define MATRIX4_HPP
24 
25 #include <iostream>
26 #include <vector>
27 
28 
29 
30 namespace cpe
31 {
32 class vec3;
33 class vec4;
34 class matrix3;
35 
37 class matrix4
38 {
39 
40 
41 public:
42 
43  // ********************************************* //
44  // ********************************************* //
45  // CONSTRUCTORS
46  // ********************************************* //
47  // ********************************************* //
48 
50  matrix4();
52  matrix4(float x00,float x01,float x02,float x03,
53  float x10,float x11,float x12,float x13,
54  float x20,float x21,float x22,float x23,
55  float x30,float x31,float x32,float x33);
57  matrix4(const matrix3& m);
58 
59  // ********************************************* //
60  // ********************************************* //
61  // Special initialization
62  // ********************************************* //
63  // ********************************************* //
64 
66  static matrix4 identity();
68  static matrix4 zeros();
70  static matrix4 rotation(const vec3& axis,float angle);
72  static matrix4 scale(float s);
74  static matrix4 scale(float s_x,float s_y,float s_z,float s_w);
76  static matrix4 translation(const vec3& translation);
78  static matrix4 transformation(const matrix3& m3,const vec3& translation);
79 
80  // ********************************************* //
81  // ********************************************* //
82  // Projection matrix (emulate glu(t) functions)
83  // ********************************************* //
84  // ********************************************* //
85 
90  static matrix4 projection_perspective(float fovy,float aspect,float z_near,float z_far);
94  static matrix4 projection_frustum(float left,float right,float bottom,float top,float near,float far);
98  static matrix4 projection_orthographic(float left,float right,float bottom,float top,float near,float far);
102  static matrix4 projection_look_at(const matrix4& current_matrix,const vec3& eye,const vec3& center,const vec3& up);
103 
104 
105  // ********************************************* //
106  // ********************************************* //
107  // Element access
108  // ********************************************* //
109  // ********************************************* //
110 
112  float operator()(const size_t& k1,const size_t& k2) const;
114  float& operator()(const size_t& k1,const size_t& k2);
115 
118  const float* pointer() const;
121  float* pointer_unprotected();
122 
123 
124 
125  // ********************************************* //
126  // ********************************************* //
127  // Math operator
128  // ********************************************* //
129  // ********************************************* //
130 
132  matrix4 operator+(const matrix4& m2) const;
134  matrix4 operator+(float s) const;
135 
137  matrix4 operator-(const matrix4& m2) const;
139  matrix4 operator-(float s) const;
140 
141 
143  matrix4 operator*(float s) const;
145  vec4 operator*(const vec4& v) const;
147  vec3 operator*(const vec3& v) const;
149  matrix4 operator*(const matrix4& m2) const;
151  matrix4 operator/(float s) const;
152 
153 
155  matrix4& operator+=(const matrix4& m);
157  matrix4& operator+=(float s);
159  matrix4& operator-=(const matrix4& m);
161  matrix4& operator-=(float s);
163  matrix4& operator*=(float s);
165  matrix4& operator/=(float s);
166 
168  matrix4 operator-() const;
169 
171  matrix4 product_compontentwise(const matrix4& m) const;
174 
176  matrix4 transposed() const;
177 
178  // ********************************************* //
179  // ********************************************* //
180  // Deformation
181  // ********************************************* //
182  // ********************************************* //
183 
185  matrix4 translated(const vec3& translation) const;
187  void translate_internal(const vec3& translation);
188 
189 
190 
191 
192 
193 
194  // ********************************************* //
195  // ********************************************* //
196  // Projection matrix
197  // ********************************************* //
198  // ********************************************* //
199 
201  std::vector<float> to_vector() const;
202 
203 private:
204 
205  // ********************************************* //
206  // ********************************************* //
207  // Internal parameters
208  // ********************************************* //
209  // ********************************************* //
210 
212  float m[16];
213 
214 
215  // ********************************************* //
216  // ********************************************* //
217  // Helper
218  // ********************************************* //
219  // ********************************************* //
220 
222  void assert_size(const size_t& k1,const size_t& k2) const;
223 
224 };
225 
226 // ********************************************* //
227 // ********************************************* //
228 // Output
229 // ********************************************* //
230 // ********************************************* //
231 
233 std::ostream& operator<<(std::ostream& stream,const matrix4& m);
234 
235 
236 
237 
239 matrix4 operator+(float s,const matrix4& m);
241 matrix4 operator-(float s,const matrix4& m);
243 matrix4 operator*(float s,const matrix4& m);
244 
245 }
246 
247 #endif
matrix4 operator+(const matrix4 &m2) const
operator
Definition: matrix4.cpp:133
static matrix4 projection_perspective(float fovy, float aspect, float z_near, float z_far)
build a perspective projection similar to gluPerspective
Definition: matrix4.cpp:362
void translate_internal(const vec3 &translation)
Translated matrix (modify the matrix)
Definition: matrix4.cpp:461
float operator()(const size_t &k1, const size_t &k2) const
Access to the k_th entry (k in [0,3])
Definition: matrix4.cpp:122
static matrix4 projection_look_at(const matrix4 &current_matrix, const vec3 &eye, const vec3 &center, const vec3 &up)
build a perspective projection similar to gluLookAt see: http://www.opengl.org/sdk/docs/man/xhtml/glu...
Definition: matrix4.cpp:432
float * pointer_unprotected()
fast pointer access
Definition: matrix4.cpp:341
const float * pointer() const
fast pointer access
Definition: matrix4.cpp:337
Matrix 3x3.
Definition: matrix3.hpp:36
matrix1x4 operator-(float s, const matrix1x4 &m)
operator
Definition: matrix1x4.cpp:82
matrix1x4 operator+(float s, const matrix1x4 &m)
operator
Definition: matrix1x4.cpp:61
matrix4 operator*(float s) const
multiply by a scalar operator
Definition: matrix4.cpp:193
static matrix4 zeros()
build zero matrix
Definition: matrix4.cpp:68
float m[16]
internal storage of the matrix
Definition: matrix4.hpp:212
matrix4 product_compontentwise(const matrix4 &m) const
does componentwise multiplication
Definition: matrix4.cpp:300
Matrix 4x4.
Definition: matrix4.hpp:37
static matrix4 projection_frustum(float left, float right, float bottom, float top, float near, float far)
build a perspective projection (frustum) similar to gluPerspective see: http://www.opengl.org/sdk/docs/man/xhtml/glFrustum.xml
Definition: matrix4.cpp:381
matrix1x4 operator*(float s, const matrix1x4 &m)
multiply by a scalar operator
Definition: matrix1x4.cpp:93
static matrix4 identity()
build identity matrix
Definition: matrix4.cpp:61
static matrix4 transformation(const matrix3 &m3, const vec3 &translation)
build from matrix3 and translation
Definition: matrix4.cpp:114
matrix4 & operator*=(float s)
internal *
Definition: matrix4.cpp:279
matrix4 & operator/=(float s)
internal /
Definition: matrix4.cpp:286
std::ostream & operator<<(std::ostream &stream, const matrix1x4 &_m)
output the vector in ostream
Definition: matrix1x4.cpp:182
void assert_size(const size_t &k1, const size_t &k2) const
assert that a size_t belongs to [[0,3]]
Definition: matrix4.cpp:327
static matrix4 rotation(const vec3 &axis, float angle)
build rotation matrix
Definition: matrix4.cpp:77
static matrix4 scale(float s)
build scaling matrix
Definition: matrix4.cpp:91
matrix4 transposed() const
transpose matrix
Definition: matrix4.cpp:354
matrix4 & operator+=(const matrix4 &m)
internal +
Definition: matrix4.cpp:249
Vectors/Points 3D.
Definition: vec3.hpp:36
matrix4 & product_compontentwise_internal(const matrix4 &m)
does componentwise multiplication
Definition: matrix4.cpp:309
matrix4 & operator-=(const matrix4 &m)
internal -
Definition: matrix4.cpp:264
static matrix4 translation(const vec3 &translation)
build translation matrix
Definition: matrix4.cpp:106
matrix4 translated(const vec3 &translation) const
Translated matrix (return of copy of the translated matrix)
Definition: matrix4.cpp:454
matrix4()
empty constructor (identity)
Definition: matrix4.cpp:34
matrix4 operator/(float s) const
divide by a scalar operator
Definition: matrix4.cpp:241
Vectors/Points 4D.
Definition: vec4.hpp:36
matrix4 operator-() const
unary negation
Definition: matrix4.cpp:293
std::vector< float > to_vector() const
convert 4x4 matrix into a 16 vector of float (glMultmatrixf)
Definition: matrix4.cpp:345
static matrix4 projection_orthographic(float left, float right, float bottom, float top, float near, float far)
build an orthographic projection similar to gluPerspective see: http://www.opengl.org/sdk/docs/man/xhtml/glOrtho.xml
Definition: matrix4.cpp:406