22#ifdef DEAL_II_WITH_CGAL
33# include <CGAL/Boolean_set_operations_2.h>
34# include <CGAL/Cartesian.h>
35# include <CGAL/Circular_kernel_intersections.h>
36# include <CGAL/Constrained_Delaunay_triangulation_2.h>
37# include <CGAL/Delaunay_mesh_face_base_2.h>
38# include <CGAL/Delaunay_mesh_size_criteria_2.h>
39# include <CGAL/Delaunay_mesher_2.h>
40# include <CGAL/Delaunay_triangulation_2.h>
41# include <CGAL/Exact_predicates_exact_constructions_kernel_with_sqrt.h>
42# include <CGAL/Kernel_traits.h>
43# include <CGAL/Polygon_2.h>
44# include <CGAL/Polygon_with_holes_2.h>
45# include <CGAL/Projection_traits_xy_3.h>
46# include <CGAL/Segment_3.h>
47# include <CGAL/Simple_cartesian.h>
48# include <CGAL/Tetrahedron_3.h>
49# include <CGAL/Triangle_2.h>
50# include <CGAL/Triangle_3.h>
51# include <CGAL/Triangulation_2.h>
52# include <CGAL/Triangulation_3.h>
53# include <CGAL/Triangulation_face_base_with_id_2.h>
54# include <CGAL/Triangulation_face_base_with_info_2.h>
58# include <type_traits>
64 using K = CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt;
65 using K_exact = CGAL::Exact_predicates_exact_constructions_kernel;
96 using Vb = CGAL::Triangulation_vertex_base_2<K>;
97 using Fbb = CGAL::Triangulation_face_base_with_info_2<FaceInfo2, K>;
98 using CFb = CGAL::Constrained_triangulation_face_base_2<K, Fbb>;
99 using Fb = CGAL::Delaunay_mesh_face_base_2<K, CFb>;
100 using Tds = CGAL::Triangulation_data_structure_2<Vb, Fb>;
101 using Itag = CGAL::Exact_predicates_tag;
102 using CDT = CGAL::Constrained_Delaunay_triangulation_2<K, Tds, Itag>;
103 using Criteria = CGAL::Delaunay_mesh_size_criteria_2<CDT>;
115 template <
typename TargetVariant>
116 struct Repackage : boost::static_visitor<TargetVariant>
118 template <
typename T>
120 operator()(
const T &t)
const
122 return TargetVariant(t);
132 template <
typename... Types>
133 std_cxx17::optional<std_cxx17::variant<Types...>>
134 convert_boost_to_std(
const boost::optional<boost::variant<Types...>> &x)
143 using std_variant = std::variant<Types...>;
144 return boost::apply_visitor(Repackage<std_variant>(), *x);
161 std::list<CDT::Edge> &border)
163 if (start->info().nesting_level != -1)
167 std::list<Face_handle> queue;
168 queue.push_back(start);
169 while (!queue.empty())
173 if (fh->info().nesting_level == -1)
175 fh->info().nesting_level = index;
176 for (
int i = 0; i < 3; i++)
180 if (n->info().nesting_level == -1)
182 if (ct.is_constrained(e))
197 for (CDT::Face_handle f : cdt.all_face_handles())
199 f->info().nesting_level = -1;
201 std::list<CDT::Edge> border;
203 while (!border.empty())
205 CDT::Edge e = border.front();
208 if (n->info().nesting_level == -1)
210 mark_domains(cdt, n, e.first->info().nesting_level + 1, border);
224 std_cxx17::optional<std_cxx17::variant<
CGALPoint2,
227 std::vector<CGALPoint2>>>
235 std::array<CGALPoint2, 3> pts0, pts1;
237 std::transform(triangle0.begin(),
240 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint2, 2>);
242 std::transform(triangle1.begin(),
245 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint2, 2>);
249 return convert_boost_to_std(
250 CGAL::intersection(cgal_triangle0, cgal_triangle1));
254 std_cxx17::optional<std_cxx17::variant<CGALPoint2, CGALSegment2>>
262 std::array<CGALPoint2, 3> pts0;
263 std::array<CGALPoint2, 2> pts1;
265 std::transform(triangle.begin(),
268 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint2, 2>);
270 std::transform(segment.begin(),
273 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint2, 2>);
277 return convert_boost_to_std(
278 CGAL::intersection(cgal_segment, cgal_triangle));
284 std::vector<Polygon_with_holes_2>
291 std::array<CGALPoint2, 4> pts0, pts1;
293 std::transform(rectangle0.begin(),
296 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint2, 2>);
298 std::transform(rectangle1.begin(),
301 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint2, 2>);
303 const CGALPolygon first_poly{pts0.begin(), pts0.end()};
304 const CGALPolygon second_poly{pts1.begin(), pts1.end()};
306 std::vector<Polygon_with_holes_2> poly_list;
307 CGAL::intersection(first_poly,
309 std::back_inserter(poly_list));
315 std_cxx17::optional<std_cxx17::variant<CGALPoint3, CGALSegment3>>
320# if DEAL_II_CGAL_VERSION_GTE(5, 5, 0)
325 std::array<CGALPoint3, 4> pts0;
326 std::array<CGALPoint3, 2> pts1;
328 std::transform(tetrahedron.begin(),
331 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint3, 3>);
333 std::transform(segment.begin(),
336 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint3, 3>);
338 CGALTetra cgal_tetrahedron{pts0[0], pts0[1], pts0[2], pts0[3]};
340 return convert_boost_to_std(
341 CGAL::intersection(cgal_segment, cgal_tetrahedron));
346 "This function requires a version of CGAL greater or equal than 5.5."));
355 std_cxx17::optional<std_cxx17::variant<
CGALPoint3,
358 std::vector<CGALPoint3>>>
363# if DEAL_II_CGAL_VERSION_GTE(5, 5, 0)
368 std::array<CGALPoint3, 4> pts0;
369 std::array<CGALPoint3, 3> pts1;
371 std::transform(tetrahedron.begin(),
374 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint3, 3>);
376 std::transform(triangle.begin(),
379 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint3, 3>);
381 CGALTetra cgal_tetrahedron{pts0[0], pts0[1], pts0[2], pts0[3]};
383 return convert_boost_to_std(
384 CGAL::intersection(cgal_triangle, cgal_tetrahedron));
390 "This function requires a version of CGAL greater or equal than 5.5."));
398 std::vector<std::array<Point<2>, 3>>
406 const auto intersection_test =
409 if (!intersection_test.empty())
411 const auto & poly = intersection_test[0].outer_boundary();
412 const unsigned int size_poly = poly.size();
418 {{CGALWrappers::cgal_point_to_dealii_point<2>(poly.vertex(0)),
419 CGALWrappers::cgal_point_to_dealii_point<2>(poly.vertex(1)),
420 CGALWrappers::cgal_point_to_dealii_point<2>(
423 else if (size_poly >= 4)
426 std::vector<std::array<Point<2>, 3>> collection;
429 cdt.insert_constraint(poly.vertices_begin(),
438 if (f->info().in_domain() &&
439 CGAL::to_double(cdt.triangle(f).area()) > tol)
441 collection.push_back(
442 {{CGALWrappers::cgal_point_to_dealii_point<2>(
443 cdt.triangle(f).vertex(0)),
444 CGALWrappers::cgal_point_to_dealii_point<2>(
445 cdt.triangle(f).vertex(1)),
446 CGALWrappers::cgal_point_to_dealii_point<2>(
447 cdt.triangle(f).vertex(2))}});
465 std::vector<std::array<Point<2>, 2>>
473 std::array<CGALPoint2, 4> pts;
475 std::transform(quad.begin(),
478 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint2, 2>);
483 CGALWrappers::dealii_point_to_cgal_point<CGALPoint2>(line[0]),
484 CGALWrappers::dealii_point_to_cgal_point<CGALPoint2>(line[1]));
486 cdt.insert_constraint(poly.vertices_begin(), poly.vertices_end(),
true);
487 std::vector<std::array<Point<2>, 2>>
vertices;
491 if (f->info().in_domain() &&
492 CGAL::to_double(cdt.triangle(f).area()) > tol &&
493 CGAL::do_intersect(segm, cdt.triangle(f)))
495 const auto intersection =
496 CGAL::intersection(segm, cdt.triangle(f));
498 boost::get<CGALSegment2>(&*intersection))
501 {{CGALWrappers::cgal_point_to_dealii_point<2>((*s)[0]),
502 CGALWrappers::cgal_point_to_dealii_point<2>((*s)[1])}});
511 std::vector<std::array<Point<3>, 2>>
516# if DEAL_II_CGAL_VERSION_GTE(5, 5, 0)
521 std::array<CGALPoint3_exact, 8> pts;
527 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint3_exact, 3>);
530 CGALWrappers::dealii_point_to_cgal_point<CGALPoint3_exact>(line[0]),
531 CGALWrappers::dealii_point_to_cgal_point<CGALPoint3_exact>(line[1]));
535 std::vector<std::array<Point<3>, 2>>
vertices;
537 cgal_triangulation.insert(pts.begin(), pts.end());
538 for (
const auto &c : cgal_triangulation.finite_cell_handles())
540 const auto &cgal_tetrahedron = cgal_triangulation.tetrahedron(c);
541 if (CGAL::do_intersect(cgal_segment, cgal_tetrahedron))
543 const auto intersection =
544 CGAL::intersection(cgal_segment, cgal_tetrahedron);
546 boost::get<CGALSegment3_exact>(&*intersection))
548 if (s->squared_length() > tol * tol)
551 {{CGALWrappers::cgal_point_to_dealii_point<3>(
553 CGALWrappers::cgal_point_to_dealii_point<3>(
564 "This function requires a version of CGAL greater or equal than 5.5."));
572 std::vector<std::array<Point<3>, 3>>
577# if DEAL_II_CGAL_VERSION_GTE(5, 5, 0)
582 std::array<CGALPoint3_exact, 8> pts_hex;
583 std::array<CGALPoint3_exact, 4> pts_quad;
589 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint3_exact, 3>);
595 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint3_exact, 3>);
598 std::vector<std::array<Point<3>, 3>>
vertices;
600 triangulation_hexa.insert(pts_hex.begin(), pts_hex.end());
604 triangulation_quad.insert(pts_quad.begin(), pts_quad.end());
606 for (
const auto &c : triangulation_hexa.finite_cell_handles())
608 const auto &tet = triangulation_hexa.tetrahedron(c);
610 for (
const auto &f : triangulation_quad.finite_facets())
612 if (CGAL::do_intersect(tet, triangulation_quad.triangle(f)))
614 const auto intersection =
615 CGAL::intersection(triangulation_quad.triangle(f), tet);
618 boost::get<CGALTriangle3_exact>(&*intersection))
620 if (CGAL::to_double(t->squared_area()) > tol * tol)
623 {{cgal_point_to_dealii_point<3>((*t)[0]),
624 cgal_point_to_dealii_point<3>((*t)[1]),
625 cgal_point_to_dealii_point<3>((*t)[2])}});
629 if (
const std::vector<CGALPoint3_exact> *vps =
630 boost::get<std::vector<CGALPoint3_exact>>(
634 tria_inter.insert(vps->begin(), vps->end());
636 for (
auto it = tria_inter.finite_facets_begin();
637 it != tria_inter.finite_facets_end();
640 const auto triangle = tria_inter.triangle(*it);
641 if (CGAL::to_double(triangle.squared_area()) >
644 std::array<Point<3>, 3> verts = {
645 {CGALWrappers::cgal_point_to_dealii_point<3>(
647 CGALWrappers::cgal_point_to_dealii_point<3>(
649 CGALWrappers::cgal_point_to_dealii_point<3>(
665 "This function requires a version of CGAL greater or equal than 5.5."));
673 std::vector<std::array<Point<3>, 4>>
681 std::array<CGALPoint3_exact, 8> pts_hex0;
682 std::array<CGALPoint3_exact, 8> pts_hex1;
688 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint3_exact, 3>);
694 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint3_exact, 3>);
698 std::vector<std::array<Point<3>, 4>>
vertices;
701 tria0.insert(pts_hex0.begin(), pts_hex0.end());
702 tria1.insert(pts_hex1.begin(), pts_hex1.end());
704 for (
const auto &c0 : tria0.finite_cell_handles())
706 const auto &tet0 = tria1.tetrahedron(c0);
707 const auto &tetg0 = CGAL::make_tetrahedron(tet0.vertex(0),
713 for (
const auto &c1 : tria1.finite_cell_handles())
715 const auto &tet1 = tria1.tetrahedron(c1);
716 const auto &tetg1 = CGAL::make_tetrahedron(tet1.vertex(0),
722 namespace PMP = CGAL::Polygon_mesh_processing;
723 const bool test_intersection =
724 PMP::corefine_and_compute_intersection(surf0, surf1, sm);
725 if (PMP::volume(sm) > tol && test_intersection)
729 triangulation_hexa.insert(sm.points().begin(),
731 for (
const auto &c : triangulation_hexa.finite_cell_handles())
733 const auto &tet = triangulation_hexa.tetrahedron(c);
735 {{CGALWrappers::cgal_point_to_dealii_point<3>(
737 CGALWrappers::cgal_point_to_dealii_point<3>(
739 CGALWrappers::cgal_point_to_dealii_point<3>(
741 CGALWrappers::cgal_point_to_dealii_point<3>(
756 template <
int dim0,
int dim1,
int spacedim>
757 std::vector<std::array<Point<spacedim>, dim1 + 1>>
763 const unsigned int n_vertices0 = vertices0.size();
764 const unsigned int n_vertices1 = vertices1.size();
767 n_vertices0 > 0 || n_vertices1 > 0,
769 "The intersection cannot be computed as at least one of the two cells has no vertices."));
771 if constexpr (dim0 == 2 && dim1 == 2 && spacedim == 2)
773 if (n_vertices0 == 4 && n_vertices1 == 4)
780 else if constexpr (dim0 == 2 && dim1 == 1 && spacedim == 2)
782 if (n_vertices0 == 4 && n_vertices1 == 2)
789 else if constexpr (dim0 == 3 && dim1 == 1 && spacedim == 3)
791 if (n_vertices0 == 8 && n_vertices1 == 2)
798 else if constexpr (dim0 == 3 && dim1 == 2 && spacedim == 3)
800 if (n_vertices0 == 8 && n_vertices1 == 4)
807 else if constexpr (dim0 == 3 && dim1 == 3 && spacedim == 3)
809 if (n_vertices0 == 8 && n_vertices1 == 8)
826 template <
int dim0,
int dim1,
int spacedim>
827 std::vector<std::array<Point<spacedim>, dim1 + 1>>
836 ReferenceCells::get_hypercube<dim0>().n_vertices(),
839 ReferenceCells::get_hypercube<dim1>().n_vertices(),
842 const auto &vertices0 =
843 CGALWrappers::get_vertices_in_cgal_order(cell0, mapping0);
844 const auto &vertices1 =
845 CGALWrappers::get_vertices_in_cgal_order(cell1, mapping1);
847 return compute_intersection_of_cells<dim0, dim1, spacedim>(vertices0,
852# include "intersections.inst"
867std::vector<std::array<Point<spacedim>, dim1 + 1>>
879template <
int dim0,
int dim1,
int spacedim>
880std::vector<std::array<Point<spacedim>, dim1 + 1>>
Abstract base class for mapping classes.
virtual boost::container::small_vector< Point< spacedim >, GeometryInfo< dim >::vertices_per_cell > get_vertices(const typename Triangulation< dim, spacedim >::cell_iterator &cell) const
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
static ::ExceptionBase & ExcNeedsCGAL()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
std_cxx17::optional< std_cxx17::variant< CGALPoint2, CGALSegment2, CGALTriangle2, std::vector< CGALPoint2 > > > compute_intersection_triangle_triangle(const ArrayView< const Point< 2 > > &triangle0, const ArrayView< const Point< 2 > > &triangle1)
std::vector< std::array< Point< 2 >, 2 > > compute_intersection_quad_line(const ArrayView< const Point< 2 > > &quad, const ArrayView< const Point< 2 > > &line, const double tol)
std::vector< std::array< Point< 3 >, 3 > > compute_intersection_hexa_quad(const ArrayView< const Point< 3 > > &hexa, const ArrayView< const Point< 3 > > &quad, const double tol)
void mark_domains(CDT &ct, Face_handle start, int index, std::list< CDT::Edge > &border)
std_cxx17::optional< std_cxx17::variant< CGALPoint3, CGALSegment3, CGALTriangle3, std::vector< CGALPoint3 > > > compute_intersection_tetra_triangle(const ArrayView< const Point< 3 > > &tetrahedron, const ArrayView< const Point< 3 > > &triangle)
std_cxx17::optional< std_cxx17::variant< CGALPoint2, CGALSegment2 > > compute_intersection_triangle_segment(const ArrayView< const Point< 2 > > &triangle, const ArrayView< const Point< 2 > > &segment)
std::vector< Polygon_with_holes_2 > compute_intersection_rect_rect(const ArrayView< const Point< 2 > > &rectangle0, const ArrayView< const Point< 2 > > &rectangle1)
std::vector< std::array< Point< 3 >, 4 > > compute_intersection_hexa_hexa(const ArrayView< const Point< 3 > > &hexa0, const ArrayView< const Point< 3 > > &hexa1, const double tol)
std::vector< std::array< Point< 3 >, 2 > > compute_intersection_hexa_line(const ArrayView< const Point< 3 > > &hexa, const ArrayView< const Point< 3 > > &line, const double tol)
std_cxx17::optional< std_cxx17::variant< CGALPoint3, CGALSegment3 > > compute_intersection_tetra_segment(const ArrayView< const Point< 3 > > &tetrahedron, const ArrayView< const Point< 3 > > &segment)
std::vector< std::array< Point< 2 >, 3 > > compute_intersection_quad_quad(const ArrayView< const Point< 2 > > &quad0, const ArrayView< const Point< 2 > > &quad1, const double tol)
K::Tetrahedron_3 CGALTetra
CGAL::Surface_mesh< K_exact::Point_3 > Surface_mesh
K::Segment_2 CGALSegment2
CGAL::Exact_predicates_tag Itag
K_exact::Tetrahedron_3 CGALTetra_exact
K_exact::Triangle_3 CGALTriangle3_exact
CDT::Vertex_handle Vertex_handle
K::Triangle_3 CGALTriangle3
K::Segment_3 CGALSegment3
CDT::Face_handle Face_handle
CGAL::Triangulation_3< K_exact > Triangulation3_exact
CGAL::Constrained_Delaunay_triangulation_2< K, Tds, Itag > CDT
CGAL::Delaunay_mesh_size_criteria_2< CDT > Criteria
CGAL::Triangulation_vertex_base_2< K > Vb
CGAL::Polygon_with_holes_2< K > Polygon_with_holes_2
CGAL::Triangulation_data_structure_2< Vb, Fb > Tds
std::vector< std::array< Point< spacedim >, dim1+1 > > compute_intersection_of_cells(const typename Triangulation< dim0, spacedim >::cell_iterator &cell0, const typename Triangulation< dim1, spacedim >::cell_iterator &cell1, const Mapping< dim0, spacedim > &mapping0, const Mapping< dim1, spacedim > &mapping1, const double tol=1e-9)
CGAL::Polygon_2< K > CGALPolygon
CGAL::Triangulation_2< K > Triangulation2
CGAL::Exact_predicates_exact_constructions_kernel K_exact
CGAL::Triangulation_3< K > Triangulation3
CGAL::Constrained_triangulation_face_base_2< K, Fbb > CFb
CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt K
K::Triangle_2 CGALTriangle2
CGAL::Delaunay_mesh_face_base_2< K, CFb > Fb
K_exact::Segment_3 CGALSegment3_exact
K_exact::Point_3 CGALPoint3_exact
CGAL::Triangulation_face_base_with_info_2< FaceInfo2, K > Fbb