浏览代码

Use submodule, move code into proper directories.

Sam Jaffe 5 年之前
父节点
当前提交
84fdf44023
共有 8 个文件被更改,包括 89 次插入91 次删除
  1. 6 0
      .gitmodules
  2. 1 0
      extern/expect
  3. 1 0
      extern/vector
  4. 2 2
      matrix.hpp
  5. 0 0
      include/math/matrix/matrix_helpers.hpp
  6. 20 20
      matrix.xcodeproj/project.pbxproj
  7. 28 34
      matrix_helper_test.cpp
  8. 31 35
      matrix_test.cpp

+ 6 - 0
.gitmodules

@@ -0,0 +1,6 @@
+[submodule "extern/vector"]
+	path = extern/vector
+	url = https://gogs.sjaffe.name/sjjaffe/cpp-mathmatic-vector
+[submodule "extern/expect"]
+	path = extern/expect
+	url = https://gogs.sjaffe.name/sjjaffe/cpp-expect

+ 1 - 0
extern/expect

@@ -0,0 +1 @@
+Subproject commit f096edddd9d8eb2ba07c2dd7ec385500c0576214

+ 1 - 0
extern/vector

@@ -0,0 +1 @@
+Subproject commit 8c9554860423c5cb000bdc233bd5eab85acfe4a5

+ 2 - 2
matrix.hpp

@@ -7,7 +7,7 @@
 
 #pragma once
 
-#include "vector/vector.hpp"
+#include "math/vector/vector.hpp"
 #include "expect/expect.hpp"
 
 namespace math { namespace matrix {
@@ -262,4 +262,4 @@ typename std::enable_if<!is_matrix<_type>::value, matrix<t, r, c> >::type
     value_type _data[R][C] = {value_type()};
   };
 
-} }
+} }

matrix_helpers.hpp → include/math/matrix/matrix_helpers.hpp


+ 20 - 20
matrix.xcodeproj/project.pbxproj

@@ -8,8 +8,9 @@
 
 /* Begin PBXBuildFile section */
 		CD0C59DE20C412C400454F82 /* GoogleMock.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD0C59C520C4127400454F82 /* GoogleMock.framework */; };
