matrix2.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 MATRIX2_HPP
23 #define MATRIX2_HPP
24 
25 #include <iostream>
26 #include <vector>
27 
28 
29 
30 namespace cpe
31 {
32 class vec2;
33 
34 
36 class matrix2
37 {
38 
39 public:
40 
41  // ********************************************* //
42  // ********************************************* //
43  // CONSTRUCTORS
44  // ********************************************* //
45  // ********************************************* //
46 
48  matrix2();
50  matrix2(float x00,float x01,
51  float x10,float x11);
52 
53  // ********************************************* //
54  // ********************************************* //
55  // Special initialization
56  // ********************************************* //
57  // ********************************************* //
58 
60  static matrix2 identity();
62  static matrix2 zeros();
64  static matrix2 rotation(float angle);
66  static matrix2 scale(float s);
68  static matrix2 scale(float s_x,float s_y);
69 
70 
71  // ********************************************* //
72  // ********************************************* //
73  // Element access
74  // ********************************************* //
75  // ********************************************* //
76 
78  float operator()(const size_t& k1,const size_t& k2) const;
80  float& operator()(const size_t& k1,const size_t& k2);
81 
84  const float* pointer() const;
87  float* pointer_unprotected();
88 
89 
90 
91  // ********************************************* //
92  // ********************************************* //
93  // Math operator
94  // ********************************************* //
95  // ********************************************* //
96 
98  matrix2 operator+(const matrix2& m2) const;
100  matrix2 operator+(float s) const;
101 
103  matrix2 operator-(const matrix2& m2) const;
105  matrix2 operator-(float s) const;
106 
107 
109  matrix2 operator*(float s) const;
111  vec2 operator*(const vec2& v) const;
113  matrix2 operator*(const matrix2& m2) const;
115  matrix2 operator/(float s) const;
116 
117 
119  matrix2& operator+=(const matrix2& m);
121  matrix2& operator+=(float s);
123  matrix2& operator-=(const matrix2& m);
125  matrix2& operator-=(float s);
127  matrix2& operator*=(float s);
129  matrix2& operator/=(float s);
130 
132  matrix2 operator-() const;
133 
135  matrix2 product_compontentwise(const matrix2& m) const;
138 
140  matrix2 transposed() const;
141 
142 
143 
144 
145 
146 
147 
148  // ********************************************* //
149  // ********************************************* //
150  // Projection matrix
151  // ********************************************* //
152  // ********************************************* //
153 
155  std::vector<float> to_vector() const;
156 
157 private:
158 
159  // ********************************************* //
160  // ********************************************* //
161  // Internal parameters
162  // ********************************************* //
163  // ********************************************* //
164 
166  float m[4];
167 
168 
169  // ********************************************* //
170  // ********************************************* //
171  // Helper
172  // ********************************************* //
173  // ********************************************* //
174 
176  void assert_size(const size_t& k1,const size_t& k2) const;
177 
178 };
179 
181 matrix2 operator+(float s,const matrix2& m);
183 matrix2 operator-(float s,const matrix2& m);
185 matrix2 operator*(float s,const matrix2& m);
186 
187 // ********************************************* //
188 // ********************************************* //
189 // Output
190 // ********************************************* //
191 // ********************************************* //
192 
194 std::ostream& operator<<(std::ostream& stream,const matrix2& m);
195 
196 
197 
198 
199 
200 
201 }
202 
203 #endif
matrix2 & operator+=(const matrix2 &m)
internal +
Definition: matrix2.cpp:171
const float * pointer() const
fast pointer access
Definition: matrix2.cpp:253
matrix2 & operator*=(float s)
internal *
Definition: matrix2.cpp:201
matrix2 operator/(float s) const
divide by a scalar operator
Definition: matrix2.cpp:165
static matrix2 scale(float s)
build scaling matrix
Definition: matrix2.cpp:64
Matrix 2x2.
Definition: matrix2.hpp:36
static matrix2 zeros()
build zero matrix
Definition: matrix2.cpp:48
matrix2 operator*(float s) const
multiply by a scalar operator
Definition: matrix2.cpp:136
matrix2 & operator/=(float s)
internal /
Definition: matrix2.cpp:208
matrix2 operator+(const matrix2 &m2) const
operator
Definition: matrix2.cpp:87
float operator()(const size_t &k1, const size_t &k2) const
Access to the k_th entry (k in [0,1])
Definition: matrix2.cpp:76
matrix2()
empty constructor (identity)
Definition: matrix2.cpp:31
matrix1x4 operator-(float s, const matrix1x4 &m)
operator
Definition: matrix1x4.cpp:82
matrix1x4 operator+(float s, const matrix1x4 &m)
operator
Definition: matrix1x4.cpp:61
matrix2 & operator-=(const matrix2 &m)
internal -
Definition: matrix2.cpp:186
void assert_size(const size_t &k1, const size_t &k2) const
assert that a size_t belongs to [[0,1]]
Definition: matrix2.cpp:243
float * pointer_unprotected()
fast pointer access
Definition: matrix2.cpp:257
std::vector< float > to_vector() const
convert 3x3 matrix into a vector of float of size 9
Definition: matrix2.cpp:261
matrix2 operator-() const
unary negation
Definition: matrix2.cpp:215
matrix1x4 operator*(float s, const matrix1x4 &m)
multiply by a scalar operator
Definition: matrix1x4.cpp:93
matrix2 transposed() const
transpose matrix
Definition: matrix2.cpp:270
std::ostream & operator<<(std::ostream &stream, const matrix1x4 &_m)
output the vector in ostream
Definition: matrix1x4.cpp:182
Vectors/Points 2D.
Definition: vec2.hpp:34
static matrix2 rotation(float angle)
build rotation matrix
Definition: matrix2.cpp:55
static matrix2 identity()
build identity matrix
Definition: matrix2.cpp:43
matrix2 product_compontentwise(const matrix2 &m) const
does componentwise multiplication
Definition: matrix2.cpp:220
float m[4]
internal storage of the matrix
Definition: matrix2.hpp:166
matrix2 & product_compontentwise_internal(const matrix2 &m)
does componentwise multiplication
Definition: matrix2.cpp:227