Commits

Jeff Hardy  committed d5ec191

Merge MatrixOps.h with Matrix.h.

  • Participants
  • Parent commits a314a0b

Comments (0)

Files changed (5)

File Matrix/Matrix/Matrix.h

 #pragma once
 
+#include <cmath>
+
 namespace Math
 {
     template<class T, int Rows, int Cols>
         Matrix<T, Rows, Cols> result = {};
         return result;
     }
-}
-    #include "MatrixOps.h"
 
-namespace Math
-{
+    namespace Matrix_detail
+    {
+        template<class T, int Rows, int Cols>
+        Matrix<T, Rows, Cols> add(const Matrix<T, Rows, Cols> &l, const Matrix<T, Rows, Cols> &r)
+        {
+            Matrix<T, Rows, Cols> result;
+
+            for(int i = 0; i < Rows; ++i)
+                for(int j = 0; j < Cols; ++j)
+                    result(i, j) = l(i, j) + r(i, j);
+
+            return result;
+        }
+
+        template<class T, int Rows, int Cols>
+        Matrix<T, Rows, Cols> scalar_multiply(const Matrix<T, Rows, Cols> &m, T s)
+        {
+            Matrix<T, Rows, Cols> result;
+            
+            for(int i = 0; i < Rows; ++i)
+                for(int j = 0; j < Cols; ++j)
+                    result(i, j) = m(i, j) * s;
+
+            return result;
+        }
+
+        template<class T, int Rows, int Cols>
+        Matrix<T, Rows, Cols> scalar_divide(const Matrix<T, Rows, Cols> &m, T s)
+        {
+            Matrix<T, Rows, Cols> result;
+            
+            for(int i = 0; i < Rows; ++i)
+                for(int j = 0; j < Cols; ++j)
+                    result(i, j) = m(i, j) / s;
+
+            return result;
+        }
+
+        template<class T, int Rows, int Cols>
+        Matrix<T, Rows, Cols> negate(const Matrix<T, Rows, Cols> &m)
+        {
+            return m * -1;
+        }
+
+        template<class T, int Rows, int Cols>
+        Matrix<T, Rows, Cols> subtract(const Matrix<T, Rows, Cols> &l, const Matrix<T, Rows, Cols> &r)
+        {
+            return l + negate(r);
+        }
+
+        template<class T, int RowsL, int ColsL, int ColsR>
+        Matrix<T, RowsL, ColsR> multiply(const Matrix<T, RowsL, ColsL> &l, const Matrix<T, ColsL, ColsR> &r)
+        {
+            Matrix<T, RowsL, ColsR> result = {};
+
+            for(int i = 0; i < RowsL; i++) 
+               for(int j = 0; j < ColsR; j++)
+                   for(int k = 0; k < ColsL; k++) 
+                       result(i, j) +=  l(i, k) * r(k, j);
+
+            return result;
+        }
+
+        template<class T, int Rows, int Cols>
+        Matrix<T, Cols, Rows> transpose(const Matrix<T, Rows, Cols> &m)
+        {
+            Matrix<T, Cols, Rows> result;
+
+            for(int i = 0; i < Rows; ++i)
+                for(int j = 0; j < Cols; ++j)
+                    result(j, i) = m(i, j);
+
+            return result;
+        }
+
+        template<class T, int Rows, int Cols>
+        bool equals(const Matrix<T, Rows, Cols> &l, const Matrix<T, Rows, Cols> &r)
+        {
+            for(int i = 0; i < Rows; ++i)
+                for(int j = 0; j < Cols; ++j)
+                    if(l(i, j) != r(i, j))
+                        return false;
+
+            return true;
+        }
+
+        template<class T, int Rows>
+        T inner_product(const Matrix<T, Rows, 1> &l, const Matrix<T, Rows, 1> &r)
+        {
+            T result = 0;
+
+            for(int i = 0; i < Rows; ++i)
+                result += l(i) * r(i);
+
+            return result;
+        }
+
+        template<class T, int Cols>
+        T inner_product(const Matrix<T, 1, Cols> &l, const Matrix<T, 1, Cols> &r)
+        {
+            T result = 0;
+
+            for(int j = 0; j < Cols; ++j)
+                result += l(j) * r(j);
+
+            return result;
+        }
+
+        template<class T, int Rows, int Cols>
+        T magnitude(const Matrix<T, Rows, Cols> &m)
+        {
+            return std::sqrt(inner_product(m, m));
+        }
+
+        template<int Rows, int Cols>
+        int magnitude(const Matrix<int, Rows, Cols> &m)
+        {
+            return (int)std::sqrt((double)inner_product(m, m));
+        }
+
+        template<class T, int Rows, int Cols>
+        Matrix<T, Rows, Cols> normalize(const Matrix<T, Rows, Cols> &m)
+        {
+            return m / magnitude(m);
+        }
+
+        template<class T>
+        Matrix<T, 3, 1> cross_product(const Matrix<T, 3, 1> &l, const Matrix<T, 3, 1> &r)
+        {
+            Matrix<T, 3, 1> result = {
+                l(1) * r(2) - l(2) * r(1),
+                l(2) * r(0) - l(0) * r(2),
+                l(0) * r(1) - l(1) * r(0)
+            };
+            return result;
+        }
+
+        template<class T>
+        Matrix<T, 1, 3> cross_product(const Matrix<T, 1, 3> &l, const Matrix<T, 1, 3> &r)
+        {
+            Matrix<T, 1, 3> result = {
+                l(1) * r(2) - l(2) * r(1),
+                l(2) * r(0) - l(0) * r(2),
+                l(0) * r(1) - l(1) * r(0)
+            };
+            return result;
+        }
+
+        template<class T>
+        T determinant(const Matrix<T, 2, 2> &m)
+        {
+            return m(0, 0) * m(1, 1) - m(0, 1) * m(1, 0);
+        }
+        
+        template<class T, int N>
+        Matrix<T, N - 1, N - 1> minor_matrix(const Matrix<T, N, N> &m, int i, int j)
+        {
+            Matrix<T, N - 1, N - 1> result;
+
+            for(int a = 0, b = 0; a < N; ++a)
+            {
+                if(a != i)
+                {
+                    for(int c = 0, d = 0; c < N; ++c)
+                    {
+                        if(c != j)
+                        {
+                            result(b, d) = m(a, c);
+                            ++d;
+                        }
+                    }
+                    ++b;
+                }
+            }
+
+            return result;
+        }
+
+        template<class T, int N>
+        T cofactor(const Matrix<T, N, N> &m, int row, int col)
+        {
+            return determinant(minor_matrix(m, row, col)) * (T)std::pow((T)-1, row + col);
+        }
+
+        template<int N>
+        int cofactor(const Matrix<int, N, N> &m, int row, int col)
+        {
+            return determinant(minor_matrix(m, row, col)) * (int)std::pow(-1.0, row + col);
+        }
+
+        template<class T, int N>
+        Matrix<T, N, N> cofactor_matrix(const Matrix<T, N, N> &m)
+        {
+            Matrix<T, N, N> result;
+
+            for(int i = 0; i < N; ++i)
+                for(int j = 0; j < N; ++j)
+                    result(i, j) = cofactor(m, i, j);
+
+            return result;
+        }
+
+        template<class T, int N>
+        Matrix<T, N, N> adjoint(const Matrix<T, N, N> &m)
+        {
+            return transpose(cofactor_matrix(m));
+        }
+
+        template<class T, int N>
+        T determinant(const Matrix<T, N, N> &m)
+        {
+            T result = 0;
+
+            // Laplace expansion of first row
+            for(int i = 0; i < N; ++i)
+                result += m(i, 0) * cofactor(m, i, 0);
+
+            return result;
+        }
+
+        template<class T>
+        Matrix<T, 2, 2> inverse(const Matrix<T, 2, 2> &m)
+        {
+            Matrix<T, 2, 2> result = {m(1, 1), -m(0, 1), -m(1, 0), m(0, 0)};
+            return result / determinant(m);
+        }
+
+        template<class T, int N>
+        Matrix<T, N, N> inverse(const Matrix<T, N, N> &m)
+        {
+            return adjoint(m) / determinant(m);
+        }
+    }
+
     template<class T, int Rows, int Cols>
     Matrix<T, Rows, Cols> operator+(const Matrix<T, Rows, Cols> &m)
     {
     template<class T, int Rows, int Cols>
     Matrix<T, Rows, Cols> operator-(const Matrix<T, Rows, Cols> &m)
     {
-        return MatrixOps::negate(m);
+        return Matrix_detail::negate(m);
     }
 
     template<class T, int Rows, int Cols>
     Matrix<T, Rows, Cols> operator~(const Matrix<T, Rows, Cols> &m)
     {
-        return MatrixOps::inverse(m);
+        return Matrix_detail::inverse(m);
     }
 
     template<class T, class U, int RowsL, int ColsL, int RowsR, int ColsR>
     Matrix<T, RowsL, ColsL> operator+(const Matrix<T, RowsL, ColsL> &l, const Matrix<U, RowsR, ColsR> &r)
     {
-        return MatrixOps::add(l, r);
+        return Matrix_detail::add(l, r);
     }
 
     template<class T, class U, int RowsL, int ColsL, int RowsR, int ColsR>
     Matrix<T, RowsL, ColsL> operator-(const Matrix<T, RowsL, ColsL> &l, const Matrix<U, RowsR, ColsR> &r)
     {
-        return MatrixOps::subtract(l, r);
+        return Matrix_detail::subtract(l, r);
     }
 
     template<class T, class U, int RowsL, int ColsL, int RowsR, int ColsR>
     Matrix<T, RowsL, ColsR> operator*(const Matrix<T, RowsL, ColsL> &l, const Matrix<U, RowsR, ColsR> &r)
     {
-        return MatrixOps::multiply(l, r);
+        return Matrix_detail::multiply(l, r);
     }
 
     template<class T, int Rows, int Cols>
     Matrix<T, Rows, Cols> operator*(const Matrix<T, Rows, Cols> &m, T s)
     {
-        return MatrixOps::scalar_multiply(m, s);
+        return Matrix_detail::scalar_multiply(m, s);
     }
 
     template<class T, int Rows, int Cols>
     Matrix<T, Rows, Cols> operator/(const Matrix<T, Rows, Cols> &m, T s)
     {
-        return MatrixOps::scalar_divide(m, s);
+        return Matrix_detail::scalar_divide(m, s);
     }
 
     template<class T, class U, int RowsL, int RowsR>
     T operator*(const Matrix<T, RowsL, 1> &l, const Matrix<U, RowsR, 1> &r)
     {
-        return MatrixOps::inner_product(l, r);
+        return Matrix_detail::inner_product(l, r);
     }
 
     template<class T, class U, int ColsL, int ColsR>
     T operator*(const Matrix<T, 1, ColsL> &l, const Matrix<U, 1, ColsR> &r)
     {
-        return MatrixOps::inner_product(l, r);
+        return Matrix_detail::inner_product(l, r);
     }
 
     template<class T, class U, int RowsL, int ColsL, int RowsR, int ColsR>
     Matrix<T, RowsL, ColsL> operator^(const Matrix<T, RowsL, ColsL> &l, const Matrix<U, RowsR, ColsR> &r)
     {
-        return MatrixOps::cross_product(l, r);
+        return Matrix_detail::cross_product(l, r);
     }
 
     template<class T, class U, int RowsL, int ColsL, int RowsR, int ColsR>
     bool operator==(const Matrix<T, RowsL, ColsL> &l, const Matrix<U, RowsR, ColsR> &r)
     {
-        return MatrixOps::equals(l, r);
+        return Matrix_detail::equals(l, r);
     }
 
-    using MatrixOps::magnitude;
-    using MatrixOps::determinant;
-    using MatrixOps::transpose;
-    using MatrixOps::normalize;
+    using Matrix_detail::magnitude;
+    using Matrix_detail::determinant;
+    using Matrix_detail::transpose;
+    using Matrix_detail::normalize;
 }

File Matrix/Matrix/Matrix.vcproj

 			<Tool
 				Name="VCCLCompilerTool"
 				Optimization="0"
+				AdditionalIncludeDirectories="&quot;C:\Users\Jeff\Libs\Boost\include\boost-1_38&quot;"
 				PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
 				MinimalRebuild="true"
 				BasicRuntimeChecks="3"
 				Name="VCCLCompilerTool"
 				Optimization="2"
 				EnableIntrinsicFunctions="true"
+				AdditionalIncludeDirectories="&quot;C:\Users\Jeff\Libs\Boost\include\boost-1_38&quot;"
 				PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;ALTERNATE_STORAGE"
 				RuntimeLibrary="2"
 				EnableFunctionLevelLinking="true"
 				>
 			</File>
 			<File
-				RelativePath=".\MatrixOps.h"
-				>
-			</File>
-			<File
 				RelativePath=".\Quaternion.h"
 				>
 			</File>

File Matrix/Matrix/MatrixOps.h

-#pragma once
-
-#include <cmath>
-
-namespace Math { namespace MatrixOps
-{
-    template<class T, int Rows, int Cols>
-    Matrix<T, Rows, Cols> add(const Matrix<T, Rows, Cols> &l, const Matrix<T, Rows, Cols> &r)
-    {
-        Matrix<T, Rows, Cols> result;
-
-        for(int i = 0; i < Rows; ++i)
-            for(int j = 0; j < Cols; ++j)
-                result(i, j) = l(i, j) + r(i, j);
-
-        return result;
-    }
-
-    template<class T, int Rows, int Cols>
-    Matrix<T, Rows, Cols> scalar_multiply(const Matrix<T, Rows, Cols> &m, T s)
-    {
-        Matrix<T, Rows, Cols> result;
-        
-        for(int i = 0; i < Rows; ++i)
-            for(int j = 0; j < Cols; ++j)
-                result(i, j) = m(i, j) * s;
-
-        return result;
-    }
-
-    template<class T, int Rows, int Cols>
-    Matrix<T, Rows, Cols> scalar_divide(const Matrix<T, Rows, Cols> &m, T s)
-    {
-        Matrix<T, Rows, Cols> result;
-        
-        for(int i = 0; i < Rows; ++i)
-            for(int j = 0; j < Cols; ++j)
-                result(i, j) = m(i, j) / s;
-
-        return result;
-    }
-
-    template<class T, int Rows, int Cols>
-    Matrix<T, Rows, Cols> negate(const Matrix<T, Rows, Cols> &m)
-    {
-        return m * -1;
-    }
-
-    template<class T, int Rows, int Cols>
-    Matrix<T, Rows, Cols> subtract(const Matrix<T, Rows, Cols> &l, const Matrix<T, Rows, Cols> &r)
-    {
-        return l + negate(r);
-    }
-
-    template<class T, int RowsL, int ColsL, int ColsR>
-    Matrix<T, RowsL, ColsR> multiply(const Matrix<T, RowsL, ColsL> &l, const Matrix<T, ColsL, ColsR> &r)
-    {
-        Matrix<T, RowsL, ColsR> result = {};
-
-        for(int i = 0; i < RowsL; i++) 
-           for(int j = 0; j < ColsR; j++)
-               for(int k = 0; k < ColsL; k++) 
-                   result(i, j) +=  l(i, k) * r(k, j);
-
-        return result;
-    }
-
-    template<class T, int Rows, int Cols>
-    Matrix<T, Cols, Rows> transpose(const Matrix<T, Rows, Cols> &m)
-    {
-        Matrix<T, Cols, Rows> result;
-
-        for(int i = 0; i < Rows; ++i)
-            for(int j = 0; j < Cols; ++j)
-                result(j, i) = m(i, j);
-
-        return result;
-    }
-
-    template<class T, int Rows, int Cols>
-    bool equals(const Matrix<T, Rows, Cols> &l, const Matrix<T, Rows, Cols> &r)
-    {
-        for(int i = 0; i < Rows; ++i)
-            for(int j = 0; j < Cols; ++j)
-                if(l(i, j) != r(i, j))
-                    return false;
-
-        return true;
-    }
-
-    template<class T, int Rows>
-    T inner_product(const Matrix<T, Rows, 1> &l, const Matrix<T, Rows, 1> &r)
-    {
-        T result = 0;
-
-        for(int i = 0; i < Rows; ++i)
-            result += l(i) * r(i);
-
-        return result;
-    }
-
-    template<class T, int Cols>
-    T inner_product(const Matrix<T, 1, Cols> &l, const Matrix<T, 1, Cols> &r)
-    {
-        T result = 0;
-
-        for(int j = 0; j < Cols; ++j)
-            result += l(j) * r(j);
-
-        return result;
-    }
-
-    template<class T, int Rows, int Cols>
-    T magnitude(const Matrix<T, Rows, Cols> &m)
-    {
-        return std::sqrt(inner_product(m, m));
-    }
-
-    template<int Rows, int Cols>
-    int magnitude(const Matrix<int, Rows, Cols> &m)
-    {
-        return (int)std::sqrt((double)inner_product(m, m));
-    }
-
-    template<class T, int Rows, int Cols>
-    Matrix<T, Rows, Cols> normalize(const Matrix<T, Rows, Cols> &m)
-    {
-        return m / magnitude(m);
-    }
-
-    template<class T>
-    Matrix<T, 3, 1> cross_product(const Matrix<T, 3, 1> &l, const Matrix<T, 3, 1> &r)
-    {
-        Matrix<T, 3, 1> result = {
-            l(1) * r(2) - l(2) * r(1),
-            l(2) * r(0) - l(0) * r(2),
-            l(0) * r(1) - l(1) * r(0)
-        };
-        return result;
-    }
-
-    template<class T>
-    Matrix<T, 1, 3> cross_product(const Matrix<T, 1, 3> &l, const Matrix<T, 1, 3> &r)
-    {
-        Matrix<T, 1, 3> result = {
-            l(1) * r(2) - l(2) * r(1),
-            l(2) * r(0) - l(0) * r(2),
-            l(0) * r(1) - l(1) * r(0)
-        };
-        return result;
-    }
-
-    template<class T>
-    T determinant(const Matrix<T, 2, 2> &m)
-    {
-        return m(0, 0) * m(1, 1) - m(0, 1) * m(1, 0);
-    }
-    
-    template<class T, int N>
-    Matrix<T, N - 1, N - 1> minor_matrix(const Matrix<T, N, N> &m, int i, int j)
-    {
-        Matrix<T, N - 1, N - 1> result;
-
-        for(int a = 0, b = 0; a < N; ++a)
-        {
-            if(a != i)
-            {
-                for(int c = 0, d = 0; c < N; ++c)
-                {
-                    if(c != j)
-                    {
-                        result(b, d) = m(a, c);
-                        ++d;
-                    }
-                }
-                ++b;
-            }
-        }
-
-        return result;
-    }
-
-    template<class T, int N>
-    T cofactor(const Matrix<T, N, N> &m, int row, int col)
-    {
-        return determinant(minor_matrix(m, row, col)) * (T)std::pow((T)-1, row + col);
-    }
-
-    template<int N>
-    int cofactor(const Matrix<int, N, N> &m, int row, int col)
-    {
-        return determinant(minor_matrix(m, row, col)) * (int)std::pow(-1.0, row + col);
-    }
-
-    template<class T, int N>
-    Matrix<T, N, N> cofactor_matrix(const Matrix<T, N, N> &m)
-    {
-        Matrix<T, N, N> result;
-
-        for(int i = 0; i < N; ++i)
-            for(int j = 0; j < N; ++j)
-                result(i, j) = cofactor(m, i, j);
-
-        return result;
-    }
-
-    template<class T, int N>
-    Matrix<T, N, N> adjoint(const Matrix<T, N, N> &m)
-    {
-        return transpose(cofactor_matrix(m));
-    }
-
-    template<class T, int N>
-    T determinant(const Matrix<T, N, N> &m)
-    {
-        T result = 0;
-
-        // Laplace expansion of first row
-        for(int i = 0; i < N; ++i)
-            result += m(i, 0) * cofactor(m, i, 0);
-
-        return result;
-    }
-
-    template<class T>
-    Matrix<T, 2, 2> inverse(const Matrix<T, 2, 2> &m)
-    {
-        Matrix<T, 2, 2> result = {m(1, 1), -m(0, 1), -m(1, 0), m(0, 0)};
-        return result / determinant(m);
-    }
-
-    template<class T, int N>
-    Matrix<T, N, N> inverse(const Matrix<T, N, N> &m)
-    {
-        return adjoint(m) / determinant(m);
-    }
-}}

File Matrix/MatrixTests/MatrixTests.cpp

                                   -6,  3, -0,
                                    2, -1,  3};
 
