步骤一:创建两个四面体

image.png

步骤二:缝合这两个四面体

发现上面的两个四面体是相离的,我们可以使用方法进行缝合
image.png

步骤三:整体平移

image.png

完整代码

  1. #include <CGAL/Linear_cell_complex_for_generalized_map.h>
  2. #include <iostream>
  3. #include <algorithm>
  4. #ifdef _DEBUG
  5. #include <CGAL/draw_linear_cell_complex.h>
  6. #endif // _DEBUG
  7. typedef CGAL::Linear_cell_complex_for_generalized_map<3> LCC_3;
  8. typedef LCC_3::Dart_handle Dart_handle;
  9. typedef LCC_3::Point Point;
  10. typedef LCC_3::FT FT;
  11. //一个仿函数,用于遍历一个体元的所有顶点
  12. // Functor used to display all the vertices of a given volume.
  13. template<class LCC>
  14. struct Display_vol_vertices : public CGAL::cpp98::unary_function<LCC, void>
  15. {
  16. Display_vol_vertices(const LCC& alcc) :
  17. lcc(alcc),
  18. nb_volume(0)
  19. {}
  20. void operator() (typename LCC::Dart& d)
  21. {
  22. std::cout << "Volume " << ++nb_volume << " : ";
  23. for (typename LCC::template One_dart_per_incident_cell_range<0, 3>::
  24. const_iterator it = lcc.template one_dart_per_incident_cell<0, 3>
  25. (lcc.dart_handle(d)).begin(),
  26. itend = lcc.template one_dart_per_incident_cell<0, 3>
  27. (lcc.dart_handle(d)).end();
  28. it != itend; ++it)
  29. {
  30. std::cout << lcc.point(it) << "; ";
  31. }
  32. std::cout << std::endl;
  33. }
  34. private:
  35. const LCC& lcc; //线性单元复合体(体模型)
  36. unsigned int nb_volume; //该体元对应的索引(第nb_volume+1个体元)
  37. };
  38. int main()
  39. {
  40. //创建一个线性单元复合体(即体模型)
  41. LCC_3 lcc;
  42. std::cout << "==========创建两个四面体==========" << std::endl;
  43. //创建两个四面体
  44. // Create two tetrahedra.
  45. Dart_handle d1 = lcc.make_tetrahedron(Point(-1, 0, 0), Point(0, 2, 0),
  46. Point(1, 0, 0), Point(1, 1, 2));
  47. Dart_handle d2 = lcc.make_tetrahedron(Point(0, 2, -1),
  48. Point(-1, 0, -1),
  49. Point(1, 0, -1),
  50. Point(1, 1, -3));
  51. //通过顶点属性容器上的迭代器,遍历线性单元复合体的所有顶点
  52. // Display all the vertices of the lcc by iterating on the
  53. // Vertex_attribute container.
  54. CGAL::IO::set_ascii_mode(std::cout);
  55. std::cout << "Vertices: ";
  56. for (LCC_3::Vertex_attribute_const_range::iterator
  57. v = lcc.vertex_attributes().begin(),
  58. vend = lcc.vertex_attributes().end();
  59. v != vend; ++v)
  60. std::cout << lcc.point_of_vertex_attribute(v) << "; ";
  61. std::cout << std::endl;
  62. //通过darts上的迭代器,遍历体元的所有顶点
  63. // Display the vertices of each volume by iterating on darts.
  64. std::for_each(lcc.one_dart_per_cell<3>().begin(),
  65. lcc.one_dart_per_cell<3>().end(),
  66. Display_vol_vertices<LCC_3>(lcc));
  67. #ifdef _DEBUG
  68. CGAL::draw(lcc);
  69. #endif // _DEBUG
  70. std::cout << "==========缝合两个四面体==========" << std::endl;
  71. //沿着一个面(两个四面体的公共面)缝合两个四面体
  72. // 3-Sew the 2 tetrahedra along one facet
  73. lcc.sew<3>(d1, d2);
  74. //通过darts上的迭代器,遍历体元的所有顶点
  75. // Display the vertices of each volume by iterating on darts.
  76. std::for_each(lcc.one_dart_per_cell<3>().begin(),
  77. lcc.one_dart_per_cell<3>().end(),
  78. Display_vol_vertices<LCC_3>(lcc));
  79. #ifdef _DEBUG
  80. CGAL::draw(lcc);
  81. #endif // _DEBUG
  82. std::cout << "==========平移操作==========" << std::endl;
  83. //平移第二个四面体
  84. // Translate the second tetrahedra by a given vector
  85. LCC_3::Vector v(3, 1, 1);
  86. for (LCC_3::One_dart_per_incident_cell_range<0, 3>::iterator
  87. it = lcc.one_dart_per_incident_cell<0, 3>(d2).begin(),
  88. itend = lcc.one_dart_per_incident_cell<0, 3>(d2).end();
  89. it != itend; ++it)
  90. {
  91. lcc.point(it) = LCC_3::Traits::Construct_translated_point_3()
  92. (lcc.point(it), v);
  93. }
  94. //通过darts上的迭代器,遍历体元的所有顶点
  95. // Display the vertices of each volume by iterating on darts.
  96. std::for_each(lcc.one_dart_per_cell<3>().begin(),
  97. lcc.one_dart_per_cell<3>().end(),
  98. Display_vol_vertices<LCC_3>(lcc));
  99. //检查线性单元复合体是否有效
  100. // We display the lcc characteristics.
  101. std::cout << "LCC characteristics: ";
  102. lcc.display_characteristics(std::cout) << ", valid=" << lcc.is_valid()
  103. << std::endl;
  104. #ifdef _DEBUG
  105. CGAL::draw(lcc);
  106. #endif // _DEBUG
  107. return EXIT_SUCCESS;
  108. }