test_common.h 5.7 KB

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