// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: t -*- // Copyright 2010 Michael Smith, all rights reserved. // This library is free software; you can redistribute it and / or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // Inspired by: /**************************************** * 3D Vector Classes * By Bill Perone (billperone@yahoo.com) */ // // 3x3 matrix implementation. // // Note that the matrix is organised in row-normal form (the same as // applies to array indexing). // // In addition to the template, this header defines the following types: // // Matrix3i 3x3 matrix of signed integers // Matrix3ui 3x3 matrix of unsigned integers // Matrix3l 3x3 matrix of signed longs // Matrix3ul 3x3 matrix of unsigned longs // Matrix3f 3x3 matrix of signed floats // #ifndef MATRIX3_H #define MATRIX3_H #include "vector3.h" // 3x3 matrix with elements of type T template class Matrix3 { public: // Vectors comprising the rows of the matrix Vector3 a, b, c; // trivial ctor // note that the Vector3 ctor will zero the vector elements Matrix3() {} // setting ctor Matrix3(const Vector3 a0, const Vector3 b0, const Vector3 c0): a(a0), b(b0), c(c0) {} // setting ctor Matrix3(const T ax, const T ay, const T az, const T bx, const T by, const T bz, const T cx, const T cy, const T cz): a(ax,ay,az), b(bx,by,bz), c(cx,cy,cz) {} // function call operator void operator () (const Vector3 a0, const Vector3 b0, const Vector3 c0) { a = a0; b = b0; c = c0; } // test for equality bool operator == (const Matrix3 &m) { return (a==m.a && b==m.b && c==m.c); } // test for inequality bool operator != (const Matrix3 &m) { return (a!=m.a || b!=m.b || c!=m.c); } // negation Matrix3 operator - (void) const { return Matrix3(-a,-b,-c); } // addition Matrix3 operator + (const Matrix3 &m) const { return Matrix3(a+m.a, b+m.b, c+m.c); } Matrix3 &operator += (const Matrix3 &m) { return *this = *this + m; } // subtraction Matrix3 operator - (const Matrix3 &m) const { return Matrix3(a-m.a, b-m.b, c-m.c); } Matrix3 &operator -= (const Matrix3 &m) { return *this = *this - m; } // uniform scaling Matrix3 operator * (const T num) const { return Matrix3(a*num, b*num, c*num); } Matrix3 &operator *= (const T num) { return *this = *this * num; } Matrix3 operator / (const T num) const { return Matrix3(a/num, b/num, c/num); } Matrix3 &operator /= (const T num) { return *this = *this / num; } // multiplication by a vector Vector3 operator *(const Vector3 &v) const { return Vector3(a.x * v.x + a.y * v.y + a.z * v.z, b.x * v.x + b.y * v.y + b.z * v.z, c.x * v.x + c.y * v.y + c.z * v.z); } // multiplication by another Matrix3 Matrix3 operator *(const Matrix3 &m) const { Matrix3 temp (Vector3(a.x * m.a.x + a.y * m.b.x + a.z * m.c.x, a.x * m.a.y + a.y * m.b.y + a.z * m.c.y, a.x * m.a.z + a.y * m.b.z + a.z * m.c.z), Vector3(b.x * m.a.x + b.y * m.b.x + b.z * m.c.x, b.x * m.a.y + b.y * m.b.y + b.z * m.c.y, b.x * m.a.z + b.y * m.b.z + b.z * m.c.z), Vector3(c.x * m.a.x + c.y * m.b.x + c.z * m.c.x, c.x * m.a.y + c.y * m.b.y + c.z * m.c.y, c.x * m.a.z + c.y * m.b.z + c.z * m.c.z)); return temp; } Matrix3 &operator *=(const Matrix3 &m) { return *this = *this * m; } // transpose the matrix Matrix3 transposed(void) const { return Matrix3(Vector3(a.x, b.x, c.x), Vector3(a.y, b.y, c.y), Vector3(a.z, b.z, c.z)); } Matrix3 transpose(void) { return *this = transposed(); } // check if any elements are NAN bool is_nan(void) { return a.is_nan() || b.is_nan() || c.is_nan(); } // fill in the matrix with a standard rotation void rotation(enum Rotation rotation); // create a rotation matrix from Euler angles void from_euler(float roll, float pitch, float yaw); // create eulers from a rotation matrix void to_euler(float *roll, float *pitch, float *yaw); }; typedef Matrix3 Matrix3i; typedef Matrix3 Matrix3ui; typedef Matrix3 Matrix3l; typedef Matrix3 Matrix3ul; typedef Matrix3 Matrix3f; #endif // MATRIX3_H