test_common.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_neq(
  113. const Eigen::MatrixBase<DerivedA> & A,
  114. const Eigen::MatrixBase<DerivedB> & B)
  115. {
  116. // Sizes should match
  117. REQUIRE(A.rows() == B.rows());
  118. REQUIRE(A.cols() == B.cols());
  119. bool all_equals = true;
  120. for(int i = 0;i<A.rows();i++)
  121. {
  122. for(int j = 0;j<A.cols();j++)
  123. {
  124. if (A(i,j) != B(i,j))
  125. {
  126. all_equals = false;
  127. }
  128. }
  129. }
  130. REQUIRE_FALSE(all_equals);
  131. }
  132. template <typename DerivedA, typename DerivedB>
  133. void assert_eq(
  134. const Eigen::SparseMatrix<DerivedA> & A,
  135. const Eigen::SparseMatrix<DerivedB> & B)
  136. {
  137. // Sizes should match
  138. REQUIRE(A.rows() == B.rows());
  139. REQUIRE(A.cols() == B.cols());
  140. Eigen::Matrix<long int,Eigen::Dynamic, 1> AI,AJ;
  141. Eigen::Matrix<long int,Eigen::Dynamic, 1> BI,BJ;
  142. Eigen::Matrix<DerivedA,Eigen::Dynamic, 1> AV;
  143. Eigen::Matrix<DerivedB,Eigen::Dynamic, 1> BV;
  144. // Assumes A and B are in same Major Ordering
  145. igl::find(A,AI,AJ,AV);
  146. igl::find(B,BI,BJ,BV);
  147. // This doesn't generalized to assert_near nicely, and it makes it hard to
  148. // tell which entries are different:
  149. assert_eq(AI,BI);
  150. assert_eq(AJ,BJ);
  151. assert_eq(AV,BV);
  152. }
  153. template <typename DerivedA, typename DerivedB, typename EpsType>
  154. void assert_near(
  155. const Eigen::MatrixBase<DerivedA> & A,
  156. const Eigen::MatrixBase<DerivedB> & B,
  157. const EpsType & eps)
  158. {
  159. // Sizes should match
  160. REQUIRE(A.rows() == B.rows());
  161. REQUIRE(A.cols() == B.cols());
  162. for(int i = 0;i<A.rows();i++)
  163. {
  164. for(int j = 0;j<A.cols();j++)
  165. {
  166. // Create an ijv tuple to trick GoogleTest into printing (i,j) so we
  167. // know where the disagreement is.
  168. //
  169. // Equivalent to ASSERT_NEAR(Aijv,Bijv)
  170. CAPTURE( i );
  171. CAPTURE( j );
  172. {
  173. // std::tuple<int,int,typename DerivedA::Scalar> Aijv {i,j,A(i,j)};
  174. // std::tuple<int,int,typename DerivedB::Scalar> Bijv {i,j,B(i,j)+eps};
  175. REQUIRE(A(i,j) < B(i,j)+eps);
  176. }
  177. {
  178. // std::tuple<int,int,typename DerivedA::Scalar> Aijv {i,j,A(i,j)+eps};
  179. // std::tuple<int,int,typename DerivedB::Scalar> Bijv {i,j,B(i,j)};
  180. REQUIRE(A(i,j)+eps > B(i,j));
  181. }
  182. }
  183. }
  184. }
  185. }