Wildmeshing Toolkit
Loading...
Searching...
No Matches
ImageSimulationMesh.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 <polysolve/nonlinear/Problem.hpp>
8#include <wmtk/envelope/Envelope.hpp>
9#include <wmtk/optimization/solver.hpp>
10#include <wmtk/simplex/Simplex.hpp>
11
12#include "ConnectedComponent.hpp"
13#include "Parameters.h"
14
15// clang-format off
16#include <wmtk/utils/DisableWarnings.hpp>
17#include <fastenvelope/FastEnvelope.h>
18#include <tbb/concurrent_queue.h>
19#include <tbb/concurrent_priority_queue.h>
20#include <tbb/concurrent_vector.h>
21#include <tbb/concurrent_map.h>
22#include <tbb/concurrent_unordered_map.h>
23#include <tbb/enumerable_thread_specific.h>
24#include <tbb/parallel_for.h>
25#include <tbb/task_arena.h>
26#include <tbb/parallel_sort.h>
27#include <wmtk/utils/EnableWarnings.hpp>
28#include <VolumeRemesher/embed.h>
29// clang-format on
30
31#include <igl/remove_unreferenced.h>
32#include <memory>
33
34#include "expression_parser/Expression.hpp"
35
36namespace wmtk::components::image_simulation {
37
39{
40public:
41 Vector3r m_pos; // exact position in rational
42 Vector3d m_posf; // position as double
47 bool m_is_rounded = false;
48
49 bool m_is_on_surface = false;
57 size_t m_order = 0;
58 std::vector<int> on_bbox_faces; // same as is_bbox_fs?
59
60 double m_sizing_scalar = 1;
61
65 size_t partition_id = 0;
66
68 VertexAttributes(const Vector3r& p);
69};
70
71
72// class EdgeAttributes
73// {
74// public:
75// bool m_is_on_open_boundary = false;
76// };
77
79{
80public:
84 bool m_is_surface_fs = false;
85
95 int m_is_bbox_fs = -1; //-1; 0~5
96
97 void reset()
98 {
99 m_is_surface_fs = false;
100 m_is_bbox_fs = -1;
101 }
102
103 void merge(const FaceAttributes& attr)
104 {
106 if (attr.m_is_bbox_fs >= 0) m_is_bbox_fs = attr.m_is_bbox_fs;
107 }
108};
109
111{
112public:
116 double m_quality;
121 CellTag tags;
122};
123
125{
126public:
127 int m_debug_print_counter = 0;
128 size_t m_tags_count = 0;
129 std::map<int64_t, std::string> m_tag_id_to_name;
130 std::map<std::string, int64_t> m_tag_name_to_id;
131
132 double time_env = 0.0;
133 igl::Timer isout_timer;
134 const double MAX_ENERGY = std::numeric_limits<double>::max();
135
136 Parameters& m_params;
137 std::vector<Vector3d> m_V_envelope;
138 std::vector<Vector3i> m_F_envelope;
139 std::shared_ptr<SampleEnvelope> m_envelope;
140 std::shared_ptr<SampleEnvelope> m_envelope_orig;
141 double m_envelope_eps = -1;
142
143 using ExprPtr = expression_parser::ExpressionPtr;
144 std::vector<std::tuple<ExprPtr, double>> m_length_regions;
145
146 bool m_collapse_check_quality = true;
147
148 // for open boundary
149 std::shared_ptr<SampleEnvelope> m_order_2_edge_envelope; // todo: add sample envelope option
150
151 tbb::enumerable_thread_specific<std::unique_ptr<polysolve::nonlinear::Solver>> m_solver;
152
153 // scaling factors
154 double m_s_amips = -1;
155 double m_s_envelope = -1;
156
157 // When set, split_edge_after binary-searches vmid onto the zero-crossing of this function.
158 // Negative = stays on v1 side, positive = stays on v2 side.
159 std::function<double(const Vector3d&)> m_voronoi_split_fn = nullptr;
160
161 ImageSimulationMesh(Parameters& _m_params, double envelope_eps, int _num_threads = 0)
162 : m_params(_m_params)
163 , m_envelope_eps(envelope_eps)
164 {
165 NUM_THREADS = _num_threads;
166 p_vertex_attrs = &m_vertex_attribute;
167 p_face_attrs = &m_face_attribute;
168 p_tet_attrs = &m_tet_attribute;
169 m_collapse_check_link_condition = false;
170 m_collapse_check_manifold = false;
171
172 // solver is lazily created on first use
173
174 optimization::deactivate_opt_logger();
175
176 m_s_amips = 1.;
177 m_s_envelope = 1. / (m_params.diag_l * m_params.eps * m_params.eps);
178
179 double& wa = m_params.w_amips;
180 double& we = m_params.w_envelope;
181 we = 1 - wa;
182 logger().info("w_envelope = {}", we);
183 }
184
189 // using EdgeAttCol = wmtk::AttributeCollection<EdgeAttributes>;
190 VertAttCol m_vertex_attribute;
191 FaceAttCol m_face_attribute;
192 TetAttCol m_tet_attribute;
193 // EdgeAttCol m_edge_attribute;
194
195 // only used with unit tests
196 void create_mesh_attributes(
197 const std::vector<VertexAttributes>& _vertex_attribute,
198 const std::vector<TetAttributes>& _tet_attribute)
199 {
200 auto n_tet = _tet_attribute.size();
201 m_vertex_attribute.resize(_vertex_attribute.size());
202 m_face_attribute.resize(4 * n_tet);
203 m_tet_attribute.resize(n_tet);
204
205 for (auto i = 0; i < _vertex_attribute.size(); i++) {
206 m_vertex_attribute[i] = _vertex_attribute[i];
207 }
208 m_tet_attribute.m_attributes = tbb::concurrent_vector<TetAttributes>(_tet_attribute.size());
209 for (auto i = 0; i < _tet_attribute.size(); i++) {
210 m_tet_attribute[i] = _tet_attribute[i];
211 }
212 for (auto i = 0; i < _tet_attribute.size(); i++) {
213 m_tet_attribute[i].m_quality = get_quality(tuple_from_tet(i));
214 }
215 }
216
217 // TODO This should not be here but inside wmtk
218 void compute_vertex_partition()
219 {
220 auto partition_id = partition_TetMesh(*this, NUM_THREADS);
221 for (auto i = 0; i < vert_capacity(); i++)
222 m_vertex_attribute[i].partition_id = partition_id[i];
223 }
224
225 // TODO This should not be here but inside wmtk
226 void compute_vertex_partition_morton()
227 {
228 if (NUM_THREADS == 0) return;
229
230 wmtk::logger().info("Number of parts: {} by morton", NUM_THREADS);
231
232 tbb::task_arena arena(NUM_THREADS);
233
234 arena.execute([&] {
235 std::vector<Eigen::Vector3d> V_v(vert_capacity());
236
237 tbb::parallel_for(
238 tbb::blocked_range<int>(0, V_v.size()),
239 [&](tbb::blocked_range<int> r) {
240 for (int i = r.begin(); i < r.end(); i++) {
241 V_v[i] = m_vertex_attribute[i].m_posf;
242 }
243 });
244
245
246 struct sortstruct
247 {
248 int order;
250 };
251
252 std::vector<sortstruct> list_v;
253 list_v.resize(V_v.size());
254 // since the morton code requires a correct scale of input vertices,
255 // we need to scale the vertices if their coordinates are out of range
256 std::vector<Eigen::Vector3d> V = V_v; // this is for rescaling vertices
257 Eigen::Vector3d vmin, vmax;
258 vmin = V.front();
259 vmax = V.front();
260
261 for (size_t j = 0; j < V.size(); j++) {
262 for (int i = 0; i < 3; i++) {
263 vmin(i) = std::min(vmin(i), V[j](i));
264 vmax(i) = std::max(vmax(i), V[j](i));
265 }
266 }
267
268 // get_bb_corners(V, vmin, vmax);
269 Eigen::Vector3d center = (vmin + vmax) / 2;
270
271 tbb::parallel_for(tbb::blocked_range<int>(0, V.size()), [&](tbb::blocked_range<int> r) {
272 for (int i = r.begin(); i < r.end(); i++) {
273 V[i] = V[i] - center;
274 }
275 });
276
277 Eigen::Vector3d scale_point =
278 vmax - center; // after placing box at origin, vmax and vmin are symetric.
279
280 double xscale, yscale, zscale;
281 xscale = fabs(scale_point[0]);
282 yscale = fabs(scale_point[1]);
283 zscale = fabs(scale_point[2]);
284 double scale = std::max(std::max(xscale, yscale), zscale);
285 if (scale > 300) {
286 tbb::parallel_for(
287 tbb::blocked_range<int>(0, V.size()),
288 [&](tbb::blocked_range<int> r) {
289 for (int i = r.begin(); i < r.end(); i++) {
290 V[i] = V[i] / scale;
291 }
292 });
293 }
294
295 constexpr int multi = 1000;
296 tbb::parallel_for(tbb::blocked_range<int>(0, V.size()), [&](tbb::blocked_range<int> r) {
297 for (int i = r.begin(); i < r.end(); i++) {
298 list_v[i].morton = Resorting::MortonCode64(
299 int(V[i][0] * multi),
300 int(V[i][1] * multi),
301 int(V[i][2] * multi));
302 list_v[i].order = i;
303 }
304 });
305
306 const auto morton_compare = [](const sortstruct& a, const sortstruct& b) {
307 return (a.morton < b.morton);
308 };
309
310 tbb::parallel_sort(list_v.begin(), list_v.end(), morton_compare);
311
312 int interval = list_v.size() / NUM_THREADS + 1;
313
314 tbb::parallel_for(
315 tbb::blocked_range<int>(0, list_v.size()),
316 [&](tbb::blocked_range<int> r) {
317 for (int i = r.begin(); i < r.end(); i++) {
318 m_vertex_attribute[list_v[i].order].partition_id = i / interval;
319 }
320 });
321 });
322 }
323
324 size_t get_partition_id(const Tuple& loc) const
325 {
326 return m_vertex_attribute[loc.vid(*this)].partition_id;
327 }
328
329 void init_envelope(const MatrixXd& V, const MatrixXi& F, const bool use_exact);
330
331 CellTag string_set_to_cell_tag(const std::set<std::string>& str_set);
332
333 void set_length_regions(const nlohmann::json& length_region_json);
334
335 double get_length2(const Tuple& l) const;
336
338
339 void write_msh(std::string file, const bool write_envelope = true);
340 void output_faces(std::string file, std::function<bool(const FaceAttributes&)> cond);
341
342public:
343 void split_all_edges();
344 bool split_edge_before(const Tuple& t) override;
345 bool split_edge_after(const Tuple& loc) override;
346
347 void smooth_all_vertices(const size_t n_iters);
348 bool smooth_before(const Tuple& t) override;
349 bool smooth_after(const Tuple& t) override;
350
351 void collapse_all_edges(bool is_limit_length = true);
352 bool collapse_edge_before(const Tuple& t) override;
353 bool collapse_edge_after(const Tuple& t) override;
354
355 void simplify();
356
357 size_t swap_all_edges_44();
358 bool swap_edge_44_before(const Tuple& t) override;
359 double swap_edge_44_energy(const std::vector<std::array<size_t, 4>>& tets, const int op_case)
360 override;
361 bool swap_edge_44_after(const Tuple& t) override;
362
363 size_t swap_all_edges_56();
364 bool swap_edge_56_before(const Tuple& t) override;
365 double swap_edge_56_energy(const std::vector<std::array<size_t, 4>>& tets, const int op_case)
366 override;
367 bool swap_edge_56_after(const Tuple& t) override;
368
369 size_t swap_all_edges_32();
370 bool swap_edge_before(const Tuple& t) override;
371 bool swap_edge_after(const Tuple& t) override;
372
373 size_t swap_all_faces();
374 bool swap_face_before(const Tuple& t) override;
375 bool swap_face_after(const Tuple& t) override;
376
377 size_t swap_all_edges_all();
378
382 bool is_inverted_f(const Tuple& loc) const;
383 bool is_inverted(const std::array<size_t, 4>& vs) const;
384 bool is_inverted(const Tuple& loc) const;
385 double get_quality(const std::array<size_t, 4>& vs) const;
386 double get_quality(const Tuple& loc) const;
387
388 std::vector<std::array<double, 12>> get_amips_assembles(const Tuple& t) const;
389
390 std::shared_ptr<polysolve::nonlinear::Problem> get_amips_energy(const Tuple& t) const;
391 std::shared_ptr<polysolve::nonlinear::Problem> get_envelope_energy(const Tuple& t) const;
392
400 bool round(const Tuple& loc);
401
406 bool all_rounded() const;
407
408 //
409 bool is_edge_on_surface(const Tuple& loc);
410 bool is_edge_on_bbox(const Tuple& loc);
411 //
412 void mesh_improvement(int max_its = 80);
413 std::tuple<double, double> local_operations(
414 const std::array<int, 4>& ops,
415 bool collapse_limit_length = true);
416 std::tuple<double, double> get_max_avg_energy();
417
418 bool check_attributes();
419
420 std::vector<std::array<size_t, 3>> get_faces_by_condition(
421 std::function<bool(const FaceAttributes&)> cond) const;
422
423 bool invariants(const std::vector<Tuple>& t) override; // this is now automatically checked
424
425 // debug use
426 std::atomic<int> cnt_split = 0, cnt_collapse = 0, cnt_swap = 0;
427
428private:
429 // tags: correspondence map from new tet-face node indices to in-triangle ids.
430 // built up while triangles are inserted.
431 tbb::concurrent_map<std::array<size_t, 3>, std::vector<int>> tet_face_tags;
432
434
436 {
437 // VertexAttributes vertex_info;
438 size_t v_new;
439 size_t v1_id;
440 size_t v2_id;
441 bool is_edge_on_surface = false;
442 bool is_edge_open_boundary = false;
443 std::vector<size_t> v1_param_type;
444 std::vector<size_t> v2_param_type;
445
446 std::vector<std::pair<FaceAttributes, std::array<size_t, 3>>> changed_faces;
447
452 std::map<simplex::Edge, TetAttributes> tets;
453 };
454 tbb::enumerable_thread_specific<SplitInfoCache> split_cache;
455
457 {
458 size_t v1_id;
459 size_t v2_id;
460 double max_energy;
461 double edge_length;
462 bool is_limit_length;
463
464 std::vector<std::pair<FaceAttributes, std::array<size_t, 3>>> changed_faces;
465 // all faces incident to the delete vertex (v1) that are on the tracked surface
466 std::vector<std::array<size_t, 3>> surface_faces;
467 // all edges incident to the deleted vertex(v1) that are on the open boundary
468 std::vector<std::array<size_t, 2>> boundary_edges;
469 std::vector<size_t> changed_tids;
470 std::vector<double> changed_energies;
471 };
472 tbb::enumerable_thread_specific<CollapseInfoCache> collapse_cache;
473
474
476 {
477 double max_energy;
478 std::map<std::array<size_t, 3>, FaceAttributes> changed_faces;
479 CellTag tet_tags;
480 };
481 tbb::enumerable_thread_specific<SwapInfoCache> swap_cache;
482
483public:
491 void init_from_image(
492 const MatrixXr& V,
493 const MatrixXi& T,
494 const MatrixSi& T_tags,
495 const std::vector<std::string>& tag_names);
496 void init_from_image(
497 const MatrixXd& V,
498 const MatrixXi& T,
499 const MatrixSi& T_tags,
500 const std::vector<std::string>& tag_names);
501
502 void init_surfaces_and_boundaries();
503
504 std::vector<std::array<size_t, 3>> triangulate_polygon_face(std::vector<Vector3r> points);
505
506 bool adjust_sizing_field_serial(double max_energy);
507
517 void find_order_2_edges();
527 bool is_order_2_edge(const Tuple& e) const;
528 bool is_order_2_edge(const std::array<size_t, 2>& e) const;
529
530 void write_vtu(const std::string& path);
531
532 void write_surface(const std::string& path) const;
533
534public:
535 // substructure functions
536
537 bool vertex_is_on_surface(const size_t vid) const override;
538
539 bool face_is_on_surface(const size_t fid) const override;
540
541 size_t get_order_of_vertex(const size_t vid) const override;
545 void init_vertex_order();
546
547public:
548 // Annotations
549
550 double tet_volume(const size_t tid) const;
551
555 std::vector<ConnectedComponent> compute_connected_components(const CellTag& tag_in) const;
556
574 std::vector<ConnectedComponent> find_holes(const std::vector<CellTag>& tag_in) const;
575
584 void compute_tag_boundary(const CellTag& tag, MatrixXd& V, MatrixXi& F) const;
585
593 void keep_largest_connected_component(
594 const std::vector<CellTag>& lcc_tags,
595 const size_t n_lcc = 1);
596
597 void fill_holes_topo(
598 const std::vector<CellTag>& fill_holes_tags,
599 double threshold = std::numeric_limits<double>::infinity());
600
601 void seal_connected_components(
602 const std::vector<CellTag>& tag_sets,
603 const std::vector<ConnectedComponent>& components);
604
605 void tight_seal_topo(
606 const std::vector<std::vector<CellTag>>& tight_seal_tag_sets,
607 double threshold = std::numeric_limits<double>::infinity());
608
609 void resolve_intersections(const std::vector<CellTag>& intersecting_tags);
610
611 void replace_tags(const std::vector<CellTag>& tags_in, const std::vector<CellTag>& tags_out);
612
623 void tag_priority(const std::vector<int64_t>& tags);
624};
625
626} // namespace wmtk::components::image_simulation
Definition Morton.h:35
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
size_t vert_capacity() const
get the current largest global vid
Definition TetMesh.h:354
Tuple tuple_from_tet(size_t tid) const
get a Tuple from global tetra index
Definition TetMesh.cpp:580
Definition ImageSimulationMesh.h:79
bool m_is_surface_fs
Definition ImageSimulationMesh.h:84
int m_is_bbox_fs
Definition ImageSimulationMesh.h:95
Definition ImageSimulationMesh.h:111
CellTag tags
Definition ImageSimulationMesh.h:121
double m_quality
Definition ImageSimulationMesh.h:116
Definition ImageSimulationMesh.h:39
size_t partition_id
Definition ImageSimulationMesh.h:65
size_t m_order
Definition ImageSimulationMesh.h:57
bool m_is_rounded
Definition ImageSimulationMesh.h:47
std::map< simplex::Edge, TetAttributes > tets
Definition ImageSimulationMesh.h:452