4#include <unordered_set>
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>
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>
26#include "ConnectedComponent.hpp"
27#include "Parameters.h"
29namespace wmtk::components::image_simulation::tri {
34 bool m_is_on_surface =
false;
35 std::vector<int> on_bbox_faces;
37 double m_sizing_scalar = 1;
39 size_t partition_id = 0;
52 bool m_is_surface_fs =
false;
66 m_is_surface_fs =
false;
72 m_is_surface_fs = m_is_surface_fs || attr.m_is_surface_fs;
81 double m_winding_number = 0;
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;
94 const double MAX_ENERGY = std::numeric_limits<double>::max();
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;
103 using VertAttCol = AttributeCollection<VertexAttributes>;
104 using EdgeAttCol = AttributeCollection<EdgeAttributes>;
105 using FaceAttCol = AttributeCollection<FaceAttributes>;
110 bool m_collapse_check_link_condition =
false;
111 bool m_collapse_check_topology =
false;
112 bool m_collapse_check_manifold =
false;
114 tbb::enumerable_thread_specific<std::unique_ptr<polysolve::nonlinear::Solver>> m_solver;
117 double m_s_amips = -1;
118 double m_s_envelope = -1;
121 : m_params(_m_params)
122 , m_envelope_eps(envelope_eps)
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;
129 optimization::deactivate_opt_logger();
136 m_s_envelope = 1. / (m_params.eps * m_params.eps);
139 double& wa = m_params.w_amips;
140 double& we = m_params.w_envelope;
142 logger().info(
"w_envelope = {}", we);
148 void partition_mesh();
151 void partition_mesh_morton();
153 size_t get_partition_id(
const Tuple& loc)
const
155 return m_vertex_attribute[loc.vid(*
this)].partition_id;
158 double get_length2(
const Tuple& l)
const;
174 const MatrixSi& T_tags,
175 const std::vector<std::string>& tag_names);
177 void init_surfaces_and_boundaries();
179 void init_envelope(
const MatrixXd& V,
const MatrixXi& F);
181 CellTag string_set_to_cell_tag(
const std::set<std::string>& str_set);
183 bool adjust_sizing_field_serial(
double max_energy);
185 void write_msh(std::string file,
const bool write_envelope =
true);
187 void write_vtu(
const std::string& path)
const;
188 void write_vtu_with_energies(
const std::string& path)
const;
190 std::vector<std::array<size_t, 2>> get_edges_by_condition(
191 std::function<
bool(
const EdgeAttributes&)> cond)
const;
194 void split_all_edges();
198 void collapse_all_edges(
bool is_limit_length =
true);
202 size_t swap_all_edges();
212 void smooth_all_vertices(
const size_t n_iters = 1);
222 std::shared_ptr<polysolve::nonlinear::Problem> get_envelope_energy(
const Tuple& t)
const;
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;
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;
242 double triangle_area(
const size_t fid)
const;
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;
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();
278 std::vector<ConnectedComponent>
find_holes(
const std::vector<CellTag>& tag_in)
const;
297 const std::vector<CellTag>& lcc_tags,
298 const size_t n_lcc = 1);
300 void fill_holes_topo(
301 const std::vector<CellTag>& fill_holes_tags,
302 double threshold = std::numeric_limits<double>::infinity());
304 void seal_connected_components(
305 const std::vector<CellTag>& tag_sets,
306 const std::vector<ConnectedComponent>& components);
308 void tight_seal_topo(
309 const std::vector<std::vector<CellTag>>& tight_seal_tag_sets,
310 double threshold = std::numeric_limits<double>::infinity());
312 void resolve_intersections(
const std::vector<CellTag>& intersecting_tags);
314 void replace_tags(
const std::vector<CellTag>& tags_in,
const std::vector<CellTag>& tags_out);
316 void tag_priority(
const std::vector<int64_t>& tags_order);
320 return m_vertex_attribute.at(vid).m_is_on_surface ||
321 !m_vertex_attribute.at(vid).on_bbox_faces.empty();
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;
343 std::vector<size_t> v1_param_type;
344 std::vector<size_t> v2_param_type;
349 std::map<simplex::Edge, EdgeAttributes> changed_edges;
355 std::map<size_t, FaceAttributes>
faces;
357 tbb::enumerable_thread_specific<SplitInfoCache> split_cache;
365 bool is_limit_length;
367 std::vector<std::pair<EdgeAttributes, std::array<size_t, 2>>> changed_edges;
369 std::vector<std::array<size_t, 2>> surface_edges;
370 std::vector<size_t> changed_fids;
371 std::vector<double> changed_energies;
373 tbb::enumerable_thread_specific<CollapseInfoCache> collapse_cache;
379 std::map<simplex::Edge, EdgeAttributes> changed_edges;
382 tbb::enumerable_thread_specific<SwapInfoCache> swap_cache;
387 std::function<double(
const Vector2d&)> m_voronoi_split_fn =
nullptr;
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
Definition ImageSimulationMeshTri.hpp:87
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
Definition Parameters.h:7
Definition ImageSimulationMeshTri.hpp:360
Definition ImageSimulationMeshTri.hpp:339
std::map< size_t, FaceAttributes > faces
Definition ImageSimulationMeshTri.hpp:355
Definition ImageSimulationMeshTri.hpp:377
Definition ImageSimulationMeshTri.hpp:32