Honeycomb  0.1
Component-Model Framework
Matrix4.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
6 
7 namespace honey
8 {
9 
10 template<class Real> class Quat_;
11 template<class Real> class Transform_;
12 template<class Real> class DecompAffine;
13 
15 
28 template<class Real, int Options>
29 class Matrix<4,4,Real,Options> : public MatrixBase<Matrix<4,4,Real,Options>>
30 {
32  template<class T> friend class MatrixBase;
33 
34  typedef Vec<2,Real> Vec2;
35  typedef Vec<3,Real> Vec3;
36  typedef Vec<4,Real> Vec4;
37  typedef Quat_<Real> Quat;
39  typedef DecompAffine<Double> DecompAffine;
40  using typename Super::Alge;
41  using typename Super::Trig;
42 
43 public:
44  using Super::s_rows;
45  using Super::s_cols;
46  using typename Super::VecCol;
47  using typename Super::VecRow;
48  using Super::mul;
49  using Super::transpose;
50  using Super::transposeMul;
51  using Super::mulTranspose;
53  using Super::m;
54 
56  Matrix() {}
57 
59  Matrix( Real _00, Real _01, Real _02, Real _03,
60  Real _10, Real _11, Real _12, Real _13,
61  Real _20, Real _21, Real _22, Real _23,
62  Real _30, Real _31, Real _32, Real _33)
63  {
64  m( 0) = _00; m( 1) = _01; m( 2) = _02; m( 3) = _03;
65  m( 4) = _10; m( 5) = _11; m( 6) = _12; m( 7) = _13;
66  m( 8) = _20; m( 9) = _21; m(10) = _22; m(11) = _23;
67  m(12) = _30; m(13) = _31; m(14) = _32; m(15) = _33;
68  }
69 
71  explicit Matrix(Real scalar) { fromScalar(scalar); }
73  Matrix(const Real* a, sdt rows, sdt cols, bool rowMajor = true) { this->resize(rows, cols); this->fromArray(a, rowMajor); }
75  Matrix(const Quat& q) { q.toMatrix(*this); }
77  Matrix(const Transform& tm) { fromTm(tm); }
79  template<class T>
80  Matrix(const MatrixBase<T>& rhs) { operator=(rhs); }
81 
83  template<class T>
84  Matrix<s_rows, T::s_cols, Real> operator*(const T& rhs) const { return Super::operator*(rhs); }
85  Matrix operator*(Real rhs) const { return Super::operator*(rhs); }
86 
89  {
90  m( 0) = 1; m( 1) = 0; m( 2) = 0; m( 3) = 0;
91  m( 4) = 0; m( 5) = 1; m( 6) = 0; m( 7) = 0;
92  m( 8) = 0; m( 9) = 0; m(10) = 1; m(11) = 0;
93  m(12) = 0; m(13) = 0; m(14) = 0; m(15) = 1;
94  return *this;
95  }
96 
98 
104  Matrix& fromTrs(const Vec3& trans, const Quat& rot = Quat::identity,
105  const Vec3& scale = Vec3::one, const Quat& skew = Quat::identity)
106  {
107  rot.toMatrix(*this);
108 
109  if (skew == Quat::identity)
110  {
111  m( 0) *= scale.x; m( 1) *= scale.y; m( 2) *= scale.z;
112  m( 4) *= scale.x; m( 5) *= scale.y; m( 6) *= scale.z;
113  m( 8) *= scale.x; m( 9) *= scale.y; m(10) *= scale.z;
114  }
115  else
116  {
117  Matrix mScale;
118  mScale.fromIdentity().setScale(scale);
119  Matrix mSkew(skew);
120  *this *= mSkew * mScale.mulTranspose(mSkew);
121  }
122 
123  setTrans(trans);
124  return *this;
125  }
126 
129  {
130  mt::for_<0, 16>([&](sdt i) { m(i) = f; });
131  return *this;
132  }
133 
135  Matrix& fromTm(const Transform& tm);
136 
138 
141  Matrix& fromObliqueProjection(const Vec3& normal, const Vec3& point, const Vec3& dir);
142 
144 
147  Matrix& fromPerspectiveProjection(const Vec3& normal, const Vec3& point, const Vec3& eye);
148 
150 
153  Matrix& fromReflection(const Vec3& normal, const Vec3& point);
154 
156 
160  Matrix& fromLookAt(const Vec3& eye, const Vec3& at, const Vec3& up);
161 
163  template<class T>
164  Matrix& operator=(const MatrixBase<T>& rhs) { Super::operator=(rhs); return *this; }
165 
166  Matrix& mul(const Matrix& rhs, Matrix& res) const
167  {
168  res( 0) = m( 0)*rhs( 0) + m( 1)*rhs( 4) + m( 2)*rhs( 8) + m( 3)*rhs(12);
169  res( 1) = m( 0)*rhs( 1) + m( 1)*rhs( 5) + m( 2)*rhs( 9) + m( 3)*rhs(13);
170  res( 2) = m( 0)*rhs( 2) + m( 1)*rhs( 6) + m( 2)*rhs(10) + m( 3)*rhs(14);
171  res( 3) = m( 0)*rhs( 3) + m( 1)*rhs( 7) + m( 2)*rhs(11) + m( 3)*rhs(15);
172 
173  res( 4) = m( 4)*rhs( 0) + m( 5)*rhs( 4) + m( 6)*rhs( 8) + m( 7)*rhs(12);
174  res( 5) = m( 4)*rhs( 1) + m( 5)*rhs( 5) + m( 6)*rhs( 9) + m( 7)*rhs(13);
175  res( 6) = m( 4)*rhs( 2) + m( 5)*rhs( 6) + m( 6)*rhs(10) + m( 7)*rhs(14);
176  res( 7) = m( 4)*rhs( 3) + m( 5)*rhs( 7) + m( 6)*rhs(11) + m( 7)*rhs(15);
177 
178  res( 8) = m( 8)*rhs( 0) + m( 9)*rhs( 4) + m(10)*rhs( 8) + m(11)*rhs(12);
179  res( 9) = m( 8)*rhs( 1) + m( 9)*rhs( 5) + m(10)*rhs( 9) + m(11)*rhs(13);
180  res(10) = m( 8)*rhs( 2) + m( 9)*rhs( 6) + m(10)*rhs(10) + m(11)*rhs(14);
181  res(11) = m( 8)*rhs( 3) + m( 9)*rhs( 7) + m(10)*rhs(11) + m(11)*rhs(15);
182 
183  res(12) = m(12)*rhs( 0) + m(13)*rhs( 4) + m(14)*rhs( 8) + m(15)*rhs(12);
184  res(13) = m(12)*rhs( 1) + m(13)*rhs( 5) + m(14)*rhs( 9) + m(15)*rhs(13);
185  res(14) = m(12)*rhs( 2) + m(13)*rhs( 6) + m(14)*rhs(10) + m(15)*rhs(14);
186  res(15) = m(12)*rhs( 3) + m(13)*rhs( 7) + m(14)*rhs(11) + m(15)*rhs(15);
187  return res;
188  }
189 
190  Matrix&& mul(const Matrix& rhs, Matrix&& res) const { return move(mul(rhs, res)); }
191 
193  VecCol operator*(const VecCol& v) const
194  {
195  VecCol res;
196  res.x = m( 0)*v.x + m( 1)*v.y + m( 2)*v.z + m( 3)*v.w;
197  res.y = m( 4)*v.x + m( 5)*v.y + m( 6)*v.z + m( 7)*v.w;
198  res.z = m( 8)*v.x + m( 9)*v.y + m(10)*v.z + m(11)*v.w;
199  res.w = m(12)*v.x + m(13)*v.y + m(14)*v.z + m(15)*v.w;
200  return res;
201  }
202 
204  Vec3 operator*(const Vec3& v) const
205  {
206  Vec3 res;
207  res.x = m( 0)*v.x + m( 1)*v.y + m( 2)*v.z + m( 3);
208  res.y = m( 4)*v.x + m( 5)*v.y + m( 6)*v.z + m( 7);
209  res.z = m( 8)*v.x + m( 9)*v.y + m(10)*v.z + m(11);
210  return res / (m(12)*v.x + m(13)*v.y + m(14)*v.z + m(15));
211  }
212 
214  Vec2 operator*(const Vec2& v) const
215  {
216  Vec2 res;
217  res.x = m( 0)*v.x + m( 1)*v.y + m( 3);
218  res.y = m( 4)*v.x + m( 5)*v.y + m( 7);
219  return res / (m(12)*v.x + m(13)*v.y + m(15));
220  }
221 
223  friend VecRow operator*(const VecRow& v, const Matrix& mat)
224  {
225  VecRow res;
226  res.x = v.x*mat( 0) + v.y*mat( 4) + v.z*mat( 8) + v.w*mat(12);
227  res.y = v.x*mat( 1) + v.y*mat( 5) + v.z*mat( 9) + v.w*mat(13);
228  res.z = v.x*mat( 2) + v.y*mat( 6) + v.z*mat(10) + v.w*mat(14);
229  res.w = v.x*mat( 3) + v.y*mat( 7) + v.z*mat(11) + v.w*mat(15);
230  return res;
231  }
232 
234  Vec3 mulAffine(const Vec3& v) const
235  {
236  Vec3 res;
237  res.x = m( 0)*v.x + m( 1)*v.y + m( 2)*v.z + m( 3);
238  res.y = m( 4)*v.x + m( 5)*v.y + m( 6)*v.z + m( 7);
239  res.z = m( 8)*v.x + m( 9)*v.y + m(10)*v.z + m(11);
240  return res;
241  }
242 
244  Vec3 mulRotScale(const Vec3& v) const
245  {
246  Vec3 res;
247  res.x = m( 0)*v.x + m( 1)*v.y + m( 2)*v.z;
248  res.y = m( 4)*v.x + m( 5)*v.y + m( 6)*v.z;
249  res.z = m( 8)*v.x + m( 9)*v.y + m(10)*v.z;
250  return res;
251  }
252 
253  Matrix& transpose(Matrix& res) const
254  {
255  res( 0)=m( 0); res( 1)=m( 4); res( 2)=m( 8); res( 3)=m(12);
256  res( 4)=m( 1); res( 5)=m( 5); res( 6)=m( 9); res( 7)=m(13);
257  res( 8)=m( 2); res( 9)=m( 6); res(10)=m(10); res(11)=m(14);
258  res(12)=m( 3); res(13)=m( 7); res(14)=m(11); res(15)=m(15);
259  return res;
260  }
261 
262  Matrix&& transpose(Matrix&& res) const { return move(transpose(res)); }
263 
265  {
266  std::swap(m(1), m(4)); std::swap(m(2), m(8)); std::swap(m(3), m(12));
267  std::swap(m(6), m(9)); std::swap(m(7), m(13));
268  std::swap(m(11), m(14));
269  }
270 
271  Matrix& transposeMul(const Matrix& rhs, Matrix& res) const
272  {
273  res( 0) = m( 0)*rhs( 0) + m( 4)*rhs( 4) + m( 8)*rhs( 8) + m(12)*rhs(12);
274  res( 1) = m( 0)*rhs( 1) + m( 4)*rhs( 5) + m( 8)*rhs( 9) + m(12)*rhs(13);
275  res( 2) = m( 0)*rhs( 2) + m( 4)*rhs( 6) + m( 8)*rhs(10) + m(12)*rhs(14);
276  res( 3) = m( 0)*rhs( 3) + m( 4)*rhs( 7) + m( 8)*rhs(11) + m(12)*rhs(15);
277 
278  res( 4) = m( 1)*rhs( 0) + m( 5)*rhs( 4) + m( 9)*rhs( 8) + m(13)*rhs(12);
279  res( 5) = m( 1)*rhs( 1) + m( 5)*rhs( 5) + m( 9)*rhs( 9) + m(13)*rhs(13);
280  res( 6) = m( 1)*rhs( 2) + m( 5)*rhs( 6) + m( 9)*rhs(10) + m(13)*rhs(14);
281  res( 7) = m( 1)*rhs( 3) + m( 5)*rhs( 7) + m( 9)*rhs(11) + m(13)*rhs(15);
282 
283  res( 8) = m( 2)*rhs( 0) + m( 6)*rhs( 4) + m(10)*rhs( 8) + m(14)*rhs(12);
284  res( 9) = m( 2)*rhs( 1) + m( 6)*rhs( 5) + m(10)*rhs( 9) + m(14)*rhs(13);
285  res(10) = m( 2)*rhs( 2) + m( 6)*rhs( 6) + m(10)*rhs(10) + m(14)*rhs(14);
286  res(11) = m( 2)*rhs( 3) + m( 6)*rhs( 7) + m(10)*rhs(11) + m(14)*rhs(15);
287 
288  res(12) = m( 3)*rhs( 0) + m( 7)*rhs( 4) + m(11)*rhs( 8) + m(15)*rhs(12);
289  res(13) = m( 3)*rhs( 1) + m( 7)*rhs( 5) + m(11)*rhs( 9) + m(15)*rhs(13);
290  res(14) = m( 3)*rhs( 2) + m( 7)*rhs( 6) + m(11)*rhs(10) + m(15)*rhs(14);
291  res(15) = m( 3)*rhs( 3) + m( 7)*rhs( 7) + m(11)*rhs(11) + m(15)*rhs(15);
292  return res;
293  }
294 
295  Matrix&& transposeMul(const Matrix& rhs, Matrix&& res) const { return move(transposeMul(rhs, res)); }
296 
297  Matrix& mulTranspose(const Matrix& rhs, Matrix& res) const
298  {
299  res( 0) = m( 0)*rhs( 0) + m( 1)*rhs( 1) + m( 2)*rhs( 2) + m( 3)*rhs( 3);
300  res( 1) = m( 0)*rhs( 4) + m( 1)*rhs( 5) + m( 2)*rhs( 6) + m( 3)*rhs( 7);
301  res( 2) = m( 0)*rhs( 8) + m( 1)*rhs( 9) + m( 2)*rhs(10) + m( 3)*rhs(11);
302  res( 3) = m( 0)*rhs(12) + m( 1)*rhs(13) + m( 2)*rhs(14) + m( 3)*rhs(15);
303 
304  res( 4) = m( 4)*rhs( 0) + m( 5)*rhs( 1) + m( 6)*rhs( 2) + m( 7)*rhs( 3);
305  res( 5) = m( 4)*rhs( 4) + m( 5)*rhs( 5) + m( 6)*rhs( 6) + m( 7)*rhs( 7);
306  res( 6) = m( 4)*rhs( 8) + m( 5)*rhs( 9) + m( 6)*rhs(10) + m( 7)*rhs(11);
307  res( 7) = m( 4)*rhs(12) + m( 5)*rhs(13) + m( 6)*rhs(14) + m( 7)*rhs(15);
308 
309  res( 8) = m( 8)*rhs( 0) + m( 9)*rhs( 1) + m(10)*rhs( 2) + m(11)*rhs( 3);
310  res( 9) = m( 8)*rhs( 4) + m( 9)*rhs( 5) + m(10)*rhs( 6) + m(11)*rhs( 7);
311  res(10) = m( 8)*rhs( 8) + m( 9)*rhs( 9) + m(10)*rhs(10) + m(11)*rhs(11);
312  res(11) = m( 8)*rhs(12) + m( 9)*rhs(13) + m(10)*rhs(14) + m(11)*rhs(15);
313 
314  res(12) = m(12)*rhs( 0) + m(13)*rhs( 1) + m(14)*rhs( 2) + m(15)*rhs( 3);
315  res(13) = m(12)*rhs( 4) + m(13)*rhs( 5) + m(14)*rhs( 6) + m(15)*rhs( 7);
316  res(14) = m(12)*rhs( 8) + m(13)*rhs( 9) + m(14)*rhs(10) + m(15)*rhs(11);
317  res(15) = m(12)*rhs(12) + m(13)*rhs(13) + m(14)*rhs(14) + m(15)*rhs(15);
318  return res;
319  }
320 
321  Matrix&& mulTranspose(const Matrix& rhs, Matrix&& res) const { return move(mulTranspose(rhs, res)); }
322 
323  Matrix& transposeMulTranspose(const Matrix& rhs, Matrix& res) const
324  {
325  res( 0) = m( 0)*rhs( 0) + m( 4)*rhs( 1) + m( 8)*rhs( 2) + m(12)*rhs( 3);
326  res( 1) = m( 0)*rhs( 4) + m( 4)*rhs( 5) + m( 8)*rhs( 6) + m(12)*rhs( 7);
327  res( 2) = m( 0)*rhs( 8) + m( 4)*rhs( 9) + m( 8)*rhs(10) + m(12)*rhs(11);
328  res( 3) = m( 0)*rhs(12) + m( 4)*rhs(13) + m( 8)*rhs(14) + m(12)*rhs(15);
329 
330  res( 4) = m( 1)*rhs( 0) + m( 5)*rhs( 1) + m( 9)*rhs( 2) + m(13)*rhs( 3);
331  res( 5) = m( 1)*rhs( 4) + m( 5)*rhs( 5) + m( 9)*rhs( 6) + m(13)*rhs( 7);
332  res( 6) = m( 1)*rhs( 8) + m( 5)*rhs( 9) + m( 9)*rhs(10) + m(13)*rhs(11);
333  res( 7) = m( 1)*rhs(12) + m( 5)*rhs(13) + m( 9)*rhs(14) + m(13)*rhs(15);
334 
335  res( 8) = m( 2)*rhs( 0) + m( 6)*rhs( 1) + m(10)*rhs( 2) + m(14)*rhs( 3);
336  res( 9) = m( 2)*rhs( 4) + m( 6)*rhs( 5) + m(10)*rhs( 6) + m(14)*rhs( 7);
337  res(10) = m( 2)*rhs( 8) + m( 6)*rhs( 9) + m(10)*rhs(10) + m(14)*rhs(11);
338  res(11) = m( 2)*rhs(12) + m( 6)*rhs(13) + m(10)*rhs(14) + m(14)*rhs(15);
339 
340  res(12) = m( 3)*rhs( 0) + m( 7)*rhs( 1) + m(11)*rhs( 2) + m(15)*rhs( 3);
341  res(13) = m( 3)*rhs( 4) + m( 7)*rhs( 5) + m(11)*rhs( 6) + m(15)*rhs( 7);
342  res(14) = m( 3)*rhs( 8) + m( 7)*rhs( 9) + m(11)*rhs(10) + m(15)*rhs(11);
343  res(15) = m( 3)*rhs(12) + m( 7)*rhs(13) + m(11)*rhs(14) + m(15)*rhs(15);
344  return res;
345  }
346 
347  Matrix&& transposeMulTranspose(const Matrix& rhs, Matrix&& res) const { return move(transposeMulTranspose(rhs, res)); }
348 
350  void orthonormalize();
351 
353  Matrix inverse(optional<Real&> det = optnull) const;
355  Matrix adjugate() const;
357  Real determinant() const;
358 
360 
365  void decompose( optional<Vec3&> trans = optnull, optional<Quat&> rot = optnull,
366  optional<Vec3&> scale = optnull, optional<Quat&> skew = optnull) const;
367 
369  Vec3 getTrans() const { return Vec3(m(3), m(7), m(11)); }
370 
372  void setTrans(const Vec3& v) { m(3) = v.x; m(7) = v.y; m(11) = v.z; }
373 
375  Quat getRot() const { return Quat().fromMatrix(*this); }
376 
378  void setRot(const Quat& q) { q.toMatrix(*this, true); }
379 
381  Vec3 getScale() const { return Vec3(m(0), m(5), m(10)); }
382 
384  void setScale(const Vec3& v, const Quat& skew = Quat::identity)
385  {
386  if (skew == Quat::identity)
387  {
388  m(0) = v.x; m(5) = v.y; m(10) = v.z;
389  }
390  else
391  {
392  auto scl = Matrix().fromIdentity();
393  scl(0) = v.x; scl(5) = v.y; scl(10) = v.z;
394  Matrix skewMat(skew);
395  scl = skewMat * scl.mulTranspose(skewMat);
396  m( 0) = scl( 0); m( 1) = scl( 1); m( 2) = scl( 2);
397  m( 4) = scl( 4); m( 5) = scl( 5); m( 6) = scl( 6);
398  m( 8) = scl( 8); m( 9) = scl( 9); m(10) = scl(10);
399  }
400  }
401 
403  void setScale(Real f) { setScale(Vec3(f)); }
404 
406  Matrix& translate(const Vec3& v) { Matrix tm; tm.fromIdentity(); tm.setTrans(v); return operator=(tm * *this); }
408  Matrix& preTranslate(const Vec3& v) { Matrix tm; tm.fromIdentity(); tm.setTrans(v); return this->operator*=(tm); }
409 
411  Matrix& rotate(const Quat& q) { return operator=(Matrix(q) * *this); }
413  Matrix& preRotate(const Quat& q) { return this->operator*=(Matrix(q)); }
414 
416  Matrix& scale(const Vec3& v, const Quat& skew = Quat::identity) { Matrix tm; tm.fromIdentity(); tm.setScale(v, skew); return operator=(tm * *this); }
418  Matrix& scale(Real f) { return scale(Vec3(f)); }
419 
421  Matrix& preScale(const Vec3& v, const Quat& skew = Quat::identity) { Matrix tm; tm.fromIdentity(); tm.setScale(v, skew); return this->operator*=(tm); }
423  Matrix& preScale(Real f) { return preScale(Vec3(f)); }
424 
425 protected:
426 
427  template<class Num>
428  Matrix& fromColMajor(const Num* a)
429  {
430  m( 0) = (Real)a[ 0]; m( 1) = (Real)a[ 4]; m( 2) = (Real)a[ 8]; m( 3) = (Real)a[12];
431  m( 4) = (Real)a[ 1]; m( 5) = (Real)a[ 5]; m( 6) = (Real)a[ 9]; m( 7) = (Real)a[13];
432  m( 8) = (Real)a[ 2]; m( 9) = (Real)a[ 6]; m(10) = (Real)a[10]; m(11) = (Real)a[14];
433  m(12) = (Real)a[ 3]; m(13) = (Real)a[ 7]; m(14) = (Real)a[11]; m(15) = (Real)a[15];
434  return *this;
435  }
436 
437  template<class Num>
438  Num* toColMajor(Num* a) const
439  {
440  a[ 0] = (Num)m( 0); a[ 1] = (Num)m( 4); a[ 2] = (Num)m( 8); a[ 3] = (Num)m(12);
441  a[ 4] = (Num)m( 1); a[ 5] = (Num)m( 5); a[ 6] = (Num)m( 9); a[ 7] = (Num)m(13);
442  a[ 8] = (Num)m( 2); a[ 9] = (Num)m( 6); a[10] = (Num)m(10); a[11] = (Num)m(14);
443  a[12] = (Num)m( 3); a[13] = (Num)m( 7); a[14] = (Num)m(11); a[15] = (Num)m(15);
444  return a;
445  }
446 
447 private:
449  void decomposeSkew( optional<Vec3&> trans, optional<Quat&> rot,
450  optional<Vec3&> scale, optional<Quat&> skew) const;
451 
452 public:
453  static const Matrix zero;
454  static const Matrix identity;
455 };
456 
458 template<class R, int Opt>
461 struct priv::map_impl<Matrix<4,4,R,Opt>, Matrix<4,4,R,Opt>>
462 {
463  template<class T, class O, class Func>
464  static O&& func(T&& m, O&& o, Func&& f)
465  {
466  mt::for_<0, 16>([&](sdt i) { o(i) = f(m(i)); });
467  return forward<O>(o);
468  }
469 };
470 
471 template<class R, int Opt>
472 struct priv::map_impl<Matrix<4,4,R,Opt>, Matrix<4,4,R,Opt>, Matrix<4,4,R,Opt>>
473 {
474  template<class T, class T2, class O, class Func>
475  static O&& func(T&& m, T2&& rhs, O&& o, Func&& f)
476  {
477  mt::for_<0, 16>([&](sdt i) { o(i) = f(m(i), rhs(i)); });
478  return forward<O>(o);
479  }
480 };
481 
482 template<class R, int O, class Accum_>
483 struct priv::reduce_impl<Matrix<4,4,R,O>, Accum_>
484 {
485  template<class T, class Accum, class Func>
486  static Accum_ func(T&& m, Accum&& initVal, Func&& f)
487  {
488  return f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(forward<Accum>(initVal),
489  m(0)), m(1)), m(2)), m(3)),
490  m(4)), m(5)), m(6)), m(7)),
491  m(8)), m(9)), m(10)), m(11)),
492  m(12)), m(13)), m(14)), m(15));
493 
494  }
495 };
496 
497 template<class R, int O, class Accum_>
498 struct priv::reduce_impl<Matrix<4,4,R,O>, Accum_, Matrix<4,4,R,O>>
499 {
500  template<class T, class T2, class Accum, class Func>
501  static Accum_ func(T&& m, T2&& rhs, Accum&& initVal, Func&& f)
502  {
503  return f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(forward<Accum>(initVal),
504  m(0),rhs(0)), m(1),rhs(1)), m(2),rhs(2)), m(3),rhs(3)),
505  m(4),rhs(4)), m(5),rhs(5)), m(6),rhs(6)), m(7),rhs(7)),
506  m(8),rhs(8)), m(9),rhs(9)), m(10),rhs(10)), m(11),rhs(11)),
507  m(12),rhs(12)), m(13),rhs(13)), m(14),rhs(14)), m(15),rhs(15));
508  }
509 };
511 
516 
517 extern template class Matrix<4,4,Float>;
518 extern template class Matrix<4,4,Double>;
519 extern template class Matrix<4,4,Quad>;
520 
521 }
522 
523 #define Section_Header
524 #include "Honey/Math/Alge/Matrix/platform/Matrix4.h"
525 #undef Section_Header
MatrixS & fromScalar(Real f)
Initialize with scalar in every element.
Definition: Base.h:72
(m x n)-dimensional matrix
Definition: Matrix.h:24
Real determinant() const
Get the determinant. Returns the pseudo-determinant if this matrix is not square. ...
Definition: Base.h:487
Matrix && mulTranspose(const Matrix &rhs, Matrix &&res) const
Definition: Matrix4.h:321
Matrix & scale(Real f)
Uniform scale.
Definition: Matrix4.h:418
Matrix()
No init.
Definition: Matrix4.h:56
Vec3 operator*(const Vec3 &v) const
Assumes vector has w = 1, transforms and projects result back into w = 1.
Definition: Matrix4.h:204
static optnull_t optnull
Null optional, use to reset an optional to an uninitialized state or test for initialization.
Definition: Optional.h:12
Matrix && transposeMulTranspose(const Matrix &rhs, Matrix &&res) const
Definition: Matrix4.h:347
const Real & m(sdt i) const
Access matrix element at index. Wrapper for convenience only, more readable than (*this)(i) ...
Definition: Base.h:567
Matrix & transposeMulTranspose(const Matrix &rhs, Matrix &res) const
Definition: Matrix4.h:323
Matrix(const Quat &q)
Construct from quaternion.
Definition: Matrix4.h:75
Matrix & translate(const Vec3 &v)
Make a tm that performs this transform first, then does a translation. ie. T * This.
Definition: Matrix4.h:406
ptrdiff_t sdt
Size difference type, shorthand for ptrdiff_t.
Definition: Core.h:92
Vec3 mulAffine(const Vec3 &v) const
Transform vector with affine part of matrix (apply pos/rot/scale). Same as operator*() but without pr...
Definition: Matrix4.h:234
Matrix4 & toMatrix(Matrix4 &rot, bool b3x3=false) const
Convert quaternion to 4x4 homogeneous rotation matrix. Set b3x3 to true to store the result only in t...
Definition: Quat.cpp:422
Vec2 operator*(const Vec2 &v) const
Assumes vector has (z,w) = (0,1), transforms and projects result back into w = 1. ...
Definition: Matrix4.h:214
Matrix && mul(const Matrix &rhs, Matrix &&res) const
Definition: Matrix4.h:190
Res && transposeMul(const T &rhs, Res &&res) const
Stores result in and returns res.
Definition: Base.h:341
Matrix & transposeMul(const Matrix &rhs, Matrix &res) const
Definition: Matrix4.h:271
Matrix(const MatrixBase< T > &rhs)
Construct from matrix of same size.
Definition: Matrix4.h:80
Matrix & preRotate(const Quat &q)
Make a tm that does a rotation first, then performs this transform. ie. This * R. ...
Definition: Matrix4.h:413
Vec< 2 > Vec2
2D column vector types
Definition: Vec2.h:202
Matrix< s_cols, s_rows, Real > transpose() const
Returns new matrix.
Definition: Base.h:325
Matrix(Real scalar)
Initialize with scalar in every element.
Definition: Matrix4.h:71
Quat_ & fromMatrix(const Matrix4 &rot)
Construct from 4x4 homogeneous matrix. Rotation is extracted from upper-left 3x3 submatrix.
Definition: Quat.cpp:91
MatrixS & fromIdentity()
Make matrix identity. For non-square matrices the identity is in the upper-left square block...
Definition: Base.h:75
Matrix(Real _00, Real _01, Real _02, Real _03, Real _10, Real _11, Real _12, Real _13, Real _20, Real _21, Real _22, Real _23, Real _30, Real _31, Real _32, Real _33)
Construct from values.
Definition: Matrix4.h:59
Res && transposeMulTranspose(const T &rhs, Res &&res) const
Stores result in and returns res.
Definition: Base.h:383
Matrix< 4, 4, Double > Matrix4_d
Definition: Matrix4.h:515
Matrix & fromColMajor(const Num *a)
Definition: Matrix4.h:428
void setScale(Real f)
Set uniform scale. Beware: overwrites rotation.
Definition: Matrix4.h:403
Matrix && transpose(Matrix &&res) const
Definition: Matrix4.h:262
Vec< 4 > Vec4
4D column vector types
Definition: Vec4.h:674
Matrix & mul(const Matrix &rhs, Matrix &res) const
Definition: Matrix4.h:166
static const Matrix zero
Definition: Matrix4.h:453
Matrix & fromScalar(Real f)
Initialize with scalar in every element.
Definition: Matrix4.h:128
Res && mul(const T &rhs, Res &&res) const
Multiply with another matrix. This mat's column size must match rhs' row size. Stores result in and r...
Definition: Base.h:161
Matrix operator*(Real rhs) const
Definition: Matrix4.h:85
Matrix & fromTrs(const Vec3 &trans, const Quat &rot=Quat::identity, const Vec3 &scale=Vec3::one, const Quat &skew=Quat::identity)
Init from translation, rotation, scale and skew.
Definition: Matrix4.h:104
Vec3 getScale() const
Get scale. Beware: bogus if mat contains rot/skew.
Definition: Matrix4.h:381
Matrix(const Real *a, sdt rows, sdt cols, bool rowMajor=true)
Initialize from array with dimensions (rows x cols). If the array is in row-major format set rowMajor...
Definition: Matrix4.h:73
Matrix & scale(const Vec3 &v, const Quat &skew=Quat::identity)
Make a tm that performs this transform first, then does a scale. ie. S * This.
Definition: Matrix4.h:416
Quat getRot() const
Get rotation. Beware: bogus if mat contains scale/skew.
Definition: Matrix4.h:375
static const Quat_ identity
Definition: Quat.h:347
VecCol operator*(const VecCol &v) const
Square matrix, so multiplying a column vector on the right returns a column vector of the same dimens...
Definition: Matrix4.h:193
Matrix && transposeMul(const Matrix &rhs, Matrix &&res) const
Definition: Matrix4.h:295
float Real
Real number type. See Real_ for real number operations and constants.
Definition: Real.h:21
MatrixS & resize(sdt rows, sdt cols)
Sets number of rows/columns and reallocates only if the size has changed (rows*cols). All previous data is lost on reallocation. Returns self.
Definition: Base.h:277
friend VecRow operator*(const VecRow &v, const Matrix &mat)
Square matrix, so multiplying a row vector on the left returns a row vector of the same dimension...
Definition: Matrix4.h:223
Enables any type to be optional so it can exist in an uninitialized null state.
Definition: Optional.h:52
void transposeInPlace()
Definition: Matrix4.h:264
Vec< 3 > Vec3
3D column vector types
Definition: Vec3.h:356
Matrix & operator=(const MatrixBase< T > &rhs)
Assign to matrix of any size. Asserts that any fixed dimensions in this matrix match those in rhs...
Definition: Matrix.h:44
Matrix< 4, 4, Real > Matrix4
Definition: Matrix4.h:513
Vec3 getTrans() const
Get translation.
Definition: Matrix4.h:369
void setTrans(const Vec3 &v)
Set translation.
Definition: Matrix4.h:372
Matrix & preTranslate(const Vec3 &v)
Make a tm that does a translation first, then performs this transform. ie. This * T...
Definition: Matrix4.h:408
MatrixS & operator*=(const MatrixBase< T > &rhs)
Definition: Base.h:181
Vec3 mulRotScale(const Vec3 &v) const
Transform vector with upper-left 3x3 rot/scale sub-matrix (no translation)
Definition: Matrix4.h:244
Quat_< Real > Quat
Definition: Quat.h:353
Matrix< s_rows, T::s_cols, Real > operator*(const T &rhs) const
Multiply with another matrix. Returns a new matrix.
Definition: Base.h:178
#define swap(a, i, j)
MatrixBase & operator=(const MatrixBase< T > &rhs)
Assign to matrix of any size. Asserts that any fixed dimensions in this matrix match those in rhs...
Definition: Base.h:97
Quaternion rotation class. Represents a counter-clockwise rotation of an angle about its axis...
Definition: Matrix4.h:10
Matrix & operator=(const MatrixBase< T > &rhs)
Assign to matrix of same size.
Definition: Matrix4.h:164
Matrix< s_cols, s_rows, Real > inverse(optional< Real & > det=optnull) const
Get the pseudo-inverse of this matrix. The pseudo-determinant will be returned in det if specified...
Definition: Base.h:421
void setScale(const Vec3 &v, const Quat &skew=Quat::identity)
Set scale. Beware: overwrites rotation.
Definition: Matrix4.h:384
Matrix & fromIdentity()
Make matrix identity.
Definition: Matrix4.h:88
Matrix & mulTranspose(const Matrix &rhs, Matrix &res) const
Definition: Matrix4.h:297
Matrix & preScale(const Vec3 &v, const Quat &skew=Quat::identity)
Make a tm that does a scale first, then performs this transform. ie. This * S.
Definition: Matrix4.h:421
Matrix(const Transform &tm)
Initialize from transform.
Definition: Matrix4.h:77
Num * toColMajor(Num *a) const
Definition: Matrix4.h:438
Transform_< Real > Transform
Definition: Transform.h:242
Matrix & preScale(Real f)
Uniform prescale.
Definition: Matrix4.h:423
Matrix()
No init.
Definition: Matrix.h:29
Matrix base class.
Definition: Base.h:17
void setRot(const Quat &q)
Set rotation. Beware: overwrites scale/skew.
Definition: Matrix4.h:378
Matrix< 4, 4, Float > Matrix4_f
Definition: Matrix4.h:514
Matrix & transpose(Matrix &res) const
Definition: Matrix4.h:253
MatrixS & fromArray(const Num *a, bool rowMajor=true)
Initialize from array. If the array is in row-major format set rowMajor to true, otherwise set to fal...
Definition: Base.h:53
Matrix< s_rows, T::s_cols, Real > operator*(const T &rhs) const
Forward to base.
Definition: Matrix4.h:84
Global Honeycomb namespace.
static const Matrix identity
Definition: Matrix4.h:454
Vec< s_cols, Real, matrix::Option::vecRow > VecRow
Definition: Base.h:49
A 3D linear transform from TRS components (translation, rotation, and scale/skew) ...
Definition: Matrix4.h:11
Matrix & rotate(const Quat &q)
Make a tm that performs this transform first, then does a rotation. ie. R * This. ...
Definition: Matrix4.h:411
Res && mulTranspose(const T &rhs, Res &&res) const
Stores result in and returns res.
Definition: Base.h:362