Ion: math/matrixutils.h Source File

1 

18 #ifndef ION_MATH_MATRIXUTILS_H_

19 #define ION_MATH_MATRIXUTILS_H_

20 

26 

30 

31 namespace ion {

32 namespace math {

33 

35 

38 

39 

40 namespace internal {

41 

44 template <int Dimension, typename T, typename VectorType>

46  const VectorType& v) {

47  VectorType result = VectorType::Zero();

48  for (int row = 0; row < Dimension; ++row) {

49  for (int col = 0; col < Dimension; ++col)

50  result[row] += m(row, col) * v[col];

51  }

52  return result;

53 }

54 

55 }

56 

58 

61 

62 

64 template <int Dimension, typename T>

67  for (int row = 0; row < Dimension; ++row) {

68  for (int col = 0; col < Dimension; ++col)

69  result(row, col) = m(col, row);

70  }

71  return result;

72 }

73 

76 template <int Dimension, typename T>

80 }

81 

84 template <int Dimension, typename T>

88 }

89 

91 template <int Dimension, typename T>

94  for (int col = 0; col < Dimension; ++col)

97  result[col] = m(row, col);

98  return result;

99 }

100 

102 template <int Dimension, typename T>

105  for (int row = 0; row < Dimension; ++row)

108  result[row] = m(row, col);

109  return result;

110 }

111 

114 template <int Dimension, typename T> ION_API

115 T Determinant(const Matrix<Dimension, T>& m);

116 

119 template <int Dimension, typename T> ION_API

120 Matrix<Dimension, T> CofactorMatrix(const Matrix<Dimension, T>& m);

121 

126 template <int Dimension, typename T> ION_API

128  const Matrix<Dimension, T>& m, T* determinant);

129 

133 template <int Dimension, typename T>

135  return AdjugateWithDeterminant<Dimension, T>(m, static_cast<T*>(NULL));

136 }

137 

142 template <int Dimension, typename T> ION_API

144  const Matrix<Dimension, T>& m, T* determinant);

145 

149 template <int Dimension, typename T>

151  return InverseWithDeterminant<Dimension, T>(m, static_cast<T*>(NULL));

152 }

153 

155 template <int Dimension, typename T>

156 static bool MatricesAlmostEqual(const Matrix<Dimension, T>& m0,

157  const Matrix<Dimension, T>& m1, T tolerance) {

158  for (int row = 0; row < Dimension; ++row) {

159  for (int col = 0; col < Dimension; ++col) {

160  if (Abs(m0(row, col) - m1(row, col)) > tolerance)

161  return false;

162  }

163  }

164  return true;

165 }

166 

170 template <int Dimension, typename T>

172 

176 template <typename T>

177 inline static void ScaleTranslationComponent(Matrix<4, T>* matrix, T scale) {

178  (*matrix)(0, 3) *= scale;

179  (*matrix)(1, 3) *= scale;

180  (*matrix)(2, 3) *= scale;

181 }

182 

183 }

184 }

185 

186 #endif // ION_MATH_MATRIXUTILS_H_

Matrix< Dimension, T > AdjugateWithDeterminant(const Matrix< Dimension, T > &m, T *determinant)

Returns the adjugate of the matrix, which is defined as the transpose of the cofactor matrix...

Vector< Dimension, T > Row(const Matrix< Dimension, T > &m, int row)

Returns a particular row of a matrix as a vector.

bool MatrixAlmostOrthogonal(const Matrix< Dimension, T > &m, T tolerance)

Returns true if the dot product of all column vector pairs in the matrix is less than a provided tole...

Vector< Dimension, T > Column(const Matrix< Dimension, T > &m, int col)

Returns a particular column of a matrix as a vector.

The Matrix class defines a square N-dimensional matrix.

T Determinant(const Matrix< Dimension, T > &m)

Public functions.

Matrix< Dimension, T > CofactorMatrix(const Matrix< Dimension, T > &m)

Returns the signed cofactor matrix (adjunct) of the matrix.

Vector< Dimension, T > operator*(const Matrix< Dimension, T > &m, const Vector< Dimension, T > &v)

Multiplies a Matrix and a column Vector of the same Dimension to produce another column Vector...

Matrix< Dimension, T > InverseWithDeterminant(const Matrix< Dimension, T > &m, T *determinant)

Returns the inverse of the matrix.

VectorType MultiplyMatrixAndVector(const Matrix< Dimension, T > &m, const VectorType &v)

Multiplies a matrix and some type of column vector (Vector or Point) to produce another column vector...

Matrix< Dimension, T > Inverse(const Matrix< Dimension, T > &m)

Returns the inverse of the matrix.

Matrix< Dimension, T > Adjugate(const Matrix< Dimension, T > &m)

Returns the adjugate of the matrix, which is defined as the transpose of the cofactor matrix...

const T Abs(const T &val)

Returns the absolute value of a number in a type-safe way.

Matrix< Dimension, T > Transpose(const Matrix< Dimension, T > &m)

Public functions.