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:
160 void init_mesh(const MatrixXd& V, const MatrixXi& F, const MatrixXi& E);
161
162 void init_surfaces_and_boundaries();
163
164 void init_envelope(const MatrixXd& V, const MatrixXi& F);
165
166 bool adjust_sizing_field_serial(double max_energy);
167
168 void write_msh_groups(std::string file, const bool write_envelope = true);
169
170 void write_vtu(const std::string& path) const;
171
172 std::vector<std::array<size_t, 2>> get_edges_by_condition(
173 std::function<bool(const EdgeAttributes&)> cond) const;
174
175public:
176 void split_all_edges();
177 bool split_edge_before(const Tuple& t) override;
178 bool split_edge_after(const Tuple& loc) override;
179
180 void collapse_all_edges(bool is_limit_length = true);
181 bool collapse_edge_before(const Tuple& t) override;
182 bool collapse_edge_after(const Tuple& t) override;
183
184 size_t swap_all_edges();
190 double swap_weight(const Tuple& t) const;
191 bool swap_edge_before(const Tuple& t) override;
192 bool swap_edge_after(const Tuple& t) override;
193
194 void smooth_all_vertices(const size_t n_iters = 1);
195 bool smooth_before(const Tuple& t) override;
196 bool smooth_after(const Tuple& t) override;
197
203 std::vector<Vector2d> get_surface_assembles(const Tuple& t) const;
204 std::shared_ptr<polysolve::nonlinear::Problem> get_envelope_energy(const Tuple& t) const;
205
206 std::vector<std::array<double, 6>> get_amips_assembles(const Tuple& t) const;
207 std::shared_ptr<polysolve::nonlinear::Problem> get_amips_energy(const Tuple& t) const;
208
212 bool is_inverted_f(const Tuple& loc) const;
213 bool is_inverted_f(const size_t fid) const;
214 bool is_inverted(const std::array<size_t, 3>& vs) const;
215 bool is_inverted(const Tuple& loc) const;
216 bool is_inverted(const size_t fid) const;
217 double get_quality(const std::array<size_t, 3>& vs) const;
218 double get_quality(const Tuple& loc) const;
219 double get_quality(const size_t fid) const;
220 bool round(const Tuple& loc);
221 //
222 bool is_edge_on_surface(const Tuple& loc) const;
223 bool is_edge_on_surface(const std::array<size_t, 2>& vids) const;
224 bool is_edge_on_bbox(const Tuple& loc) const;
225 bool is_edge_on_bbox(const std::array<size_t, 2>& vids) const;
226
227 //
228 void mesh_improvement(int max_its = 80);
229
230 std::tuple<double, double> local_operations(
231 const std::array<int, 4>& ops,
232 bool collapse_limit_length = true);
233 std::tuple<double, double> get_max_avg_energy();
234
235 void compute_winding_numbers(const std::vector<std::string>& input_paths);
236 int flood_fill();
237
238 bool vertex_is_on_surface(const size_t vid) const override
239 {
240 return m_vertex_attribute.at(vid).m_is_on_surface ||
241 !m_vertex_attribute.at(vid).on_bbox_faces.empty();
242 }
243 bool edge_is_on_surface(const std::array<size_t, 2>& vids) const override
244 {
245 if (!vertex_is_on_surface(vids[0]) || !vertex_is_on_surface(vids[1])) {
246 return false;
247 }
248
249 const auto [_, eid] = tuple_from_edge(vids);
250 bool on_surface = m_edge_attribute.at(eid).m_is_surface_fs;
251 bool on_bbox = m_edge_attribute.at(eid).m_is_bbox_fs >= 0;
252 return on_surface || on_bbox;
253 }
254
255private:
257
259 {
260 // VertexAttributes vertex_info;
261 size_t v1_id;
262 size_t v2_id;
263 std::vector<size_t> v1_param_type;
264 std::vector<size_t> v2_param_type;
265
266 EdgeAttributes old_e_attrs;
267
268 // std::vector<std::pair<EdgeAttributes, std::array<size_t, 2>>> changed_edges;
269 std::map<simplex::Edge, EdgeAttributes> changed_edges;
270
275 std::map<size_t, FaceAttributes> faces;
276 };
277 tbb::enumerable_thread_specific<SplitInfoCache> split_cache;
278
280 {
281 size_t v1_id;
282 size_t v2_id;
283 double max_energy;
284 double edge_length;
285 bool is_limit_length;
286
287 std::vector<std::pair<EdgeAttributes, std::array<size_t, 2>>> changed_edges;
288 // all faces incident to the delete vertex (v1) that are on the tracked surface
289 std::vector<std::array<size_t, 2>> surface_edges;
290 std::vector<size_t> changed_fids;
291 std::vector<double> changed_energies;
292 };
293 tbb::enumerable_thread_specific<CollapseInfoCache> collapse_cache;
294
295
297 {
298 double max_energy;
299 std::map<simplex::Edge, EdgeAttributes> changed_edges;
300 std::set<int64_t> face_tags;
301 };
302 tbb::enumerable_thread_specific<SwapInfoCache> swap_cache;
303};
304
305
306} // 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:243
bool split_edge_after(const Tuple &loc) override
User specified modifications and desideratas after an edge split.
Definition EdgeSplitting.cpp:113
void init_mesh(const MatrixXd &V, const MatrixXi &F, const MatrixXi &E)
Init mesh from IGL-style matrices.
Definition TriWildMesh.cpp:203
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:238
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
bool is_inverted_f(const Tuple &loc) const
Inversion check using only floating point numbers.
Definition TriWildMesh.cpp:832
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:275