-    BOOST_CHECK_EQUAL(MatrixOps::cofactor_matrix(m), expected);
+    BOOST_CHECK_EQUAL(Matrix_detail::cofactor_matrix(m), expected);
 }
 
 BOOST_AUTO_TEST_CASE( test_adjoint )
                                    4,  3, -1,
                                   -3, -0,  3};
 
-    BOOST_CHECK_EQUAL(MatrixOps::adjoint(m), expected);
+    BOOST_CHECK_EQUAL(Matrix_detail::adjoint(m), expected);
 }
 
 BOOST_AUTO_TEST_CASE( test_inverse )

File Matrix/MatrixTests/MatrixTests.vcproj

 				Name="VCCLCompilerTool"
 				Optimization="2"
 				EnableIntrinsicFunctions="true"
-				AdditionalIncludeDirectories="..\Matrix;C:\Lib\boost_1_38_0"
+				AdditionalIncludeDirectories="..\Matrix;&quot;C:\Users\Jeff\Libs\Boost\include\boost-1_38&quot;"
 				PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
 				RuntimeLibrary="2"
 				EnableFunctionLevelLinking="true"
 				>
 				<FileConfiguration
 					Name="Debug|Win32"
-					ExcludedFromBuild="true"
 					>
 					<Tool
 						Name="VCCLCompilerTool"
 				>
 				<FileConfiguration
 					Name="Debug|Win32"
+					ExcludedFromBuild="true"
 					>
 					<Tool
 						Name="VCCLCompilerTool"