18 #ifndef ION_MATH_MATRIX_H_
19 #define ION_MATH_MATRIX_H_
34 template <
int Dimension,
typename T>
50 T m10,
T m11,
T m12,
T m13,
51 T m20,
T m21,
T m22,
T m23,
52 T m30,
T m31,
T m32,
T m33);
55 explicit Matrix(
const T array[Dimension * Dimension]);
69 CheckIndices(row, col);
70 return elem_[row][col];
79 CheckIndices(row, col);
80 return elem_[row][col];
88 T*
Data() {
return &elem_[0][0]; }
89 const T*
Data()
const {
return &elem_[0][0]; }
104 return Addition(lhs, rhs);
109 return Subtraction(lhs, rhs);
114 return Product(m0, m1);
119 return AreEqual(m0, m1);
122 return !AreEqual(m0, m1);
127 void CheckIndices(
int row,
int col)
const {
132 DCHECK(row >= 0 && row < Dimension && col >= 0 && col < Dimension);
136 void MultiplyScalar(
T s);
142 static bool AreEqual(
const Matrix& m0,
const Matrix& m1);
144 T elem_[Dimension][Dimension];
148 template <
int Dimension,
typename T>
149 std::ostream& operator<<(std::ostream& out, const Matrix<Dimension, T>& m) {
151 for (
int row = 0; row < Dimension; ++row) {
152 for (
int col = 0; col < Dimension; ++col) {
154 if (col != Dimension - 1)
157 if (row < Dimension - 1)
164 template <
int Dimension,
typename T>
168 for (
int row = 0; row < Dimension; ++row) {
169 for (
int col = 0; col < Dimension; ++col) {
171 if (col != Dimension - 1 && !base::GetExpectedChar<','>(in))
174 if (row != Dimension - 1 && !base::GetExpectedChar<';'>(in))
189 template <
int Dimension,
typename T>
198 template <
int Dimension,
typename T>
201 T m20,
T m21,
T m22) {
214 template <
int Dimension,
typename T>
216 T m10,
T m11,
T m12,
T m13,
217 T m20,
T m21,
T m22,
T m23,
218 T m30,
T m31,
T m32,
T m33) {
238 template <
int Dimension,
typename T>
240 #if defined(ION_PLATFORM_WINDOWS) && defined(ION_ARCH_X86_64)
241 if ((reinterpret_cast<size_t>(&array[0]) & 0xF) != 0) {
246 for (
int i = 0; i < Dimension; ++i) {
247 for (
int j = 0; j < Dimension; ++j) {
248 elem_[i][j] = array[i*Dimension+j];
254 memcpy(elem_, array,
sizeof(elem_));
257 template <
int Dimension,
typename T>
template <
typename U>
259 for (
int i = 0; i < Dimension; ++i) {
260 for (
int j = 0; j < Dimension; ++j)
261 elem_[i][j] = static_cast<const T>(other(i, j));
265 template <
int Dimension,
typename T>
268 for (
int row = 0; row < Dimension; ++row) {
269 for (
int col = 0; col < Dimension; ++col)
270 result.elem_[row][col] = static_cast<T>(0);
275 template <
int Dimension,
typename T>
278 for (
int row = 0; row < Dimension; ++row) {
279 for (
int col = 0; col < Dimension; ++col) {
280 result.elem_[row][col] =
281 row == col ?
static_cast<T>(1) : static_cast<T>(0);
287 template <
int Dimension,
typename T>
289 for (
int row = 0; row < Dimension; ++row) {
290 for (
int col = 0; col < Dimension; ++col)
291 elem_[row][col] *= s;
295 template <
int Dimension,
typename T>
296 Matrix<Dimension, T> Matrix<Dimension, T>::Negation()
const {
298 for (
int row = 0; row < Dimension; ++row) {
299 for (
int col = 0; col < Dimension; ++col)
300 result.elem_[row][col] = -elem_[row][col];
305 template <
int Dimension,
typename T>
306 Matrix<Dimension, T> Matrix<Dimension, T>::Scale(
const Matrix& m,
T s) {
308 for (
int row = 0; row < Dimension; ++row) {
309 for (
int col = 0; col < Dimension; ++col)
310 result.elem_[row][col] = m.elem_[row][col] * s;
315 template<
int Dimension,
typename T>
316 Matrix<Dimension, T> Matrix<Dimension, T>::Addition(
317 const Matrix<Dimension, T> &lhs,
const Matrix<Dimension, T> &rhs) {
318 Matrix<Dimension, T> result;
319 for (
int row = 0; row < Dimension; ++row) {
320 for (
int col = 0; col < Dimension; ++col)
321 result.elem_[row][col] = lhs.elem_[row][col] + rhs.elem_[row][col];
326 template<
int Dimension,
typename T>
327 Matrix<Dimension, T> Matrix<Dimension, T>::Subtraction(
328 const Matrix<Dimension, T> &lhs,
const Matrix<Dimension, T> &rhs) {
329 Matrix<Dimension, T> result;
330 for (
int row = 0; row < Dimension; ++row) {
331 for (
int col = 0; col < Dimension; ++col)
332 result.elem_[row][col] = lhs.elem_[row][col] - rhs.elem_[row][col];
337 template <
int Dimension,
typename T>
338 Matrix<Dimension, T> Matrix<Dimension, T>::Product(
const Matrix& m0,
341 for (
int row = 0; row < Dimension; ++row) {
342 for (
int col = 0; col < Dimension; ++col) {
343 for (
int i = 0; i < Dimension; ++i)
344 result.elem_[row][col] += m0.elem_[row][i] * m1.elem_[i][col];
350 template <
int Dimension,
typename T>
351 bool Matrix<Dimension, T>::AreEqual(
const Matrix& m0,
const Matrix& m1) {
352 for (
int row = 0; row < Dimension; ++row) {
353 for (
int col = 0; col < Dimension; ++col) {
354 if (m0.elem_[row][col] != m1.elem_[row][col])
377 #endif // ION_MATH_MATRIX_H_
friend bool operator!=(const Matrix &m0, const Matrix &m1)
Matrix< 4, float > Matrix4f
void operator*=(T s)
Self-modifying multiplication operators.
void operator*=(const Matrix &m)
Matrix< 2, double > Matrix2d
T & operator()(int row, int col)
Mutable element accessors.
Matrix< 3, float > Matrix3f
Matrix operator-() const
Unary operators.
static Matrix Identity()
Returns an identity Matrix.
Matrix< 4, double > Matrix4d
The Matrix class defines a square N-dimensional matrix.
friend Matrix operator*(const Matrix &m0, const Matrix &m1)
Binary multiplication operator.
const T * operator[](int row) const
std::istream & GetExpectedChar(std::istream &in)
Reads a single character from the stream and returns the stream.
Copyright 2016 Google Inc.
friend Matrix operator*(const Matrix &m, T s)
Binary scale operators.
Matrix< 2, float > Matrix2f
Dimension- and type-specific typedefs.
std::istream & GetExpectedString(std::istream &in, const std::string &expected)
Attempts to read a string from the stream and returns the stream.
friend bool operator==(const Matrix &m0, const Matrix &m1)
Exact equality and inequality comparisons.
friend Matrix operator*(T s, const Matrix &m)
T * Data()
Return a pointer to the data for interfacing with libraries.
static Matrix Zero()
Returns a Matrix containing all zeroes.
#define ION_STATIC_ASSERT(expr, message)
Copyright 2016 Google Inc.
std::istream & operator>>(std::istream &in, Angle< T > &a)
Matrix()
The default constructor zero-initializes all elements.
friend Matrix operator+(const Matrix &lhs, const Matrix &rhs)
Binary matrix addition.
friend Matrix operator-(const Matrix &lhs, const Matrix &rhs)
Binary matrix subtraction.
const T & operator()(int row, int col) const
Read-only element accessors.
Matrix< 3, double > Matrix3d