projected_delaunay.cpp 4.9 KB

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