repmat.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <test_common.h>
  2. #include <Eigen/Sparse>
  3. #include <igl/repmat.h>
  4. template <typename T, int majorType>
  5. void checkRepmat(
  6. Eigen::SparseMatrix<T, majorType> &A,
  7. Eigen::SparseMatrix<T, majorType> &B,
  8. const int rows,
  9. const int cols,
  10. const int r,
  11. const int c)
  12. {
  13. for (int i = 0; i < rows; i++)
  14. {
  15. for (int j = 0; j < cols; j++)
  16. {
  17. for (int ii = 0; ii < r; ii++)
  18. {
  19. for (int jj = 0; jj < c; jj++)
  20. {
  21. REQUIRE (A.coeff(i, j) == B.coeff(i + ii * rows, j + jj * cols));
  22. }
  23. }
  24. }
  25. }
  26. }
  27. template <typename T, int majorType>
  28. void testRepmat(
  29. const int rows,
  30. const int cols,
  31. const int r,
  32. const int c)
  33. {
  34. Eigen::SparseMatrix<T, majorType> A;
  35. A = (Eigen::Matrix<T, Eigen::Dynamic,
  36. Eigen::Dynamic>().setRandom(rows, cols).sparseView());
  37. Eigen::SparseMatrix<T, majorType> B;
  38. igl::repmat(A, r, c, B);
  39. REQUIRE (B.rows() == r * rows);
  40. REQUIRE (B.cols() == c * cols);
  41. checkRepmat<T, majorType>(A, B, rows, cols, r, c);
  42. }
  43. TEST_CASE("repmat: sparse rowMajor", "[igl]")
  44. {
  45. testRepmat<double, Eigen::RowMajor>(4, 5, 2, 4);
  46. testRepmat< int, Eigen::RowMajor>(2, 8, 3, 4);
  47. testRepmat< float, Eigen::RowMajor>(6, 10, 2, 2);
  48. }
  49. TEST_CASE("repmat: sparse colMajor", "[igl]")
  50. {
  51. testRepmat<double, Eigen::ColMajor>(4, 5, 3, 5);
  52. testRepmat< int, Eigen::ColMajor>(2, 8, 3, 5);
  53. testRepmat< float, Eigen::ColMajor>(6, 10, 3, 5);
  54. }