summaryrefslogtreecommitdiffstats
path: root/src/math/matrix.h
blob: 463ff0fb509c9ac2bce9b72d25558b473a6fb4f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// math/matrix.h

/* Matrix struct and functions */

#pragma once

#include "const.h"

#include <cmath>

/* TODO

 void      MatRotateXZY(D3DMATRIX &mat, D3DVECTOR angle);
 void      MatRotateZXY(D3DMATRIX &mat, D3DVECTOR angle);

*/

// Math module namespace
namespace Math
{

/** 4x4 matrix

  Represents an universal 4x4 matrix that can be used in OpenGL and DirectX engines.
  Contains the required methods for operating on matrices (inverting, multiplying, etc.).

  All methods are made inline to maximize optimization.

 **/
struct Matrix
{
  //! Matrix values in row-major format
  float m[16];

  //! Creates the indentity matrix
  inline Matrix()
  {
    LoadIdentity();
  }

  //! Creates the matrix from given values
  /** \a m  values in row-major format */
  inline Matrix(float m[16])
  {
    this->m = m;
  }

  //! Loads the zero matrix
  inline void LoadZero()
  {
    for (int i = 0; i < 16; ++i)
      m[i] = 0.0f;
  }

  //! Loads the identity matrix
  inline void LoadIdentity()
  {
    LoadZero();
    m[0] = m[1][1] = m[2][2] = m[3][3] = 1.0f;
  }

  //! Calculates the determinant of the matrix
  /** \returns the determinant */
  float Det() const
  {
    float result = 0.0f;
    for (int i = 0; i < 4; ++i)
    {
      result += m[0][i] * Cofactor(0, i);
    }
    return result;
  }

  //! Transposes the matrix
  void Transpose()
  {
    Matrix temp = *this;
    for (int r = 0; r < 4; ++r)
    {
      for (int c = 0; c < 4; ++c)
      {
        m[r][c] = temp.m[c][r];
      }
    }
  }

  //! Calculates the cofactor of the matrix
  /** \a r row (0 to 3)
      \a c column (0 to 3)
      \returns the cofactor or 0.0f if invalid r, c given*/
  float Cofactor(int r, int c) const
  {
    if ((r < 0) || (r > 3) || (c < 0) || (c > 3))
      return 0.0f;

    float tab[3][3];
    int tabR = 0;
    for (int r = 0; r < 4; ++r)
    {
      if (r == i) continue;
      int tabC = 0;
      for (int c = 0; c < 4; ++c)
      {
        if (c == j) continue;
        tab[tabR][tabC] = m[r][c];
        ++tabC;
      }
      ++tabR;
    }

    float result =   tab[0][0] * (tab[1][1] * tab[2][2] - tab[1][2] * tab[2][1])
                   - tab[0][1] * (tab[1][0] * tab[2][2] - tab[1][2] * tab[2][0])
                   + tab[0][2] * (tab[1][0] * tab[2][1] - tab[1][1] * tab[2][0]);

    if ((i + j) % 2 == 0)
      result = -result;

    return result;
  }

  //! Inverts the matrix
  inline void Invert()
  {
    float d = Det();
    if (fabs(d) <= Math::TOLERANCE)
      return;

    Matrix temp = *this;
    for (int r = 0; r < 4; ++r)
    {
      for (int c = 0; c < 4; ++c)
      {
        m[r][c] = (1.0f / d) * temp.Cofactor(r, c);
      }
    }

    Tranpose();
  }

  //! Multiplies the matrix with the given matrix
  /** \a right right-hand matrix */
  inline void Multiply(const Matrix &right)
  {
    Matrix left = *this;
    for (int r = 0; r < 4; ++r)
    {
      for (int c = 0; c < 4; ++c)
      {
        m[r][c] = 0.0;
        for (int i = 0; i < 4; ++i)
        {
          m[r][c] += left.m[r][i] * right.m[i][c];
        }
      }
    }
  }
};

//! Convenience function for inverting a matrix
/** \a m input matrix
    \a result result -- inverted matrix */
void InvertMatrix(const Matrix &m, Matrix &result)
{
  result = m;
  result.Invert();
}

//! Convenience function for multiplying a matrix
/** \a left left-hand matrix
    \a right right-hand matrix
    \a result result -- multiplied matrices */*
void MultiplyMatrices(const Matrix &left, const Matrix &right, Matrix &result)
{
  result = left;
  result.Multiply(right);
}

}; // namespace Math