PolyFEM
Loading...
Searching...
No Matches
MMGRemesh.cpp
Go to the documentation of this file.
1// Original source from cellogram (https://github.com/cellogram/cellogram/blob/master/src/cellogram/remesh_adaptive.h)
2// Authors: Tobias Lendenmann, Teseo Schneider, Jérémie Dumas, Marco Tarini
3// License: MIT (https://github.com/cellogram/cellogram/blob/master/LICENSE)
4
5#ifdef POLYFEM_WITH_MMG
6
8#include "MMGRemesh.hpp"
9// #include <cellogram/MeshUtils.h>
10#include <algorithm>
11#include <iomanip>
12#include <cassert>
13#include <stdexcept>
14#include <vector>
15#include <igl/boundary_facets.h>
16#include <mmg/libmmg.h>
17
21//
22// Wrapper for 3D remeshing comes from:
23// https://github.com/mxncr/mmgig
24//
25
26namespace polyfem::mesh
27{
28 namespace
29 {
30 // Flip triangle/tet if there signed volume is negative.
31 bool orient_simplices(const Eigen::MatrixXd &vertices, Eigen::MatrixXi &elements)
32 {
33 for (int e = 0; e < elements.rows(); ++e)
34 {
35 double vol = 0;
36 if (elements.cols() == 3)
37 {
39 vertices.row(elements(e, 0)),
40 vertices.row(elements(e, 1)),
41 vertices.row(elements(e, 2)));
42 }
43 else
44 {
45 assert(elements.cols() == 4);
47 vertices.row(elements(e, 0)),
48 vertices.row(elements(e, 1)),
49 vertices.row(elements(e, 2)),
50 vertices.row(elements(e, 3)));
51 }
52
53 if (vol == 0)
54 {
55 logger().error("MMG produced a degenerate simplex {}", e);
56 return false;
57 }
58 if (vol < 0)
59 std::swap(elements(e, 1), elements(e, 2));
60 }
61 return true;
62 }
63
65 struct SimplexMesh
66 {
67 Eigen::MatrixXd V;
68 Eigen::MatrixXi F;
69 Eigen::MatrixXi T;
70 };
71
72 SimplexMesh make_simplex_mesh(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, const Eigen::MatrixXi &T)
73 {
74 SimplexMesh M;
75 M.V.resize(V.rows(), 3);
76 for (int i = 0; i < V.rows(); ++i)
77 {
78 M.V(i, 0) = V(i, 0);
79 M.V(i, 1) = V(i, 1);
80 M.V(i, 2) = (V.cols() == 2 ? 0 : V(i, 2));
81 }
82 if (F.cols() != 3 && F.rows() != 0)
83 throw std::runtime_error("Mesh faces not supported");
84 M.F = F;
85 M.F.conservativeResize(F.rows(), 3);
86 if (T.cols() != 4 && T.rows() != 0)
87 throw std::runtime_error("Mesh cells not supported");
88 M.T = T;
89 M.T.conservativeResize(T.rows(), 4);
90 return M;
91 }
92
93 bool mmg_to_simplex_mesh(const MMG5_pMesh mmg, SimplexMesh &M)
94 {
95 logger().trace("converting MMG5_pMesh to Eigen ...");
96 /* Notes:
97 * - indexing seems to start at 1 in MMG */
98
99 assert(mmg->dim == 2 || mmg->dim == 3);
100 M.V.resize(mmg->np, 3);
101 for (int v = 0; v < mmg->np; ++v)
102 {
103 for (int d = 0; d < mmg->dim; ++d)
104 {
105 M.V(v, d) = mmg->point[v + 1].c[d];
106 }
107 // Vertices are 3D, init z coordinate to zero for 2D MMG mesh.
108 if (mmg->dim == 2)
109 M.V(v, 2) = 0.;
110 }
111 M.F.resize(mmg->nt, 3);
112 for (int t = 0; t < mmg->nt; ++t)
113 {
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;
117 }
118 M.T.resize(mmg->dim == 3 ? mmg->ne : 0, 4);
119 for (int c = 0; c < M.T.rows(); ++c)
120 {
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;
125 }
126
127 return true;
128 }
129
130 bool simplex_mesh_to_mmg3d(const SimplexMesh &M, MMG5_pMesh &mmg, MMG5_pSol &sol)
131 {
132 logger().trace("converting Eigen mesh to MMG5_pMesh ...");
133
134 MMG3D_Init_mesh(MMG5_ARG_start, MMG5_ARG_ppMesh, &mmg, MMG5_ARG_ppMet, &sol, MMG5_ARG_end);
135
136 if (MMG3D_Set_meshSize(mmg, (int)M.V.rows(), (int)M.T.rows(), 0, /* nb prisms */
137 (int)M.F.rows(), 0, /* nb quad */
138 0 /* nb edges */
139 )
140 != 1)
141 {
142 logger().error("failed to MMG3D_Set_meshSize");
143 return false;
144 }
145
146 for (int v = 0; v < mmg->np; ++v)
147 {
148 for (int d = 0; d < 3; ++d)
149 {
150 mmg->point[v + 1].c[d] = M.V(v, d);
151 }
152 }
153 for (int t = 0; t < mmg->nt; ++t)
154 {
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;
158 }
159 for (int c = 0; c < mmg->ne; ++c)
160 {
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;
165 }
166
167 if (MMG3D_Set_solSize(mmg, sol, MMG5_Vertex, (int)M.V.rows(), MMG5_Scalar) != 1)
168 {
169 logger().error("failed to MMG3D_Set_solSize");
170 return false;
171 }
172 for (int v = 0; v < M.V.rows(); ++v)
173 {
174 sol->m[v + 1] = 1.;
175 }
176 if (MMG3D_Chk_meshData(mmg, sol) != 1)
177 {
178 logger().error("error in mmg: inconsistent mesh and sol");
179 return false;
180 }
181
182 MMG3D_Set_handGivenMesh(mmg); /* because we don't use the API functions */
183
184 return true;
185 }
186
187 bool simplex_mesh_to_mmg2d(const SimplexMesh &M, MMG5_pMesh &mmg, MMG5_pSol &sol)
188 {
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))
191 {
192 logger().error("mmg2d_remesh: failed to allocate the MMG mesh");
193 return false;
194 }
195
196 for (int v = 0; v < mmg->np; ++v)
197 {
198 mmg->point[v + 1].c[0] = M.V(v, 0);
199 mmg->point[v + 1].c[1] = M.V(v, 1);
200 }
201 for (int t = 0; t < mmg->nt; ++t)
202 {
203 // MMG use 1 based indexing.
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;
207 }
208
209 if (!MMG2D_Set_solSize(mmg, sol, MMG5_Vertex, M.V.rows(), MMG5_Scalar))
210 {
211 logger().error("failed to MMG2D_Set_solSize");
212 return false;
213 }
214
215 for (int v = 0; v < M.V.rows(); ++v)
216 sol->m[v + 1] = 1.;
217
218 if (!MMG2D_Chk_meshData(mmg, sol))
219 {
220 logger().error("error in mmg: inconsistent mesh and sol");
221 return false;
222 }
223 return true;
224 }
225
226 void mmg2d_free(MMG5_pMesh mmg, MMG5_pSol sol)
227 {
228 MMG2D_Free_all(MMG5_ARG_start,
229 MMG5_ARG_ppMesh, &mmg, MMG5_ARG_ppMet, &sol, MMG5_ARG_end);
230 }
231
232 void mmg3d_free(MMG5_pMesh mmg, MMG5_pSol sol)
233 {
234 MMG3D_Free_all(MMG5_ARG_start,
235 MMG5_ARG_ppMesh, &mmg, MMG5_ARG_ppMet, &sol, MMG5_ARG_end);
236 }
237
239 bool mmg2d_tri_remesh(
240 const SimplexMesh &M,
241 const Eigen::VectorXd *metric,
242 const std::vector<int> *pinned_vertices,
243 SimplexMesh &M_out,
244 const MmgOptions &opt)
245 {
246 MMG5_pMesh mesh = nullptr;
247 MMG5_pSol met = nullptr;
248 bool ok = simplex_mesh_to_mmg2d(M, mesh, met);
249 if (!ok)
250 {
251 logger().error("mmg2d_remesh: failed to convert mesh to MMG5_pMesh");
252 mmg2d_free(mesh, met);
253 return false;
254 }
255
256 // Pin vertices.
257 if (pinned_vertices != nullptr)
258 {
259 for (const int v : *pinned_vertices)
260 {
261 assert(v >= 0 && v < M.V.rows());
262 MMG2D_Set_requiredVertex(mesh, v + 1);
263 }
264 }
265
266 /* Set remeshing options */
267 MMG2D_Set_dparameter(mesh, met, MMG2D_DPARAM_angleDetection, opt.angle_value);
268 if (opt.enable_anisotropy)
269 {
270 MMG2D_Set_solSize(mesh, met, MMG5_Vertex, 0, MMG5_Tensor);
271 }
272 const bool with_metric = opt.metric_attribute != "no_metric";
273 if (opt.hsiz == 0. || with_metric)
274 {
275 if (with_metric || !opt.optim)
276 {
277 MMG2D_Set_dparameter(mesh, met, MMG2D_DPARAM_hmin, opt.hmin);
278 MMG2D_Set_dparameter(mesh, met, MMG2D_DPARAM_hmax, opt.hmax);
279 }
280 else
281 {
282 met->np = 0;
283 }
284 }
285 else
286 {
287 met->np = 0;
288 MMG2D_Set_dparameter(mesh, met, MMG2D_DPARAM_hsiz, opt.hsiz);
289 }
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));
298 if (with_metric)
299 {
300 if (metric == nullptr)
301 {
302 logger().error("mmg2D_remesh: {} is not a vertex attribute, cancel", opt.metric_attribute);
303 mmg2d_free(mesh, met);
304 return false;
305 }
306 for (int v = 0; v < M.V.rows(); ++v)
307 {
308 met->m[v + 1] = (*metric)(v);
309 }
310 }
311
312 int ier = MMG2D_mmg2dlib(mesh, met);
313 if (ier != MMG5_SUCCESS)
314 {
315 logger().error("mmg2d_remesh: failed to remesh");
316 mmg2d_free(mesh, met);
317 return false;
318 }
319
320 ok = mmg_to_simplex_mesh(mesh, M_out);
321
322 mmg2d_free(mesh, met);
323 return ok;
324 }
325
327 bool mmg3d_tet_remesh(
328 const SimplexMesh &M,
329 const Eigen::VectorXd *metric,
330 const std::vector<int> *pinned_vertices,
331 SimplexMesh &M_out,
332 const MmgOptions &opt)
333 {
334 MMG5_pMesh mesh = nullptr;
335 MMG5_pSol met = nullptr;
336 bool ok = simplex_mesh_to_mmg3d(M, mesh, met);
337 if (!ok)
338 {
339 logger().error("mmg3d_remesh: failed to convert mesh to MMG5_pMesh");
340 mmg3d_free(mesh, met);
341 return false;
342 }
343
344 // Pin vertices and triangles.
345 std::vector<bool> is_pinned(M.V.rows(), false);
346 if (pinned_vertices != nullptr)
347 {
348 for (const int v : *pinned_vertices)
349 {
350 assert(v >= 0 && v < M.V.rows());
351 is_pinned[v] = true;
352 MMG3D_Set_requiredVertex(mesh, v + 1);
353 }
354 }
355 for (int f = 0; f < M.F.rows(); ++f)
356 {
357 if (is_pinned[M.F(f, 0)]
358 && is_pinned[M.F(f, 1)]
359 && is_pinned[M.F(f, 2)])
360 {
361 MMG3D_Set_requiredTriangle(mesh, f + 1);
362 }
363 }
364
365 /* Set remeshing options */
366 MMG3D_Set_dparameter(mesh, met, MMG3D_DPARAM_angleDetection, opt.angle_value);
367 if (opt.enable_anisotropy)
368 {
369 MMG3D_Set_solSize(mesh, met, MMG5_Vertex, 0, MMG5_Tensor);
370 }
371 const bool with_metric = opt.metric_attribute != "no_metric";
372 if (opt.hsiz == 0. || with_metric)
373 {
374 if (with_metric || !opt.optim)
375 {
376 MMG3D_Set_dparameter(mesh, met, MMG3D_DPARAM_hmin, opt.hmin);
377 MMG3D_Set_dparameter(mesh, met, MMG3D_DPARAM_hmax, opt.hmax);
378 }
379 else
380 {
381 met->np = 0;
382 }
383 }
384 else
385 {
386 met->np = 0;
387 MMG3D_Set_dparameter(mesh, met, MMG3D_DPARAM_hsiz, opt.hsiz);
388 }
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));
399 if (with_metric)
400 {
401 if (metric == nullptr)
402 {
403 logger().error("mmg3D_remesh: {} is not a vertex attribute, cancel", opt.metric_attribute);
404 mmg3d_free(mesh, met);
405 return false;
406 }
407 for (int v = 0; v < M.V.rows(); ++v)
408 {
409 met->m[v + 1] = (*metric)(v);
410 }
411 }
412
413 int ier = MMG3D_mmg3dlib(mesh, met);
414 if (ier != MMG5_SUCCESS)
415 {
416 logger().error("mmg3d_remesh: failed to remesh");
417 mmg3d_free(mesh, met);
418 return false;
419 }
420
421 ok = mmg_to_simplex_mesh(mesh, M_out);
422
423 mmg3d_free(mesh, met);
424 return ok;
425 }
426
427 } // anonymous namespace
428
430
431 bool remesh_2d(
432 const Eigen::MatrixXd &V,
433 const Eigen::MatrixXi &F,
434 Eigen::MatrixXd &OV,
435 Eigen::MatrixXi &OF,
436 MmgOptions opt,
437 const std::vector<int> *pinned_vertices)
438 {
439 assert(V.cols() == 2);
440 assert(F.cols() == 3);
441 const SimplexMesh M = make_simplex_mesh(V, F, Eigen::MatrixXi(0, 4));
442 SimplexMesh M_out;
443 if (!mmg2d_tri_remesh(M, nullptr, pinned_vertices, M_out, opt))
444 return false;
445 OV = M_out.V;
446 OF = M_out.F;
447 // The exchanged vertices are 3D, truncate unused third column.
448 OV.conservativeResize(OV.rows(), 2);
449 // MMG does not preserve orientation. Ensure singed area is positive.
450 return orient_simplices(OV, OF);
451 }
452
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)
455 {
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));
460 SimplexMesh M_out;
461
462 opt.metric_attribute = "scalar";
463 if (!mmg2d_tri_remesh(M, &S, nullptr, M_out, opt))
464 return;
465 OV = M_out.V;
466 OF = M_out.F;
467 OV.conservativeResize(OV.rows(), 2);
468 orient_simplices(OV, OF);
469 }
470
471 bool remesh_3d(
472 const Eigen::MatrixXd &V,
473 const Eigen::MatrixXi &T,
474 Eigen::MatrixXd &OV,
475 Eigen::MatrixXi &OF,
476 Eigen::MatrixXi &OT,
477 MmgOptions opt,
478 const std::vector<int> *pinned_vertices)
479 {
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);
485 SimplexMesh M_out;
486 if (!mmg3d_tet_remesh(M, nullptr, pinned_vertices, M_out, opt))
487 return false;
488 OV = M_out.V;
489 OF = M_out.F;
490 OT = M_out.T;
491 // MMG does not preserve orientation. Ensure singed area is positive.
492 return orient_simplices(OV, OT);
493 }
494
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)
497 {
498 assert(V.cols() == 3);
499 assert(V.rows() == S.size());
500 const SimplexMesh M = make_simplex_mesh(V, Eigen::MatrixXi(0, 3), T);
501 SimplexMesh M_out;
502
503 // Remeshing options
504 opt.metric_attribute = "scalar";
505
506 // Remesh volume
507 if (!mmg3d_tet_remesh(M, &S, nullptr, M_out, opt))
508 return;
509
510 // Convert output
511 OV = M_out.V;
512 OF = M_out.F;
513 OT = M_out.T;
514 orient_simplices(OV, OT);
515 }
516
517} // namespace polyfem::mesh
518
519#endif
int V
Eigen::MatrixXd F
M
Definition eigs.py:94
list vertices
Definition p_bases.py:238
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.
Definition Logger.cpp:44