v0.16.3
Loading...
Searching...
No Matches
CgalImplicitMesh.cpp
Go to the documentation of this file.
1#include "CGALTools.hpp"
2#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
3#include <CGAL/Labeled_mesh_domain_3.h>
4#include <CGAL/Mesh_complex_3_in_triangulation_3.h>
5#include <CGAL/Mesh_criteria_3.h>
6#include <CGAL/Mesh_triangulation_3.h>
7#include <CGAL/make_mesh_3.h>
8#include <cmath>
9#include <limits>
10#include <map>
11#include <stdexcept>
12
13namespace MoFEM {
16 const std::function<double(const std::array<double, 3> &)> &sdf,
17 const ImplicitMeshSettings &s) const {
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())}) -
38 s.isovalue;
39 if (!std::isfinite(v))
40 throw std::invalid_argument(
41 "Implicit function returned a non-finite value");
42 return v;
43 };
44 auto domain = Domain::create_implicit_mesh_domain(
45 function,
46 K::Sphere_3(K::Point_3(s.centre[0], s.centre[1], s.centre[2]),
47 s.radius * s.radius),
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");
61 TetMeshData next;
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())
66 return it->second;
67 if (next.points.size() >=
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())});
74 next.vertexIds.push_back(n + 1);
75 indices.emplace(v, n);
76 return n;
77 };
78 for (auto c = mesh.cells_in_complex_begin(); c != mesh.cells_in_complex_end();
79 ++c) {
80 next.cells.push_back({index(c->vertex(0)), index(c->vertex(1)),
81 index(c->vertex(2)), index(c->vertex(3))});
82 next.cellLabels.push_back(mesh.subdomain_index(c));
83 }
84 for (auto f = mesh.facets_in_complex_begin();
85 f != mesh.facets_in_complex_end(); ++f) {
86 std::array<int, 3> face;
87 int n = 0;
88 for (int d = 0; d != 4; ++d)
89 if (d != f->second)
90 face[n++] = index(f->first->vertex(d));
91 next.boundary.push_back(face);
92 // A single implicit region has one boundary patch.
93 next.surfaceLabels.push_back(1);
94 }
95 result = std::move(next);
96}
97} // namespace MoFEM
std::map< EntityHandle, int > index
TetMeshData result
CGAL algorithms independent of the MoFEM/MOAB database.
WorkMesh mesh
std::string type
It it
Definition Schur.cpp:2568
const double c
speed of light (cm/ns)
const double v
phase velocity of light in medium (cm/ns)
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
Definition sdf.py:1
void createImplicitMesh(TetMeshData &result, const std::function< double(const std::array< double, 3 > &)> &sdf, const ImplicitMeshSettings &settings) const
std::vector< std::array< int, 3 > > boundary
Definition CGALTools.hpp:20
std::vector< int > cellLabels
Definition CGALTools.hpp:21
std::vector< std::array< int, 4 > > cells
Zero-based point indices.
Definition CGALTools.hpp:19
std::vector< std::array< double, 3 > > points
Definition CGALTools.hpp:17
std::vector< int > vertexIds
Definition CGALTools.hpp:18
std::vector< int > surfaceLabels
Definition CGALTools.hpp:21