15#ifndef dealii_cgal_utilities_h
16#define dealii_cgal_utilities_h
24#ifdef DEAL_II_WITH_CGAL
29# include <boost/hana.hpp>
31# include <CGAL/Cartesian.h>
32# include <CGAL/Complex_2_in_triangulation_3.h>
33# include <CGAL/Exact_predicates_exact_constructions_kernel.h>
34# include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
35# include <CGAL/Kernel_traits.h>
36# include <CGAL/Mesh_complex_3_in_triangulation_3.h>
37# include <CGAL/Mesh_criteria_3.h>
38# include <CGAL/Mesh_triangulation_3.h>
42# include <CGAL/Polygon_mesh_processing/corefinement.h>
44# include <CGAL/Polygon_mesh_processing/measure.h>
45# include <CGAL/Polygon_mesh_processing/remesh.h>
46# include <CGAL/Polygon_mesh_processing/triangulate_faces.h>
47# include <CGAL/Polyhedral_mesh_domain_with_features_3.h>
48# include <CGAL/Simple_cartesian.h>
49# include <CGAL/Surface_mesh.h>
50# include <CGAL/Triangulation_3.h>
51# include <CGAL/boost/graph/copy_face_graph.h>
52# include <CGAL/boost/graph/selection.h>
53# include <CGAL/convex_hull_3.h>
54# include <CGAL/make_mesh_3.h>
55# include <CGAL/make_surface_mesh.h>
60# include <type_traits>
79# ifdef CGAL_CONCURRENT_MESH_3
140 template <
typename C3t3>
143 CGAL::Surface_mesh<typename C3t3::Point::Point> &surface_mesh,
190 template <
typename CGALPo
intType>
193 const CGAL::Surface_mesh<CGALPointType> &surface_mesh_1,
194 const CGAL::Surface_mesh<CGALPointType> &surface_mesh_2,
196 CGAL::Surface_mesh<CGALPointType> &output_surface_mesh);
209 template <
typename CGALTriangulationType>
212 const unsigned int degree);
229 template <
int dim0,
int dim1,
int spacedim>
232 const typename ::Triangulation<dim0, spacedim>::cell_iterator &cell0,
233 const typename ::Triangulation<dim1, spacedim>::cell_iterator &cell1,
234 const unsigned int degree,
258 template <
int dim0,
int dim1,
int spacedim>
261 const typename ::Triangulation<dim0, spacedim>::cell_iterator &cell0,
262 const typename ::Triangulation<dim1, spacedim>::cell_iterator &cell1,
263 const unsigned int degree,
295 template <
typename CGALPo
intType>
305 template <
typename C3t3>
308 CGAL::Surface_mesh<typename C3t3::Point::Point> &surface_mesh,
310 const AdditionalData<3> &data)
312 using CGALPointType =
typename C3t3::Point::Point;
313 Assert(CGAL::is_closed(surface_mesh),
314 ExcMessage(
"The surface mesh must be closed."));
316 using K =
typename CGAL::Kernel_traits<CGALPointType>::Kernel;
317 using Mesh_domain = CGAL::Polyhedral_mesh_domain_with_features_3<
319 CGAL::Surface_mesh<CGALPointType>>;
320 using Tr =
typename CGAL::
321 Mesh_triangulation_3<Mesh_domain, CGAL::Default, ConcurrencyTag>::type;
322 using Mesh_criteria = CGAL::Mesh_criteria_3<Tr>;
324 CGAL::Polygon_mesh_processing::triangulate_faces(surface_mesh);
325 Mesh_domain domain(surface_mesh);
326 domain.detect_features();
327 Mesh_criteria criteria(CGAL::parameters::facet_size = data.facet_size,
328 CGAL::parameters::facet_angle = data.facet_angle,
329 CGAL::parameters::facet_distance =
331 CGAL::parameters::cell_radius_edge_ratio =
332 data.cell_radius_edge_ratio,
333 CGAL::parameters::cell_size = data.cell_size);
337 CGAL::parameters::no_perturb(),
338 CGAL::parameters::no_exude());
343 template <
typename CGALPo
intType>
346 const CGAL::Surface_mesh<CGALPointType> &surface_mesh_1,
347 const CGAL::Surface_mesh<CGALPointType> &surface_mesh_2,
349 CGAL::Surface_mesh<CGALPointType> &output_surface_mesh)
351 Assert(output_surface_mesh.is_empty(),
353 "output_surface_mesh must be empty upon calling this function"));
354 Assert(CGAL::is_closed(surface_mesh_1),
356 "The input surface_mesh_1 must be a closed surface mesh."));
357 Assert(CGAL::is_closed(surface_mesh_2),
359 "The input surface_mesh_2 must be a closed surface mesh."));
360 Assert(CGAL::is_triangle_mesh(surface_mesh_1),
361 ExcMessage(
"The first CGAL mesh must be triangulated."));
362 Assert(CGAL::is_triangle_mesh(surface_mesh_2),
363 ExcMessage(
"The second CGAL mesh must be triangulated."));
366 auto surf_1 = surface_mesh_1;
367 auto surf_2 = surface_mesh_2;
368 namespace PMP = CGAL::Polygon_mesh_processing;
369 switch (boolean_operation)
372 res = PMP::corefine_and_compute_union(surf_1,
374 output_surface_mesh);
377 res = PMP::corefine_and_compute_intersection(surf_1,
379 output_surface_mesh);
382 res = PMP::corefine_and_compute_difference(surf_1,
384 output_surface_mesh);
387 PMP::corefine(surf_1,
389 output_surface_mesh = std::move(surf_1);
393 output_surface_mesh.clear();
398 ExcMessage(
"The boolean operation was not successfully computed."));
403 template <
typename CGALTriangulationType>
406 const unsigned int degree)
409 Assert(CGALTriangulationType::Point::Ambient_dimension::value == 3,
412 ExcMessage(
"The degree of the Quadrature formula is not positive."));
414 constexpr int spacedim =
415 CGALTriangulationType::Point::Ambient_dimension::value;
416 std::vector<std::array<::Point<spacedim>, spacedim + 1>>
419 std::array<::Point<spacedim>, spacedim + 1>
simplex;
420 for (
const auto &f : tria.finite_cell_handles())
422 for (
unsigned int i = 0; i < (spacedim + 1); ++i)
425 cgal_point_to_dealii_point<spacedim>(f->vertex(i)->point());
428 vec_of_simplices.push_back(simplex);
436 template <
int dim0,
int dim1,
int spacedim>
439 const typename ::Triangulation<dim0, spacedim>::cell_iterator &cell0,
440 const typename ::Triangulation<dim1, spacedim>::cell_iterator &cell1,
441 const unsigned int degree,
446 Assert(dim0 == 3 && dim1 == 3 && spacedim == 3,
462 cell0, cell1, degree, mapping0, mapping1);
466 using K = CGAL::Exact_predicates_inexact_constructions_kernel;
467 using CGALPoint = CGAL::Point_3<K>;
468 using CGALTriangulation = CGAL::Triangulation_3<K>;
469 CGAL::Surface_mesh<CGALPoint> surface_1, surface_2, out_surface;
473 CGAL::Polygon_mesh_processing::triangulate_faces(surface_1);
474 CGAL::Polygon_mesh_processing::triangulate_faces(surface_2);
475 Assert(CGAL::is_triangle_mesh(surface_1) &&
476 CGAL::is_triangle_mesh(surface_2),
477 ExcMessage(
"The surface must be a triangle mesh."));
480 CGALTriangulation tria;
481 tria.insert(out_surface.points().begin(), out_surface.points().end());
488 template <
int dim0,
int dim1,
int spacedim>
491 const typename ::Triangulation<dim0, spacedim>::cell_iterator &cell0,
492 const typename ::Triangulation<dim1, spacedim>::cell_iterator &cell1,
493 const unsigned int degree,
497 Assert(dim0 == 3 && dim1 == 3 && spacedim == 3,
499 using K = CGAL::Exact_predicates_inexact_constructions_kernel;
500 using CGALPoint = CGAL::Point_3<K>;
501 using CGALTriangulation = CGAL::Triangulation_3<K>;
503 CGAL::Surface_mesh<CGALPoint> surface_1, surface_2, out_surface;
507 CGAL::Polygon_mesh_processing::triangulate_faces(surface_1);
508 CGAL::Polygon_mesh_processing::triangulate_faces(surface_2);
514 CGAL::Surface_mesh<CGALPoint> dummy;
515 CGALTriangulation tr;
516 CGAL::convex_hull_3(out_surface.points().begin(),
517 out_surface.points().end(),
519 tr.insert(dummy.points().begin(), dummy.points().end());
525 template <
typename CGALPo
intType>
528 const AdditionalData<3> &data)
530 using K = CGAL::Exact_predicates_inexact_constructions_kernel;
531 using Mesh_domain = CGAL::Polyhedral_mesh_domain_with_features_3<K>;
533 using Polyhedron = CGAL::Mesh_polyhedron_3<K>::type;
535 using Tr = CGAL::Mesh_triangulation_3<Mesh_domain>::type;
537 CGAL::Mesh_complex_3_in_triangulation_3<Tr,
538 Mesh_domain::Corner_index,
539 Mesh_domain::Curve_index>;
541 using Mesh_criteria = CGAL::Mesh_criteria_3<Tr>;
544 CGAL::copy_face_graph(cgal_mesh, poly);
547 std::vector<Polyhedron *> poly_ptrs_vector(1, &poly);
551 Mesh_domain domain(poly_ptrs_vector.begin(), poly_ptrs_vector.end());
553 domain.detect_features();
556 const double default_value_edge_size = std::numeric_limits<double>::max();
557 if (data.edge_size > 0 &&
558 std::abs(data.edge_size - default_value_edge_size) > 1e-12)
560 Mesh_criteria criteria(CGAL::parameters::edge_size = data.edge_size,
561 CGAL::parameters::facet_size = data.facet_size,
562 CGAL::parameters::facet_angle = data.facet_angle,
563 CGAL::parameters::facet_distance =
565 CGAL::parameters::cell_radius_edge_ratio =
566 data.cell_radius_edge_ratio,
567 CGAL::parameters::cell_size = data.cell_size);
569 C3t3 c3t3 = CGAL::make_mesh_3<C3t3>(domain,
571 CGAL::parameters::no_perturb(),
572 CGAL::parameters::no_exude());
574 CGAL::facets_in_complex_3_to_triangle_mesh(c3t3, cgal_mesh);
576 else if (
std::abs(data.edge_size - default_value_edge_size) <= 1e-12)
578 Mesh_criteria criteria(CGAL::parameters::facet_size = data.facet_size,
579 CGAL::parameters::facet_angle = data.facet_angle,
580 CGAL::parameters::facet_distance =
582 CGAL::parameters::cell_radius_edge_ratio =
583 data.cell_radius_edge_ratio,
584 CGAL::parameters::cell_size = data.cell_size);
586 C3t3 c3t3 = CGAL::make_mesh_3<C3t3>(domain,
588 CGAL::parameters::no_perturb(),
589 CGAL::parameters::no_exude());
591 CGAL::facets_in_complex_3_to_triangle_mesh(c3t3, cgal_mesh);
607 template <
int spacedim>
609 resort_dealii_vertices_to_cgal_order(
const unsigned int structdim,
617 if constexpr (spacedim == 2)
632 template <
int dim,
int spacedim>
633 std::vector<Point<spacedim>>
634 get_vertices_in_cgal_order(
635 const typename ::Triangulation<dim, spacedim>::cell_iterator &cell,
639 const unsigned int n_vertices = cell->n_vertices();
644 std::vector<Point<spacedim>> ordered_vertices(n_vertices);
647 ordered_vertices.begin());
649 resort_dealii_vertices_to_cgal_order(dim, ordered_vertices);
651 return ordered_vertices;
664 template <
int dim,
int spacedim>
665 std::vector<Point<spacedim>>
666 get_vertices_in_cgal_order(
667 const typename ::Triangulation<dim, spacedim>::cell_iterator &cell,
668 const unsigned int face_no,
672 const unsigned int n_vertices = cell->face(face_no)->n_vertices();
678 std::vector<Point<spacedim>> ordered_vertices(n_vertices);
679 std::copy_n(mapping.
get_vertices(cell, face_no).begin(),
681 ordered_vertices.begin());
683 resort_dealii_vertices_to_cgal_order(dim - 1, ordered_vertices);
685 return ordered_vertices;
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
Quadrature< spacedim > mapped_quadrature(const std::vector< std::array< Point< spacedim >, dim+1 > > &simplices) const
static ReferenceCell n_vertices_to_type(const int dim, const unsigned int n_vertices)
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
#define DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define DEAL_II_ASSERT_UNREACHABLE()
const Mapping< dim, spacedim > & get_default_linear_mapping(const Triangulation< dim, spacedim > &triangulation)
void remesh_surface(CGAL::Surface_mesh< CGALPointType > &surface_mesh, const AdditionalData< 3 > &data=AdditionalData< 3 >{})
::Quadrature< CGALTriangulationType::Point::Ambient_dimension::value > compute_quadrature(const CGALTriangulationType &tria, const unsigned int degree)
CGAL::Sequential_tag ConcurrencyTag
CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt K
::Quadrature< spacedim > compute_quadrature_on_boolean_operation(const typename ::Triangulation< dim0, spacedim >::cell_iterator &cell0, const typename ::Triangulation< dim1, spacedim >::cell_iterator &cell1, const unsigned int degree, const BooleanOperation &bool_op, const Mapping< dim0, spacedim > &mapping0=(ReferenceCells::get_hypercube< dim0 >() .template get_default_linear_mapping< dim0, spacedim >()), const Mapping< dim1, spacedim > &mapping1=(ReferenceCells::get_hypercube< dim1 >() .template get_default_linear_mapping< dim1, spacedim >()))
void cgal_surface_mesh_to_cgal_triangulation(CGAL::Surface_mesh< typename C3t3::Point::Point > &surface_mesh, C3t3 &triangulation, const AdditionalData< 3 > &data=AdditionalData< 3 >{})
void dealii_cell_to_cgal_surface_mesh(const typename ::Triangulation< dim, spacedim >::cell_iterator &cell, const ::Mapping< dim, spacedim > &mapping, CGAL::Surface_mesh< CGALPointType > &mesh)
void compute_boolean_operation(const CGAL::Surface_mesh< CGALPointType > &surface_mesh_1, const CGAL::Surface_mesh< CGALPointType > &surface_mesh_2, const BooleanOperation &boolean_operation, CGAL::Surface_mesh< CGALPointType > &output_surface_mesh)
::Quadrature< spacedim > compute_quadrature_on_intersection(const typename ::Triangulation< dim0, spacedim >::cell_iterator &cell0, const typename ::Triangulation< dim1, spacedim >::cell_iterator &cell1, const unsigned int degree, const Mapping< dim0, spacedim > &mapping0, const Mapping< dim1, spacedim > &mapping1)
void simplex(Triangulation< dim, dim > &tria, const std::vector< Point< dim > > &vertices)
constexpr const ReferenceCell Quadrilateral
constexpr const ReferenceCell & get_hypercube()
constexpr const ReferenceCell & get_simplex()
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation