projected_delaunay.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "projected_delaunay.h"
  9. #include <iostream>
  10. #include <cassert>
  11. #if CGAL_VERSION_NR < 1040611000
  12. # warning "CGAL Version < 4.6.1 may result in crashes. Please upgrade CGAL"
  13. #endif
  14. template <typename Kernel>
  15. IGL_INLINE void igl::copyleft::cgal::projected_delaunay(
  16. const CGAL::Triangle_3<Kernel> & A,
  17. const std::vector<CGAL::Object> & A_objects_3,
  18. CGAL::Constrained_triangulation_plus_2<
  19. CGAL::Constrained_Delaunay_triangulation_2<
  20. Kernel,
  21. CGAL::Triangulation_data_structure_2<
  22. CGAL::Triangulation_vertex_base_2<Kernel>,
  23. CGAL::Constrained_triangulation_face_base_2<Kernel> >,
  24. CGAL::Exact_intersections_tag> > & cdt)
  25. {
  26. // 3D Primitives
  27. typedef CGAL::Point_3<Kernel> Point_3;
  28. typedef CGAL::Segment_3<Kernel> Segment_3;
  29. typedef CGAL::Triangle_3<Kernel> Triangle_3;
  30. typedef CGAL::Plane_3<Kernel> Plane_3;
  31. //typedef CGAL::Tetrahedron_3<Kernel> Tetrahedron_3;
  32. typedef CGAL::Point_2<Kernel> Point_2;
  33. //typedef CGAL::Segment_2<Kernel> Segment_2;
  34. //typedef CGAL::Triangle_2<Kernel> Triangle_2;
  35. typedef CGAL::Triangulation_vertex_base_2<Kernel> TVB_2;
  36. typedef CGAL::Constrained_triangulation_face_base_2<Kernel> CTFB_2;
  37. typedef CGAL::Triangulation_data_structure_2<TVB_2,CTFB_2> TDS_2;
  38. typedef CGAL::Exact_intersections_tag Itag;
  39. typedef CGAL::Constrained_Delaunay_triangulation_2<Kernel,TDS_2,Itag>
  40. CDT_2;
  41. typedef CGAL::Constrained_triangulation_plus_2<CDT_2> CDT_plus_2;
  42. // http://www.cgal.org/Manual/3.2/doc_html/cgal_manual/Triangulation_2/Chapter_main.html#Section_2D_Triangulations_Constrained_Plus
  43. // Plane of triangle A
  44. Plane_3 P(A.vertex(0),A.vertex(1),A.vertex(2));
  45. // Insert triangle into vertices
  46. typename CDT_plus_2::Vertex_handle corners[3];
  47. typedef size_t Index;
  48. for(Index i = 0;i<3;i++)
  49. {
  50. const Point_3 & p3 = A.vertex(i);
  51. const Point_2 & p2 = P.to_2d(p3);
  52. typename CDT_plus_2::Vertex_handle corner = cdt.insert(p2);
  53. corners[i] = corner;
  54. }
  55. // Insert triangle edges as constraints
  56. for(Index i = 0;i<3;i++)
  57. {
  58. cdt.insert_constraint( corners[(i+1)%3], corners[(i+2)%3]);
  59. }
  60. // Insert constraints for intersection objects
  61. for( const auto & obj : A_objects_3)
  62. {
  63. if(const Segment_3 *iseg = CGAL::object_cast<Segment_3 >(&obj))
  64. {
  65. // Add segment constraint
  66. cdt.insert_constraint(P.to_2d(iseg->vertex(0)),P.to_2d(iseg->vertex(1)));
  67. }else if(const Point_3 *ipoint = CGAL::object_cast<Point_3 >(&obj))
  68. {
  69. // Add point
  70. cdt.insert(P.to_2d(*ipoint));
  71. } else if(const Triangle_3 *itri = CGAL::object_cast<Triangle_3 >(&obj))
  72. {
  73. // Add 3 segment constraints
  74. cdt.insert_constraint(P.to_2d(itri->vertex(0)),P.to_2d(itri->vertex(1)));
  75. cdt.insert_constraint(P.to_2d(itri->vertex(1)),P.to_2d(itri->vertex(2)));
  76. cdt.insert_constraint(P.to_2d(itri->vertex(2)),P.to_2d(itri->vertex(0)));
  77. } else if(const std::vector<Point_3 > *polyp =
  78. CGAL::object_cast< std::vector<Point_3 > >(&obj))
  79. {
  80. const std::vector<Point_3 > & poly = *polyp;
  81. const Index m = poly.size();
  82. assert(m>=2);
  83. for(Index p = 0;p<m;p++)
  84. {
  85. const Index np = (p+1)%m;
  86. cdt.insert_constraint(P.to_2d(poly[p]),P.to_2d(poly[np]));
  87. }
  88. }else
  89. {
  90. assert(false && "What is this object?!");
  91. }
  92. }
  93. }
  94. #ifdef IGL_STATIC_LIBRARY
  95. // Explicit template instantiation
  96. template void igl::copyleft::cgal::projected_delaunay<CGAL::Epeck>(CGAL::Triangle_3<CGAL::Epeck> const&, std::vector<CGAL::Object, std::allocator<CGAL::Object> > const&, CGAL::Constrained_triangulation_plus_2<CGAL::Constrained_Delaunay_triangulation_2<CGAL::Epeck, CGAL::Triangulation_data_structure_2<CGAL::Triangulation_vertex_base_2<CGAL::Epeck, CGAL::Triangulation_ds_vertex_base_2<void> >, CGAL::Constrained_triangulation_face_base_2<CGAL::Epeck, CGAL::Triangulation_face_base_2<CGAL::Epeck, CGAL::Triangulation_ds_face_base_2<void> > > >, CGAL::Exact_intersections_tag> >&);
  97. template void igl::copyleft::cgal::projected_delaunay<CGAL::Epick>(CGAL::Triangle_3<CGAL::Epick> const&, std::vector<CGAL::Object, std::allocator<CGAL::Object> > const&, CGAL::Constrained_triangulation_plus_2<CGAL::Constrained_Delaunay_triangulation_2<CGAL::Epick, CGAL::Triangulation_data_structure_2<CGAL::Triangulation_vertex_base_2<CGAL::Epick, CGAL::Triangulation_ds_vertex_base_2<void> >, CGAL::Constrained_triangulation_face_base_2<CGAL::Epick, CGAL::Triangulation_face_base_2<CGAL::Epick, CGAL::Triangulation_ds_face_base_2<void> > > >, CGAL::Exact_intersections_tag> >&);
  98. #endif