test_common.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. Eigen::PlainObjectBase<DerivedV>& TC)
  79. {
  80. igl::read_triangle_mesh(data_path(filename), V, F, TC);
  81. }
  82. // TODO: this seems like a pointless indirection. Should just find and
  83. // replace test_common::load_matrix(X,...) with
  84. // igl::readDMAT(test_common::data_path(X),...)
  85. template<typename Derived>
  86. void load_matrix(
  87. const std::string& filename,
  88. Eigen::PlainObjectBase<Derived>& M)
  89. {
  90. igl::readDMAT(data_path(filename), M);
  91. }
  92. template <typename DerivedA, typename DerivedB>
  93. void assert_eq(
  94. const Eigen::MatrixBase<DerivedA> & A,
  95. const Eigen::MatrixBase<DerivedB> & B)
  96. {
  97. // Sizes should match
  98. REQUIRE(A.rows() == B.rows());
  99. REQUIRE(A.cols() == B.cols());
  100. for(int i = 0;i<A.rows();i++)
  101. {
  102. for(int j = 0;j<A.cols();j++)
  103. {
  104. // Create an ijv tuple to trick GoogleTest into printing (i,j) so we
  105. // know where the disagreement is.
  106. std::tuple<int,int,typename DerivedA::Scalar> Aijv {i,j,A(i,j)};
  107. std::tuple<int,int,typename DerivedB::Scalar> Bijv {i,j,B(i,j)};
  108. REQUIRE(Aijv == Bijv);
  109. }
  110. }
  111. }
  112. template <typename DerivedA, typename DerivedB>
  113. void assert_neq(
  114. const Eigen::MatrixBase<DerivedA> & A,
  115. const Eigen::MatrixBase<DerivedB> & B)
  116. {
  117. // Sizes should match
  118. REQUIRE(A.rows() == B.rows());
  119. REQUIRE(A.cols() == B.cols());
  120. bool all_equals = true;
  121. for(int i = 0;i<A.rows();i++)
  122. {
  123. for(int j = 0;j<A.cols();j++)
  124. {
  125. if (A(i,j) != B(i,j))
  126. {
  127. all_equals = false;
  128. }
  129. }
  130. }
  131. REQUIRE_FALSE(all_equals);
  132. }
  133. template <typename DerivedA, typename DerivedB>
  134. void assert_eq(
  135. const Eigen::SparseMatrix<DerivedA> & A,
  136. const Eigen::SparseMatrix<DerivedB> & B)
  137. {
  138. // Sizes should match
  139. REQUIRE(A.rows() == B.rows());
  140. REQUIRE(A.cols() == B.cols());
  141. Eigen::Matrix<long int,Eigen::Dynamic, 1> AI,AJ;
  142. Eigen::Matrix<long int,Eigen::Dynamic, 1> BI,BJ;
  143. Eigen::Matrix<DerivedA,Eigen::Dynamic, 1> AV;
  144. Eigen::Matrix<DerivedB,Eigen::Dynamic, 1> BV;
  145. // Assumes A and B are in same Major Ordering
  146. igl::find(A,AI,AJ,AV);
  147. igl::find(B,BI,BJ,BV);
  148. // This doesn't generalized to assert_near nicely, and it makes it hard to
  149. // tell which entries are different:
  150. assert_eq(AI,BI);
  151. assert_eq(AJ,BJ);
  152. assert_eq(AV,BV);
  153. }
  154. template <typename DerivedA, typename DerivedB, typename EpsType>
  155. void assert_near(
  156. const Eigen::MatrixBase<DerivedA> & A,
  157. const Eigen::MatrixBase<DerivedB> & B,
  158. const EpsType & eps)
  159. {
  160. // Sizes should match
  161. REQUIRE(A.rows() == B.rows());
  162. REQUIRE(A.cols() == B.cols());
  163. for(int i = 0;i<A.rows();i++)
  164. {
  165. for(int j = 0;j<A.cols();j++)
  166. {
  167. // Create an ijv tuple to trick GoogleTest into printing (i,j) so we
  168. // know where the disagreement is.
  169. //
  170. // Equivalent to ASSERT_NEAR(Aijv,Bijv)
  171. CAPTURE( i );
  172. CAPTURE( j );
  173. {
  174. // std::tuple<int,int,typename DerivedA::Scalar> Aijv {i,j,A(i,j)};
  175. // std::tuple<int,int,typename DerivedB::Scalar> Bijv {i,j,B(i,j)+eps};
  176. REQUIRE(A(i,j) < B(i,j)+eps);
  177. }
  178. {
  179. // std::tuple<int,int,typename DerivedA::Scalar> Aijv {i,j,A(i,j)+eps};
  180. // std::tuple<int,int,typename DerivedB::Scalar> Bijv {i,j,B(i,j)};
  181. REQUIRE(A(i,j)+eps > B(i,j));
  182. }
  183. }
  184. }
  185. }
  186. }