15#include <igl/boundary_facets.h>
16#include <mmg/libmmg.h>
31 bool orient_simplices(
const Eigen::MatrixXd &vertices, Eigen::MatrixXi &elements)
33 for (
int e = 0;
e < elements.rows(); ++
e)
36 if (elements.cols() == 3)
45 assert(elements.cols() == 4);
55 logger().error(
"MMG produced a degenerate simplex {}", e);
59 std::swap(elements(e, 1), elements(e, 2));
72 SimplexMesh make_simplex_mesh(
const Eigen::MatrixXd &
V,
const Eigen::MatrixXi &
F,
const Eigen::MatrixXi &T)
75 M.V.resize(
V.rows(), 3);
76 for (
int i = 0; i <
V.rows(); ++i)
80 M.V(i, 2) = (
V.cols() == 2 ? 0 :
V(i, 2));
82 if (
F.cols() != 3 &&
F.rows() != 0)
83 throw std::runtime_error(
"Mesh faces not supported");
85 M.F.conservativeResize(
F.rows(), 3);
86 if (
T.cols() != 4 &&
T.rows() != 0)
87 throw std::runtime_error(
"Mesh cells not supported");
89 M.T.conservativeResize(
T.rows(), 4);
93 bool mmg_to_simplex_mesh(
const MMG5_pMesh mmg, SimplexMesh &M)
95 logger().trace(
"converting MMG5_pMesh to Eigen ...");
99 assert(mmg->dim == 2 || mmg->dim == 3);
100 M.V.resize(mmg->np, 3);
101 for (
int v = 0; v < mmg->np; ++v)
103 for (
int d = 0; d < mmg->dim; ++d)
105 M.V(v, d) = mmg->point[v + 1].c[d];
111 M.F.resize(mmg->nt, 3);
112 for (
int t = 0; t < mmg->nt; ++t)
114 M.F(t, 0) = mmg->tria[t + 1].v[0] - 1;
115 M.F(t, 1) = mmg->tria[t + 1].v[1] - 1;
116 M.F(t, 2) = mmg->tria[t + 1].v[2] - 1;
118 M.T.resize(mmg->dim == 3 ? mmg->ne : 0, 4);
119 for (
int c = 0; c <
M.T.rows(); ++c)
121 M.T(c, 0) = mmg->tetra[c + 1].v[0] - 1;
122 M.T(c, 1) = mmg->tetra[c + 1].v[1] - 1;
123 M.T(c, 2) = mmg->tetra[c + 1].v[2] - 1;
124 M.T(c, 3) = mmg->tetra[c + 1].v[3] - 1;
130 bool simplex_mesh_to_mmg3d(
const SimplexMesh &M, MMG5_pMesh &mmg, MMG5_pSol &sol)
132 logger().trace(
"converting Eigen mesh to MMG5_pMesh ...");
134 MMG3D_Init_mesh(MMG5_ARG_start, MMG5_ARG_ppMesh, &mmg, MMG5_ARG_ppMet, &sol, MMG5_ARG_end);
136 if (MMG3D_Set_meshSize(mmg, (
int)
M.V.rows(), (
int)
M.T.rows(), 0,
142 logger().error(
"failed to MMG3D_Set_meshSize");
146 for (
int v = 0; v < mmg->np; ++v)
148 for (
int d = 0; d < 3; ++d)
150 mmg->point[v + 1].c[d] =
M.V(v, d);
153 for (
int t = 0; t < mmg->nt; ++t)
155 mmg->tria[t + 1].v[0] =
M.F(t, 0) + 1;
156 mmg->tria[t + 1].v[1] =
M.F(t, 1) + 1;
157 mmg->tria[t + 1].v[2] =
M.F(t, 2) + 1;
159 for (
int c = 0; c < mmg->ne; ++c)
161 mmg->tetra[c + 1].v[0] =
M.T(c, 0) + 1;
162 mmg->tetra[c + 1].v[1] =
M.T(c, 1) + 1;
163 mmg->tetra[c + 1].v[2] =
M.T(c, 2) + 1;
164 mmg->tetra[c + 1].v[3] =
M.T(c, 3) + 1;
167 if (MMG3D_Set_solSize(mmg, sol, MMG5_Vertex, (
int)
M.V.rows(), MMG5_Scalar) != 1)
169 logger().error(
"failed to MMG3D_Set_solSize");
172 for (
int v = 0; v <
M.V.rows(); ++v)
176 if (MMG3D_Chk_meshData(mmg, sol) != 1)
178 logger().error(
"error in mmg: inconsistent mesh and sol");
182 MMG3D_Set_handGivenMesh(mmg);
187 bool simplex_mesh_to_mmg2d(
const SimplexMesh &M, MMG5_pMesh &mmg, MMG5_pSol &sol)
189 MMG2D_Init_mesh(MMG5_ARG_start, MMG5_ARG_ppMesh, &mmg, MMG5_ARG_ppMet, &sol, MMG5_ARG_end);
190 if (!MMG2D_Set_meshSize(mmg,
M.V.rows(),
M.F.rows(), 0, 0))
192 logger().error(
"mmg2d_remesh: failed to allocate the MMG mesh");
196 for (
int v = 0; v < mmg->np; ++v)
198 mmg->point[v + 1].c[0] =
M.V(v, 0);
199 mmg->point[v + 1].c[1] =
M.V(v, 1);
201 for (
int t = 0; t < mmg->nt; ++t)
204 mmg->tria[t + 1].v[0] =
M.F(t, 0) + 1;
205 mmg->tria[t + 1].v[1] =
M.F(t, 1) + 1;
206 mmg->tria[t + 1].v[2] =
M.F(t, 2) + 1;
209 if (!MMG2D_Set_solSize(mmg, sol, MMG5_Vertex,
M.V.rows(), MMG5_Scalar))
211 logger().error(
"failed to MMG2D_Set_solSize");
215 for (
int v = 0; v <
M.V.rows(); ++v)
218 if (!MMG2D_Chk_meshData(mmg, sol))
220 logger().error(
"error in mmg: inconsistent mesh and sol");
226 void mmg2d_free(MMG5_pMesh mmg, MMG5_pSol sol)
228 MMG2D_Free_all(MMG5_ARG_start,
229 MMG5_ARG_ppMesh, &mmg, MMG5_ARG_ppMet, &sol, MMG5_ARG_end);
232 void mmg3d_free(MMG5_pMesh mmg, MMG5_pSol sol)
234 MMG3D_Free_all(MMG5_ARG_start,
235 MMG5_ARG_ppMesh, &mmg, MMG5_ARG_ppMet, &sol, MMG5_ARG_end);
239 bool mmg2d_tri_remesh(
240 const SimplexMesh &M,
241 const Eigen::VectorXd *metric,
242 const std::vector<int> *pinned_vertices,
244 const MmgOptions &opt)
246 MMG5_pMesh mesh =
nullptr;
247 MMG5_pSol met =
nullptr;
248 bool ok = simplex_mesh_to_mmg2d(M, mesh, met);
251 logger().error(
"mmg2d_remesh: failed to convert mesh to MMG5_pMesh");
252 mmg2d_free(mesh, met);
257 if (pinned_vertices !=
nullptr)
259 for (
const int v : *pinned_vertices)
261 assert(v >= 0 && v <
M.V.rows());
262 MMG2D_Set_requiredVertex(mesh, v + 1);
267 MMG2D_Set_dparameter(mesh, met, MMG2D_DPARAM_angleDetection, opt.angle_value);
268 if (opt.enable_anisotropy)
270 MMG2D_Set_solSize(mesh, met, MMG5_Vertex, 0, MMG5_Tensor);
272 const bool with_metric = opt.metric_attribute !=
"no_metric";
273 if (opt.hsiz == 0. || with_metric)
275 if (with_metric || !opt.optim)
277 MMG2D_Set_dparameter(mesh, met, MMG2D_DPARAM_hmin, opt.hmin);
278 MMG2D_Set_dparameter(mesh, met, MMG2D_DPARAM_hmax, opt.hmax);
288 MMG2D_Set_dparameter(mesh, met, MMG2D_DPARAM_hsiz, opt.hsiz);
290 MMG2D_Set_dparameter(mesh, met, MMG2D_DPARAM_hausd, opt.hausd);
291 MMG2D_Set_dparameter(mesh, met, MMG2D_DPARAM_hgrad, opt.hgrad);
292 MMG2D_Set_iparameter(mesh, met, MMG2D_IPARAM_angle,
int(opt.angle_detection));
293 MMG2D_Set_iparameter(mesh, met, MMG2D_IPARAM_noswap,
int(opt.noswap));
294 MMG2D_Set_iparameter(mesh, met, MMG2D_IPARAM_noinsert,
int(opt.noinsert));
295 MMG2D_Set_iparameter(mesh, met, MMG2D_IPARAM_nomove,
int(opt.nomove));
296 MMG2D_Set_iparameter(mesh, met, MMG2D_IPARAM_nosurf,
int(opt.nosurf));
297 MMG2D_Set_iparameter(mesh, met, MMG2D_IPARAM_optim,
int(opt.optim));
300 if (metric ==
nullptr)
302 logger().error(
"mmg2D_remesh: {} is not a vertex attribute, cancel", opt.metric_attribute);
303 mmg2d_free(mesh, met);
306 for (
int v = 0; v <
M.V.rows(); ++v)
308 met->m[v + 1] = (*metric)(v);
312 int ier = MMG2D_mmg2dlib(mesh, met);
313 if (ier != MMG5_SUCCESS)
315 logger().error(
"mmg2d_remesh: failed to remesh");
316 mmg2d_free(mesh, met);
320 ok = mmg_to_simplex_mesh(mesh, M_out);
322 mmg2d_free(mesh, met);
327 bool mmg3d_tet_remesh(
328 const SimplexMesh &M,
329 const Eigen::VectorXd *metric,
330 const std::vector<int> *pinned_vertices,
332 const MmgOptions &opt)
334 MMG5_pMesh mesh =
nullptr;
335 MMG5_pSol met =
nullptr;
336 bool ok = simplex_mesh_to_mmg3d(M, mesh, met);
339 logger().error(
"mmg3d_remesh: failed to convert mesh to MMG5_pMesh");
340 mmg3d_free(mesh, met);
345 std::vector<bool> is_pinned(
M.V.rows(),
false);
346 if (pinned_vertices !=
nullptr)
348 for (
const int v : *pinned_vertices)
350 assert(v >= 0 && v <
M.V.rows());
352 MMG3D_Set_requiredVertex(mesh, v + 1);
355 for (
int f = 0;
f <
M.F.rows(); ++
f)
357 if (is_pinned[
M.F(f, 0)]
358 && is_pinned[
M.F(f, 1)]
359 && is_pinned[
M.F(f, 2)])
361 MMG3D_Set_requiredTriangle(mesh, f + 1);
366 MMG3D_Set_dparameter(mesh, met, MMG3D_DPARAM_angleDetection, opt.angle_value);
367 if (opt.enable_anisotropy)
369 MMG3D_Set_solSize(mesh, met, MMG5_Vertex, 0, MMG5_Tensor);
371 const bool with_metric = opt.metric_attribute !=
"no_metric";
372 if (opt.hsiz == 0. || with_metric)
374 if (with_metric || !opt.optim)
376 MMG3D_Set_dparameter(mesh, met, MMG3D_DPARAM_hmin, opt.hmin);
377 MMG3D_Set_dparameter(mesh, met, MMG3D_DPARAM_hmax, opt.hmax);
387 MMG3D_Set_dparameter(mesh, met, MMG3D_DPARAM_hsiz, opt.hsiz);
389 MMG3D_Set_dparameter(mesh, met, MMG3D_DPARAM_hausd, opt.hausd);
390 MMG3D_Set_dparameter(mesh, met, MMG3D_DPARAM_hgrad, opt.hgrad);
391 MMG3D_Set_iparameter(mesh, met, MMG3D_IPARAM_angle,
int(opt.angle_detection));
392 MMG3D_Set_iparameter(mesh, met, MMG3D_IPARAM_noswap,
int(opt.noswap));
393 MMG3D_Set_iparameter(mesh, met, MMG3D_IPARAM_noinsert,
int(opt.noinsert));
394 MMG3D_Set_iparameter(mesh, met, MMG3D_IPARAM_nomove,
int(opt.nomove));
395 MMG3D_Set_iparameter(mesh, met, MMG3D_IPARAM_nosurf,
int(opt.nosurf));
396 MMG3D_Set_iparameter(mesh, met, MMG3D_IPARAM_opnbdy,
int(opt.opnbdy));
397 MMG3D_Set_iparameter(mesh, met, MMG3D_IPARAM_optim,
int(opt.optim));
398 MMG3D_Set_iparameter(mesh, met, MMG3D_IPARAM_optimLES,
int(opt.optimLES));
401 if (metric ==
nullptr)
403 logger().error(
"mmg3D_remesh: {} is not a vertex attribute, cancel", opt.metric_attribute);
404 mmg3d_free(mesh, met);
407 for (
int v = 0; v <
M.V.rows(); ++v)
409 met->m[v + 1] = (*metric)(v);
413 int ier = MMG3D_mmg3dlib(mesh, met);
414 if (ier != MMG5_SUCCESS)
416 logger().error(
"mmg3d_remesh: failed to remesh");
417 mmg3d_free(mesh, met);
421 ok = mmg_to_simplex_mesh(mesh, M_out);
423 mmg3d_free(mesh, met);
432 const Eigen::MatrixXd &
V,
433 const Eigen::MatrixXi &
F,
437 const std::vector<int> *pinned_vertices)
439 assert(
V.cols() == 2);
440 assert(
F.cols() == 3);
441 const SimplexMesh
M = make_simplex_mesh(
V,
F, Eigen::MatrixXi(0, 4));
443 if (!mmg2d_tri_remesh(M,
nullptr, pinned_vertices, M_out, opt))
448 OV.conservativeResize(OV.rows(), 2);
450 return orient_simplices(OV, OF);
453 void remesh_adaptive_2d(
const Eigen::MatrixXd &
V,
const Eigen::MatrixXi &
F,
const Eigen::VectorXd &S,
454 Eigen::MatrixXd &OV, Eigen::MatrixXi &OF, MmgOptions opt)
456 assert(
V.cols() == 2 ||
V.cols() == 3);
457 assert(
V.rows() == S.size());
458 assert(
F.cols() == 3);
459 const SimplexMesh
M = make_simplex_mesh(
V,
F, Eigen::MatrixXi(0, 4));
462 opt.metric_attribute =
"scalar";
463 if (!mmg2d_tri_remesh(M, &S,
nullptr, M_out, opt))
467 OV.conservativeResize(OV.rows(), 2);
468 orient_simplices(OV, OF);
472 const Eigen::MatrixXd &
V,
473 const Eigen::MatrixXi &T,
478 const std::vector<int> *pinned_vertices)
480 assert(
V.cols() == 3);
481 assert(
T.cols() == 4);
482 Eigen::MatrixXi boundary;
483 igl::boundary_facets(T, boundary);
484 const SimplexMesh
M = make_simplex_mesh(
V, boundary, T);
486 if (!mmg3d_tet_remesh(M,
nullptr, pinned_vertices, M_out, opt))
492 return orient_simplices(OV, OT);
495 void remesh_adaptive_3d(
const Eigen::MatrixXd &
V,
const Eigen::MatrixXi &T,
const Eigen::VectorXd &S,
496 Eigen::MatrixXd &OV, Eigen::MatrixXi &OF, Eigen::MatrixXi &OT, MmgOptions opt)
498 assert(
V.cols() == 3);
499 assert(
V.rows() == S.size());
500 const SimplexMesh
M = make_simplex_mesh(
V, Eigen::MatrixXi(0, 3), T);
504 opt.metric_attribute =
"scalar";
507 if (!mmg3d_tet_remesh(M, &S,
nullptr, M_out, opt))
514 orient_simplices(OV, OT);
double tetrahedron_volume(const Eigen::Vector3d &a, const Eigen::Vector3d &b, const Eigen::Vector3d &c, const Eigen::Vector3d &d)
Compute the signed volume of a tetrahedron defined by four points.
double triangle_area_2D(const Eigen::Vector2d &a, const Eigen::Vector2d &b, const Eigen::Vector2d &c)
Compute the signed area of a 2D triangle defined by three points.
spdlog::logger & logger()
Retrieves the current logger.