test_common.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #pragma once
  2. #include <igl/read_triangle_mesh.h>
  3. #include <igl/find.h>
  4. #include <igl/readDMAT.h>
  5. #include <Eigen/Core>
  6. #include <catch2/catch.hpp>
  7. #include <cctype>
  8. #include <string>
  9. #include <functional>
  10. #include <algorithm>
  11. #include <tuple>
  12. namespace test_common
  13. {
  14. template<typename Param, typename Fun>
  15. void run_test_cases(const std::vector<Param> &params, Fun test_case)
  16. {
  17. for(const auto &p : params)
  18. test_case(p);
  19. }
  20. inline std::vector<std::string> closed_genus_0_meshes()
  21. {
  22. return
  23. {
  24. "cube.obj",
  25. "decimated-knight.obj",
  26. "boolean_minus_test_cube.obj",
  27. "boolean_minus_test_green.obj",
  28. };
  29. };
  30. inline std::vector<std::string> closed_manifold_meshes()
  31. {
  32. std::vector<std::string> meshes = closed_genus_0_meshes();
  33. meshes.insert(meshes.end(),
  34. {
  35. "TinyTorus.obj",
  36. });
  37. return meshes;
  38. };
  39. inline std::vector<std::string> manifold_meshes()
  40. {
  41. std::vector<std::string> meshes = closed_manifold_meshes();
  42. meshes.insert(meshes.end(),
  43. {
  44. "bunny.off",
  45. "elephant.off",
  46. "hemisphere.obj",
  47. });
  48. return meshes;
  49. };
  50. inline std::vector<std::string> tet_meshes()
  51. {
  52. return
  53. {
  54. "decimated-knight.mesh"
  55. };
  56. };
  57. inline std::vector<std::string> all_meshes()
  58. {
  59. std::vector<std::string> meshes = manifold_meshes();
  60. meshes.insert(meshes.end(),
  61. {
  62. "truck.obj",
  63. });
  64. return meshes;
  65. };
  66. inline std::string data_path(std::string s)
  67. {
  68. return std::string(LIBIGL_DATA_DIR) + "/" + s;
  69. };
  70. // TODO: this seems like a pointless indirection. Should just find and
  71. // replace test_common::load_mesh(X,...) with
  72. // igl::read_triangle_mesh(test_common::data_path(X),...)
  73. template<typename DerivedV, typename DerivedF>
  74. void load_mesh(
  75. const std::string& filename,
  76. Eigen::PlainObjectBase<DerivedV>& V,
  77. Eigen::PlainObjectBase<DerivedF>& F)
  78. {
  79. igl::read_triangle_mesh(data_path(filename), V, F);
  80. }
  81. // TODO: this seems like a pointless indirection. Should just find and
  82. // replace test_common::load_matrix(X,...) with
  83. // igl::readDMAT(test_common::data_path(X),...)
  84. template<typename Derived>
  85. void load_matrix(
  86. const std::string& filename,
  87. Eigen::PlainObjectBase<Derived>& M)
  88. {
  89. igl::readDMAT(data_path(filename), M);
  90. }
  91. template <typename DerivedA, typename DerivedB>
  92. void assert_eq(
  93. const Eigen::MatrixBase<DerivedA> & A,
  94. const Eigen::MatrixBase<DerivedB> & B)
  95. {
  96. // Sizes should match
  97. REQUIRE(A.rows() == B.rows());
  98. REQUIRE(A.cols() == B.cols());
  99. for(int i = 0;i<A.rows();i++)
  100. {
  101. for(int j = 0;j<A.cols();j++)
  102. {
  103. // Create an ijv tuple to trick GoogleTest into printing (i,j) so we
  104. // know where the disagreement is.
  105. std::tuple<int,int,typename DerivedA::Scalar> Aijv {i,j,A(i,j)};
  106. std::tuple<int,int,typename DerivedB::Scalar> Bijv {i,j,B(i,j)};
  107. REQUIRE(Aijv == Bijv);
  108. }
  109. }
  110. }
  111. template <typename DerivedA, typename DerivedB>
  112. void assert_eq(
  113. const Eigen::SparseMatrix<DerivedA> & A,
  114. const Eigen::SparseMatrix<DerivedB> & B)
  115. {
  116. // Sizes should match
  117. REQUIRE(A.rows() == B.rows());
  118. REQUIRE(A.cols() == B.cols());
  119. Eigen::Matrix<long int,Eigen::Dynamic, 1> AI,AJ;
  120. Eigen::Matrix<long int,Eigen::Dynamic, 1> BI,BJ;
  121. Eigen::Matrix<DerivedA,Eigen::Dynamic, 1> AV;
  122. Eigen::Matrix<DerivedB,Eigen::Dynamic, 1> BV;
  123. // Assumes A and B are in same Major Ordering
  124. igl::find(A,AI,AJ,AV);
  125. igl::find(B,BI,BJ,BV);
  126. // This doesn't generalized to assert_near nicely, and it makes it hard to
  127. // tell which entries are different:
  128. assert_eq(AI,BI);
  129. assert_eq(AJ,BJ);
  130. assert_eq(AV,BV);
  131. }
  132. template <typename DerivedA, typename DerivedB, typename EpsType>
  133. void assert_near(
  134. const Eigen::MatrixBase<DerivedA> & A,
  135. const Eigen::MatrixBase<DerivedB> & B,
  136. const EpsType & eps)
  137. {
  138. // Sizes should match
  139. REQUIRE(A.rows() == B.rows());
  140. REQUIRE(A.cols() == B.cols());
  141. for(int i = 0;i<A.rows();i++)
  142. {
  143. for(int j = 0;j<A.cols();j++)
  144. {
  145. // Create an ijv tuple to trick GoogleTest into printing (i,j) so we
  146. // know where the disagreement is.
  147. //
  148. // Equivalent to ASSERT_NEAR(Aijv,Bijv)
  149. CAPTURE( i );
  150. CAPTURE( j );
  151. {
  152. // std::tuple<int,int,typename DerivedA::Scalar> Aijv {i,j,A(i,j)};
  153. // std::tuple<int,int,typename DerivedB::Scalar> Bijv {i,j,B(i,j)+eps};
  154. REQUIRE(A(i,j) < B(i,j)+eps);
  155. }
  156. {
  157. // std::tuple<int,int,typename DerivedA::Scalar> Aijv {i,j,A(i,j)+eps};
  158. // std::tuple<int,int,typename DerivedB::Scalar> Bijv {i,j,B(i,j)};
  159. REQUIRE(A(i,j)+eps > B(i,j));
  160. }
  161. }
  162. }
  163. }
  164. }