3#include <wmtk/OptimizerParameters.h>
4#include <wmtk/RationalPositions.h>
5#include <wmtk/SurfaceTagAttributes.h>
6#include <wmtk/TetMesh.h>
7#include <wmtk/Types.hpp>
8#include <wmtk/envelope/Envelope.hpp>
9#include <wmtk/optimization/SmoothVertex.hpp>
10#include <wmtk/optimization/solver.hpp>
11#include <wmtk/simplex/Simplex.hpp>
12#include <wmtk/threading/enumerable_thread_specific.hpp>
13#include <wmtk/utils/SurfaceTopology.hpp>
58 bool m_is_on_surface =
false;
67 std::vector<int> on_bbox_faces;
69 double m_sizing_scalar = 1;
83 VertAttCol m_vertex_attribute;
84 FaceAttCol m_face_attribute;
129 double m_s_envelope = -1.;
131 double time_env = 0.0;
132 igl::Timer isout_timer;
152 bool is_force_split_edge(
const size_t v1,
const size_t v2)
const
159 size_t m_high_valence_claim_size = 0;
160 std::atomic<size_t> m_high_valence_rejects = 0;
182 virtual void set_cell_quality(
const size_t tid,
const double q) = 0;
206 void compute_vertex_partition();
207 void compute_vertex_partition_morton();
209 size_t get_partition_id(
const Tuple& loc)
const
211 return m_vertex_attribute[loc.vid(*
this)].partition_id;
214 double get_length2(
const Tuple& l)
const;
216 bool is_inverted(
const std::array<size_t, 4>& vs)
const;
217 bool is_inverted(
const Tuple& loc)
const;
221 double get_quality(
const std::array<size_t, 4>& vs)
const;
222 double get_quality(
const Tuple& loc)
const;
223 std::tuple<double, double> get_max_avg_energy();
227 int m_debug_print_counter = 0;
228 void mesh_improvement(
int max_its = 80);
229 std::tuple<double, double> local_operations(
230 const std::array<int, 4>& ops,
231 bool collapse_limit_length =
true);
245 return m_vertex_attribute.at(vid).m_is_rounded;
253 bool is_edge_on_surface(
const Tuple& loc);
254 bool is_edge_on_bbox(
const Tuple& loc);
289 const std::array<size_t, 3>& vids)
const
299 if (!env)
return false;
300 const auto& VA = m_vertex_attribute;
301 return env->is_outside({{VA[a].m_posf, VA[b].m_posf, VA[c].m_posf}});
308 std::vector<std::array<size_t, 3>> get_faces_by_condition(
311 void output_faces(std::string file, std::function<
bool(
const FaceAttributes&)> cond);
336 void split_all_edges();
341 void collapse_all_edges(
bool is_limit_length =
true);
369 size_t cells_before = 0;
370 size_t cells_after = 0;
371 double max_energy_before = 0.;
372 double max_energy_after = 0.;
378 size_t swap_all_edges_32();
382 size_t swap_all_edges_44();
387 size_t swap_all_edges_56();
392 size_t swap_all_faces();
395 size_t swap_all_edges_all();
397 bool prepare_surface_flip(
const Tuple& t,
const std::vector<size_t>& incident_tets);
402 return wmtk::utils::surface_topology_signature(*
this, [
this](
size_t fid) {
403 return m_face_attribute[fid].m_is_surface_fs;
406 void warn_if_surface_topology_changed(
const SurfaceTopoSignature& before,
const char* where)
409 wmtk::utils::warn_if_surface_topology_changed(before, surface_topology_signature(), where);
413 std::atomic<int> cnt_swap = 0;
414 std::atomic<int> cnt_surface_swap = 0;
415 std::atomic<int> cnt_surface_swap_32 = 0, cnt_surface_swap_44 = 0, cnt_surface_swap_56 = 0;
417 double swap_edge_44_energy(
const std::vector<std::array<size_t, 4>>& tets,
const int op_case)
419 double swap_edge_56_energy(
const std::vector<std::array<size_t, 4>>& tets,
const int op_case)
429 virtual std::shared_ptr<SampleEnvelope> smoothing_energy_envelope(
const size_t vid)
const = 0;
433 void smooth_all_vertices(
const size_t n_iters = 1);
435 bool invariants(
const std::vector<Tuple>& t)
override;
441 virtual double optimization_stop_metric()
const {
return m_params.stop_energy; }
442 virtual size_t refine_sizing_around_worst(
double max_metric) = 0;
443 virtual void write_optimization_debug_output(
const std::string& path) = 0;
444 virtual void optimization_sanity_checks_extra() {}
445 virtual bool optimization_stop_at_float()
const {
return false; }
450 virtual bool collapse_quality_allowed(
size_t v1,
double quality,
double ring_max)
const
452 return !m_vertex_attribute.at(v1).m_is_rounded || quality <= ring_max;
454 virtual bool collapse_is_order_2_edge(
const std::array<size_t, 2>&) {
return false; }
456 collapse_after_connectivity(
size_t,
size_t,
const std::vector<std::array<size_t, 2>>&)
460 virtual void collapse_after_vertex(
size_t,
size_t) {}
475 virtual bool allow_surface_swap()
const = 0;
476 virtual bool check_surface_topology()
const = 0;
480 virtual bool swap_before_surface(
const std::vector<size_t>&,
size_t,
size_t,
size_t,
size_t)
492 bool is_surface_flip =
false;
493 size_t sf_a = 0, sf_b = 0, sf_c = 0, sf_d = 0;
502 bool is_edge_on_surface =
false;
503 bool is_edge_open_boundary =
false;
504 size_t edge_order = 0;
505 double max_quality_before = 0.;
506 std::vector<std::pair<FaceAttributes, std::array<size_t, 3>>> changed_faces;
514 double max_energy = 0.;
515 double edge_length = 0.;
516 std::vector<std::pair<FaceAttributes, std::array<size_t, 3>>> changed_faces;
517 std::vector<std::array<size_t, 3>> surface_faces;
518 std::vector<std::array<size_t, 2>> boundary_edges;
519 std::vector<size_t> changed_tids;
520 std::vector<double> changed_energies;
541 std::vector<size_t> ring;
542 std::vector<size_t> frontier;
543 std::vector<size_t> next;
544 std::vector<size_t> one_ring;
545 std::vector<uint32_t> stamp;
548 std::vector<std::pair<size_t, double>> saved_qualities;
553 const std::vector<size_t>&
563 size_t collapse_all_edges_impl(
bool is_limit_length,
int lock_ring,
size_t max_passes = 0);
Several attribute collections for the same simplex type, behind one container.
Definition AttributeCollection.hpp:132
The rounded/exact bookkeeping shared by every mesh that keeps both coordinates.
Definition RationalPositions.h:21
Whether a codimension-1 simplex is tracked surface, and which bbox side it lies on.
Definition SurfaceTagAttributes.h:15
a Tuple refers to a global vid and a global tet id, and a local edge id and local face id
Definition TetMesh.h:49
Tuple tuple_from_vertex(size_t vid) const
get a Tuple from global vertex index
Definition TetMesh.cpp:627
What tetwild and simwild's 3D mesh share.
Definition TetOptimizerMesh.h:45
bool m_collapse_limit_length
Definition TetOptimizerMesh.h:143
void gradation_smooth_sizing(double grade, const std::vector< size_t > &seeds)
Grade the refined sizing region into its surroundings (monotone, only lowers).
Definition TetOptimizerMesh.cpp:367
virtual std::shared_ptr< SampleEnvelope > surface_envelope_for_face(const std::array< size_t, 3 > &vids) const
Envelope the tracked-surface triangle vids must stay inside.
Definition TetOptimizerMesh.h:288
double swap_edge_44_energy(const std::vector< std::array< size_t, 4 > > &tets, const int op_case) override
User specified energy to decide which of the 4 possible orientations should be chosen.
Definition TetOptimizerMesh.cpp:247
bool swap_edge_44_before(const Tuple &t) override
User specified preparations and desideratas for an 4-4 edge swap before changing the connectivity.
Definition TetOptimizerMeshSwaps.cpp:543
size_t m_force_split_count
Force-splits taken in the current split pass. Diagnostic only.
Definition TetOptimizerMesh.h:150
AttributeContainerGroup m_vertex_attr_group
What p_vertex_attrs points at, so a derived class can register more.
Definition TetOptimizerMesh.h:92
bool vertex_is_on_surface(const size_t vid) const override
Is a vertex part of the substructure.
Definition TetOptimizerMesh.cpp:669
bool collapse_edge_after(const Tuple &t) override
User specified modifications and desideratas for after an edge collapse.
Definition TetOptimizerMeshCollapse.cpp:341
std::unique_ptr< std::atomic< int >[]> m_high_valence_claim
Per-pass claims for the shared high-valence split gate.
Definition TetOptimizerMesh.h:158
std::vector< size_t > all_vertex_ids() const override
Every live vertex, in the mesh's own iteration order.
Definition TetOptimizerMesh.cpp:356
double region_max_quality_rel(const std::vector< size_t > &vids) const
Definition TetOptimizerMeshCollapse.cpp:529
bool m_coarsen_mode
Set for the duration of coarsen_mesh(); read-only while a pass is running.
Definition TetOptimizerMesh.h:528
bool swap_face_after(const Tuple &t) override
User specified modifications and desideratas for after a 2-3 face swap.
Definition TetOptimizerMeshSwaps.cpp:442
bool swap_edge_44_after(const Tuple &t) override
User specified modifications and desideratas for after a 4-4 edge swap.
Definition TetOptimizerMeshSwaps.cpp:584
bool swap_edge_before(const Tuple &t) override
User specified preparations and desideratas for an 3-2 edge swap before changing the conenctivity.
Definition TetOptimizerMeshSwaps.cpp:115
bool swap_face_before(const Tuple &t) override
User specified preparations and desideratas for an 2-3 face swap befroe changing the geometry.
Definition TetOptimizerMeshSwaps.cpp:384
virtual void split_after_vertex(size_t, bool)
Application metadata not represented by the shared vertex attributes.
Definition TetOptimizerMesh.h:473
bool surface_triangle_is_outside(const size_t a, const size_t b, const size_t c) const
Definition TetOptimizerMesh.h:296
bool swap_edge_56_after(const Tuple &t) override
User specified modifications and desideratas for after a 5-6 edge swap.
Definition TetOptimizerMeshSwaps.cpp:705
virtual bool split_after_cells(size_t, size_t, size_t, const std::vector< Tuple > &)
Restore application cell data on the children made by a split.
Definition TetOptimizerMesh.h:465
virtual double cell_quality(const size_t tid) const =0
The quality of cell tid, and how to write it.
virtual bool collapse_before_vertex(size_t, size_t, double)
Definition TetOptimizerMesh.h:449
virtual std::vector< size_t > active_vertices() const
Definition TetOptimizerMesh.cpp:431
wmtk::threading::enumerable_thread_specific< std::unique_ptr< polysolve::nonlinear::Solver > > m_solver
Per-thread Newton solver for smoothing; created on first use.
Definition TetOptimizerMesh.h:136
size_t get_order_of_vertex(const size_t vid) const override
Get the order of a vertex.
Definition TetOptimizerMesh.cpp:679
double swap_edge_56_energy(const std::vector< std::array< size_t, 4 > > &tets, const int op_case) override
User specified energy to decide which of the 5 possible orientations should be chosen.
Definition TetOptimizerMesh.cpp:262
bool collapse_edge_before(const Tuple &t) override
User specified preparations and desideratas for an edge collapse before changing the connectivity.
Definition TetOptimizerMeshCollapse.cpp:77
virtual bool is_open_boundary_edge(const Tuple &e)
Whether edge e lies on the boundary of the tracked surface.
Definition TetOptimizerMesh.h:266
int m_iterations_used
Shared TetWild/SimWild outer optimization schedule.
Definition TetOptimizerMesh.h:226
bool is_inverted_f(const Tuple &loc) const
Inversion check using only the double positions.
Definition TetOptimizerMesh.cpp:443
virtual bool swap_after_cells(const std::vector< size_t > &, bool)
Propagate application data to the cells made by a successful topological swap.
Definition TetOptimizerMesh.h:485
virtual double quality_rel(const size_t tid) const
A cell's quality relative to the quality it is required to reach; <= 1 means it meets it.
Definition TetOptimizerMesh.h:200
AttributeContainerGroup m_face_attr_group
What p_face_attrs points at, so a derived class can register more.
Definition TetOptimizerMesh.h:107
virtual bool split_adjust_position(size_t, const std::vector< Tuple > &)
Definition TetOptimizerMesh.h:471
int edge_incident_surface_face_count(const Tuple &e)
How many of the faces incident to edge e are on the tracked surface.
Definition TetOptimizerMesh.cpp:616
bool round_vertex(const size_t vid) override
Definition TetOptimizerMesh.h:250
bool swap_edge_56_before(const Tuple &t) override
User specified preparations and desideratas for a 5-6 edge swap before changing the connectivity.
Definition TetOptimizerMeshSwaps.cpp:665
static constexpr double MAX_ENERGY
The sentinel get_quality returns for an element AMIPS cannot score.
Definition TetOptimizerMesh.h:117
virtual std::shared_ptr< SampleEnvelope > smoothing_containment_envelope(const size_t vid) const
Envelope the resulting surface triangles are checked against.
Definition TetOptimizerMesh.cpp:349
bool split_edge_before(const Tuple &t) override
User specified preparations and desideratas for an edge split before changing the connectivity.
Definition TetOptimizerMeshSplit.cpp:92
bool swap_edge_after(const Tuple &t) override
User specified modifications and desideratas for after a 3-2 edge swap.
Definition TetOptimizerMeshSwaps.cpp:289
std::set< simplex::Edge > m_force_split_edges
Definition TetOptimizerMesh.h:148
bool smooth_before(const Tuple &t) override
User specified preparations and desideratas for smoothing a vertex.
Definition TetOptimizerMesh.cpp:277
optimization::SmoothRejectCounters m_smooth_rejects
Why smoothing attempts were refused, reported once per pass.
Definition TetOptimizerMesh.h:139
size_t coarsen_mesh()
Coarsen the mesh without letting the max energy rise.
Definition TetOptimizerMeshCollapse.cpp:581
bool round(const Tuple &v)
Round a vertex position to floating point, if that inverts no incident tet.
Definition TetOptimizerMesh.cpp:511
bool smooth_vertex_reversible(size_t vid, CoarsenScratch &scr)
One smoothing attempt on vid, restoring everything it wrote if it is rejected.
Definition TetOptimizerMeshCollapse.cpp:540
virtual std::tuple< double, double > optimization_quality_stats()
Definition TetOptimizerMesh.cpp:28
bool coarsen_collapse_edge(const Tuple &e, std::vector< Tuple > &new_tets)
One collapse under the coarsening rules, outside a coarsening pass.
Definition TetOptimizerMeshCollapse.cpp:569
double m_s_amips
Definition TetOptimizerMesh.h:128
double active_quality_threshold() const
Cell-quality threshold above which a tet is "active" (worth operating on) for the skip-good-regions f...
Definition TetOptimizerMesh.h:324
virtual bool split_before_cells(const Tuple &, const std::vector< Tuple > &)
Cache application cell data before a split. TetWild needs none; SimWild caches tags.
Definition TetOptimizerMesh.h:463
const std::vector< size_t > & collect_vertex_ball(const size_t *seeds, size_t n_seeds, int n, CoarsenScratch &scr) const
Vertices within n edges of seeds, in BFS order. Uses coarsen_scratch.
Definition TetOptimizerMeshCollapse.cpp:468
virtual bool swap_before_interior(const std::vector< size_t > &)
Application data attached to the old cells. TetWild has none; SimWild caches tags.
Definition TetOptimizerMesh.h:479
OptimizerParameters & m_params
Definition TetOptimizerMesh.h:121
bool swap_edge_56_accept_case(const std::array< size_t, 3 > &new_face) override
Filter which of the 5-6 orientations may be chosen.
Definition TetOptimizerMeshSwaps.cpp:270
std::shared_ptr< SampleEnvelope > m_envelope
Surface envelope: what a surface vertex is pulled toward and checked against.
Definition TetOptimizerMesh.h:124
bool split_edge_after(const Tuple &loc) override
This function computes the attributes for the added simplices. User specified modifications and desid...
Definition TetOptimizerMeshSplit.cpp:185
bool vertex_is_rounded(const size_t vid) const override
Whether this vertex's double position is currently trusted.
Definition TetOptimizerMesh.h:243
bool face_is_on_surface(const size_t fid) const override
Is a face part of the substructure.
Definition TetOptimizerMesh.cpp:674
bool swap_edge_44_accept_case(const std::array< size_t, 2 > &new_edge) override
Filter which of the 4-4 orientations may be chosen.
Definition TetOptimizerMeshSwaps.cpp:261
bool smooth_after(const Tuple &t) override
User specified modifications and desideratas for after smoothing a vertex.
Definition TetOptimizerMesh.cpp:291
Definition Simplex.hpp:46
Definition enumerable_thread_specific.hpp:26
Definition AttributeCollection.hpp:36
The parameters tetwild, triwild and simwild all share.
Definition OptimizerParameters.h:28
Per-thread buffers for the coarsening composite, so it allocates nothing.
Definition TetOptimizerMesh.h:540
What coarsen_mesh() achieved, for the run report. Zeroed when the pass is off.
Definition TetOptimizerMesh.h:367
Definition TetOptimizerMesh.h:511
double region_max_rel_before
Definition TetOptimizerMesh.h:523
Definition TetOptimizerMesh.h:499
Definition TetOptimizerMesh.h:488
Definition TetOptimizerMesh.h:48
size_t partition_id
Required for multi-threading.
Definition TetOptimizerMesh.h:72
size_t m_order
Definition TetOptimizerMesh.h:66
bool m_is_rounded
Definition TetOptimizerMesh.h:56
Why a smoothing attempt was refused, counted per pass.
Definition SmoothVertex.hpp:31
A topological fingerprint of a tracked surface inside a tet mesh.
Definition SurfaceTopology.hpp:23