21#ifdef DEAL_II_WITH_CGAL
30# include <CGAL/Boolean_set_operations_2.h>
31# include <CGAL/Cartesian.h>
32# include <CGAL/Circular_kernel_intersections.h>
33# include <CGAL/Constrained_Delaunay_triangulation_2.h>
34# include <CGAL/Delaunay_mesh_face_base_2.h>
35# include <CGAL/Delaunay_mesh_size_criteria_2.h>
36# include <CGAL/Delaunay_mesher_2.h>
37# include <CGAL/Delaunay_triangulation_2.h>
38# include <CGAL/Exact_predicates_exact_constructions_kernel_with_sqrt.h>
39# include <CGAL/Kernel_traits.h>
40# include <CGAL/Polygon_2.h>
41# include <CGAL/Polygon_with_holes_2.h>
42# include <CGAL/Projection_traits_xy_3.h>
43# include <CGAL/Segment_3.h>
44# include <CGAL/Simple_cartesian.h>
45# include <CGAL/Tetrahedron_3.h>
46# include <CGAL/Triangle_2.h>
47# include <CGAL/Triangle_3.h>
48# include <CGAL/Triangulation_2.h>
49# include <CGAL/Triangulation_3.h>
50# include <CGAL/Triangulation_face_base_with_id_2.h>
51# include <CGAL/Triangulation_face_base_with_info_2.h>
61 using K = CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt;
62 using K_exact = CGAL::Exact_predicates_exact_constructions_kernel;
92 using Vb = CGAL::Triangulation_vertex_base_2<K>;
93 using Fbb = CGAL::Triangulation_face_base_with_info_2<FaceInfo2, K>;
94 using CFb = CGAL::Constrained_triangulation_face_base_2<K, Fbb>;
95 using Fb = CGAL::Delaunay_mesh_face_base_2<K, CFb>;
96 using Tds = CGAL::Triangulation_data_structure_2<Vb, Fb>;
97 using Itag = CGAL::Exact_predicates_tag;
98 using CDT = CGAL::Constrained_Delaunay_triangulation_2<K, Tds, Itag>;
99 using Criteria = CGAL::Delaunay_mesh_size_criteria_2<CDT>;
103 template <
class T,
class... Types>
107 return std::get_if<T>(v);
110 template <
class T,
class... Types>
114 return boost::get<T>(v);
125 template <
typename TargetVariant>
126 struct Repackage : boost::static_visitor<TargetVariant>
128 template <
typename T>
130 operator()(
const T &t)
const
132 return TargetVariant(t);
142 template <
typename... Types>
143 std::optional<std::variant<Types...>>
144 convert_boost_to_std(
const boost::optional<boost::variant<Types...>> &x)
153 using std_variant = std::variant<Types...>;
154 return boost::apply_visitor(Repackage<std_variant>(), *x);
164 template <
typename... Types>
165 const std::optional<std::variant<Types...>> &
166 convert_boost_to_std(
const std::optional<std::variant<Types...>> &opt)
178 std::list<CDT::Edge> &border)
180 if (start->info().nesting_level != -1)
184 std::list<Face_handle> queue;
185 queue.push_back(start);
186 while (!queue.empty())
190 if (fh->info().nesting_level == -1)
192 fh->info().nesting_level = index;
193 for (
int i = 0; i < 3; i++)
197 if (n->info().nesting_level == -1)
199 if (ct.is_constrained(e))
214 for (CDT::Face_handle f : cdt.all_face_handles())
216 f->info().nesting_level = -1;
218 std::list<CDT::Edge> border;
220 while (!border.empty())
222 CDT::Edge e = border.front();
225 if (n->info().nesting_level == -1)
227 mark_domains(cdt, n, e.first->info().nesting_level + 1, border);
244 std::vector<CGALPoint2>>>
252 std::array<CGALPoint2, 3> pts0, pts1;
254 std::transform(triangle0.begin(),
257 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint2, 2>);
259 std::transform(triangle1.begin(),
262 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint2, 2>);
266 return convert_boost_to_std(
267 CGAL::intersection(cgal_triangle0, cgal_triangle1));
271 std::optional<std::variant<CGALPoint2, CGALSegment2>>
279 std::array<CGALPoint2, 3> pts0;
280 std::array<CGALPoint2, 2> pts1;
282 std::transform(triangle.begin(),
285 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint2, 2>);
287 std::transform(segment.begin(),
290 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint2, 2>);
294 return convert_boost_to_std(
295 CGAL::intersection(cgal_segment, cgal_triangle));
301 std::vector<Polygon_with_holes_2>
308 std::array<CGALPoint2, 4> pts0, pts1;
310 std::transform(rectangle0.begin(),
313 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint2, 2>);
315 std::transform(rectangle1.begin(),
318 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint2, 2>);
320 const CGALPolygon first_poly{pts0.begin(), pts0.end()};
321 const CGALPolygon second_poly{pts1.begin(), pts1.end()};
323 std::vector<Polygon_with_holes_2> poly_list;
324 CGAL::intersection(first_poly,
326 std::back_inserter(poly_list));
332 std::optional<std::variant<CGALPoint3, CGALSegment3>>
337# if DEAL_II_CGAL_VERSION_GTE(5, 5, 0)
342 std::array<CGALPoint3, 4> pts0;
343 std::array<CGALPoint3, 2> pts1;
345 std::transform(tetrahedron.begin(),
348 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint3, 3>);
350 std::transform(segment.begin(),
353 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint3, 3>);
355 CGALTetra cgal_tetrahedron{pts0[0], pts0[1], pts0[2], pts0[3]};
357 return convert_boost_to_std(
358 CGAL::intersection(cgal_segment, cgal_tetrahedron));
363 "This function requires a version of CGAL greater or equal than 5.5."));
375 std::vector<CGALPoint3>>>
380# if DEAL_II_CGAL_VERSION_GTE(5, 5, 0)
385 std::array<CGALPoint3, 4> pts0;
386 std::array<CGALPoint3, 3> pts1;
388 std::transform(tetrahedron.begin(),
391 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint3, 3>);
393 std::transform(triangle.begin(),
396 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint3, 3>);
398 CGALTetra cgal_tetrahedron{pts0[0], pts0[1], pts0[2], pts0[3]};
400 return convert_boost_to_std(
401 CGAL::intersection(cgal_triangle, cgal_tetrahedron));
407 "This function requires a version of CGAL greater or equal than 5.5."));
415 std::vector<std::array<Point<2>, 3>>
423 const auto intersection_test =
426 if (!intersection_test.empty())
428 const auto &poly = intersection_test[0].outer_boundary();
429 const unsigned int size_poly = poly.size();
435 {{CGALWrappers::cgal_point_to_dealii_point<2>(poly.vertex(0)),
436 CGALWrappers::cgal_point_to_dealii_point<2>(poly.vertex(1)),
437 CGALWrappers::cgal_point_to_dealii_point<2>(
440 else if (size_poly >= 4)
443 std::vector<std::array<Point<2>, 3>> collection;
446 cdt.insert_constraint(poly.vertices_begin(),
454 if (f->info().in_domain() &&
455 CGAL::to_double(cdt.triangle(f).area()) > tol)
457 collection.push_back(
458 {{CGALWrappers::cgal_point_to_dealii_point<2>(
459 cdt.triangle(f).vertex(0)),
460 CGALWrappers::cgal_point_to_dealii_point<2>(
461 cdt.triangle(f).vertex(1)),
462 CGALWrappers::cgal_point_to_dealii_point<2>(
463 cdt.triangle(f).vertex(2))}});
481 std::vector<std::array<Point<2>, 2>>
489 std::array<CGALPoint2, 4> pts;
491 std::transform(quad.begin(),
494 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint2, 2>);
499 CGALWrappers::dealii_point_to_cgal_point<CGALPoint2>(line[0]),
500 CGALWrappers::dealii_point_to_cgal_point<CGALPoint2>(line[1]));
502 cdt.insert_constraint(poly.vertices_begin(), poly.vertices_end(),
true);
503 std::vector<std::array<Point<2>, 2>> vertices;
507 if (f->info().in_domain() &&
508 CGAL::to_double(cdt.triangle(f).area()) > tol &&
509 CGAL::do_intersect(segm, cdt.triangle(f)))
511 const auto intersection =
512 CGAL::intersection(segm, cdt.triangle(f));
513 if (
const CGALSegment2 *s = get_if_<CGALSegment2>(&*intersection))
516 {{CGALWrappers::cgal_point_to_dealii_point<2>((*s)[0]),
517 CGALWrappers::cgal_point_to_dealii_point<2>((*s)[1])}});
526 std::vector<std::array<Point<3>, 2>>
531# if DEAL_II_CGAL_VERSION_GTE(5, 5, 0)
536 std::array<CGALPoint3_exact, 8> pts;
542 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint3_exact, 3>);
545 CGALWrappers::dealii_point_to_cgal_point<CGALPoint3_exact>(line[0]),
546 CGALWrappers::dealii_point_to_cgal_point<CGALPoint3_exact>(line[1]));
550 std::vector<std::array<Point<3>, 2>> vertices;
552 cgal_triangulation.insert(pts.begin(), pts.end());
553 for (
const auto &c : cgal_triangulation.finite_cell_handles())
555 const auto &cgal_tetrahedron = cgal_triangulation.tetrahedron(c);
556 if (CGAL::do_intersect(cgal_segment, cgal_tetrahedron))
558 const auto intersection =
559 CGAL::intersection(cgal_segment, cgal_tetrahedron);
561 get_if_<CGALSegment3_exact>(&*intersection))
563 if (s->squared_length() > tol * tol)
566 {{CGALWrappers::cgal_point_to_dealii_point<3>(
568 CGALWrappers::cgal_point_to_dealii_point<3>(
579 "This function requires a version of CGAL greater or equal than 5.5."));
587 std::vector<std::array<Point<3>, 3>>
592# if DEAL_II_CGAL_VERSION_GTE(5, 5, 0)
597 std::array<CGALPoint3_exact, 8> pts_hex;
598 std::array<CGALPoint3_exact, 4> pts_quad;
604 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint3_exact, 3>);
610 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint3_exact, 3>);
613 std::vector<std::array<Point<3>, 3>> vertices;
615 triangulation_hexa.insert(pts_hex.begin(), pts_hex.end());
619 triangulation_quad.insert(pts_quad.begin(), pts_quad.end());
621 for (
const auto &c : triangulation_hexa.finite_cell_handles())
623 const auto &tet = triangulation_hexa.tetrahedron(c);
625 for (
const auto &f : triangulation_quad.finite_facets())
627 if (CGAL::do_intersect(tet, triangulation_quad.triangle(f)))
629 const auto intersection =
630 CGAL::intersection(triangulation_quad.triangle(f), tet);
633 get_if_<CGALTriangle3_exact>(&*intersection))
635 if (CGAL::to_double(t->squared_area()) > tol * tol)
638 {{cgal_point_to_dealii_point<3>((*t)[0]),
639 cgal_point_to_dealii_point<3>((*t)[1]),
640 cgal_point_to_dealii_point<3>((*t)[2])}});
644 if (
const std::vector<CGALPoint3_exact> *vps =
645 get_if_<std::vector<CGALPoint3_exact>>(&*intersection))
648 tria_inter.insert(vps->begin(), vps->end());
650 for (
auto it = tria_inter.finite_facets_begin();
651 it != tria_inter.finite_facets_end();
654 const auto triangle = tria_inter.triangle(*it);
655 if (CGAL::to_double(triangle.squared_area()) >
658 std::array<Point<3>, 3> verts = {
659 {CGALWrappers::cgal_point_to_dealii_point<3>(
661 CGALWrappers::cgal_point_to_dealii_point<3>(
663 CGALWrappers::cgal_point_to_dealii_point<3>(
666 vertices.push_back(verts);
679 "This function requires a version of CGAL greater or equal than 5.5."));
687 std::vector<std::array<Point<3>, 4>>
695 std::array<CGALPoint3_exact, 8> pts_hex0;
696 std::array<CGALPoint3_exact, 8> pts_hex1;
702 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint3_exact, 3>);
708 &CGALWrappers::dealii_point_to_cgal_point<CGALPoint3_exact, 3>);
712 std::vector<std::array<Point<3>, 4>> vertices;
715 tria0.insert(pts_hex0.begin(), pts_hex0.end());
716 tria1.insert(pts_hex1.begin(), pts_hex1.end());
718 for (
const auto &c0 : tria0.finite_cell_handles())
720 const auto &tet0 = tria1.tetrahedron(c0);
721 const auto &tetg0 = CGAL::make_tetrahedron(tet0.vertex(0),
727 for (
const auto &c1 : tria1.finite_cell_handles())
729 const auto &tet1 = tria1.tetrahedron(c1);
730 const auto &tetg1 = CGAL::make_tetrahedron(tet1.vertex(0),
736 namespace PMP = CGAL::Polygon_mesh_processing;
737 const bool test_intersection =
738 PMP::corefine_and_compute_intersection(surf0, surf1, sm);
739 if (PMP::volume(sm) > tol && test_intersection)
743 triangulation_hexa.insert(sm.points().begin(),
745 for (
const auto &c : triangulation_hexa.finite_cell_handles())
747 const auto &tet = triangulation_hexa.tetrahedron(c);
749 {{CGALWrappers::cgal_point_to_dealii_point<3>(
751 CGALWrappers::cgal_point_to_dealii_point<3>(
753 CGALWrappers::cgal_point_to_dealii_point<3>(
755 CGALWrappers::cgal_point_to_dealii_point<3>(
770 template <
int structdim0,
int structdim1,
int spacedim>
771 std::vector<std::array<Point<spacedim>, structdim1 + 1>>
777 const unsigned int n_vertices0 = vertices0.size();
778 const unsigned int n_vertices1 = vertices1.size();
781 n_vertices0 > 0 || n_vertices1 > 0,
783 "The intersection cannot be computed as at least one of the two cells has no vertices."));
785 if constexpr (structdim0 == 2 && structdim1 == 2 && spacedim == 2)
787 if (n_vertices0 == 4 && n_vertices1 == 4)
794 else if constexpr (structdim0 == 2 && structdim1 == 1 && spacedim == 2)
796 if (n_vertices0 == 4 && n_vertices1 == 2)
803 else if constexpr (structdim0 == 3 && structdim1 == 1 && spacedim == 3)
805 if (n_vertices0 == 8 && n_vertices1 == 2)
812 else if constexpr (structdim0 == 3 && structdim1 == 2 && spacedim == 3)
814 if (n_vertices0 == 8 && n_vertices1 == 4)
821 else if constexpr (structdim0 == 3 && structdim1 == 3 && spacedim == 3)
823 if (n_vertices0 == 8 && n_vertices1 == 8)
840 template <
int structdim0,
int structdim1,
int spacedim>
841 std::vector<std::array<Point<spacedim>, structdim1 + 1>>
850 ReferenceCells::get_hypercube<structdim0>().n_vertices(),
853 ReferenceCells::get_hypercube<structdim1>().n_vertices(),
856 const auto &vertices0 =
857 CGALWrappers::get_vertices_in_cgal_order(cell0, mapping0);
858 const auto &vertices1 =
859 CGALWrappers::get_vertices_in_cgal_order(cell1, mapping1);
861 return compute_intersection_of_cells<structdim0, structdim1, spacedim>(
862 vertices0, vertices1, tol);
865# include "intersections.inst"
875template <
int structdim0,
880std::vector<std::array<Point<spacedim>, structdim1 + 1>>
892template <
int structdim0,
int structdim1,
int spacedim>
893std::vector<std::array<Point<spacedim>, structdim1 + 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)
#define DEAL_II_NOT_IMPLEMENTED()
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::optional< std::variant< CGALPoint3, CGALSegment3 > > compute_intersection_tetra_segment(const ArrayView< const Point< 3 > > &tetrahedron, const ArrayView< const Point< 3 > > &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::optional< std::variant< CGALPoint3, CGALSegment3, CGALTriangle3, std::vector< CGALPoint3 > > > compute_intersection_tetra_triangle(const ArrayView< const Point< 3 > > &tetrahedron, const ArrayView< const Point< 3 > > &triangle)
std::optional< std::variant< CGALPoint2, CGALSegment2 > > compute_intersection_triangle_segment(const ArrayView< const Point< 2 > > &triangle, const ArrayView< const Point< 2 > > &segment)
std::optional< std::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 >, 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
const T * get_if_(const std::variant< Types... > *v)
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
std::vector< std::array< Point< spacedim >, structdim1+1 > > compute_intersection_of_cells(const typename Triangulation< structdim0, spacedim >::cell_iterator &cell0, const typename Triangulation< structdim1, spacedim >::cell_iterator &cell1, const Mapping< structdim0, spacedim > &mapping0, const Mapping< structdim1, spacedim > &mapping1, const double tol=1e-9)
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
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