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 std::vector<size_t> active_vertices() const override;
131
144 size_t refine_sizing_around_worst(double max_metric = 0.) override;
145
146 void write_msh(std::string file, const bool write_envelope = true);
147
148 void write_vtu(const std::string& path) const;
149 void write_vtu_with_energies(const std::string& path) const;
150
151public:
157 std::vector<Vector2d> get_surface_assembles(const Tuple& t) const;
158 std::shared_ptr<polysolve::nonlinear::Problem> get_envelope_energy(const Tuple& t) const;
159
160 std::vector<std::array<double, 6>> get_amips_assembles(const Tuple& t) const;
161 std::shared_ptr<polysolve::nonlinear::Problem> get_amips_energy(const Tuple& t) const;
162
167
168
169 double triangle_area(const size_t fid) const;
170
174 std::vector<ConnectedComponent> compute_connected_components(const CellTag& tag_in) const;
175 std::vector<ConnectedComponent> compute_connected_components(const ExprPtr& expr) const;
176
194 std::vector<ConnectedComponent> find_holes(const std::vector<CellTag>& tag_in) const;
195
203 void compute_tag_boundary(const CellTag& tag, MatrixXd& V, MatrixXi& E) const;
204
213 const std::vector<CellTag>& lcc_tags,
214 const size_t n_lcc = 1);
215
216 void fill_holes_topo(
217 const std::vector<CellTag>& fill_holes_tags,
218 double threshold = std::numeric_limits<double>::infinity());
219
220 void seal_connected_components(
221 const std::vector<CellTag>& tag_sets,
222 const std::vector<ConnectedComponent>& components);
223
224 void tight_seal_topo(
225 const std::vector<std::vector<CellTag>>& tight_seal_tag_sets,
226 double threshold = std::numeric_limits<double>::infinity());
227
228 void resolve_overlaps(const std::vector<std::array<ExprPtr, 2>>& intersecting_tags);
229
230 void replace_tags(const std::vector<CellTag>& tags_in, const std::vector<CellTag>& tags_out);
231
232 void tag_priority(const std::vector<int64_t>& tags_order);
233
234private:
235 // When set, split_edge_after binary-searches vmid onto the zero-crossing of this function.
236 // Negative = stays on v1 side, positive = stays on v2 side.
237 // Set before split_edge(), cleared immediately after.
238 std::function<double(const Vector2d&)> m_voronoi_split_fn = nullptr;
239 size_t m_last_split_vertex = 0;
240
241protected:
242 std::tuple<double, double> optimization_quality_stats() override;
243 double optimization_stop_metric() const override { return 1.; }
244 bool optimization_stop_at_float() const override { return m_sim_params.stop_at_float; }
245
246 void write_smoothing_debug_output(const std::string& path) const override { write_vtu(path); }
247
248 bool collapse_quality_allowed(size_t v1, size_t fid, double q, double ring_max) const override;
249 void collapse_after_vertex(size_t v1, size_t v2) override;
250 bool split_adjust_position(size_t v_new, const std::vector<Tuple>& children) override;
251 void split_after_vertex(size_t v_new) override { m_last_split_vertex = v_new; }
252};
253
254} // 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:31
What triwild and simwild's 2D mesh share.
Definition TriOptimizerMesh.h:38
OptimizerParameters & m_params
Definition TriOptimizerMesh.h:135
Definition SimWildMeshTri.hpp:42
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:512
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:38
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:1025
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:444
std::tuple< double, double > optimization_quality_stats() override
Definition SimWildMeshTri.cpp:454
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:154
Definition TriOptimizerMesh.h:73
Definition TriOptimizerMesh.h:41
The fields shared with tetwild and triwild live in wmtk::OptimizerParameters.
Definition Parameters.h:10