Honeycomb  0.1
Component-Model Framework
Matrix4.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2013 Daniel Carter. Distributed under the Boost Software License v1.0.
2 #ifdef HONEY_DX9
3 #ifdef Section_Header
4 
5 namespace honey
6 {
7 
8 template<> Matrix<4,4,Float>& Matrix<4,4,Float>::mul(const Matrix& rhs, Matrix& res) const;
9 template<> Matrix<4,4,Float>& Matrix<4,4,Float>::transposeMul(const Matrix& rhs, Matrix& res) const;
10 template<> Matrix<4,4,Float>& Matrix<4,4,Float>::mulTranspose(const Matrix& rhs, Matrix& res) const;
11 template<> Matrix<4,4,Float>& Matrix<4,4,Float>::transposeMulTranspose(const Matrix& rhs, Matrix& res) const;
12 template<> Matrix<4,4,Float> Matrix<4,4,Float>::inverse(option<Real&> det) const;
14 template<> void Matrix<4,4,Float>::decompose( option<Vec3&> trans, option<Quat&> rot,
15  option<Vec3&> scale, option<Quat&> skew) const;
16 
17 }
18 
19 #endif
20 
21 #ifdef Section_Source
22 
23 namespace honey
24 {
25 
26 static FLOAT* DX(Float& f) { return reinterpret_cast<FLOAT*>(&f); }
27 static D3DXVECTOR3* DX(Vec3_f& v) { return reinterpret_cast<D3DXVECTOR3*>(&v); }
28 static D3DXQUATERNION* DX(Quat_f& q) { return reinterpret_cast<D3DXQUATERNION*>(&q); }
29 static D3DXMATRIX* DX(Matrix4_f& m) { return reinterpret_cast<D3DXMATRIX*>(&m); }
30 static const D3DXMATRIX* DX(const Matrix4_f& m) { return reinterpret_cast<const D3DXMATRIX*>(&m); }
31 
32 template<> Matrix<4,4,Float>& Matrix<4,4,Float>::mul(const Matrix& rhs, Matrix& res) const
33 {
34  D3DXMatrixMultiply(DX(res), DX(*this), DX(rhs));
35  return res;
36 }
37 
38 template<> Matrix<4,4,Float>& Matrix<4,4,Float>::transposeMul(const Matrix& rhs, Matrix& res) const
39 {
40  Matrix trans = transpose();
41  D3DXMatrixMultiply(DX(res), DX(trans), DX(rhs));
42  return res;
43 }
44 
45 template<> Matrix<4,4,Float>& Matrix<4,4,Float>::mulTranspose(const Matrix& rhs, Matrix& res) const
46 {
47  Matrix trans = rhs.transpose();
48  D3DXMatrixMultiply(DX(res), DX(*this), DX(trans));
49  return res;
50 }
51 
52 template<> Matrix<4,4,Float>& Matrix<4,4,Float>::transposeMulTranspose(const Matrix& rhs, Matrix& res) const
53 {
54  D3DXMatrixMultiplyTranspose(DX(res), DX(rhs), DX(*this));
55  return res;
56 }
57 
58 template<> Matrix<4,4,Float> Matrix<4,4,Float>::inverse(option<Real&> det) const
59 {
60  Matrix ret;
61  D3DXMatrixInverse(DX(ret), det ? DX(*det) : nullptr, DX(*this));
62  return ret;
63 }
64 
66 {
67  return D3DXMatrixDeterminant(DX(*this));
68 }
69 
70 template<> void Matrix<4,4,Float>::decompose( option<Vec3&> trans_, option<Quat&> rot_,
71  option<Vec3&> scale_, option<Quat&> skew) const
72 {
73  Vec3 trans, scale; Quat rot;
74  D3DXMatrixDecompose(DX(scale), DX(rot), DX(trans), DX(*this));
75 
76  if (skew)
77  {
78  //Skew algorithm is expensive, only do it if scale is non-uniform.
79  Real tol = scale.x * 1.0e-4;
80  if (!Alge::isNear(scale.x, scale.y, tol) || !Alge::isNear(scale.x, scale.z, tol))
81  {
82  decomposeSkew(trans_, rot_, scale_, skew);
83  return;
84  }
85  skew = Quat::identity;
86  }
87 
88  if (trans_) trans_ = getTrans();
89  if (rot_) rot_ = rot.inverse(); //Dx mat is transposed, so quat is inversed
90  if (scale_) scale_ = scale;
91 }
92 
93 }
94 
95 #endif
96 #endif
Real determinant() const
Get the determinant. Returns the pseudo-determinant if this matrix is not square. ...
Definition: Base.h:487
Float_::Real Float
float type
Definition: Float.h:61
Vec< 3, Float > Vec3_f
Definition: Vec3.h:357
Res && transposeMul(const T &rhs, Res &&res) const
Stores result in and returns res.
Definition: Base.h:341
Res && transposeMulTranspose(const T &rhs, Res &&res) const
Stores result in and returns res.
Definition: Base.h:383
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
static bool isNear(Int a, Int b, Int tol)
Check whether two numbers are near each other, given a tolerance.
Definition: Alge.h:105
static const Quat_ identity
Definition: Quat.h:347
float Real
Real number type. See Real_ for real number operations and constants.
Definition: Real.h:21
Vec< 3 > Vec3
3D column vector types
Definition: Vec3.h:356
Quat_< Real > Quat
Definition: Quat.h:353
Quat_< Float > Quat_f
Definition: Quat.h:354
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
Matrix< 4, 4, Float > Matrix4_f
Definition: Matrix4.h:514
Global Honeycomb namespace.
Res && mulTranspose(const T &rhs, Res &&res) const
Stores result in and returns res.
Definition: Base.h:362