16 const std::function<
double(
const std::array<double, 3> &)> &
sdf,
18 using K = CGAL::Exact_predicates_inexact_constructions_kernel;
19 using Domain = CGAL::Labeled_mesh_domain_3<K>;
20 using Tr = CGAL::Mesh_triangulation_3<Domain, CGAL::Default,
21 CGAL::Sequential_tag>
::type;
22 using Complex = CGAL::Mesh_complex_3_in_triangulation_3<Tr>;
23 for (
double x : {
s.radius,
s.facetAngle,
s.facetSize,
s.facetDistance,
24 s.cellSize,
s.cellRadiusEdgeRatio,
s.relativeError})
25 if (!std::isfinite(x) || x <= 0)
26 throw std::invalid_argument(
27 "Implicit mesh criteria must be finite and positive");
28 for (
auto x :
s.centre)
29 if (!std::isfinite(x))
30 throw std::invalid_argument(
"Non-finite bounding sphere centre");
31 if (!
sdf || !std::isfinite(
s.isovalue) ||
s.facetAngle > 30 ||
32 s.cellRadiusEdgeRatio <= 2 ||
s.relativeError >= 1 ||
33 !std::isfinite(
s.radius *
s.radius))
34 throw std::invalid_argument(
"Invalid implicit mesh function or criteria");
35 auto function = [&](
const K::Point_3 &p) {
36 const double v =
sdf({CGAL::to_double(p.x()), CGAL::to_double(p.y()),
37 CGAL::to_double(p.z())}) -
39 if (!std::isfinite(
v))
40 throw std::invalid_argument(
41 "Implicit function returned a non-finite value");
44 auto domain = Domain::create_implicit_mesh_domain(
46 K::Sphere_3(K::Point_3(
s.centre[0],
s.centre[1],
s.centre[2]),
48 CGAL::parameters::relative_error_bound(
s.relativeError));
49 CGAL::Mesh_criteria_3<Tr> criteria(
50 CGAL::parameters::facet_angle(
s.facetAngle)
51 .facet_size(
s.facetSize)
52 .facet_distance(
s.facetDistance)
53 .cell_radius_edge_ratio(
s.cellRadiusEdgeRatio)
54 .cell_size(
s.cellSize));
55 auto mesh = CGAL::make_mesh_3<Complex>(
56 domain, criteria, CGAL::parameters::no_perturb().no_exude(),
57 CGAL::parameters::mesh_3_options(
58 CGAL::parameters::number_of_initial_points(64)));
59 if (!
mesh.number_of_cells_in_complex())
60 throw std::runtime_error(
"Implicit domain produced no tetrahedra");
62 std::map<Tr::Vertex_handle, int> indices;
63 auto index = [&](Tr::Vertex_handle
v) {
64 auto it = indices.find(
v);
65 if (
it != indices.end())
68 static_cast<size_t>(std::numeric_limits<int>::max()))
69 throw std::length_error(
"Implicit mesh exceeds integer index range");
70 const int n = next.
points.size();
71 const auto &p =
v->point().point();
72 next.
points.push_back({CGAL::to_double(p.x()), CGAL::to_double(p.y()),
73 CGAL::to_double(p.z())});
75 indices.emplace(
v,
n);
78 for (
auto c =
mesh.cells_in_complex_begin();
c !=
mesh.cells_in_complex_end();
84 for (
auto f =
mesh.facets_in_complex_begin();
85 f !=
mesh.facets_in_complex_end(); ++
f) {
86 std::array<int, 3> face;
88 for (
int d = 0;
d != 4; ++
d)
90 face[
n++] =
index(
f->first->vertex(
d));