edge_lengths.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <test_common.h>
  2. #include <igl/edge_lengths.h>
  3. #include <iostream>
  4. TEST_CASE("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. //2. Check edge_lengths
  26. double side = 1.0; //lenght of a side
  27. double diag = sqrt(2.0); //lenght of a diagonal
  28. Eigen::MatrixXd L;
  29. igl::edge_lengths(V,F,L);
  30. REQUIRE (L.rows() == F.rows());
  31. REQUIRE (L.cols() == 3);
  32. //All edges in unit cube measure 1.0 or sqrt(2) in diagonals
  33. for(int f = 0;f<L.rows();f++)
  34. {
  35. //All edge_lengths_squared must be exactly "side" or "diag"
  36. for(int e = 0;e<3;e++)
  37. if (L(f,e) > 1.1*side)
  38. REQUIRE (L(f,e) == diag);
  39. else
  40. REQUIRE (L(f,e) == side);
  41. //All sides sum exactly side + side + diag
  42. REQUIRE (side + side + diag == L.row(f).sum());
  43. }
  44. //Check the cube of huge sides
  45. scale = 1.0e8;
  46. side = scale; //lenght of a side
  47. diag = scale*sqrt(2.0); //lenght of a diagonal
  48. igl::edge_lengths(V_huge,F,L);
  49. REQUIRE (L.rows() == F.rows());
  50. REQUIRE (L.cols() == 3);
  51. for(int f = 0;f<L.rows();f++)
  52. {
  53. //All edge_lengths_squared must be exactly "side" or "diag"
  54. for(int e = 0;e<3;e++)
  55. if (L(f,e) > 1.1*side)
  56. REQUIRE (L(f,e) == diag);
  57. else
  58. REQUIRE (L(f,e) == side);
  59. //All sides sum exactly side + side + diag
  60. REQUIRE (side + side + diag == Approx (L.row(f).sum()).margin( epsilon));
  61. }
  62. //Check the cube of tiny sides
  63. scale = 1.0e-8;
  64. side = scale; //lenght of a side
  65. diag = scale*sqrt(2.0); //lenght of a diagonal
  66. igl::edge_lengths(V_tiny,F,L);
  67. REQUIRE (L.rows() == F.rows());
  68. REQUIRE (L.cols() == 3);
  69. for(int f = 0;f<L.rows();f++)
  70. {
  71. //All edge_lengths_squared must be exactly "side" or "diag"
  72. for(int e = 0;e<3;e++)
  73. if (L(f,e) > 1.1*side)
  74. REQUIRE (L(f,e) == diag);
  75. else
  76. REQUIRE (L(f,e) == side);
  77. //All sides sum exactly side + side + diag
  78. REQUIRE (side + side + diag == L.row(f).sum());
  79. }
  80. }