Wildmeshing Toolkit
Loading...
Searching...
No Matches
ManExtractMesh.h
1#pragma once
2
3#include <igl/Timer.h>
4#include <wmtk/TetMesh.h>
5#include <wmtk/utils/Morton.h>
6#include <wmtk/utils/PartitionMesh.h>
7#include <bitset>
8#include <wmtk/envelope/Envelope.hpp>
9#include <wmtk/simplex/Simplex.hpp>
10#include "Parameters.h"
11
12// clang-format off
13#include <wmtk/utils/DisableWarnings.hpp>
14#include <tbb/concurrent_map.h>
15#include <tbb/parallel_sort.h>
16#include <wmtk/utils/EnableWarnings.hpp>
17// clang-format on
18
19// #include <igl/remove_unreferenced.h>
20// #include <memory>
21
22
23namespace wmtk::components::manifold_extraction {
24
25
26// for all attributes:
27// label: 0=default, 1=input, 2=offset
29{
30public:
31 Vector3d m_posf;
32 int label = 0;
33 bool on_surf = false;
34
36 VertexAttributes(const Vector3d& p);
37};
38
39
41{
42public:
43 int label = 0;
44 bool on_surf = false;
45};
46
47
49{
50public:
51 int label = 0;
52 bool on_surf = false;
53};
54
55
57{
58public:
59 bool in_out = false; // in or out of mesh body
60 int label = 0;
61 std::set<int64_t> tag;
62};
63
64
66{
67public:
68 int m_vtu_counter = 0;
69 int m_surfvtu_counter = 0;
70 std::array<size_t, 4> init_counts = {{0, 0, 0, 0}};
71 bool m_boundary_input_vert_found = false;
72
73 Parameters& m_params;
74 size_t m_tags_count;
75
76 // tag mapping stuff
77 std::map<std::string, int64_t> m_tag_name_to_id;
78 std::map<int64_t, std::string> m_tag_id_to_name;
79 std::set<int64_t> m_in_tag_ids;
80 std::set<int64_t> m_replace_tag_ids;
81
82 // dont actually use this
83 bool m_has_envelope = false;
84 MatrixXd m_V_envelope;
85 MatrixXi m_F_envelope;
86
91 VertAttCol m_vertex_attribute;
92 EdgeAttCol m_edge_attribute;
93 FaceAttCol m_face_attribute;
94 TetAttCol m_tet_attribute;
95
96 ManExtractMesh(Parameters& _m_params, int _num_threads = 0)
97 : m_params(_m_params)
98 {
99 NUM_THREADS = _num_threads;
100 p_vertex_attrs = &m_vertex_attribute;
101 p_edge_attrs = &m_edge_attribute;
102 p_face_attrs = &m_face_attribute;
103 p_tet_attrs = &m_tet_attribute;
104 }
105
106 ~ManExtractMesh() {}
107
108 // splitting/invariants
109 bool split_edge_before(const Tuple& t) override;
110 bool split_edge_after(const Tuple& t) override;
111 bool split_face_before(const Tuple& t) override;
112 bool split_face_after(const Tuple& t) override;
113 bool split_tet_before(const Tuple& t) override;
114 bool split_tet_after(const Tuple& t) override;
115 bool invariants(const std::vector<Tuple>& t) override; // this is now automatically checked
116
117private:
123 {
124 size_t v1_id;
125 size_t v2_id;
126 VertexAttributes new_v;
127 EdgeAttributes split_e; // split edge
128 std::map<size_t, EdgeAttributes> internal_e; // internal edges
129 std::map<simplex::Edge, EdgeAttributes> external_e; // edge is boundary edge (not link)
130 std::map<simplex::Edge, EdgeAttributes> link_e;
131 std::map<size_t, FaceAttributes> split_f; // faces incident to split edge
132 std::map<simplex::Edge, FaceAttributes> internal_f; // new internal faces
133 std::map<std::pair<simplex::Edge, size_t>, FaceAttributes> external_f; // retained faces
134 std::map<simplex::Edge, TetAttributes> tets; // tets incident to split edge
135 };
136 tbb::enumerable_thread_specific<EdgeSplitCache> edge_split_cache;
137
143 {
144 size_t v1_id;
145 size_t v2_id;
146 size_t v3_id;
147 std::map<simplex::Edge, EdgeAttributes> existing_e; // retained edges
148 std::map<simplex::Face, FaceAttributes> existing_f; // retained faces
149 int splitf_label; // split face
150 std::map<size_t, TetAttributes> tets; // incident tets
151 };
152 tbb::enumerable_thread_specific<FaceSplitCache> face_split_cache;
153
159 {
160 std::array<size_t, 4> v_ids;
161 std::map<simplex::Edge, EdgeAttributes> existing_e; // retained edges
162 std::map<simplex::Face, FaceAttributes> existing_f; // retained faces
163 TetAttributes tet; // split tet
164 };
165 tbb::enumerable_thread_specific<TetSplitCache> tet_split_cache;
166
167public:
171 void init_from_image(
172 const MatrixXd& V,
173 const MatrixXi& T,
174 const MatrixSi& T_tags,
175 const MatrixXd& V_env,
176 const MatrixXi& F_env,
177 const std::vector<std::string>& tag_names);
178
182 void label_surface_simplices(bool need_to_clear);
183
188 std::pair<size_t, size_t> label_non_manifold(); // return # of non manifold edges, verts
189
194 bool edge_is_manifold(const Tuple& t) const;
195
199 void edge_dfs_helper(std::set<size_t>& visited_tids, const Tuple& t) const;
200
207 bool vertex_is_manifold(const Tuple& t) const;
208
213 std::set<size_t>& visited_tids,
214 const Tuple& t,
215 const bool include,
216 const std::vector<simplex::Face>& b_out_faces) const;
217
221 bool is_boundary_vertex(size_t vid) const;
222
226 std::vector<simplex::Face> get_boundary_faces_for_out_tets(size_t vid) const;
227
232 bool is_simplicially_embedded() const;
233
238 bool tet_is_simp_emb(const Tuple& t) const;
239
245
250 void perform_offset();
251
255 void set_offset_tags();
256
261 void extract_surface_mesh(MatrixXd& V, MatrixXi& F);
262
266 void write_input_complex(const std::string& path);
267
271 void write_surface(const std::string& path);
272
276 void write_vtu(const std::string& path); // debugging, write .vtu of tet mesh
277
281 void write_msh_groups(const std::string& path);
282
283private: // helpers
284 // /**
285 // * @brief return true if all tags in tag1 are present in tag2. If tag2 is empty, only return
286 // * true if tag1 empty (ambient).
287 // */
288 // bool all_tags_present(std::set<int64_t> tag1, std::set<int64_t> tag2)
289 // {
290 // if (tag2.empty()) {
291 // return tag1.empty();
292 // }
293 // if (tag1.empty()) { // tag1 is ambient and tag2 is not.
294 // return false;
295 // }
296
297 // for (const int64_t& i : tag1) {
298 // if (tag2.find(i) == tag2.end()) {
299 // return false;
300 // }
301 // }
302 // return true;
303 // }
304
310 bool any_tag_present(const std::set<int64_t>& tag1, const std::set<int64_t>& tag2)
311 {
312 if (tag2.empty()) {
313 return tag1.empty();
314 }
315 if (tag1.empty()) { // tag1 is ambient, tag2 is not
316 return false;
317 }
318
319 for (const int64_t& i : tag1) {
320 if (tag2.find(i) != tag2.end()) {
321 return true;
322 }
323 }
324 return false;
325 }
326};
327
328} // namespace wmtk::components::manifold_extraction
a Tuple refers to a global vid and a global tet id, and a local edge id and local face id
Definition TetMesh.h:48
Definition TetMesh.h:23
bool is_boundary_vertex(size_t vid) const
check if a vertex is on the boundary of the mesh
Definition ManExtractMesh.cpp:397
void set_offset_tags()
update tet tags based off offset label and union/subtract mode. if union mode, all tets with label 2 ...
Definition ManExtractMesh.cpp:627
bool vertex_is_manifold(const Tuple &t) const
check if a vertex is manifold. A vertex is manifold if all 'inside' and all 'outside' tets adjacent t...
Definition ManExtractMesh.cpp:287
bool any_tag_present(const std::set< int64_t > &tag1, const std::set< int64_t > &tag2)
determine if any tag from tag1 is also present in tag2.
Definition ManExtractMesh.h:310
void init_from_image(const MatrixXd &V, const MatrixXi &T, const MatrixSi &T_tags, const MatrixXd &V_env, const MatrixXi &F_env, const std::vector< std::string > &tag_names)
initialize mesh from vertices, faces, and tag matrices
Definition ManExtractMesh.cpp:44
void perform_offset()
perform offset via "marching tets". All edges between input complex and background are split (ie,...
Definition ManExtractMesh.cpp:579
bool split_edge_before(const Tuple &t) override
User specified preparations and desideratas for an edge split before changing the connectivity.
Definition ComponentSplitting.cpp:8
void write_msh_groups(const std::string &path)
write tet mesh to msh file with physical groups format
Definition ManExtractMesh.cpp:916
void vertex_dfs_helper(std::set< size_t > &visited_tids, const Tuple &t, const bool include, const std::vector< simplex::Face > &b_out_faces) const
dfs helper for vertex manifold check
Definition ManExtractMesh.cpp:348
bool split_edge_after(const Tuple &t) override
This function computes the attributes for the added simplices. User specified modifications and desid...
Definition ComponentSplitting.cpp:84
void write_surface(const std::string &path)
write surface (boundary of in_tag)
Definition ManExtractMesh.cpp:842
void label_surface_simplices(bool need_to_clear)
set on_surf boolean for all simplices
Definition ManExtractMesh.cpp:124
void edge_dfs_helper(std::set< size_t > &visited_tids, const Tuple &t) const
dfs helper for edge manifold check
Definition ManExtractMesh.cpp:265
bool split_face_after(const Tuple &t) override
Compute the attributes for the added simplices.
Definition ComponentSplitting.cpp:228
bool tet_is_simp_emb(const Tuple &t) const
check if a tet meets simplicial embedding criteria (i.e., contains zero/one vertex,...
Definition ManExtractMesh.cpp:458
bool edge_is_manifold(const Tuple &t) const
check if an edge is manifold, ie if all 'inside' tets adjacent to the edge form single face-reachable...
Definition ManExtractMesh.cpp:224
std::vector< simplex::Face > get_boundary_faces_for_out_tets(size_t vid) const
collect faces on the boundary of the mesh that belong to an 'outside' tet.
Definition ManExtractMesh.cpp:418
void extract_surface_mesh(MatrixXd &V, MatrixXi &F)
extract tri mesh separating 'inside' tets from 'outside' tets. Resulting mesh is guaranteed to be man...
Definition ManExtractMesh.cpp:645
void simplicial_embedding()
make the background mesh a simplicial embedding via tet/face/edge splits. Since the input complex is ...
Definition ManExtractMesh.cpp:485
std::pair< size_t, size_t > label_non_manifold()
label nonmanifold simplices (edges and verts). Corresponding attributes have label set to 1
Definition ManExtractMesh.cpp:186
bool split_tet_before(const Tuple &t) override
User specified preparations and desideratas for a tet split before changing the connectivity.
Definition ComponentSplitting.cpp:308
void write_vtu(const std::string &path)
write tet mesh to vtu file
Definition ManExtractMesh.cpp:861
bool split_face_before(const Tuple &t) override
User specified preparations and desideratas for a face split before changing the connectivity.
Definition ComponentSplitting.cpp:154
bool is_simplicially_embedded() const
check if input complex (simplices labeled 1) are simplicially embedded by the background mesh
Definition ManExtractMesh.cpp:441
void write_input_complex(const std::string &path)
write input complex (non manifold components) to vtu file
Definition ManExtractMesh.cpp:760
bool split_tet_after(const Tuple &t) override
Compute the attributes for the added simplices.
Definition ComponentSplitting.cpp:344
attributes cache for edge split. All simplices inherent attributes from their parent during the split...
Definition ManExtractMesh.h:123
attributes cache for face split. All simplices inheret attributes from their parent during the split.
Definition ManExtractMesh.h:143
attributes cache for tet split. All simplices inheret attributes from their parent during the split.
Definition ManExtractMesh.h:159