Euphoria
mat3.cc
Go to the documentation of this file.
1 #include "base/mat3.h"
2 
3 
4 
5 namespace eu
6 {
7  mat3f::mat3f(const mat2f& mat)
8  : data
9  {
10  mat.get(0,0), mat.get(0,1), 0,
11  mat.get(1,0), mat.get(1,1), 0,
12  0, 0, 1
13  }
14  {
15  }
16 
17 
18  [[nodiscard]]
19  mat3f
21  (
22  float t00, float t01, float t02,
23  float t10, float t11, float t12,
24  float t20, float t21, float t22
25  )
26  {
27  return
28  {
29  t00, t01, t02,
30  t10, t11, t12,
31  t20, t21, t22
32  };
33  }
34 
35 
36  [[nodiscard]]
37  mat3f
38  mat3f::from_major(const Scale3f& major)
39  {
40  const float zero = 0;
41  return from_row_major
42  (
43  major.x, zero, zero,
44  zero, major.y, zero,
45  zero, zero, major.z
46  );
47  }
48 
49 
50  [[nodiscard]]
51  mat3f
52  mat3f::from_scale(const Scale3f& scale)
53  {
54  return from_major(scale);
55  }
56 
57 
58  [[nodiscard]]
59  mat3f
61  {
62  return from_row_major
63  (
64  1, 0, t.x,
65  0, 1, t.y,
66  0, 0, 1
67  );
68  }
69 
70 
71  [[nodiscard]]
72  mat3f
74  {
75  const auto c = cos(a);
76  const auto s = sin(a);
77  return from_row_major
78  (
79  1, 0, 0,
80  0, c, -s,
81  0, s, c
82  );
83  }
84 
85 
86  [[nodiscard]]
87  mat3f
89  {
90  const auto c = cos(a);
91  const auto s = sin(a);
92  return from_row_major
93  (
94  c, 0, s,
95  0, 1, 0,
96  -s, 0, c
97  );
98  }
99 
100 
101  [[nodiscard]]
102  mat3f
104  {
105  const auto c = cos(a);
106  const auto s = sin(a);
107  return from_row_major(c, -s, 0, s, c, 0, 0, 0, 1);
108  }
109 
110 
111  [[nodiscard]]
112  mat3f
114  {
115  const float rcos = cos(aa.angle);
116  const float rsin = sin(aa.angle);
117 
118  const auto u = aa.axis.x;
119  const auto v = aa.axis.y;
120  const auto w = aa.axis.z;
121 
122  return mat3f::from_col_major
123  (
124  rcos + u * u * (1 - rcos),
125  w * rsin + v * u * (1 - rcos),
126  -v * rsin + w * u * (1 - rcos),
127 
128  -w * rsin + u * v * (1 - rcos),
129  rcos + v * v * (1 - rcos),
130  u * rsin + w * v * (1 - rcos),
131 
132  v * rsin + u * w * (1 - rcos),
133  -u * rsin + v * w * (1 - rcos),
134  rcos + w * w * (1 - rcos)
135  );
136  }
137 
138 
139  vec3f
141  {
142  return {get(0, 0), get(1, 1), get(2, 2)};
143  }
144 
145 
146  vec3f
147  mat3f::get_axis(int col) const
148  {
149  return get_column(col);
150  }
151 
152 
153  vec3f
155  {
156  return get_axis(0);
157  }
158 
159 
160  vec3f
162  {
163  return get_axis(1);
164  }
165 
166 
167  vec3f
169  {
170  return get_axis(2);
171  }
172 
173 
174  mat3f
176  {
177  return from_col_major
178  (
179  get(0, 0), get(0, 1), get(0, 2),
180  get(1, 0), get(1, 1), get(1, 2),
181  get(2, 0), get(2, 1), get(2, 2)
182  );
183  }
184 
185 
186  void
188  {
189 #define OP(i) data[i] += rhs.data[i]
190  OP(0); OP(1); OP(2);
191  OP(3); OP(4); OP(5);
192  OP(6); OP(7); OP(8);
193 #undef OP
194  }
195 
196 
197  void
199  {
200 #define OP(i) data[i] -= rhs.data[i]
201  OP(0); OP(1); OP(2);
202  OP(3); OP(4); OP(5);
203  OP(6); OP(7); OP(8);
204 #undef OP
205  }
206 
207 
208  mat3f
209  mat3f::get_rotated(const AxisAngle& aa) const
210  {
211  return *this * from_axis_angle(aa);
212  }
213 
214 
215  mat3f
216  mat3f::get_scaled(const Scale3f& scale) const
217  {
218  return *this * from_scale(scale);
219  }
220 
221 
222  const float*
224  {
225  return data;
226  }
227 
228 
229  float*
231  {
232  return data;
233  }
234 
235 
236  float
237  mat3f::get(int row, int col) const
238  {
239  return data[col * 3 + row];
240  }
241 
242 
243  vec3f
244  mat3f::get_column(int c) const
245  {
246  return {get(0, c), get(1, c), get(2, c)};
247  }
248 
249 
250  vec3f
251  mat3f::get_row(int r) const
252  {
253  return {get(r, 0), get(r, 1), get(r, 2)};
254  }
255 
256 
257  std::string to_string(const mat3f& m)
258  {
259  return fmt::format("({}, {}, {})", m.get_row(0), m.get_row(1), m.get_row(2));
260  }
261 
262 
263  mat3f
264  operator+(const mat3f& lhs, const mat3f& rhs)
265  {
266  mat3f t = lhs;
267  t += rhs;
268  return t;
269  }
270 
271 
272  mat3f
273  operator-(const mat3f& lhs, const mat3f& rhs)
274  {
275  mat3f t = lhs;
276  t -= rhs;
277  return t;
278  }
279 
280  float
281  get_component_multiply_sum(const vec3f& lhs, const vec3f& rhs)
282  {
283  const auto x = lhs.x * rhs.x;
284  const auto y = lhs.y * rhs.y;
285  const auto z = lhs.z* rhs.z;
286  return x + y + z;
287  }
288 
289 
290  mat3f operator*(const mat3f& lhs, const mat3f& rhs)
291  {
292 #define OP(r, c) \
293  get_component_multiply_sum(vec3f{lhs.get_row(r)}, vec3f{rhs.get_column(c)})
294  return mat3f::from_row_major
295  (
296  OP(0, 0), OP(0, 1), OP(0, 2),
297  OP(1, 0), OP(1, 1), OP(1, 2),
298  OP(2, 0), OP(2, 1), OP(2, 2)
299  );
300 #undef OP
301  }
302 
303 
304  vec3f operator*(const mat3f& lhs, const vec3f& rhs)
305  {
306 #define OP(r) get_component_multiply_sum(vec3f{lhs.get_row(r)}, rhs)
307  return {OP(0), OP(1), OP(2)};
308 #undef OP
309  }
310 
311 
312 }
#define OP(i)
Definition: assert.h:90
Angle operator+(const Angle &lhs, const Angle &rhs)
Definition: angle.cc:123
Angle operator-(const Angle &lhs, const Angle &rhs)
Definition: angle.cc:132
std::string to_string(const Aabb &a)
Definition: aabb.cc:110
float cos(const Angle &ang)
Definition: angle.cc:68
float sin(const Angle &ang)
Definition: angle.cc:61
Angle operator*(const Angle &lhs, float rhs)
Definition: angle.cc:149
float get_component_multiply_sum(const vec2f &lhs, const vec2f &rhs)
Definition: mat2.cc:131
unit3f axis
a unit-vector
Definition: axisangle.h:13
Angle angle
rotation according to right-hand rule
Definition: axisangle.h:16
Definition: mat2.h:10
float get(int row, int col) const
Definition: mat2.cc:87
Definition: mat3.h:12
static mat3f from_rot_z(const Angle &a)
Definition: mat3.cc:103
mat3f get_rotated(const AxisAngle &aa) const
Definition: mat3.cc:209
float get(int row, int col) const
Definition: mat3.cc:237
static mat3f from_axis_angle(const AxisAngle &aa)
Definition: mat3.cc:113
vec3f get_major() const
Definition: mat3.cc:140
vec3f get_axis(int col) const
Definition: mat3.cc:147
void operator-=(const mat3f &rhs)
Definition: mat3.cc:198
static mat3f from_col_major(float t00, float t01, float t02, float t10, float t11, float t12, float t20, float t21, float t22)
Definition: mat3.cc:21
vec3f get_y_axis() const
Definition: mat3.cc:161
static mat3f from_rot_x(const Angle &a)
Definition: mat3.cc:73
static mat3f from_translation2d(const vec2f &t)
Definition: mat3.cc:60
constexpr static mat3f from_row_major(float t00, float t10, float t20, float t01, float t11, float t21, float t02, float t12, float t22)
Definition: mat3.h:43
static mat3f from_major(const Scale3f &major)
Definition: mat3.cc:38
static mat3f from_scale(const Scale3f &scale)
Definition: mat3.cc:52
vec3f get_row(int r) const
Definition: mat3.cc:251
vec3f get_column(int c) const
Definition: mat3.cc:244
mat3f get_transposed() const
Definition: mat3.cc:175
float * get_data_ptr()
Definition: mat3.cc:230
static mat3f from_rot_y(const Angle &a)
Definition: mat3.cc:88
void operator+=(const mat3f &rhs)
Definition: mat3.cc:187
mat3f get_scaled(const Scale3f &scale) const
Definition: mat3.cc:216
vec3f get_z_axis() const
Definition: mat3.cc:168
vec3f get_x_axis() const
Definition: mat3.cc:154
Definition: vec2.h:33
Definition: vec3.h:48
float x
Definition: vec3.h:49
float y
Definition: vec3.h:50
float z
Definition: vec3.h:51