Shadowrun: Awakened 29 September 2011 - Build 871
Matrix.hpp
Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2009 Christopher A. Taylor.  All rights reserved.
00003 
00004     Redistribution and use in source and binary forms, with or without
00005     modification, are permitted provided that the following conditions are met:
00006 
00007     * Redistributions of source code must retain the above copyright notice,
00008       this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright notice,
00010       this list of conditions and the following disclaimer in the documentation
00011       and/or other materials provided with the distribution.
00012     * Neither the name of LibCat nor the names of its contributors may be used
00013       to endorse or promote products derived from this software without
00014       specific prior written permission.
00015 
00016     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019     ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00020     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00021     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00022     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00023     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00024     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00025     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026     POSSIBILITY OF SUCH DAMAGE.
00027 */
00028 
00029 #ifndef CAT_MATRIX_HPP
00030 #define CAT_MATRIX_HPP
00031 
00032 #include <cat/gfx/Vector.hpp>
00033 
00034 namespace cat {
00035 
00036 #define FOR_EACH_ELEMENT(index) for (int index = 0; index < ELEMENTS; ++index)
00037 
00038 
00039 /*
00040     4x4 matrix elements arranged column major (row, column):
00041 
00042         m[0]  m[4]  m[8]  m[12]
00043         m[1]  m[5]  m[9]  m[13]
00044         m[2]  m[6]  m[10] m[14]
00045         m[3]  m[7]  m[11] m[15]
00046 
00047     Matrix element order corresponds to OpenGL matrix ordering s.t. in a
00048     4x4 matrix the elements define the following new coordinate system:
00049 
00050         new x-axis vector: { m[0]  m[1]  m[2]  }
00051         new y-axis vector: { m[4]  m[5]  m[6]  }
00052         new z-axis vector: { m[7]  m[9]  m[10] }
00053         new origin:        { m[12] m[13] m[14] }
00054 */
00055 
00056 template<int ROWS, int COLS, class Scalar> class Matrix
00057 {
00058 protected:
00059     static const int ELEMENTS = ROWS * COLS;
00060     Scalar _elements[ROWS * COLS];
00061 
00062 public:
00063     // Short-hand for the current matrix type
00064     typedef Matrix<ROWS, COLS, Scalar> mytype;
00065 
00066     // Default constructor does not initialize elements
00067     Matrix()
00068     {
00069     }
00070 
00071     // Copy constructor
00072     Matrix(const mytype &u)
00073     {
00074         memcpy(_elements, u._elements, sizeof(_elements));
00075     }
00076 
00077     // Assignment operator
00078     mytype &operator=(const mytype &u)
00079     {
00080         memcpy(_elements, u._elements, sizeof(_elements));
00081     }
00082 
00083     // Load zero matrix
00084     void loadZero()
00085     {
00086         OBJCLR(_elements);
00087     }
00088 
00089     // Load identity matrix
00090     void loadIdentity()
00091     {
00092         OBJCLR(_elements);
00093 
00094         // Write a 1 along the diagonal
00095         for (int ii = 0; ii < ROWS && ii < COLS; ++ii)
00096         {
00097             _elements[ii * ROWS + ii] = static_cast<Scalar>( 1 );
00098         }
00099     }
00100 
00101     // Addition in-place
00102     mytype &operator+=(const mytype &u)
00103     {
00104         FOR_EACH_ELEMENT(ii) _elements[ii] += u._elements[ii];
00105     }
00106 
00107     // Subtraction in-place
00108     mytype &operator-=(const mytype &u)
00109     {
00110         FOR_EACH_ELEMENT(ii) _elements[ii] -= u._elements[ii];
00111     }
00112 
00113     // Multiplication by scalar in-place
00114     mytype &operator*=(Scalar u)
00115     {
00116         FOR_EACH_ELEMENT(ii) _elements[ii] *= u;
00117     }
00118 
00119     // Division by scalar in-place
00120     mytype &operator/=(Scalar u)
00121     {
00122         FOR_EACH_ELEMENT(ii) _elements[ii] /= u;
00123     }
00124 
00125     // Matrix multiplication
00126     template<int OTHER_COLS>
00127     Matrix<ROWS, OTHER_COLS, Scalar> operator*(const Matrix<COLS, OTHER_COLS, Scalar> &u)
00128     {
00129         Matrix<ROWS, OTHER_COLS, Scalar> result;
00130 
00131         // For each row of the matrix product,
00132         for (int r = 0; r < ROWS; ++r)
00133         {
00134             // For each column of the matrix product,
00135             for (int c = 0; c < OTHER_COLS; ++c)
00136             {
00137                 Scalar x = static_cast<Scalar>( 0 );
00138 
00139                 // For each row of the right operand (u),
00140                 for (int ii = 0; ii < COLS; ++ii)
00141                 {
00142                     // Accumulate sum of products
00143                     x += (*this)(r, ii) * u(ii, c);
00144                 }
00145 
00146                 // Write the sum
00147                 result(r, c) = x;
00148             }
00149         }
00150 
00151         return result;
00152     }
00153 
00154     // Accessors
00155     inline Scalar &operator()(int ii) { return _elements[ii]; }
00156     inline const Scalar &operator()(int ii) const { return _elements[ii]; }
00157 
00158     inline Scalar &operator()(int row, int col) { return _elements[col * ROWS + row]; }
00159     inline const Scalar &operator()(int row, int col) const { return _elements[col * ROWS + row]; }
00160 };
00161 
00162 
00163 // Short-hand for common usages:
00164 
00165 typedef Matrix<2, 2, u32> Matrix2x2u;
00166 typedef Matrix<3, 3, u32> Matrix3x3u;
00167 typedef Matrix<4, 4, u32> Matrix4x4u;
00168 
00169 typedef Matrix<2, 2, s32> Matrix2x2i;
00170 typedef Matrix<3, 3, s32> Matrix3x3i;
00171 typedef Matrix<4, 4, s32> Matrix4x4i;
00172 
00173 typedef Matrix<2, 2, f32> Matrix2x2f;
00174 typedef Matrix<3, 3, f32> Matrix3x3f;
00175 typedef Matrix<4, 4, f32> Matrix4x4f;
00176 
00177 typedef Matrix<2, 2, f64> Matrix2x2d;
00178 typedef Matrix<3, 3, f64> Matrix3x3d;
00179 typedef Matrix<4, 4, f64> Matrix4x4d;
00180 
00181 
00182 #undef FOR_EACH_ELEMENT
00183 
00184 } // namespace cat
00185 
00186 #endif // CAT_MATRIX_HPP

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net