squared_edge_lengths.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <test_common.h>
  2. #include <igl/squared_edge_lengths.h>
  3. #include <iostream>
  4. TEST_CASE("squared_edge_lengths: cube", "[igl]")
  5. {
  6. //The allowed error for this test
  7. const double epsilon = 1e-15;
  8. Eigen::MatrixXd V;
  9. Eigen::MatrixXi F;
  10. //This is a cube of dimensions 1.0x1.0x1.0
  11. test_common::load_mesh("cube.obj", V, F);
  12. //Create scaled versions of the cube
  13. double scale = 1.0;
  14. double huge_scale = 1.0e8;
  15. double tiny_scale = 1.0e-8;
  16. Eigen::MatrixXd V_huge = V * huge_scale;
  17. Eigen::MatrixXd V_tiny = V * tiny_scale;
  18. //Prepare another mesh with triangles along side diagonals of the cube
  19. //These triangles are form a regular tetrahedron of side sqrt(2)
  20. Eigen::MatrixXi F_tet(4,3);
  21. F_tet << 4,6,1,
  22. 6,4,3,
  23. 4,1,3,
  24. 1,6,3;
  25. //1. Check edge_lengths_squared function
  26. double side_sq = 1.0; //squared lenght of a side
  27. double diag_sq = 2.0; //squared lenght of a diagonal
  28. Eigen::MatrixXd L_sq;
  29. igl::squared_edge_lengths(V,F,L_sq);
  30. REQUIRE (L_sq.rows() == F.rows());
  31. REQUIRE (L_sq.cols() == 3);
  32. //All edges in unit cube measure 1.0 or sqrt(2) in diagonals
  33. for(int f = 0;f<L_sq.rows();f++)
  34. {
  35. //All edge_lengths_squared must be exactly "side_sq" or "diag_sq"
  36. for(int e = 0;e<3;e++)
  37. if (L_sq(f,e) > 1.1)
  38. REQUIRE (L_sq(f,e) == diag_sq);
  39. else
  40. REQUIRE (L_sq(f,e) == side_sq);
  41. //All sides sum exactly side_sq + side_sq + diag_sq
  42. REQUIRE (side_sq + side_sq + diag_sq == L_sq.row(f).sum());
  43. }
  44. //Check the regular tetrahedron
  45. igl::squared_edge_lengths(V,F_tet,L_sq);
  46. REQUIRE (L_sq.rows() == F_tet.rows());
  47. REQUIRE (L_sq.cols() == 3);
  48. //All edges measure sqrt(2)
  49. for(int f = 0;f<L_sq.rows();f++)
  50. {
  51. //All edge_lengths_squared must be exactly "diag_sq"
  52. for(int e = 0;e<3;e++)
  53. REQUIRE (L_sq(f,e) == 2.0);
  54. }
  55. //Scale the cube to have huge sides
  56. side_sq = huge_scale * huge_scale; //squared lenght of a side
  57. diag_sq = 2.0 * side_sq; //squared lenght of a diagonal
  58. igl::squared_edge_lengths(V_huge,F,L_sq);
  59. REQUIRE (L_sq.rows() == F.rows());
  60. REQUIRE (L_sq.cols() == 3);
  61. for(int f = 0;f<L_sq.rows();f++)
  62. {
  63. //All edge_lengths_squared must be exactly side_sq or diag_sq
  64. for(int e = 0;e<3;e++)
  65. if (L_sq(f,e) > 1.1*side_sq)
  66. REQUIRE (L_sq(f,e) == diag_sq);
  67. else
  68. REQUIRE (L_sq(f,e) == side_sq);
  69. //All sides sum exactly side_sq + side_sq + diag_sq
  70. REQUIRE (side_sq + side_sq + diag_sq == L_sq.row(f).sum());
  71. }
  72. //Check the equilateral triangles
  73. igl::squared_edge_lengths(V_huge,F_tet,L_sq);
  74. REQUIRE (L_sq.rows() == F_tet.rows());
  75. REQUIRE (L_sq.cols() == 3);
  76. //All edges measure sqrt(2)
  77. for(int f = 0;f<L_sq.rows();f++)
  78. {
  79. //All edge_lengths_squared must be exactly "diag_sq"
  80. for(int e = 0;e<3;e++)
  81. REQUIRE (L_sq(f,e) == diag_sq);
  82. }
  83. //Scale the cube to have tiny sides
  84. side_sq = tiny_scale * tiny_scale; //squared lenght of a side
  85. diag_sq = 2.0 * side_sq; //squared lenght of a diagonal
  86. igl::squared_edge_lengths(V_tiny,F,L_sq);
  87. REQUIRE (L_sq.rows() == F.rows());
  88. REQUIRE (L_sq.cols() == 3);
  89. for(int f = 0;f<L_sq.rows();f++)
  90. {
  91. //All edge_lengths_squared must be exactly side_sq or diag_sq
  92. for(int e = 0;e<3;e++)
  93. if (L_sq(f,e) > 1.1*side_sq)
  94. REQUIRE (L_sq(f,e) == diag_sq);
  95. else
  96. REQUIRE (L_sq(f,e) == side_sq);
  97. //All sides sum exactly side_sq + side_sq + diag_sq
  98. REQUIRE (side_sq + side_sq + diag_sq == L_sq.row(f).sum());
  99. }
  100. //Check the regular tetrahedron
  101. igl::squared_edge_lengths(V_tiny,F_tet,L_sq);
  102. REQUIRE (L_sq.rows() == F_tet.rows());
  103. REQUIRE (L_sq.cols() == 3);
  104. //All edges measure sqrt(2)
  105. for(int f = 0;f<L_sq.rows();f++)
  106. {
  107. //All edge_lengths_squared must be exactly "diag_sq"
  108. for(int e = 0;e<3;e++)
  109. REQUIRE (L_sq(f,e) == diag_sq);
  110. }
  111. }