test_common.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #pragma once
  2. // These are not directly used but would otherwise be included in most files.
  3. // Leaving them included here.
  4. #include <igl/read_triangle_mesh.h>
  5. #include <igl/readDMAT.h>
  6. #include <igl/find.h>
  7. #include <Eigen/Core>
  8. #include <catch2/catch.hpp>
  9. #include <cctype>
  10. #include <string>
  11. #include <functional>
  12. #include <algorithm>
  13. #include <tuple>
  14. // Disable lengthy tests in debug mode
  15. #ifdef NDEBUG
  16. #define IGL_DEBUG_OFF ""
  17. #else
  18. #define IGL_DEBUG_OFF "[!hide]"
  19. #endif
  20. namespace test_common
  21. {
  22. template<typename Param, typename Fun>
  23. void run_test_cases(const std::vector<Param> &params, Fun test_case)
  24. {
  25. for(const auto &p : params)
  26. {
  27. // Can't use INFO( p ) because we're not sure how to print p
  28. test_case(p);
  29. }
  30. }
  31. template<typename Fun>
  32. void run_test_cases(const std::vector<std::string> &params, Fun test_case)
  33. {
  34. for(const auto &p : params)
  35. {
  36. INFO( p );
  37. test_case(p);
  38. }
  39. }
  40. inline std::vector<std::string> closed_genus_0_meshes()
  41. {
  42. return
  43. {
  44. "cube.obj",
  45. "decimated-knight.obj",
  46. "boolean_minus_test_cube.obj",
  47. "boolean_minus_test_green.obj",
  48. };
  49. };
  50. inline std::vector<std::string> closed_manifold_meshes()
  51. {
  52. std::vector<std::string> meshes = closed_genus_0_meshes();
  53. meshes.insert(meshes.end(),
  54. {
  55. "TinyTorus.obj",
  56. });
  57. return meshes;
  58. };
  59. inline std::vector<std::string> manifold_meshes()
  60. {
  61. std::vector<std::string> meshes = closed_manifold_meshes();
  62. meshes.insert(meshes.end(),
  63. {
  64. "bunny.off",
  65. "elephant.off",
  66. "hemisphere.obj",
  67. });
  68. return meshes;
  69. };
  70. inline std::vector<std::string> tet_meshes()
  71. {
  72. return
  73. {
  74. "decimated-knight.mesh"
  75. };
  76. };
  77. inline std::vector<std::string> all_meshes()
  78. {
  79. std::vector<std::string> meshes = manifold_meshes();
  80. meshes.insert(meshes.end(),
  81. {
  82. "truck.obj",
  83. });
  84. return meshes;
  85. };
  86. inline std::string data_path(std::string s)
  87. {
  88. return std::string(LIBIGL_DATA_DIR) + "/" + s;
  89. };
  90. template <typename DerivedA, typename DerivedB>
  91. void assert_eq(
  92. const Eigen::MatrixBase<DerivedA> & A,
  93. const Eigen::MatrixBase<DerivedB> & B)
  94. {
  95. // Sizes should match
  96. REQUIRE(A.rows() == B.rows());
  97. REQUIRE(A.cols() == B.cols());
  98. for(int i = 0;i<A.rows();i++)
  99. {
  100. for(int j = 0;j<A.cols();j++)
  101. {
  102. // Create an ijv tuple to trick GoogleTest into printing (i,j) so we
  103. // know where the disagreement is.
  104. std::tuple<int,int,typename DerivedA::Scalar> Aijv {i,j,A(i,j)};
  105. std::tuple<int,int,typename DerivedB::Scalar> Bijv {i,j,B(i,j)};
  106. REQUIRE(Aijv == Bijv);
  107. }
  108. }
  109. }
  110. template <typename DerivedA, typename DerivedB>
  111. void assert_neq(
  112. const Eigen::MatrixBase<DerivedA> & A,
  113. const Eigen::MatrixBase<DerivedB> & B)
  114. {
  115. // Sizes should match
  116. REQUIRE(A.rows() == B.rows());
  117. REQUIRE(A.cols() == B.cols());
  118. bool all_equals = true;
  119. for(int i = 0;i<A.rows();i++)
  120. {
  121. for(int j = 0;j<A.cols();j++)
  122. {
  123. if (A(i,j) != B(i,j))
  124. {
  125. all_equals = false;
  126. }
  127. }
  128. }
  129. REQUIRE_FALSE(all_equals);
  130. }
  131. template <typename DerivedA, typename DerivedB>
  132. void assert_eq(
  133. const Eigen::SparseMatrix<DerivedA> & A,
  134. const Eigen::SparseMatrix<DerivedB> & B)
  135. {
  136. // Sizes should match
  137. REQUIRE(A.rows() == B.rows());
  138. REQUIRE(A.cols() == B.cols());
  139. Eigen::Matrix<long int,Eigen::Dynamic, 1> AI,AJ;
  140. Eigen::Matrix<long int,Eigen::Dynamic, 1> BI,BJ;
  141. Eigen::Matrix<DerivedA,Eigen::Dynamic, 1> AV;
  142. Eigen::Matrix<DerivedB,Eigen::Dynamic, 1> BV;
  143. // Assumes A and B are in same Major Ordering
  144. igl::find(A,AI,AJ,AV);
  145. igl::find(B,BI,BJ,BV);
  146. // This doesn't generalized to assert_near nicely, and it makes it hard to
  147. // tell which entries are different:
  148. assert_eq(AI,BI);
  149. assert_eq(AJ,BJ);
  150. assert_eq(AV,BV);
  151. }
  152. template <typename DerivedA, typename DerivedB, typename EpsType>
  153. void assert_near(
  154. const Eigen::MatrixBase<DerivedA> & A,
  155. const Eigen::MatrixBase<DerivedB> & B,
  156. const EpsType & eps)
  157. {
  158. // Sizes should match
  159. REQUIRE(A.rows() == B.rows());
  160. REQUIRE(A.cols() == B.cols());
  161. for(int i = 0;i<A.rows();i++)
  162. {
  163. for(int j = 0;j<A.cols();j++)
  164. {
  165. // Create an ijv tuple to trick GoogleTest into printing (i,j) so we
  166. // know where the disagreement is.
  167. //
  168. // Equivalent to ASSERT_NEAR(Aijv,Bijv)
  169. CAPTURE( i );
  170. CAPTURE( j );
  171. {
  172. // std::tuple<int,int,typename DerivedA::Scalar> Aijv {i,j,A(i,j)};
  173. // std::tuple<int,int,typename DerivedB::Scalar> Bijv {i,j,B(i,j)+eps};
  174. REQUIRE(A(i,j) < B(i,j)+eps);
  175. }
  176. {
  177. // std::tuple<int,int,typename DerivedA::Scalar> Aijv {i,j,A(i,j)+eps};
  178. // std::tuple<int,int,typename DerivedB::Scalar> Bijv {i,j,B(i,j)};
  179. REQUIRE(A(i,j)+eps > B(i,j));
  180. }
  181. }
  182. }
  183. }
  184. }