Browse Source

Add test for scalar.
Add multiplication function that allows a 4x4 matrix to multiply by a vector of size 3.
Also see change:2d74e5c1b41583291172c1457c02d84be5a2362e in the vector project

Sam Jaffe 7 years ago
parent
commit
f07e4112cf
4 changed files with 17 additions and 0 deletions
  1. 2 0
      matrix.hpp
  2. 1 0
      matrix_dummy.cpp
  3. 7 0
      matrix_helper_test.cpp
  4. 7 0
      matrix_helpers.hpp

+ 2 - 0
matrix.hpp

@@ -19,6 +19,8 @@ namespace math { namespace matrix {
   class matrix;
   template <typename T, size_t R, size_t C>
   struct is_matrix<matrix<T, R, C> > { static const constexpr bool value = true; };
+  template <typename T, size_t N>
+  struct is_matrix<vector::vector<T, N>> { static const constexpr bool value = true; };
   
   namespace concat_strategy {
     struct {} horizonal;

+ 1 - 0
matrix_dummy.cpp

@@ -15,4 +15,5 @@ namespace math { namespace matrix {
   template matrix<double, 4, 4> diagonal(vector::vector<double, 4> const &);
   template matrix<double, 4, 4> translation(vector::vector<double, 3> const &);
   template matrix<double, 4, 4> scalar(vector::vector<double, 3> const &);
+  template vector::vector<double, 3> operator*(matrix<double, 4, 4> const &, vector::vector<double, 3> const &);
 } }

+ 7 - 0
matrix_helper_test.cpp

@@ -31,3 +31,10 @@ TEST(MatrixHelper, DiagonalWillFillWithAllZeros) {
     EXPECT_TRUE(i == j || diag.at(i, j) == 0);
   }
 }
+
+TEST(MatrixHelper, ScalarMatrixPiecewiseStretchesVector) {
+  vec<3> const vector({1.5, 0.5, -1.0});
+  vec<3> const x({2.0, 3.8, 11.0});
+  auto const A = math::matrix::scalar(vector);
+  EXPECT_THAT(A*x, vec<3>({3.0, 1.9, -11.0}));
+}

+ 7 - 0
matrix_helpers.hpp

@@ -80,4 +80,11 @@ namespace math { namespace matrix {
     return rotation(theta, r).concat(identity<G, D - RD>(), concat_strategy::diagonal);
   }
   
+  template <typename T, size_t N>
+  vector::vector<T, N> operator*(matrix<T, N+1, N+1> const & lhs, vector::vector<T, N> const & rhs) {
+    vector::vector<T, N+1> tmp(rhs);
+    tmp[N] = 1;
+    return vector::vector<T, N>(lhs * tmp);
+  }
+  
 } }