Wildmeshing Toolkit
Loading...
Searching...
No Matches
ImageSimulationMeshTri.hpp
1#pragma once
2
3#include <limits>
4#include <unordered_set>
5
6#include <wmtk/utils/PartitionMesh.h>
7#include <wmtk/utils/VectorUtils.h>
8#include <polysolve/nonlinear/Problem.hpp>
9#include <wmtk/AttributeCollection.hpp>
10#include <wmtk/Types.hpp>
11#include <wmtk/envelope/Envelope.hpp>
12#include <wmtk/optimization/solver.hpp>
13
14// clang-format off
15#include <wmtk/utils/DisableWarnings.hpp>
16#include <igl/write_triangle_mesh.h>
17#include <tbb/concurrent_priority_queue.h>
18#include <tbb/concurrent_vector.h>
19#include <tbb/enumerable_thread_specific.h>
20#include <tbb/parallel_for.h>
21#include <tbb/parallel_sort.h>
22#include <fastenvelope/FastEnvelope.h>
23#include <wmtk/utils/EnableWarnings.hpp>
24// clang-format on
25
26#include "ConnectedComponent.hpp"
27#include "Parameters.h"
28
29namespace wmtk::components::image_simulation::tri {
30
32{
33 Vector2d m_pos;
34 bool m_is_on_surface = false;
35 std::vector<int> on_bbox_faces;
36
37 double m_sizing_scalar = 1;
38
39 size_t partition_id = 0;
40
42 VertexAttributes(const Vector2d& p)
43 : m_pos(p)
44 {}
45};
46
48{
49public:
50 double tag; // TODO: is this used?
51
52 bool m_is_surface_fs = false; // 0; 1
62 int m_is_bbox_fs = -1; //-1; 0~5
63
64 void reset()
65 {
66 m_is_surface_fs = false;
67 m_is_bbox_fs = -1;
68 }
69
70 void merge(const EdgeAttributes& attr)
71 {
72 m_is_surface_fs = m_is_surface_fs || attr.m_is_surface_fs;
73 if (attr.m_is_bbox_fs >= 0) m_is_bbox_fs = attr.m_is_bbox_fs;
74 }
75};
76
78{
79public:
80 double m_quality;
81 double m_winding_number = 0;
82 CellTag tags;
83 int part_id = -1;
84};
85
87{
88public:
89 int m_debug_print_counter = 0;
90 size_t m_tags_count = 0;
91 std::map<int64_t, std::string> m_tag_id_to_name;
92 std::map<std::string, int64_t> m_tag_name_to_id;
93
94 const double MAX_ENERGY = std::numeric_limits<double>::max();
95
96 Parameters& m_params;
97 std::vector<Vector2d> m_V_envelope;
98 std::vector<Vector2i> m_E_envelope;
99 std::shared_ptr<SampleEnvelope> m_envelope;
100 std::shared_ptr<SampleEnvelope> m_envelope_orig;
101 double m_envelope_eps = -1;
102
103 using VertAttCol = AttributeCollection<VertexAttributes>;
104 using EdgeAttCol = AttributeCollection<EdgeAttributes>;
105 using FaceAttCol = AttributeCollection<FaceAttributes>;
106 VertAttCol m_vertex_attribute;
107 EdgeAttCol m_edge_attribute;
108 FaceAttCol m_face_attribute;
109
110 bool m_collapse_check_link_condition = false; // classical link condition
111 bool m_collapse_check_topology = false; // sanity check
112 bool m_collapse_check_manifold = false; // manifoldness check after collapse
113
114 tbb::enumerable_thread_specific<std::unique_ptr<polysolve::nonlinear::Solver>> m_solver;
115
116 // scaling factors
117 double m_s_amips = -1;
118 double m_s_envelope = -1;
119
120 ImageSimulationMeshTri(Parameters& _m_params, double envelope_eps, int _num_threads = 0)
121 : m_params(_m_params)
122 , m_envelope_eps(envelope_eps)
123 {
124 NUM_THREADS = _num_threads;
125 p_vertex_attrs = &m_vertex_attribute;
126 p_edge_attrs = &m_edge_attribute;
127 p_face_attrs = &m_face_attribute;
128
129 optimization::deactivate_opt_logger();
130
131 m_s_amips = 1.;
136 m_s_envelope = 1. / (m_params.eps * m_params.eps);
137
138
139 double& wa = m_params.w_amips;
140 double& we = m_params.w_envelope;
141 we = 1 - wa;
142 logger().info("w_envelope = {}", we);
143 }
144
146
147 // TODO: this should not be here
148 void partition_mesh();
149
150 // TODO: morton should not be here, but inside wmtk
151 void partition_mesh_morton();
152
153 size_t get_partition_id(const Tuple& loc) const
154 {
155 return m_vertex_attribute[loc.vid(*this)].partition_id;
156 }
157
158 double get_length2(const Tuple& l) const;
159
160
161public:
171 void init_from_image(
172 const MatrixXd& V,
173 const MatrixXi& T,
174 const MatrixSi& T_tags,
175 const std::vector<std::string>& tag_names);
176
177 void init_surfaces_and_boundaries();
178
179 void init_envelope(const MatrixXd& V, const MatrixXi& F);
180
181 CellTag string_set_to_cell_tag(const std::set<std::string>& str_set);
182
183 bool adjust_sizing_field_serial(double max_energy);
184
185 void write_msh(std::string file, const bool write_envelope = true);
186
187 void write_vtu(const std::string& path) const;
188 void write_vtu_with_energies(const std::string& path) const;
189
190 std::vector<std::array<size_t, 2>> get_edges_by_condition(
191 std::function<bool(const EdgeAttributes&)> cond) const;
192
193public:
194 void split_all_edges();
195 bool split_edge_before(const Tuple& t) override;
196 bool split_edge_after(const Tuple& loc) override;
197
198 void collapse_all_edges(bool is_limit_length = true);
199 bool collapse_edge_before(const Tuple& t) override;
200 bool collapse_edge_after(const Tuple& t) override;
201
202 size_t swap_all_edges();
208 double swap_weight(const Tuple& t) const;
209 bool swap_edge_before(const Tuple& t) override;
210 bool swap_edge_after(const Tuple& t) override;
211
212 void smooth_all_vertices(const size_t n_iters = 1);
213 bool smooth_before(const Tuple& t) override;
214 bool smooth_after(const Tuple& t) override;
215
221 std::vector<Vector2d> get_surface_assembles(const Tuple& t) const;
222 std::shared_ptr<polysolve::nonlinear::Problem> get_envelope_energy(const Tuple& t) const;
223
224 std::vector<std::array<double, 6>> get_amips_assembles(const Tuple& t) const;
225 std::shared_ptr<polysolve::nonlinear::Problem> get_amips_energy(const Tuple& t) const;
226
231 //
235 bool is_inverted(const std::array<size_t, 3>& vs) const;
236 bool is_inverted(const Tuple& loc) const;
237 bool is_inverted(const size_t fid) const;
238 double get_quality(const std::array<size_t, 3>& vs) const;
239 double get_quality(const Tuple& loc) const;
240 double get_quality(const size_t fid) const;
241
242 double triangle_area(const size_t fid) const;
243
244 //
245 bool is_edge_on_surface(const Tuple& loc) const;
246 bool is_edge_on_surface(const std::array<size_t, 2>& vids) const;
247 bool is_edge_on_bbox(const Tuple& loc) const;
248 bool is_edge_on_bbox(const std::array<size_t, 2>& vids) const;
249 //
250 void mesh_improvement(int max_its = 80);
251 std::tuple<double, double> local_operations(
252 const std::array<int, 4>& ops,
253 bool collapse_limit_length = true);
254 std::tuple<double, double> get_max_avg_energy();
255
259 std::vector<ConnectedComponent> compute_connected_components(const CellTag& tag_in) const;
260
278 std::vector<ConnectedComponent> find_holes(const std::vector<CellTag>& tag_in) const;
279
287 void compute_tag_boundary(const CellTag& tag, MatrixXd& V, MatrixXi& E) const;
288
297 const std::vector<CellTag>& lcc_tags,
298 const size_t n_lcc = 1);
299
300 void fill_holes_topo(
301 const std::vector<CellTag>& fill_holes_tags,
302 double threshold = std::numeric_limits<double>::infinity());
303
304 void seal_connected_components(
305 const std::vector<CellTag>& tag_sets,
306 const std::vector<ConnectedComponent>& components);
307
308 void tight_seal_topo(
309 const std::vector<std::vector<CellTag>>& tight_seal_tag_sets,
310 double threshold = std::numeric_limits<double>::infinity());
311
312 void resolve_intersections(const std::vector<CellTag>& intersecting_tags);
313
314 void replace_tags(const std::vector<CellTag>& tags_in, const std::vector<CellTag>& tags_out);
315
316 void tag_priority(const std::vector<int64_t>& tags_order);
317
318 bool vertex_is_on_surface(const size_t vid) const override
319 {
320 return m_vertex_attribute.at(vid).m_is_on_surface ||
321 !m_vertex_attribute.at(vid).on_bbox_faces.empty();
322 }
323 bool edge_is_on_surface(const std::array<size_t, 2>& vids) const override
324 {
325 if (!vertex_is_on_surface(vids[0]) || !vertex_is_on_surface(vids[1])) {
326 return false;
327 }
328
329 const auto [_, eid] = tuple_from_edge(vids);
330 bool on_surface = m_edge_attribute.at(eid).m_is_surface_fs;
331 bool on_bbox = m_edge_attribute.at(eid).m_is_bbox_fs >= 0;
332 return on_surface || on_bbox;
333 }
334
335private:
337
339 {
340 // VertexAttributes vertex_info;
341 size_t v1_id;
342 size_t v2_id;
343 std::vector<size_t> v1_param_type;
344 std::vector<size_t> v2_param_type;
345
346 EdgeAttributes old_e_attrs;
347
348 // std::vector<std::pair<EdgeAttributes, std::array<size_t, 2>>> changed_edges;
349 std::map<simplex::Edge, EdgeAttributes> changed_edges;
350
355 std::map<size_t, FaceAttributes> faces;
356 };
357 tbb::enumerable_thread_specific<SplitInfoCache> split_cache;
358
360 {
361 size_t v1_id;
362 size_t v2_id;
363 double max_energy;
364 double edge_length;
365 bool is_limit_length;
366
367 std::vector<std::pair<EdgeAttributes, std::array<size_t, 2>>> changed_edges;
368 // all faces incident to the delete vertex (v1) that are on the tracked surface
369 std::vector<std::array<size_t, 2>> surface_edges;
370 std::vector<size_t> changed_fids;
371 std::vector<double> changed_energies;
372 };
373 tbb::enumerable_thread_specific<CollapseInfoCache> collapse_cache;
374
375
377 {
378 double max_energy;
379 std::map<simplex::Edge, EdgeAttributes> changed_edges;
380 CellTag face_tags;
381 };
382 tbb::enumerable_thread_specific<SwapInfoCache> swap_cache;
383
384 // When set, split_edge_after binary-searches vmid onto the zero-crossing of this function.
385 // Negative = stays on v1 side, positive = stays on v2 side.
386 // Set before split_edge(), cleared immediately after.
387 std::function<double(const Vector2d&)> m_voronoi_split_fn = nullptr;
388};
389
390} // namespace wmtk::components::image_simulation::tri
Definition TriMesh.h:28
Tuple tuple_from_edge(size_t vid1, size_t vid2, size_t fid) const
Definition TriMesh.cpp:1479
Definition ImageSimulationMeshTri.hpp:48
int m_is_bbox_fs
Definition ImageSimulationMeshTri.hpp:62
Definition ImageSimulationMeshTri.hpp:78
bool split_edge_after(const Tuple &loc) override
User specified modifications and desideratas after an edge split.
Definition ImageSimulationMeshTri.cpp:876
bool collapse_edge_after(const Tuple &t) override
User specified modifications and desideratas after an edge collapse.
Definition ImageSimulationMeshTri.cpp:1224
bool vertex_is_on_surface(const size_t vid) const override
Is a vertex part of the substructure.
Definition ImageSimulationMeshTri.hpp:318
void log_total_surface_energy()
Definition ImageSimulationMeshTri.cpp:1636
double swap_weight(const Tuple &t) const
The quality improvement of a swap.
Definition ImageSimulationMeshTri.cpp:1314
bool smooth_before(const Tuple &t) override
User specified preparations and desideratas for an edge smooth.
Definition ImageSimulationMeshTri.cpp:1445
bool split_edge_before(const Tuple &t) override
User specified preparations and desideratas for an edge split.
Definition ImageSimulationMeshTri.cpp:833
void mesh_improvement(int max_its=80)
Definition ImageSimulationMeshTri.cpp:1785
void init_from_image(const MatrixXd &V, const MatrixXi &T, const MatrixSi &T_tags, const std::vector< std::string > &tag_names)
Init from meshes image.
Definition ImageSimulationMeshTri.cpp:167
std::vector< ConnectedComponent > compute_connected_components(const CellTag &tag_in) const
Find all connected components that contain the tag_in tags.
Definition AnnotationsTri.cpp:5
bool edge_is_on_surface(const std::array< size_t, 2 > &vids) const override
Is an edge part of the substructure.
Definition ImageSimulationMeshTri.hpp:323
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 AnnotationsTri.cpp:65
std::vector< Vector2d > get_surface_assembles(const Tuple &t) const
A vector containing the vertex position and all positions of the surface neighbors.
Definition ImageSimulationMeshTri.cpp:1549
bool swap_edge_after(const Tuple &t) override
User specified modifications and desideras after an edge swap.
Definition ImageSimulationMeshTri.cpp:1380
ImageSimulationMeshTri(Parameters &_m_params, double envelope_eps, int _num_threads=0)
Definition ImageSimulationMeshTri.hpp:120
bool swap_edge_before(const Tuple &t) override
User specified preparations and desideratas for an edge swap including 1.can't swap on boundary edge....
Definition ImageSimulationMeshTri.cpp:1344
void compute_tag_boundary(const CellTag &tag, MatrixXd &V, MatrixXi &E) const
Compute the boundary of a tag.
Definition AnnotationsTri.cpp:141
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 AnnotationsTri.cpp:346
bool collapse_edge_before(const Tuple &t) override
User specified preparations and desideratas for an edge collapse including the link check as collapse...
Definition ImageSimulationMeshTri.cpp:1060
bool smooth_after(const Tuple &t) override
User specified modifications and desideras after an edge smooth.
Definition ImageSimulationMeshTri.cpp:1455
std::map< size_t, FaceAttributes > faces
Definition ImageSimulationMeshTri.hpp:355
Definition ImageSimulationMeshTri.hpp:32