Wildmeshing Toolkit
Loading...
Searching...
No Matches
SimWildMeshTri.hpp
1#pragma once
2
3#include <wmtk/MeshData.hpp>
4
5#include <wmtk/TriOptimizerMesh.h>
6
7#include <wmtk/SurfaceTagAttributes.h>
8#include <limits>
9#include <unordered_set>
10
11#include <wmtk/utils/PartitionMesh.h>
12#include <wmtk/utils/VectorUtils.h>
13#include <polysolve/nonlinear/Problem.hpp>
14#include <wmtk/AttributeCollection.hpp>
15#include <wmtk/Types.hpp>
16#include <wmtk/envelope/Envelope.hpp>
17#include <wmtk/optimization/SmoothVertex.hpp>
18#include <wmtk/optimization/solver.hpp>
19
20// clang-format off
21#include <wmtk/utils/DisableWarnings.hpp>
22#include <igl/write_triangle_mesh.h>
23#include <wmtk/threading/enumerable_thread_specific.hpp>
24#include <fastenvelope/FastEnvelope.h>
25#include <wmtk/utils/EnableWarnings.hpp>
26// clang-format on
27
28#include "ConnectedComponent.hpp"
29#include "Parameters.h"
30#include "expression_parser/Expression.hpp"
31
32
33namespace wmtk::components::simwild::tri {
34
39using VertexAttributes = wmtk::TriOptimizerMesh::VertexAttributes;
40using EdgeAttributes = wmtk::TriOptimizerMesh::EdgeAttributes;
41using FaceAttributes = wmtk::TriOptimizerMesh::FaceAttributes;
42
44{
45public:
46 using ExprPtr = expression_parser::ExpressionPtr;
47
51
52 std::vector<Vector2d> m_V_envelope;
53 std::vector<Vector2i> m_E_envelope;
54
55 std::vector<std::tuple<ExprPtr, double>> m_sizing_field;
56 std::vector<std::tuple<ExprPtr, double>> m_quality_field;
57
58 bool m_collapse_check_link_condition = false; // classical link condition
59 bool m_collapse_check_topology = false; // sanity check
60 bool m_collapse_check_manifold = false; // manifoldness check after collapse
61
64 bool smoothing_position_is_allowed(const size_t, const Vector2d&) const override
65 {
66 return true;
67 }
68
69 SimWildMeshTri(Parameters& _m_params, double envelope_eps, int _num_threads = 0)
70 : wmtk::TriOptimizerMesh(_m_params)
71 , m_sim_params(_m_params)
72 {
73 m_envelope_eps = envelope_eps;
74 NUM_THREADS = _num_threads;
75
76 optimization::deactivate_opt_logger();
77
78 double& wa = m_params.w_amips;
79 double& we = m_params.w_envelope;
80 we = 1 - wa;
81 logger().info("w_envelope = {}", we);
82 }
83
84 ~SimWildMeshTri() {}
85
86public:
96 void init_from_image(
97 const MatrixXd& V,
98 const MatrixXi& T,
99 const MatrixSi& T_tags,
100 const std::vector<std::string>& tag_names);
101
109 void init_from_image(
110 const MatrixXr& V,
111 const MatrixXi& T,
112 const MatrixSi& T_tags,
113 const std::vector<std::string>& tag_names);
114
115 void init_surfaces_and_boundaries();
116
117 void init_envelope(const MatrixXd& V, const MatrixXi& F);
118
119 CellTag string_set_to_cell_tag(const std::set<std::string>& str_set);
120
121 void set_sizing_field(const nlohmann::json& sizing_field_json);
122
123 void set_quality_field(const nlohmann::json& quality_field_json);
124
125 double target_quality(const size_t tid) const;
126 double target_quality(const Tuple& t) const;
129 double quality_rel(const size_t tid) const override;
130 double quality_rel(const Tuple& t) const;
131 bool check_mesh_quality(double& max_rel_quality, const bool verbose = false) const;
132
147 bool check_interface_edges_tagged(const bool verbose = false) const;
148
160 void update_attributes() override;
161 std::vector<size_t> active_vertices() const override;
162
175 size_t refine_sizing_around_worst(double max_metric = 0.) override;
176
177
178 void write_vtu(const std::string& path, MeshMap* output = nullptr) const;
179 void write_vtu_with_energies(const std::string& path) const;
180
181public:
187 std::vector<Vector2d> get_surface_assembles(const Tuple& t) const;
188 std::shared_ptr<polysolve::nonlinear::Problem> get_envelope_energy(const Tuple& t) const;
189
190 std::vector<std::array<double, 6>> get_amips_assembles(const Tuple& t) const;
191 std::shared_ptr<polysolve::nonlinear::Problem> get_amips_energy(const Tuple& t) const;
192
197
198
199 double triangle_area(const size_t fid) const;
200
204 std::vector<ConnectedComponent> compute_connected_components(const CellTag& tag_in) const;
205 std::vector<ConnectedComponent> compute_connected_components(const ExprPtr& expr) const;
206
224 std::vector<ConnectedComponent> find_holes(const std::vector<CellTag>& tag_in) const;
225
233 void compute_tag_boundary(const CellTag& tag, MatrixXd& V, MatrixXi& E) const;
234
243 const std::vector<CellTag>& lcc_tags,
244 const size_t n_lcc = 1);
245
246 void fill_holes_topo(
247 const std::vector<CellTag>& fill_holes_tags,
248 double threshold = std::numeric_limits<double>::infinity());
249
250 void seal_connected_components(
251 const std::vector<CellTag>& tag_sets,
252 const std::vector<ConnectedComponent>& components);
253
254 void tight_seal_topo(
255 const std::vector<std::vector<CellTag>>& tight_seal_tag_sets,
256 double threshold = std::numeric_limits<double>::infinity());
257
290 void tight_seal_geom(
291 const std::vector<int64_t>& tags,
292 double distance,
293 double trace_rel_tol = 0.01,
294 const std::string& selection = "explicit",
295 int64_t min_regions = 2,
296 bool sanity = false);
297
298 void resolve_overlaps(const std::vector<std::array<ExprPtr, 2>>& intersecting_tags);
299
300 void replace_tags(const std::vector<CellTag>& tags_in, const std::vector<CellTag>& tags_out);
301
302 void tag_priority(const std::vector<int64_t>& tags_order);
303
304private:
305 // When set, split_edge_after binary-searches vmid onto the zero-crossing of this function.
306 // Negative = stays on v1 side, positive = stays on v2 side.
307 // Set before split_edge(), cleared immediately after.
308 std::function<double(const Vector2d&)> m_voronoi_split_fn = nullptr;
309 size_t m_last_split_vertex = 0;
310
311 // tight_seal_geom's helpers that need the mesh, defined in TightSealGeom.cpp
316 const std::vector<size_t>& fids,
317 const std::function<bool(size_t)>& in_set,
318 SimpleBVH::BVH& bvh) const;
324 const simplex::Edge& e,
325 std::unordered_set<size_t>& faces,
326 std::vector<char>& filled,
327 const std::function<void(size_t, size_t)>& on_child);
335 const std::vector<size_t>& fids,
336 const std::vector<int>& region,
337 const std::vector<double>& dist_R_at,
338 const std::function<double(const Vector2d&)>& dist_R,
339 double radius,
340 double tol,
341 SimpleBVH::BVH& bvh,
342 size_t& n_traced,
343 size_t& n_failed) const;
347 void tight_seal_geom_sanity(const std::vector<int64_t>& tags, const std::vector<char>& filled)
348 const;
349
350protected:
351 std::tuple<double, double> optimization_quality_stats() override;
352 double optimization_stop_metric() const override { return 1.; }
353 bool optimization_stop_at_float() const override { return m_sim_params.stop_at_float; }
354
355 void write_smoothing_debug_output(const std::string& path) const override { write_vtu(path); }
356
357 bool collapse_quality_allowed(size_t v1, size_t fid, double q, double ring_max) const override;
358 bool split_adjust_position(size_t v_new, const std::vector<Tuple>& children) override;
359 void split_after_vertex(size_t v_new) override { m_last_split_vertex = v_new; }
360};
361
362} // namespace wmtk::components::simwild::tri
Definition SurfaceTagAttributes.h:24
Definition TriMesh.h:36
What triwild and simwild's 2D mesh share.
Definition TriOptimizerMesh.h:42
OptimizerParameters & m_params
Definition TriOptimizerMesh.h:175
Definition SimWildMeshTri.hpp:44
bool check_interface_edges_tagged(const bool verbose=false) const
Verify that every interface between unlike tags carries a surface edge.
Definition SimWildMeshTri.cpp:857
void update_attributes() override
Update the attributes of the mesh after an iteration of operations.
Definition SimWildMeshTri.cpp:956
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 SimWildMeshTri.cpp:537
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 SimWildMeshTri.cpp:41
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:450
std::vector< ConnectedComponent > compute_connected_components(const CellTag &tag_in) const
Find all connected components that contain the tag_in tags.
Definition AnnotationsTri.cpp:6
void log_total_surface_energy()
Definition SimWildMeshTri.cpp:1063
void tight_seal_geom_sanity(const std::vector< int64_t > &tags, const std::vector< char > &filled) const
Definition TightSealGeom.cpp:474
bool smoothing_position_is_allowed(const size_t, const Vector2d &) const override
Definition SimWildMeshTri.hpp:64
Parameters & m_sim_params
Definition SimWildMeshTri.hpp:50
void compute_tag_boundary(const CellTag &tag, MatrixXd &V, MatrixXi &E) const
Compute the boundary of a tag.
Definition AnnotationsTri.cpp:201
double quality_rel(const size_t tid) const override
Definition SimWildMeshTri.cpp:467
size_t tight_seal_split_tracked(const simplex::Edge &e, std::unordered_set< size_t > &faces, std::vector< char > &filled, const std::function< void(size_t, size_t)> &on_child)
Definition TightSealGeom.cpp:419
bool tight_seal_boundary_bvh(const std::vector< size_t > &fids, const std::function< bool(size_t)> &in_set, SimpleBVH::BVH &bvh) const
Definition TightSealGeom.cpp:340
std::tuple< double, double > optimization_quality_stats() override
Definition SimWildMeshTri.cpp:477
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:126
std::vector< Vector2d > get_surface_assembles(const Tuple &t) const
A vector containing the vertex position and all positions of the surface neighbors.
void tight_seal_geom(const std::vector< int64_t > &tags, double distance, double trace_rel_tol=0.01, const std::string &selection="explicit", int64_t min_regions=2, bool sanity=false)
Geometric tight seal between the regions R_i, the faces tagged with tags[i] and nothing else.
Definition AnnotationsTri.cpp:579
bool tight_seal_dilation_surface_bvh(const std::vector< size_t > &fids, const std::vector< int > &region, const std::vector< double > &dist_R_at, const std::function< double(const Vector2d &)> &dist_R, double radius, double tol, SimpleBVH::BVH &bvh, size_t &n_traced, size_t &n_failed) const
Definition TightSealGeom.cpp:363
Definition Simplex.hpp:46
double w_amips
Definition OptimizerParameters.h:172
Definition TriOptimizerMesh.h:95
Definition TriOptimizerMesh.h:45
The fields shared with tetwild and triwild live in wmtk::OptimizerParameters.
Definition Parameters.h:10