• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

matrix3.h

Go to the documentation of this file.
00001 // -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: t -*-
00002 
00003 // Copyright 2010 Michael Smith, all rights reserved.
00004 
00005 //      This library is free software; you can redistribute it and / or
00006 //      modify it under the terms of the GNU Lesser General Public
00007 //      License as published by the Free Software Foundation; either
00008 //      version 2.1 of the License, or (at your option) any later version.
00009 
00010 // Inspired by:
00011 /****************************************
00012  * 3D Vector Classes
00013  * By Bill Perone (billperone@yahoo.com)
00014  */
00015 
00016 //
00017 // 3x3 matrix implementation.
00018 //
00019 // Note that the matrix is organised in row-normal form (the same as
00020 // applies to array indexing).
00021 //
00022 // In addition to the template, this header defines the following types:
00023 //
00024 // Matrix3i             3x3 matrix of signed integers
00025 // Matrix3ui    3x3 matrix of unsigned integers
00026 // Matrix3l             3x3 matrix of signed longs
00027 // Matrix3ul    3x3 matrix of unsigned longs
00028 // Matrix3f             3x3 matrix of signed floats
00029 //
00030 
00031 #ifndef MATRIX3_H
00032 #define MATRIX3_H
00033 
00034 #include "vector3.h"
00035 
00036 // 3x3 matrix with elements of type T
00037 template <typename T>
00038 class Matrix3 {
00039 public:
00040 
00041         // Vectors comprising the rows of the matrix
00042         Vector3<T>      a, b, c;
00043 
00044         // trivial ctor
00045         // note that the Vector3 ctor will zero the vector elements
00046         Matrix3<T>() {}
00047 
00048         // setting ctor
00049         Matrix3<T>(const Vector3<T> a0, const Vector3<T> b0, const Vector3<T> c0): a(a0), b(b0), c(c0) {}
00050 
00051         // setting ctor
00052         Matrix3<T>(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) {}        
00053         
00054         // function call operator
00055         void operator () (const Vector3<T> a0, const Vector3<T> b0, const Vector3<T> c0)
00056         {       a = a0; b = b0; c = c0;  }
00057 
00058         // test for equality
00059         bool operator == (const Matrix3<T> &m)
00060         {       return (a==m.a && b==m.b && c==m.c);    }
00061 
00062         // test for inequality
00063         bool operator != (const Matrix3<T> &m)
00064         {       return (a!=m.a || b!=m.b || c!=m.c);    }
00065 
00066         // negation
00067         Matrix3<T> operator - (void) const
00068         {       return Matrix3<T>(-a,-b,-c);    }
00069 
00070         // addition
00071         Matrix3<T> operator + (const Matrix3<T> &m) const
00072         {   return Matrix3<T>(a+m.a, b+m.b, c+m.c);      }
00073         Matrix3<T> &operator += (const Matrix3<T> &m)
00074         {       return *this = *this + m;       }
00075 
00076         // subtraction
00077         Matrix3<T> operator - (const Matrix3<T> &m) const
00078         {   return Matrix3<T>(a-m.a, b-m.b, c-m.c);      }
00079         Matrix3<T> &operator -= (const Matrix3<T> &m)
00080         {       return *this = *this - m;       }
00081            
00082         // uniform scaling
00083         Matrix3<T> operator * (const T num) const
00084         {       return Matrix3<T>(a*num, b*num, c*num); }
00085         Matrix3<T> &operator *= (const T num)
00086         {       return *this = *this * num;     }
00087          Matrix3<T> operator / (const T num) const
00088         {       return Matrix3<T>(a/num, b/num, c/num); }
00089         Matrix3<T> &operator /= (const T num)
00090         {       return *this = *this / num;     }
00091         
00092         // multiplication by a vector
00093         Vector3<T> operator *(const Vector3<T> &v) const
00094         {
00095                 return Vector3<T>(a.x * v.x + a.y * v.y + a.z * v.z,
00096                                                   b.x * v.x + b.y * v.y + b.z * v.z,
00097                                                   c.x * v.x + c.y * v.y + c.z * v.z);                                             
00098         }
00099 
00100         // multiplication by another Matrix3<T>
00101         Matrix3<T> operator *(const Matrix3<T> &m) const
00102         {
00103                 Matrix3<T> temp (Vector3<T>(a.x * m.a.x + a.y * m.b.x + a.z * m.c.x,
00104                                                                         a.x * m.a.y + a.y * m.b.y + a.z * m.c.y,
00105                                                                         a.x * m.a.z + a.y * m.b.z + a.z * m.c.z),
00106                                                  Vector3<T>(b.x * m.a.x + b.y * m.b.x + b.z * m.c.x,
00107                                                                         b.x * m.a.y + b.y * m.b.y + b.z * m.c.y,
00108                                                                         b.x * m.a.z + b.y * m.b.z + b.z * m.c.z),
00109                                                  Vector3<T>(c.x * m.a.x + c.y * m.b.x + c.z * m.c.x,
00110                                                                         c.x * m.a.y + c.y * m.b.y + c.z * m.c.y,
00111                                                                         c.x * m.a.z + c.y * m.b.z + c.z * m.c.z));
00112                 return temp;
00113         }
00114         Matrix3<T> &operator *=(const Matrix3<T> &m)
00115         {       return *this = *this * m;       }
00116 
00117         // transpose the matrix
00118         Matrix3<T> transposed(void)
00119         {
00120                 return Matrix3<T>(Vector3<T>(a.x, b.x, c.x),
00121                                                   Vector3<T>(a.y, b.y, c.y),
00122                                                   Vector3<T>(a.z, b.z, c.z));
00123         }
00124         Matrix3<T> transpose(void)
00125         {       return *this = transposed();    }
00126 
00127 };
00128 
00129 typedef Matrix3<int>                    Matrix3i;
00130 typedef Matrix3<unsigned int>   Matrix3ui;
00131 typedef Matrix3<long>                   Matrix3l;
00132 typedef Matrix3<unsigned long>  Matrix3ul;
00133 typedef Matrix3<float>                  Matrix3f;
00134 
00135 #endif // MATRIX3_H

Generated for ArduPilot Libraries by doxygen