-		CD0C59DF20C412C800454F82 /* matrix_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD0C59CC20C4128F00454F82 /* matrix_test.cpp */; };
-		CD0C59E120C4314B00454F82 /* matrix_helper_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD0C59E020C4314B00454F82 /* matrix_helper_test.cpp */; };
+		CD0C59DF20C412C800454F82 /* matrix_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD0C59CC20C4128F00454F82 /* matrix_test.cxx */; };
+		CD0C59E120C4314B00454F82 /* matrix_helper_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD0C59E020C4314B00454F82 /* matrix_helper_test.cxx */; };
+		CD10427024E8757B00C0DF2A /* math in Resources */ = {isa = PBXBuildFile; fileRef = CD10426F24E8757B00C0DF2A /* math */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -52,12 +53,11 @@
 
 /* Begin PBXFileReference section */
 		CD0C59BD20C4127400454F82 /* GoogleMock.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = GoogleMock.xcodeproj; path = "../../../gmock-xcode-master/GoogleMock.xcodeproj"; sourceTree = "<group>"; };
-		CD0C59CC20C4128F00454F82 /* matrix_test.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = matrix_test.cpp; sourceTree = "<group>"; };
+		CD0C59CC20C4128F00454F82 /* matrix_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = matrix_test.cxx; sourceTree = "<group>"; };
 		CD0C59D120C412AD00454F82 /* matrix_test.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = matrix_test.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
-		CD0C59D520C412AD00454F82 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = matrix_test/Info.plist; sourceTree = "<group>"; };
-		CD0C59E020C4314B00454F82 /* matrix_helper_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = matrix_helper_test.cpp; sourceTree = "<group>"; };
-		CD0E428D1D9B3955002FFED1 /* matrix.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = matrix.hpp; sourceTree = "<group>"; };
-		CD0E428E1D9B3955002FFED1 /* matrix_helpers.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = matrix_helpers.hpp; sourceTree = "<group>"; };
+		CD0C59E020C4314B00454F82 /* matrix_helper_test.cxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = matrix_helper_test.cxx; sourceTree = "<group>"; };
+		CD10426E24E8756100C0DF2A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
+		CD10426F24E8757B00C0DF2A /* math */ = {isa = PBXFileReference; lastKnownFileType = folder; name = math; path = include/math; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -87,8 +87,9 @@
 			isa = PBXGroup;
 			children = (
 				CD0C59BD20C4127400454F82 /* GoogleMock.xcodeproj */,
-				CD78202F1E44F424001D22E5 /* src */,
+				CD10426F24E8757B00C0DF2A /* math */,
 				CD7820301E44F42B001D22E5 /* test */,
+				CD10426D24E8756100C0DF2A /* matrix_test */,
 				CD0E427A1D9B38A9002FFED1 /* Products */,
 			);
 			sourceTree = "<group>";
@@ -101,23 +102,21 @@
 			name = Products;
 			sourceTree = "<group>";
 		};
-		CD78202F1E44F424001D22E5 /* src */ = {
+		CD10426D24E8756100C0DF2A /* matrix_test */ = {
 			isa = PBXGroup;
 			children = (
-				CD0E428D1D9B3955002FFED1 /* matrix.hpp */,
-				CD0E428E1D9B3955002FFED1 /* matrix_helpers.hpp */,
+				CD10426E24E8756100C0DF2A /* Info.plist */,
 			);
-			name = src;
+			path = matrix_test;
 			sourceTree = "<group>";
 		};
 		CD7820301E44F42B001D22E5 /* test */ = {
 			isa = PBXGroup;
 			children = (
-				CD0C59D520C412AD00454F82 /* Info.plist */,
-				CD0C59CC20C4128F00454F82 /* matrix_test.cpp */,
-				CD0C59E020C4314B00454F82 /* matrix_helper_test.cpp */,
+				CD0C59CC20C4128F00454F82 /* matrix_test.cxx */,
+				CD0C59E020C4314B00454F82 /* matrix_helper_test.cxx */,
 			);
-			name = test;
+			path = test;
 			sourceTree = "<group>";
 		};
 /* End PBXGroup section */
@@ -215,6 +214,7 @@
 			isa = PBXResourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CD10427024E8757B00C0DF2A /* math in Resources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -225,8 +225,8 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
-				CD0C59E120C4314B00454F82 /* matrix_helper_test.cpp in Sources */,
-				CD0C59DF20C412C800454F82 /* matrix_test.cpp in Sources */,
+				CD0C59E120C4314B00454F82 /* matrix_helper_test.cxx in Sources */,
+				CD0C59DF20C412C800454F82 /* matrix_test.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -314,7 +314,7 @@
 				MTL_ENABLE_DEBUG_INFO = YES;
 				ONLY_ACTIVE_ARCH = YES;
 				SDKROOT = macosx;
-				USER_HEADER_SEARCH_PATHS = "../../paradigm/declarative/expect/include/ ../";
+				USER_HEADER_SEARCH_PATHS = "include/ extern/vector/include/ extern/expect/include/";
 				WARNING_CFLAGS = "-Wno-gnu-zero-variadic-macro-arguments";
 			};
 			name = Debug;
@@ -363,7 +363,7 @@
 				MACOSX_DEPLOYMENT_TARGET = 10.10;
 				MTL_ENABLE_DEBUG_INFO = NO;
 				SDKROOT = macosx;
-				USER_HEADER_SEARCH_PATHS = "../../paradigm/declarative/expect/include/ ../";
+				USER_HEADER_SEARCH_PATHS = "include/ extern/vector/include/ extern/expect/include/";
 				WARNING_CFLAGS = "-Wno-gnu-zero-variadic-macro-arguments";
 			};
 			name = Release;

+ 28 - 34
matrix_helper_test.cpp

@@ -7,7 +7,7 @@
 
 #include <gmock/gmock.h>
 
-#include "matrix_helpers.hpp"
+#include "math/matrix/matrix_helpers.hpp"
 
 template <std::size_t N> using vec = math::vector::vector<double, N>;
 
@@ -25,13 +25,11 @@ namespace math { namespace matrix {
     }
     (*out) << "]";
   }
-} }
+}}
 
 TEST(MatrixHelper, IdentityFunctionProducesOnes) {
   auto iden = math::matrix::identity<double, 4>();
-  VECTOR_FOR_EACH_RANGE(i, 4) {
-    EXPECT_THAT(iden.at(i, i), 1.0);
-  }
+  VECTOR_FOR_EACH_RANGE(i, 4) { EXPECT_THAT(iden.at(i, i), 1.0); }
   MATRIX_FOR_EACH_RANGE(i, 4, j, 4) {
     EXPECT_TRUE(i == j || iden.at(i, j) == 0);
   }
@@ -40,9 +38,7 @@ TEST(MatrixHelper, IdentityFunctionProducesOnes) {
 TEST(MatrixHelper, DiagonalWillFillWithAllZeros) {
   vec<4> const vector({1.5, 0.5, -1.0, 2.2});
   auto diag = math::matrix::diagonal(vector);
-  VECTOR_FOR_EACH_RANGE(i, 4) {
-    EXPECT_THAT(diag.at(i, i), vector.at(i));
-  }
+  VECTOR_FOR_EACH_RANGE(i, 4) { EXPECT_THAT(diag.at(i, i), vector.at(i)); }
   MATRIX_FOR_EACH_RANGE(i, 4, j, 4) {
     EXPECT_TRUE(i == j || diag.at(i, j) == 0);
   }
@@ -52,14 +48,14 @@ 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, vector * x);
+  EXPECT_THAT(A * x, vector * x);
 }
 
 TEST(MatrixHelper, TranslationMatrixAddsToEachElement) {
   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::translation(vector);
-  EXPECT_THAT(A*x, vector + x);
+  EXPECT_THAT(A * x, vector + x);
 }
 
 using matr3 = math::matrix::matrix<double, 3, 3>;
@@ -67,54 +63,52 @@ using math::matrix::rotation;
 using math::matrix::rotation_t;
 namespace rotate = math::matrix::rotate;
 
-class RotationTest : public ::testing::TestWithParam<std::tuple<rotation_t<3>, matr3>> {
-  
-};
+class RotationTest
+    : public ::testing::TestWithParam<std::tuple<rotation_t<3>, matr3>> {};
 
 TEST(MatrixHelper, RotateByHalfPi) {
-  auto const theta = M_PI/2;
+  auto const theta = M_PI / 2;
   auto const iden = math::matrix::identity<double, 2>();
-  auto const expected = math::matrix::matrix<double, 2, 2>({{{0, -1},{1, 0}}});
-  
+  auto const expected = math::matrix::matrix<double, 2, 2>({{{0, -1}, {1, 0}}});
+
   auto result = iden * rotation(theta, rotate::ROT_2D);
   MATRIX_FOR_EACH_RANGE(i, 2, j, 2) {
     EXPECT_THAT(result(i, j),
-                ::testing::DoubleNear(expected(i, j), std::numeric_limits<double>::epsilon()));
+                ::testing::DoubleNear(expected(i, j),
+                                      std::numeric_limits<double>::epsilon()));
   }
 }
 
 TEST_P(RotationTest, RotateByHalfPi) {
-  auto const theta = M_PI/2;
+  auto const theta = M_PI / 2;
   auto const iden = math::matrix::identity<double, 3>();
   auto const expected = std::get<1>(GetParam());
-  
+
   auto result = iden * rotation(theta, std::get<0>(GetParam()));
   MATRIX_FOR_EACH_RANGE(i, 3, j, 3) {
     EXPECT_THAT(result(i, j),
-                ::testing::DoubleNear(expected(i, j), std::numeric_limits<double>::epsilon()));
+                ::testing::DoubleNear(expected(i, j),
+                                      std::numeric_limits<double>::epsilon()));
   }
 }
 
 TEST(MatrixHelper, RotateIntoLargerMatrixArea) {
-  auto const theta = M_PI/2;
+  auto const theta = M_PI / 2;
   auto const iden = math::matrix::identity<double, 4>();
-  auto const expected = math::matrix::matrix<double, 4, 4>({{
-    {0,-1,0,0},
-    {1, 0,0,0},
-    {0, 0,1,0},
-    {0, 0,0,1}
-  }});
-  
+  auto const expected = math::matrix::matrix<double, 4, 4>(
+      {{{0, -1, 0, 0}, {1, 0, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}});
+
   auto result = iden * rotation<4>(theta, rotate::Z_AXIS);
   MATRIX_FOR_EACH_RANGE(i, 4, j, 4) {
     EXPECT_THAT(result(i, j),
-                ::testing::DoubleNear(expected(i, j), std::numeric_limits<double>::epsilon()));
+                ::testing::DoubleNear(expected(i, j),
+                                      std::numeric_limits<double>::epsilon()));
   }
 }
 
 std::vector<std::tuple<rotation_t<3>, matr3>> values{
-  {rotate::X_AXIS, matr3({{{1, 0,0},{0,0,-1},{ 0,1,0}}})},
-  {rotate::Y_AXIS, matr3({{{0, 0,1},{0,1, 0},{-1,0,0}}})},
-  {rotate::Z_AXIS, matr3({{{0,-1,0},{1,0, 0},{ 0,0,1}}})}
-};
-INSTANTIATE_TEST_CASE_P(MatrixHelper, RotationTest, ::testing::ValuesIn(values));
+    {rotate::X_AXIS, matr3({{{1, 0, 0}, {0, 0, -1}, {0, 1, 0}}})},
+    {rotate::Y_AXIS, matr3({{{0, 0, 1}, {0, 1, 0}, {-1, 0, 0}}})},
+    {rotate::Z_AXIS, matr3({{{0, -1, 0}, {1, 0, 0}, {0, 0, 1}}})}};
+INSTANTIATE_TEST_CASE_P(MatrixHelper, RotationTest,
+                        ::testing::ValuesIn(values));

+ 31 - 35
matrix_test.cpp

@@ -7,19 +7,17 @@
 
 #include <gmock/gmock.h>
 
-#include "matrix.hpp"
+#include "math/matrix/matrix.hpp"
 
 using matr2i = math::matrix::matrix<int, 2, 2>;
 using matr2 = math::matrix::matrix<double, 2, 2>;
 
-matr2i identity2i() { return matr2i({{{1,0},{0,1}}}); }
-matr2 identity2() { return matr2({{{1,0},{0,1}}}); }
+matr2i identity2i() { return matr2i({{{1, 0}, {0, 1}}}); }
+matr2 identity2() { return matr2({{{1, 0}, {0, 1}}}); }
 
 TEST(Matrix, Equality) {
-  EXPECT_THAT(identity2i(),
-              matr2i({{{1,0},{0,1}}}));
-  EXPECT_THAT(identity2i(),
-              ::testing::Not(matr2i({{{0,1},{0,1}}})));
+  EXPECT_THAT(identity2i(), matr2i({{{1, 0}, {0, 1}}}));
+  EXPECT_THAT(identity2i(), ::testing::Not(matr2i({{{0, 1}, {0, 1}}})));
 }
 
 TEST(Matrix, AtRowBeyondBoundaryThrowsException) {
@@ -56,52 +54,50 @@ TEST(Matrix, ConstAtColumnBeyondBoundaryThrowsException) {
 
 TEST(Matrix, TransposeOfSquareMatrixRotatesAroundDiagonal) {
   matr2i mat({{{1, 1}, {2, 2}}});
-  EXPECT_THAT(mat.transpose(),
-              matr2i({{{1, 2}, {1, 2}}}));
+  EXPECT_THAT(mat.transpose(), matr2i({{{1, 2}, {1, 2}}}));
 }
 
 TEST(Matrix, TransposeOfMatrixFlipsDimension) {
   using matr3x2i = math::matrix::matrix<int, 3, 2>;
   using matr2x3i = math::matrix::matrix<int, 2, 3>;
   matr3x2i mat({{{1, 1}, {2, 2}, {3, 3}}});
-  EXPECT_THAT(mat.transpose(),
-              matr2x3i({{{1, 2, 3}, {1, 2, 3}}}));
+  EXPECT_THAT(mat.transpose(), matr2x3i({{{1, 2, 3}, {1, 2, 3}}}));
 }
 
 TEST(Matrix, AdditionSumsAllElements) {
   auto iden = identity2i();
-  auto result = matr2i({{{2,0},{0,2}}});
+  auto result = matr2i({{{2, 0}, {0, 2}}});
   EXPECT_THAT(iden + iden, result);
 }
 
 TEST(Matrix, DefaultConstructorIsZeros) {
-  auto zero = matr2i({{{0,0},{0,0}}});
+  auto zero = matr2i({{{0, 0}, {0, 0}}});
   EXPECT_THAT(matr2i{}, zero);
 }
 
 TEST(Matrix, SubtractionDiffsAllElements) {
   auto iden = identity2i();
-  EXPECT_THAT(iden-iden, matr2i());
+  EXPECT_THAT(iden - iden, matr2i());
 }
 
 TEST(Matrix, NegationAltersAllElements) {
-  EXPECT_THAT(-identity2i(), matr2i({{{-1,0},{0,-1}}}));
+  EXPECT_THAT(-identity2i(), matr2i({{{-1, 0}, {0, -1}}}));
 }
 
 TEST(Matrix, MultiplicationAltersAllElements) {
-  EXPECT_THAT(2*identity2(), matr2({{{2.0,0.0},{0.0,2.0}}}));
-  EXPECT_THAT(identity2()*2, matr2({{{2.0,0.0},{0.0,2.0}}}));
+  EXPECT_THAT(2 * identity2(), matr2({{{2.0, 0.0}, {0.0, 2.0}}}));
+  EXPECT_THAT(identity2() * 2, matr2({{{2.0, 0.0}, {0.0, 2.0}}}));
 }
 
 TEST(Matrix, DivisionAltersAllElements) {
-  EXPECT_THAT(identity2()/2.0, matr2({{{0.5,0.0},{0.0,0.5}}}));
+  EXPECT_THAT(identity2() / 2.0, matr2({{{0.5, 0.0}, {0.0, 0.5}}}));
 }
 
 TEST(Matrix, MultiplyingSameSizeMatricesProducesSameSize) {
-  auto A = matr2i({{{1,2},{2,3}}});
-  auto B = matr2i({{{1,0},{1,1}}});
-  EXPECT_THAT(A*B, matr2i({{{3,2},{5,3}}}));
-  EXPECT_THAT(B*A, matr2i({{{1,2},{3,5}}}));
+  auto A = matr2i({{{1, 2}, {2, 3}}});
+  auto B = matr2i({{{1, 0}, {1, 1}}});
+  EXPECT_THAT(A * B, matr2i({{{3, 2}, {5, 3}}}));
+  EXPECT_THAT(B * A, matr2i({{{1, 2}, {3, 5}}}));
 }
 
 TEST(Matrix, MultiplyingDifferentSizeMatricesChangesSize) {
@@ -109,20 +105,20 @@ TEST(Matrix, MultiplyingDifferentSizeMatricesChangesSize) {
   using matr2x3i = math::matrix::matrix<int, 2, 3>;
   using matr3x2i = math::matrix::matrix<int, 3, 2>;
   using matr3x3i = math::matrix::matrix<int, 3, 3>;
-  auto A = matr3x2i({{{1,0},{0,1},{1,1}}});
-  auto B = matr2x3i({{{0,1,0},{1,0,1}}});
-  EXPECT_THAT(A*B, matr3x3i({{{0,1,0},{1,0,1},{1,1,1}}}));
-  EXPECT_THAT(B*A, matr2x2i({{{0,1},{2,1}}}));
+  auto A = matr3x2i({{{1, 0}, {0, 1}, {1, 1}}});
+  auto B = matr2x3i({{{0, 1, 0}, {1, 0, 1}}});
+  EXPECT_THAT(A * B, matr3x3i({{{0, 1, 0}, {1, 0, 1}, {1, 1, 1}}}));
+  EXPECT_THAT(B * A, matr2x2i({{{0, 1}, {2, 1}}}));
 }
 
 TEST(Matrix, VectorMultiplicationProducesVector) {
   using vec2i = math::vector::vector<int, 2>;
-  auto A = matr2i({{{1,0},{1,2}}});
-  auto x = vec2i({1,2});
-  EXPECT_THAT(A*x, vec2i({1,5}));
+  auto A = matr2i({{{1, 0}, {1, 2}}});
+  auto x = vec2i({1, 2});
+  EXPECT_THAT(A * x, vec2i({1, 5}));
 }
 
-//TEST(Matrix, Composition) {
+// TEST(Matrix, Composition) {
 //  using namespace math::matrix;
 //  using vec4 = math::vector::vector<double, 4>;
 //  using vec3 = math::vector::vector<double, 3>;
@@ -137,16 +133,16 @@ TEST(Matrix, VectorMultiplicationProducesVector) {
 
 TEST(Matrix, MatrixConstructableFromVector) {
   using vec3 = math::vector::vector<double, 3>;
-  vec3 v = vec3({1,2,3});
+  vec3 v = vec3({1, 2, 3});
   math::matrix::matrix<double, 3, 1> m{v};
-  EXPECT_THAT(m(0,0), v[0]);
-  EXPECT_THAT(m(1,0), v[1]);
-  EXPECT_THAT(m(2,0), v[2]);
+  EXPECT_THAT(m(0, 0), v[0]);
+  EXPECT_THAT(m(1, 0), v[1]);
+  EXPECT_THAT(m(2, 0), v[2]);
 }
 
 TEST(Matrix, CanAlterEntireRowInOneExpression) {
   matr2i A = identity2i();
   matr2i const B = 2 * A;
   A[0] = B[0];
-  EXPECT_THAT(A, matr2i({{{2,0},{0,1}}}));
+  EXPECT_THAT(A, matr2i({{{2, 0}, {0, 1}}}));
 }