avg_edge_length.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <test_common.h>
  2. #include <igl/avg_edge_length.h>
  3. #include <iostream>
  4. TEST_CASE("avg_edge_length: 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 avg_edge_length function
  26. double side_sq = 1.0; //squared lenght of a side
  27. double diag_sq = 2.0; //squared lenght of a diagonal
  28. double avg;
  29. avg = igl::avg_edge_length(V,F);
  30. REQUIRE (avg == Approx ((12.*sqrt(side_sq) + 6.*sqrt(diag_sq))/(12.+6.)).margin( epsilon));
  31. //Check the regular tetrahedron
  32. avg = igl::avg_edge_length(V,F_tet);
  33. REQUIRE (avg == Approx (sqrt(diag_sq)).margin( epsilon));
  34. //Scale the cube to have huge sides
  35. side_sq = huge_scale * huge_scale; //squared lenght of a side
  36. diag_sq = 2.0 * side_sq; //squared lenght of a diagonal
  37. avg = igl::avg_edge_length(V_huge,F);
  38. REQUIRE (avg == Approx ((12.*sqrt(side_sq) + 6.*sqrt(diag_sq))/(12.+6.)).margin( epsilon*huge_scale));
  39. //Check the equilateral triangles
  40. avg = igl::avg_edge_length(V_huge,F_tet);
  41. REQUIRE (avg == Approx (sqrt(diag_sq)).margin( epsilon*huge_scale));
  42. //Scale the cube to have tiny sides
  43. side_sq = tiny_scale * tiny_scale; //squared lenght of a side
  44. diag_sq = 2.0 * side_sq; //squared lenght of a diagonal
  45. avg = igl::avg_edge_length(V_tiny,F);
  46. REQUIRE (avg == Approx ((12.*sqrt(side_sq) + 6.*sqrt(diag_sq))/(12.+6.)).margin( epsilon*tiny_scale));
  47. //Check the regular tetrahedron
  48. avg = igl::avg_edge_length(V_tiny,F_tet);
  49. REQUIRE (avg == Approx (sqrt(diag_sq)).margin( epsilon*tiny_scale));
  50. }