deal.II version 9.7.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
grid_generator_cgal.cc
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2022 - 2025 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
16
19
20#ifdef DEAL_II_WITH_CGAL
21// Functions needed by the CGAL mesh generation utilities are inside
23
24# include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
25# include <CGAL/Labeled_mesh_domain_3.h>
26# include <CGAL/Mesh_triangulation_3.h>
27#endif
28
29
31
32// work around the problem that doxygen for some reason lists all template
33// specializations in this file
34#ifndef DOXYGEN
35
36namespace GridGenerator
37{
38 template <int dim>
39 void
40 implicit_function(Triangulation<dim, 3> &tria,
41 const Function<3> &dealii_implicit_function,
42 const CGALWrappers::AdditionalData<dim> &data,
43 const Point<3> &interior_point,
44 const double &outer_ball_radius)
45 {
46# ifdef DEAL_II_WITH_CGAL
47 Assert(dealii_implicit_function.n_components == 1,
49 "The implicit function must have exactly one component."));
50 Assert(dealii_implicit_function.value(interior_point) < 0,
52 "The implicit function must be negative at the interior point."));
53 Assert(outer_ball_radius > 0,
54 ExcMessage("The outer ball radius must be positive."));
55 Assert(tria.n_active_cells() == 0,
56 ExcMessage("The triangulation must be empty."));
57
58 if constexpr (dim == 3)
59 {
60 using K = CGAL::Exact_predicates_inexact_constructions_kernel;
61 using NumberType = K::FT;
62 using Point_3 = K::Point_3;
63 using Sphere_3 = K::Sphere_3;
64
65 using Mesh_domain = CGAL::Labeled_mesh_domain_3<K>;
66 using Tr =
67 CGAL::Mesh_triangulation_3<Mesh_domain,
68 CGAL::Default,
70 using C3t3 = CGAL::Mesh_complex_3_in_triangulation_3<Tr, int, int>;
71 using Mesh_criteria = CGAL::Mesh_criteria_3<Tr>;
72
73
74 auto cgal_implicit_function = [&](const Point_3 &p) {
75 return NumberType(
76 dealii_implicit_function.value(Point<3>(p.x(), p.y(), p.z())));
77 };
78
79 Mesh_domain domain = Mesh_domain::create_implicit_mesh_domain(
80 cgal_implicit_function,
81 K::Sphere_3(
82 Point_3(interior_point[0], interior_point[1], interior_point[2]),
83 outer_ball_radius * outer_ball_radius));
84
85 Mesh_criteria criteria(CGAL::parameters::facet_size = data.facet_size,
86 CGAL::parameters::facet_angle = data.facet_angle,
87 CGAL::parameters::facet_distance =
88 data.facet_distance,
89 CGAL::parameters::cell_radius_edge_ratio =
91 CGAL::parameters::cell_size = data.cell_size);
92
93 auto cgal_triangulation = CGAL::make_mesh_3<C3t3>(domain, criteria);
94 CGALWrappers::cgal_triangulation_to_dealii_triangulation(
95 cgal_triangulation, tria);
96 }
97 else if constexpr (dim == 2)
98 {
99 // default triangulation for Surface_mesher
100 using Tr = CGAL::Surface_mesh_default_triangulation_3;
101 using C2t3 = CGAL::Complex_2_in_triangulation_3<Tr>;
102 using GT = Tr::Geom_traits;
103 using Sphere_3 = GT::Sphere_3;
104 using Point_3 = GT::Point_3;
105 using FT = GT::FT;
106 using Function = FT (*)(Point_3);
107 using Surface_3 = CGAL::Implicit_surface_3<GT, Function>;
108 using Surface_mesh = CGAL::Surface_mesh<Point_3>;
109
110
111 auto cgal_implicit_function = [&](const Point_3 &p) {
112 return FT(
113 dealii_implicit_function.value(Point<3>(p.x(), p.y(), p.z())));
114 };
115
116 Surface_3 surface(cgal_implicit_function,
117 Sphere_3(Point_3(interior_point[0],
118 interior_point[1],
119 interior_point[2]),
120 outer_ball_radius * outer_ball_radius));
121
122 Tr tr;
123 C2t3 c2t3(tr);
124 Surface_mesh mesh;
125
126 CGAL::Surface_mesh_default_criteria_3<Tr> criteria(data.angular_bound,
127 data.radius_bound,
128 data.distance_bound);
129 CGAL::make_surface_mesh(c2t3,
130 surface,
131 criteria,
132 CGAL::Non_manifold_tag());
133 CGAL::facets_in_complex_2_to_triangle_mesh(c2t3, mesh);
134 CGALWrappers::cgal_surface_mesh_to_dealii_triangulation(mesh, tria);
135 }
136 else
137 {
138 Assert(false, ExcImpossibleInDim(dim));
139 }
140
141# else
142
143 (void)tria;
144 (void)dealii_implicit_function;
145 (void)data;
146 (void)interior_point;
147 (void)outer_ball_radius;
148 AssertThrow(false, ExcMessage("This function needs CGAL to be installed."));
149
150# endif
151 }
152
153
154
155 void
156 surface_mesh_to_volumetric_mesh(const Triangulation<2, 3> &surface_tria,
157 Triangulation<3> &vol_tria,
158 const CGALWrappers::AdditionalData<3> &data)
159 {
160# ifdef DEAL_II_WITH_CGAL
161 Assert(
162 surface_tria.n_cells() > 0,
164 "The input triangulation cannot be empty when calling this function."));
165 Assert(
166 vol_tria.n_cells() == 0,
168 "The output triangulation must be empty when calling this function."));
169 using K = CGAL::Exact_predicates_inexact_constructions_kernel;
170 using Point_3 = K::Point_3;
171
172 using Mesh_domain =
173 CGAL::Polyhedral_mesh_domain_with_features_3<K,
174 CGAL::Surface_mesh<Point_3>>;
175 using Tr = CGAL::Mesh_triangulation_3<Mesh_domain,
176 CGAL::Default,
178 using Mesh_criteria = CGAL::Mesh_criteria_3<Tr>;
179 using C3t3 =
180 CGAL::Mesh_complex_3_in_triangulation_3<Tr,
181 Mesh_domain::Corner_index,
182 Mesh_domain::Curve_index>;
183
184 CGAL::Surface_mesh<Point_3> mesh;
185 // This function "fills" the missing arrow of the following diagram.
186 // Tria<2,3> Tria<3>
187 // | ^
188 // | |
189 // | |
190 // | |
191 // V |
192 // CGAL::Surface_mesh -----------> CGAL::C3t3
194 CGAL::Polygon_mesh_processing::triangulate_faces(mesh);
195 CGAL::Polygon_mesh_processing::stitch_borders(mesh);
196 Mesh_domain domain(mesh);
197 domain.detect_features();
198 Mesh_criteria criteria(CGAL::parameters::facet_size = data.facet_size,
199 CGAL::parameters::facet_angle = data.facet_angle,
200 CGAL::parameters::facet_distance =
201 data.facet_distance,
202 CGAL::parameters::cell_radius_edge_ratio =
204 CGAL::parameters::cell_size = data.cell_size);
205 const auto cgal_triangulation = CGAL::make_mesh_3<C3t3>(domain, criteria);
206 CGALWrappers::cgal_triangulation_to_dealii_triangulation(cgal_triangulation,
207 vol_tria);
208
209# else
210
211 (void)surface_tria;
212 (void)vol_tria;
213 (void)data;
214 AssertThrow(false, ExcMessage("This function needs CGAL to be installed."));
215
216# endif
217 }
218} // namespace GridGenerator
219
220// explicit instantiations
221# include "grid/grid_generator_cgal.inst"
222
223#endif // DOXYGEN
224
const unsigned int n_components
Definition function.h:164
virtual RangeNumberType value(const Point< dim > &p, const unsigned int component=0) const
unsigned int n_active_cells() const
unsigned int n_cells() const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:40
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:41
#define Assert(cond, exc)
static ::ExceptionBase & ExcImpossibleInDim(int arg1)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
void dealii_tria_to_cgal_surface_mesh(const ::Triangulation< dim, spacedim > &triangulation, CGAL::Surface_mesh< CGALPointType > &mesh)
CGAL::Sequential_tag ConcurrencyTag
Definition utilities.h:87
CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt K
CGAL::Surface_mesh< K_exact::Point_3 > Surface_mesh
void surface_mesh_to_volumetric_mesh(const Triangulation< 2, 3 > &surface_tria, Triangulation< 3 > &vol_tria, const CGALWrappers::AdditionalData< 3 > &data=CGALWrappers::AdditionalData< 3 >{})
void implicit_function(Triangulation< dim, 3 > &tria, const Function< 3 > &implicit_function, const CGALWrappers::AdditionalData< dim > &data=CGALWrappers::AdditionalData< dim >{}, const Point< 3 > &interior_point=Point< 3 >(), const double &outer_ball_radius=1.0)