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, const bool use_exact);
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;
166 std::vector<size_t> active_vertices() const override;
167
168
170
171 void write_msh(std::string file, const bool write_envelope = true);
172
173public:
174 void simplify();
175
180 bool all_rounded() const;
181
182 //
183 //
184 // debug use
185 std::atomic<int> cnt_split = 0, cnt_collapse = 0;
186
187protected:
188 std::tuple<double, double> optimization_quality_stats() override;
189 double optimization_stop_metric() const override { return 1.; }
190 bool optimization_stop_at_float() const override { return m_sim_params.stop_at_float; }
191 void write_optimization_debug_output(const std::string& path) override { write_vtu(path); }
192
193 bool collapse_before_vertex(size_t v1, size_t v2, double edge_length) override;
194 bool collapse_quality_allowed(size_t v1, double quality, double ring_max) const override;
195 bool collapse_is_order_2_edge(const std::array<size_t, 2>& e) override;
196 bool collapse_after_connectivity(
197 size_t v1,
198 size_t v2,
199 const std::vector<std::array<size_t, 2>>& boundary_edges) override;
200 void collapse_after_vertex(size_t v1, size_t v2) override;
201
202 bool split_before_cells(const Tuple& edge, const std::vector<Tuple>& parents) override;
203 bool split_after_cells(size_t v1, size_t v2, size_t v_new, const std::vector<Tuple>& children)
204 override;
205 bool split_adjust_position(size_t v_new, const std::vector<Tuple>& children) override;
206
207 bool swap_before_interior(const std::vector<size_t>& tids) override;
208 bool swap_before_surface(
209 const std::vector<size_t>& tids,
210 size_t a,
211 size_t b,
212 size_t c,
213 size_t d) override;
214 bool swap_after_cells(const std::vector<size_t>& tids, bool is_surface_flip) override;
215
216private:
218
220 {
221 size_t v_new = 0;
223 std::map<simplex::Edge, TetAttributes> tets;
224 };
226
228 {
229 CellTag tet_tags;
230 std::map<size_t, CellTag> ring_tags;
231 };
233
234public:
242 void init_from_image(
243 const MatrixXr& V,
244 const MatrixXi& T,
245 const MatrixSi& T_tags,
246 const std::vector<std::string>& tag_names);
247 void init_from_image(
248 const MatrixXd& V,
249 const MatrixXi& T,
250 const MatrixSi& T_tags,
251 const std::vector<std::string>& tag_names);
252
253 void init_surfaces_and_boundaries();
254
255
268 size_t refine_sizing_around_worst(double max_metric = 0.) override;
269
270
280 void find_order_2_edges();
290 bool is_order_2_edge(const Tuple& e) const;
291 bool is_order_2_edge(const std::array<size_t, 2>& e) const;
292
293 void write_vtu(const std::string& path);
294
295 void write_surface(const std::string& path) const;
296
297public:
298 // substructure functions
299
303 void init_vertex_order();
304
305public:
306 // Annotations
307
308 double tet_volume(const size_t tid) const;
309
313 std::vector<ConnectedComponent> compute_connected_components(const CellTag& tag_in) const;
314 std::vector<ConnectedComponent> compute_connected_components(const ExprPtr& expr) const;
315
333 std::vector<ConnectedComponent> find_holes(const std::vector<CellTag>& tag_in) const;
334
343 void compute_tag_boundary(const CellTag& tag, MatrixXd& V, MatrixXi& F) const;
344
353 const std::vector<CellTag>& lcc_tags,
354 const size_t n_lcc = 1);
355
356 void fill_holes_topo(
357 const std::vector<CellTag>& fill_holes_tags,
358 double threshold = std::numeric_limits<double>::infinity());
359
360 void seal_connected_components(
361 const std::vector<CellTag>& tag_sets,
362 const std::vector<ConnectedComponent>& components);
363
364 void tight_seal_topo(
365 const std::vector<std::vector<CellTag>>& tight_seal_tag_sets,
366 double threshold = std::numeric_limits<double>::infinity());
367
368 void resolve_overlaps(const std::vector<std::array<ExprPtr, 2>>& intersecting_tags);
369
370 void replace_tags(const std::vector<CellTag>& tags_in, const std::vector<CellTag>& tags_out);
371
382 void tag_priority(const std::vector<int64_t>& tags);
383};
384
385} // 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:49
Tuple tuple_from_tet(size_t tid) const
get a Tuple from global tetra index
Definition TetMesh.cpp:640
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:45
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:642
bool all_rounded() const
Check if all vertices of the mesh are rounded.
Definition SimWildMesh.cpp:441
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:229
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
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:407
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:168
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
bool is_order_2_edge(const Tuple &e) const
Checks if an edge COULD be an open boundary edge.
Definition VolumemesherInsertion.cpp:448
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:184
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:158
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:154
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:223