Wildmeshing Toolkit
Loading...
Searching...
No Matches
SimWildMeshTri.hpp
1#pragma once
2
3#include <wmtk/TriOptimizerMesh.h>
4
5#include <wmtk/SurfaceTagAttributes.h>
6#include <limits>
7#include <unordered_set>
8
9#include <wmtk/utils/PartitionMesh.h>
10#include <wmtk/utils/VectorUtils.h>
11#include <polysolve/nonlinear/Problem.hpp>
12#include <wmtk/AttributeCollection.hpp>
13#include <wmtk/Types.hpp>
14#include <wmtk/envelope/Envelope.hpp>
15#include <wmtk/optimization/SmoothVertex.hpp>
16#include <wmtk/optimization/solver.hpp>
17
18// clang-format off
19#include <wmtk/utils/DisableWarnings.hpp>
20#include <igl/write_triangle_mesh.h>
21#include <wmtk/threading/enumerable_thread_specific.hpp>
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#include "expression_parser/Expression.hpp"
29
30
31namespace wmtk::components::simwild::tri {
32
37using VertexAttributes = wmtk::TriOptimizerMesh::VertexAttributes;
38using EdgeAttributes = wmtk::TriOptimizerMesh::EdgeAttributes;
39using FaceAttributes = wmtk::TriOptimizerMesh::FaceAttributes;
40
42{
43public:
44 using ExprPtr = expression_parser::ExpressionPtr;
45
49
50 std::vector<Vector2d> m_V_envelope;
51 std::vector<Vector2i> m_E_envelope;
52
53 std::vector<std::tuple<ExprPtr, double>> m_sizing_field;
54 std::vector<std::tuple<ExprPtr, double>> m_quality_field;
55
56 bool m_collapse_check_link_condition = false; // classical link condition
57 bool m_collapse_check_topology = false; // sanity check
58 bool m_collapse_check_manifold = false; // manifoldness check after collapse
59
62 bool smoothing_position_is_allowed(const size_t, const Vector2d&) const override
63 {
64 return true;
65 }
66
67 SimWildMeshTri(Parameters& _m_params, double envelope_eps, int _num_threads = 0)
68 : wmtk::TriOptimizerMesh(_m_params)
69 , m_sim_params(_m_params)
70 {
71 m_envelope_eps = envelope_eps;
72 NUM_THREADS = _num_threads;
73
74 optimization::deactivate_opt_logger();
75
76 double& wa = m_params.w_amips;
77 double& we = m_params.w_envelope;
78 we = 1 - wa;
79 logger().info("w_envelope = {}", we);
80 }
81
82 ~SimWildMeshTri() {}
83
84public:
94 void init_from_image(
95 const MatrixXd& V,
96 const MatrixXi& T,
97 const MatrixSi& T_tags,
98 const std::vector<std::string>& tag_names);
99
107 void init_from_image(
108 const MatrixXr& V,
109 const MatrixXi& T,
110 const MatrixSi& T_tags,
111 const std::vector<std::string>& tag_names);
112
113 void init_surfaces_and_boundaries();
114
115 void init_envelope(const MatrixXd& V, const MatrixXi& F);
116
117 CellTag string_set_to_cell_tag(const std::set<std::string>& str_set);
118
119 void set_sizing_field(const nlohmann::json& sizing_field_json);
120
121 void set_quality_field(const nlohmann::json& quality_field_json);
122
123 double target_quality(const size_t tid) const;
124 double target_quality(const Tuple& t) const;
127 double quality_rel(const size_t tid) const override;
128 double quality_rel(const Tuple& t) const;
129 bool check_mesh_quality(double& max_rel_quality, const bool verbose = false) const;
130
145 bool check_interface_edges_tagged(const bool verbose = false) const;
146
158 void update_attributes() override;
159 std::vector<size_t> active_vertices() const override;
160
173 size_t refine_sizing_around_worst(double max_metric = 0.) override;
174
175 void write_msh(std::string file, const bool write_envelope = true);
176
177 void write_vtu(const std::string& path) const;
178 void write_vtu_with_energies(const std::string& path) const;
179
180public:
186 std::vector<Vector2d> get_surface_assembles(const Tuple& t) const;
187 std::shared_ptr<polysolve::nonlinear::Problem> get_envelope_energy(const Tuple& t) const;
188
189 std::vector<std::array<double, 6>> get_amips_assembles(const Tuple& t) const;
190 std::shared_ptr<polysolve::nonlinear::Problem> get_amips_energy(const Tuple& t) const;
191
196
197
198 double triangle_area(const size_t fid) const;
199
203 std::vector<ConnectedComponent> compute_connected_components(const CellTag& tag_in) const;
204 std::vector<ConnectedComponent> compute_connected_components(const ExprPtr& expr) const;
205
223 std::vector<ConnectedComponent> find_holes(const std::vector<CellTag>& tag_in) const;
224
232 void compute_tag_boundary(const CellTag& tag, MatrixXd& V, MatrixXi& E) const;
233
242 const std::vector<CellTag>& lcc_tags,
243 const size_t n_lcc = 1);
244
245 void fill_holes_topo(
246 const std::vector<CellTag>& fill_holes_tags,
247 double threshold = std::numeric_limits<double>::infinity());
248
249 void seal_connected_components(
250 const std::vector<CellTag>& tag_sets,
251 const std::vector<ConnectedComponent>& components);
252
253 void tight_seal_topo(
254 const std::vector<std::vector<CellTag>>& tight_seal_tag_sets,
255 double threshold = std::numeric_limits<double>::infinity());
256
257 void resolve_overlaps(const std::vector<std::array<ExprPtr, 2>>& intersecting_tags);
258
259 void replace_tags(const std::vector<CellTag>& tags_in, const std::vector<CellTag>& tags_out);
260
261 void tag_priority(const std::vector<int64_t>& tags_order);
262
263private:
264 // When set, split_edge_after binary-searches vmid onto the zero-crossing of this function.
265 // Negative = stays on v1 side, positive = stays on v2 side.
266 // Set before split_edge(), cleared immediately after.
267 std::function<double(const Vector2d&)> m_voronoi_split_fn = nullptr;
268 size_t m_last_split_vertex = 0;
269
270protected:
271 std::tuple<double, double> optimization_quality_stats() override;
272 double optimization_stop_metric() const override { return 1.; }
273 bool optimization_stop_at_float() const override { return m_sim_params.stop_at_float; }
274
275 void write_smoothing_debug_output(const std::string& path) const override { write_vtu(path); }
276
277 bool collapse_quality_allowed(size_t v1, size_t fid, double q, double ring_max) const override;
278 bool split_adjust_position(size_t v_new, const std::vector<Tuple>& children) override;
279 void split_after_vertex(size_t v_new) override { m_last_split_vertex = v_new; }
280};
281
282} // namespace wmtk::components::simwild::tri
Whether a codimension-1 simplex is tracked surface, and which bbox side it lies on.
Definition SurfaceTagAttributes.h:15
Definition TriMesh.h:32
What triwild and simwild's 2D mesh share.
Definition TriOptimizerMesh.h:39
OptimizerParameters & m_params
Definition TriOptimizerMesh.h:136
Definition SimWildMeshTri.hpp:42
bool check_interface_edges_tagged(const bool verbose=false) const
Verify that every interface between unlike tags carries a surface edge.
Definition SimWildMeshTri.cpp:953
void update_attributes() override
Update the attributes of the mesh after an iteration of operations.
Definition SimWildMeshTri.cpp:1052
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:530
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:40
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:448
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
void log_total_surface_energy()
Definition SimWildMeshTri.cpp:1159
bool smoothing_position_is_allowed(const size_t, const Vector2d &) const override
Definition SimWildMeshTri.hpp:62
Parameters & m_sim_params
Definition SimWildMeshTri.hpp:48
void compute_tag_boundary(const CellTag &tag, MatrixXd &V, MatrixXi &E) const
Compute the boundary of a tag.
Definition AnnotationsTri.cpp:200
double quality_rel(const size_t tid) const override
Definition SimWildMeshTri.cpp:460
std::tuple< double, double > optimization_quality_stats() override
Definition SimWildMeshTri.cpp:470
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:125
std::vector< Vector2d > get_surface_assembles(const Tuple &t) const
A vector containing the vertex position and all positions of the surface neighbors.
double w_amips
Definition OptimizerParameters.h:171
Definition TriOptimizerMesh.h:74
Definition TriOptimizerMesh.h:42
The fields shared with tetwild and triwild live in wmtk::OptimizerParameters.
Definition Parameters.h:10