Wildmeshing Toolkit
Loading...
Searching...
No Matches
TriWildMesh.h
1#pragma once
2
3#include <wmtk/utils/PartitionMesh.h>
4#include <wmtk/utils/VectorUtils.h>
5#include <polysolve/nonlinear/Problem.hpp>
6#include <wmtk/AttributeCollection.hpp>
7#include <wmtk/Types.hpp>
8#include <wmtk/envelope/Envelope.hpp>
9#include <wmtk/optimization/solver.hpp>
10
11// clang-format off
12#include <wmtk/utils/DisableWarnings.hpp>
13#include <fastenvelope/FastEnvelope.h>
14#include <tbb/concurrent_priority_queue.h>
15#include <tbb/concurrent_vector.h>
16#include <tbb/enumerable_thread_specific.h>
17#include <tbb/parallel_for.h>
18#include <tbb/parallel_sort.h>
19#include <wmtk/utils/EnableWarnings.hpp>
20// clang-format on
21
22#include "Parameters.h"
23
24namespace wmtk::components::triwild {
25
26// TODO: missing comments on what these attributes are
28{
29public:
30 Vector2d m_posf;
31 Vector2r m_pos;
32 bool m_is_rounded = false;
33
34 bool m_is_on_surface = false;
35 std::vector<int> on_bbox_faces;
36
37 double m_sizing_scalar = 1;
38
39 size_t partition_id = 0;
40
42 VertexAttributes(const Vector2r& p);
43};
44
46{
47public:
48 double tag; // TODO: is this used?
49
50 bool m_is_surface_fs = false; // 0; 1
59 int m_is_bbox_fs = -1; //-1; 0~3
60
61 void reset()
62 {
63 m_is_surface_fs = false;
64 m_is_bbox_fs = -1;
65 }
66
67 void merge(const EdgeAttributes& attr)
68 {
69 m_is_surface_fs = m_is_surface_fs || attr.m_is_surface_fs;
70 if (attr.m_is_bbox_fs >= 0) m_is_bbox_fs = attr.m_is_bbox_fs;
71 }
72};
73
75{
76public:
77 double m_quality;
78 double m_winding_number = 0;
79 std::set<int64_t> tags;
80 int part_id = -1;
81};
82
84{
85public:
86 int m_debug_print_counter = 0;
87 size_t m_tags_count = 0;
88 std::map<int64_t, std::string> m_tag_id_to_name;
89 std::map<std::string, int64_t> m_tag_name_to_id;
90
91 const double MAX_ENERGY = 1e50;
92
93 Parameters& m_params;
94 std::vector<Vector2d> m_V_envelope;
95 std::vector<Vector2i> m_E_envelope;
96 std::shared_ptr<SampleEnvelope> m_envelope;
97 double m_envelope_eps = -1;
98
102 VertAttCol m_vertex_attribute;
103 EdgeAttCol m_edge_attribute;
104 FaceAttCol m_face_attribute;
105
106 tbb::enumerable_thread_specific<std::unique_ptr<polysolve::nonlinear::Solver>> m_solver;
107
108 // scaling factors
109 double m_s_amips = -1;
110 double m_s_envelope = -1;
111
112 TriWildMesh(Parameters& _m_params, double envelope_eps, int _num_threads = 0)
113 : m_params(_m_params)
114 , m_envelope_eps(envelope_eps)
115 {
116 NUM_THREADS = _num_threads;
117 p_vertex_attrs = &m_vertex_attribute;
118 p_edge_attrs = &m_edge_attribute;
119 p_face_attrs = &m_face_attribute;
120
121 optimization::deactivate_opt_logger();
122
123 m_s_amips = 1.;
128 m_s_envelope = 1. / (m_params.eps * m_params.eps);
129
130
131 double& wa = m_params.w_amips;
132 double& we = m_params.w_envelope;
133 we = 1 - wa;
134 logger().info("w_envelope = {}", we);
135 }
136
137 ~TriWildMesh() {}
138
139 // TODO: this should not be here
140 void partition_mesh();
141
142 // TODO: morton should not be here, but inside wmtk
143 void partition_mesh_morton();
144
145 size_t get_partition_id(const Tuple& loc) const
146 {
147 return m_vertex_attribute[loc.vid(*this)].partition_id;
148 }
149
150 double get_length2(const Tuple& l) const;
151
152public:
161 void init_mesh(
162 const MatrixXd& V,
163 const MatrixXi& F,
164 const MatrixXi& E,
165 const std::vector<std::string>& tag_names);
166
167 void init_surfaces_and_boundaries();
168
169 void init_envelope(const MatrixXd& V, const MatrixXi& F);
170
171 bool adjust_sizing_field_serial(double max_energy);
172
173 void write_msh_groups(std::string file, const bool write_envelope = true);
174
175 void write_vtu(const std::string& path) const;
176
177 std::vector<std::array<size_t, 2>> get_edges_by_condition(
178 std::function<bool(const EdgeAttributes&)> cond) const;
179
180public:
181 void split_all_edges();
182 bool split_edge_before(const Tuple& t) override;
183 bool split_edge_after(const Tuple& loc) override;
184
185 void collapse_all_edges(bool is_limit_length = true);
186 bool collapse_edge_before(const Tuple& t) override;
187 bool collapse_edge_after(const Tuple& t) override;
188
189 size_t swap_all_edges();
195 double swap_weight(const Tuple& t) const;
196 bool swap_edge_before(const Tuple& t) override;
197 bool swap_edge_after(const Tuple& t) override;
198
199 void smooth_all_vertices(const size_t n_iters = 1);
200 bool smooth_before(const Tuple& t) override;
201 bool smooth_after(const Tuple& t) override;
202
208 std::vector<Vector2d> get_surface_assembles(const Tuple& t) const;
209 std::shared_ptr<polysolve::nonlinear::Problem> get_envelope_energy(const Tuple& t) const;
210
211 std::vector<std::array<double, 6>> get_amips_assembles(const Tuple& t) const;
212 std::shared_ptr<polysolve::nonlinear::Problem> get_amips_energy(const Tuple& t) const;
213
217 bool is_inverted_f(const Tuple& loc) const;
218 bool is_inverted_f(const size_t fid) const;
219 bool is_inverted(const std::array<size_t, 3>& vs) const;
220 bool is_inverted(const Tuple& loc) const;
221 bool is_inverted(const size_t fid) const;
222 double get_quality(const std::array<size_t, 3>& vs) const;
223 double get_quality(const Tuple& loc) const;
224 double get_quality(const size_t fid) const;
225 bool round(const Tuple& loc);
226 //
227 bool is_edge_on_surface(const Tuple& loc) const;
228 bool is_edge_on_surface(const std::array<size_t, 2>& vids) const;
229 bool is_edge_on_bbox(const Tuple& loc) const;
230 bool is_edge_on_bbox(const std::array<size_t, 2>& vids) const;
231
232 //
233 void mesh_improvement(int max_its = 80);
234
235 std::tuple<double, double> local_operations(
236 const std::array<int, 4>& ops,
237 bool collapse_limit_length = true);
238 std::tuple<double, double> get_max_avg_energy();
239
240 void compute_winding_numbers(const std::vector<std::string>& input_paths);
241 int flood_fill();
242
243 bool vertex_is_on_surface(const size_t vid) const override
244 {
245 return m_vertex_attribute.at(vid).m_is_on_surface ||
246 !m_vertex_attribute.at(vid).on_bbox_faces.empty();
247 }
248 bool edge_is_on_surface(const std::array<size_t, 2>& vids) const override
249 {
250 if (!vertex_is_on_surface(vids[0]) || !vertex_is_on_surface(vids[1])) {
251 return false;
252 }
253
254 const auto [_, eid] = tuple_from_edge(vids);
255 bool on_surface = m_edge_attribute.at(eid).m_is_surface_fs;
256 bool on_bbox = m_edge_attribute.at(eid).m_is_bbox_fs >= 0;
257 return on_surface || on_bbox;
258 }
259
260private:
262
264 {
265 // VertexAttributes vertex_info;
266 size_t v1_id;
267 size_t v2_id;
268 std::vector<size_t> v1_param_type;
269 std::vector<size_t> v2_param_type;
270
271 EdgeAttributes old_e_attrs;
272
273 // std::vector<std::pair<EdgeAttributes, std::array<size_t, 2>>> changed_edges;
274 std::map<simplex::Edge, EdgeAttributes> changed_edges;
275
280 std::map<size_t, FaceAttributes> faces;
281 };
282 tbb::enumerable_thread_specific<SplitInfoCache> split_cache;
283
285 {
286 size_t v1_id;
287 size_t v2_id;
288 double max_energy;
289 double edge_length;
290 bool is_limit_length;
291
292 std::vector<std::pair<EdgeAttributes, std::array<size_t, 2>>> changed_edges;
293 // all faces incident to the delete vertex (v1) that are on the tracked surface
294 std::vector<std::array<size_t, 2>> surface_edges;
295 std::vector<size_t> changed_fids;
296 std::vector<double> changed_energies;
297 };
298 tbb::enumerable_thread_specific<CollapseInfoCache> collapse_cache;
299
300
302 {
303 double max_energy;
304 std::map<simplex::Edge, EdgeAttributes> changed_edges;
305 std::set<int64_t> face_tags;
306 };
307 tbb::enumerable_thread_specific<SwapInfoCache> swap_cache;
308};
309
310
311} // namespace wmtk::components::triwild
Definition TriMesh.h:28
Tuple tuple_from_edge(size_t vid1, size_t vid2, size_t fid) const
Definition TriMesh.cpp:1479
int m_is_bbox_fs
Definition TriWildMesh.h:59
Definition TriWildMesh.h:84
bool edge_is_on_surface(const std::array< size_t, 2 > &vids) const override
Is an edge part of the substructure.
Definition TriWildMesh.h:248
bool split_edge_after(const Tuple &loc) override
User specified modifications and desideratas after an edge split.
Definition EdgeSplitting.cpp:113
bool swap_edge_after(const Tuple &t) override
User specified modifications and desideras after an edge swap.
Definition EdgeSwapping.cpp:145
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 EdgeSwapping.cpp:109
bool vertex_is_on_surface(const size_t vid) const override
Is a vertex part of the substructure.
Definition TriWildMesh.h:243
void mesh_improvement(int max_its=80)
Definition TriWildMesh.cpp:42
bool collapse_edge_before(const Tuple &t) override
User specified preparations and desideratas for an edge collapse including the link check as collapse...
Definition EdgeCollapsing.cpp:75
void init_mesh(const MatrixXd &V, const MatrixXi &F, const MatrixXi &E, const std::vector< std::string > &tag_names)
Init mesh from IGL-style matrices.
Definition TriWildMesh.cpp:203
bool is_inverted_f(const Tuple &loc) const
Inversion check using only floating point numbers.
Definition TriWildMesh.cpp:842
bool split_edge_before(const Tuple &t) override
User specified preparations and desideratas for an edge split.
Definition EdgeSplitting.cpp:73
TriWildMesh(Parameters &_m_params, double envelope_eps, int _num_threads=0)
Definition TriWildMesh.h:112
bool smooth_after(const Tuple &t) override
User specified modifications and desideras after an edge smooth.
Definition Smooth.cpp:35
bool smooth_before(const Tuple &t) override
User specified preparations and desideratas for an edge smooth.
Definition Smooth.cpp:16
bool collapse_edge_after(const Tuple &t) override
User specified modifications and desideratas after an edge collapse.
Definition EdgeCollapsing.cpp:252
double swap_weight(const Tuple &t) const
The quality improvement of a swap.
Definition EdgeSwapping.cpp:79
std::vector< Vector2d > get_surface_assembles(const Tuple &t) const
A vector containing the vertex position and all positions of the surface neighbors.
Definition Smooth.cpp:177
Definition Parameters.h:5
std::map< size_t, FaceAttributes > faces
Definition TriWildMesh.h:280