Wildmeshing Toolkit
Loading...
Searching...
No Matches
SimWildMesh.h
1#pragma once
2
3#include <igl/Timer.h>
4#include <wmtk/SurfaceTagAttributes.h>
5#include <wmtk/TetMesh.h>
6#include <wmtk/TetOptimizerMesh.h>
7#include <wmtk/utils/PartitionMesh.h>
8#include <polysolve/nonlinear/Problem.hpp>
9#include <wmtk/envelope/Envelope.hpp>
10#include <wmtk/optimization/SmoothVertex.hpp>
11#include <wmtk/optimization/solver.hpp>
12#include <wmtk/simplex/Simplex.hpp>
13#include <wmtk/threading/enumerable_thread_specific.hpp>
14#include <wmtk/threading/parallel_for.hpp>
15
16#include "ConnectedComponent.hpp"
17#include "Parameters.h"
18
19// clang-format off
20#include <wmtk/utils/DisableWarnings.hpp>
21#include <fastenvelope/FastEnvelope.h>
22#include <VolumeRemesher/embed.h>
23#include <wmtk/utils/EnableWarnings.hpp>
24// clang-format on
25
26#include <igl/remove_unreferenced.h>
27#include <atomic>
28#include <memory>
29
30#include <wmtk/utils/SurfaceTopology.hpp>
31#include <wmtk/utils/partition_utils.hpp>
32#include "expression_parser/Expression.hpp"
33
34namespace wmtk::components::simwild {
35
39using VertexAttributes = wmtk::TetOptimizerMesh::VertexAttributes;
40using FaceAttributes = wmtk::TetOptimizerMesh::FaceAttributes;
41
43{
44public:
48 double m_quality;
53 CellTag tags;
54};
55
57{
58public:
59 using ExprPtr = expression_parser::ExpressionPtr;
60
61 size_t m_tags_count = 0;
62 std::map<int64_t, std::string> m_tag_id_to_name;
63 std::map<std::string, int64_t> m_tag_name_to_id;
64
68 std::vector<Vector3d> m_V_envelope;
69 std::vector<Vector3i> m_F_envelope;
70 double m_envelope_eps = -1;
71
72 std::vector<std::tuple<ExprPtr, double>> m_sizing_field;
73 std::vector<std::tuple<ExprPtr, double>> m_quality_field;
74
75 bool m_collapse_check_quality = true;
76
77 // for open boundary
79 std::shared_ptr<SampleEnvelope> m_order_2_edge_envelope;
80
82 std::shared_ptr<SampleEnvelope> smoothing_energy_envelope(const size_t vid) const override;
83
84 // When set, split_edge_after binary-searches vmid onto the zero-crossing of this function.
85 // Negative = stays on v1 side, positive = stays on v2 side.
86 std::function<double(const Vector3d&)> m_voronoi_split_fn = nullptr;
87
88 SimWildMesh(Parameters& _m_params, double envelope_eps, int _num_threads = 0)
89 : wmtk::TetOptimizerMesh(_m_params, nullptr)
90 , m_sim_params(_m_params)
91 {
92 m_envelope_eps = envelope_eps;
93 NUM_THREADS = _num_threads;
94 p_tet_attrs = &m_tet_attribute;
95 m_collapse_check_link_condition = false;
96 m_collapse_check_manifold = false;
97
98 // solver is lazily created on first use
99
100 optimization::deactivate_opt_logger();
101
102 m_s_envelope = 1. / (m_params.eps * m_params.eps);
103
104 double& wa = m_params.w_amips;
105 double& we = m_params.w_envelope;
106 we = 1 - wa;
107 logger().info("w_envelope = {}", we);
108 }
109
110 ~SimWildMesh() {}
112 TetAttCol m_tet_attribute;
113
114 double cell_quality(const size_t tid) const override { return m_tet_attribute[tid].m_quality; }
115 void set_cell_quality(const size_t tid, const double q) override
116 {
117 m_tet_attribute[tid].m_quality = q;
118 }
119 bool allow_surface_swap() const override { return m_sim_params.allow_surface_swap; }
120 bool check_surface_topology() const override { return m_sim_params.check_surface_topology; }
121
122 // only used with unit tests
123 void create_mesh_attributes(
124 const std::vector<VertexAttributes>& _vertex_attribute,
125 const std::vector<TetAttributes>& _tet_attribute)
126 {
127 const size_t n_tet = _tet_attribute.size();
128 m_vertex_attribute.resize(_vertex_attribute.size());
129 m_face_attribute.resize(4 * n_tet);
130 m_tet_attribute.resize(n_tet);
131
132 for (size_t i = 0; i < _vertex_attribute.size(); i++)
133 m_vertex_attribute[i] = _vertex_attribute[i];
134
135 // Keep whatever init() reserved. AttributeCollection::resize only ever grows, so the
136 // calls above cannot shrink a collection -- but assigning m_attributes directly does,
137 // and it used to drop the tet attributes to exactly n_tet. The attributes have to stay
138 // at least as large as the connectivity: an operation that creates a new element
139 // indexes them by its id, and a 5->6 swap creates one. tetwild hit the same thing (see
140 // the note on its copy); there the overrun wrote a double past the end and libc++ let
141 // it pass, here it assigns a std::set and segfaults outright.
142 const size_t tcap = std::max(n_tet, m_tet_attribute.size());
143 m_tet_attribute.m_attributes = std::vector<TetAttributes>(tcap);
144 for (size_t i = 0; i < n_tet; i++) m_tet_attribute[i] = _tet_attribute[i];
145 for (size_t i = 0; i < n_tet; i++)
146 m_tet_attribute[i].m_quality = get_quality(tuple_from_tet(i));
147 }
148
149 // TODO This should not be here but inside wmtk
150 // TODO This should not be here but inside wmtk
151 void init_envelope(const MatrixXd& V, const MatrixXi& F);
152
153 CellTag string_set_to_cell_tag(const std::set<std::string>& str_set);
154
155 void set_sizing_field(const nlohmann::json& sizing_field_json);
156
157 void set_quality_field(const nlohmann::json& quality_field_json);
158
159 double target_quality(const size_t tid) const;
160 double target_quality(const Tuple& t) const;
163 double quality_rel(const size_t tid) const override;
164 double quality_rel(const Tuple& t) const;
165 bool check_mesh_quality(double& max_rel_quality, const bool verbose = false) const;
175 bool check_interface_faces_tagged(const bool verbose = false) const;
176
189 void update_attributes() override;
190
191 std::vector<size_t> active_vertices() const override;
192
193
195
196 void write_msh(std::string file, const bool write_envelope = true);
197
198public:
199 void simplify();
200
205 bool all_rounded() const;
206
207 //
208 //
209 // debug use
210 std::atomic<int> cnt_split = 0, cnt_collapse = 0;
211
212protected:
213 std::tuple<double, double> optimization_quality_stats() override;
214 double optimization_stop_metric() const override { return 1.; }
215 bool optimization_stop_at_float() const override { return m_sim_params.stop_at_float; }
216 void write_optimization_debug_output(const std::string& path) override { write_vtu(path); }
217
218 bool collapse_before_vertex(size_t v1, size_t v2, double edge_length) override;
219 bool collapse_quality_allowed(size_t v1, double quality, double ring_max) const override;
220 bool collapse_is_order_2_edge(const std::array<size_t, 2>& e) override;
221 bool collapse_after_connectivity(
222 size_t v1,
223 size_t v2,
224 const std::vector<std::array<size_t, 2>>& boundary_edges) override;
225
226 bool split_before_cells(const Tuple& edge, const std::vector<Tuple>& parents) override;
227 bool split_after_cells(size_t v1, size_t v2, size_t v_new, const std::vector<Tuple>& children)
228 override;
229 bool split_adjust_position(size_t v_new, const std::vector<Tuple>& children) override;
230
231 bool swap_before_interior(const std::vector<size_t>& tids) override;
232 bool swap_before_surface(
233 const std::vector<size_t>& tids,
234 size_t a,
235 size_t b,
236 size_t c,
237 size_t d) override;
238 bool swap_after_cells(const std::vector<size_t>& tids, bool is_surface_flip) override;
239
240private:
242
244 {
245 size_t v_new = 0;
247 std::map<simplex::Edge, TetAttributes> tets;
248 };
250
252 {
253 CellTag tet_tags;
254 std::map<size_t, CellTag> ring_tags;
255 };
257
258public:
266 void init_from_image(
267 const MatrixXr& V,
268 const MatrixXi& T,
269 const MatrixSi& T_tags,
270 const std::vector<std::string>& tag_names);
271 void init_from_image(
272 const MatrixXd& V,
273 const MatrixXi& T,
274 const MatrixSi& T_tags,
275 const std::vector<std::string>& tag_names);
276
277 void init_surfaces_and_boundaries();
278
279
292 size_t refine_sizing_around_worst(double max_metric = 0.) override;
293
294
304 void find_order_2_edges();
314 bool is_order_2_edge(const Tuple& e) const;
315 bool is_order_2_edge(const std::array<size_t, 2>& e) const;
316
317 void write_vtu(const std::string& path);
318
319 void write_surface(const std::string& path) const;
320
321public:
322 // substructure functions
323
327 void init_vertex_order();
328
329public:
330 // Annotations
331
332 double tet_volume(const size_t tid) const;
333
337 std::vector<ConnectedComponent> compute_connected_components(const CellTag& tag_in) const;
338 std::vector<ConnectedComponent> compute_connected_components(const ExprPtr& expr) const;
339
357 std::vector<ConnectedComponent> find_holes(const std::vector<CellTag>& tag_in) const;
358
367 void compute_tag_boundary(const CellTag& tag, MatrixXd& V, MatrixXi& F) const;
368
377 const std::vector<CellTag>& lcc_tags,
378 const size_t n_lcc = 1);
379
380 void fill_holes_topo(
381 const std::vector<CellTag>& fill_holes_tags,
382 double threshold = std::numeric_limits<double>::infinity());
383
384 void seal_connected_components(
385 const std::vector<CellTag>& tag_sets,
386 const std::vector<ConnectedComponent>& components);
387
388 void tight_seal_topo(
389 const std::vector<std::vector<CellTag>>& tight_seal_tag_sets,
390 double threshold = std::numeric_limits<double>::infinity());
391
392 void resolve_overlaps(const std::vector<std::array<ExprPtr, 2>>& intersecting_tags);
393
394 void replace_tags(const std::vector<CellTag>& tags_in, const std::vector<CellTag>& tags_out);
395
406 void tag_priority(const std::vector<int64_t>& tags);
407};
408
409} // namespace wmtk::components::simwild
Whether a codimension-1 simplex is tracked surface, and which bbox side it lies on.
Definition SurfaceTagAttributes.h:15
a Tuple refers to a global vid and a global tet id, and a local edge id and local face id
Definition TetMesh.h:50
Tuple tuple_from_tet(size_t tid) const
get a Tuple from global tetra index
Definition TetMesh.cpp:605
What tetwild and simwild's 3D mesh share.
Definition TetOptimizerMesh.h:45
OptimizerParameters & m_params
Definition TetOptimizerMesh.h:121
Definition SimWildMesh.h:57
bool swap_after_cells(const std::vector< size_t > &tids, bool is_surface_flip) override
Propagate application data to the cells made by a successful topological swap.
Definition EdgeSwapping.cpp:48
std::vector< ConnectedComponent > compute_connected_components(const CellTag &tag_in) const
Find all connected components that contain the tag_in tags.
Definition AnnotationsTet.cpp:7
bool split_before_cells(const Tuple &edge, const std::vector< Tuple > &parents) override
Cache application cell data before a split. TetWild needs none; SimWild caches tags.
Definition EdgeSplitting.cpp:7
bool collapse_before_vertex(size_t v1, size_t v2, double edge_length) override
Definition EdgeCollapsing.cpp:8
void init_vertex_order()
Compute the vertex order for every vertex.
Definition SimWildMesh.cpp:810
bool all_rounded() const
Check if all vertices of the mesh are rounded.
Definition SimWildMesh.cpp:591
size_t refine_sizing_around_worst(double max_metric=0.) override
Escape a stuck max energy by refining the sizing field around the worst elements.
Definition SimWildMesh.cpp:376
bool split_after_cells(size_t v1, size_t v2, size_t v_new, const std::vector< Tuple > &children) override
Restore application cell data on the children made by a split.
Definition EdgeSplitting.cpp:20
bool check_interface_faces_tagged(const bool verbose=false) const
Verify that every interface between unlike tags carries a surface face.
Definition SimWildMesh.cpp:284
Parameters & m_sim_params
Definition SimWildMesh.h:67
void find_order_2_edges()
Find open boundary edges of the embedded surface and initialize a BVH for the open boundary.
Definition VolumemesherInsertion.cpp:410
std::shared_ptr< SampleEnvelope > m_order_2_edge_envelope
Follows m_envelope's use_exact; see where it is built in VolumemesherInsertion.cpp.
Definition SimWildMesh.h:79
double cell_quality(const size_t tid) const override
The quality of cell tid, and how to write it.
Definition SimWildMesh.h:114
void compute_tag_boundary(const CellTag &tag, MatrixXd &V, MatrixXi &F) const
Compute the boundary of a tag.
Definition AnnotationsTet.cpp:202
void keep_largest_connected_component(const std::vector< CellTag > &lcc_tags, const size_t n_lcc=1)
Keep only the largest connected component for each of the distinct tag_0 values, and engulf all other...
Definition AnnotationsTet.cpp:445
std::tuple< double, double > optimization_quality_stats() override
Definition SimWildMesh.cpp:170
void init_from_image(const MatrixXr &V, const MatrixXi &T, const MatrixSi &T_tags, const std::vector< std::string > &tag_names)
Init from meshes image.
Definition VolumemesherInsertion.cpp:10
std::shared_ptr< SampleEnvelope > smoothing_energy_envelope(const size_t vid) const override
Envelope a vertex is pulled toward while smoothing.
Definition Smooth.cpp:5
void tag_priority(const std::vector< int64_t > &tags)
Gives tags priority over others.
Definition AnnotationsTet.cpp:687
void update_attributes() override
Update the attributes of the mesh after an iteration of operations.
Definition SimWildMesh.cpp:188
bool is_order_2_edge(const Tuple &e) const
Checks if an edge COULD be an open boundary edge.
Definition VolumemesherInsertion.cpp:451
bool split_adjust_position(size_t v_new, const std::vector< Tuple > &children) override
Definition EdgeSplitting.cpp:50
std::vector< size_t > active_vertices() const override
Definition SimWildMesh.cpp:239
void simplify()
Definition EdgeCollapsing.cpp:44
std::vector< ConnectedComponent > find_holes(const std::vector< CellTag > &tag_in) const
Find all regions that do not contain the tags from tag_in.
Definition AnnotationsTet.cpp:127
bool swap_before_interior(const std::vector< size_t > &tids) override
Application data attached to the old cells. TetWild has none; SimWild caches tags.
Definition EdgeSwapping.cpp:7
double quality_rel(const size_t tid) const override
Definition SimWildMesh.cpp:160
Definition SimWildMesh.h:43
double m_quality
Definition SimWildMesh.h:48
CellTag tags
Definition SimWildMesh.h:53
Definition enumerable_thread_specific.hpp:26
Definition AttributeCollection.hpp:36
double w_amips
Definition OptimizerParameters.h:171
Definition TetOptimizerMesh.h:48
The fields shared with tetwild and triwild live in wmtk::OptimizerParameters.
Definition Parameters.h:10
std::map< simplex::Edge, TetAttributes > tets
Parent cell data, keyed by the edge opposite the split edge.
Definition SimWildMesh.h:247