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>
59 bool m_is_on_surface =
false;
68 std::vector<int> on_bbox_faces;
70 double m_sizing_scalar = 1;
84 VertAttCol m_vertex_attribute;
85 FaceAttCol m_face_attribute;
130 double m_s_envelope = -1.;
132 double time_env = 0.0;
133 igl::Timer isout_timer;
157 bool is_force_split_edge(
const size_t v1,
const size_t v2)
const
164 size_t m_high_valence_claim_size = 0;
165 std::atomic<size_t> m_high_valence_rejects = 0;
187 virtual void set_cell_quality(
const size_t tid,
const double q) = 0;
211 void compute_vertex_partition();
212 void compute_vertex_partition_morton();
214 size_t get_partition_id(
const Tuple& loc)
const
216 return m_vertex_attribute[loc.vid(*
this)].partition_id;
219 double get_length2(
const Tuple& l)
const;
221 bool is_inverted(
const std::array<size_t, 4>& vs)
const;
222 bool is_inverted(
const Tuple& loc)
const;
226 double get_quality(
const std::array<size_t, 4>& vs)
const;
227 double get_quality(
const Tuple& loc)
const;
228 std::tuple<double, double> get_max_avg_energy();
241 int m_debug_print_counter = 0;
248 void mesh_improvement(
int max_its = 80);
249 std::tuple<double, double> local_operations(
250 const std::array<int, 4>& ops,
251 bool collapse_limit_length =
true);
265 return m_vertex_attribute.at(vid).m_is_rounded;
273 bool is_edge_on_surface(
const Tuple& loc);
274 bool is_edge_on_bbox(
const Tuple& loc);
309 const std::array<size_t, 3>& vids)
const
319 if (!env)
return false;
320 const auto& VA = m_vertex_attribute;
321 return env->is_outside({{VA[a].m_posf, VA[b].m_posf, VA[c].m_posf}});
328 std::vector<std::array<size_t, 3>> get_faces_by_condition(
331 void output_faces(std::string file, std::function<
bool(
const FaceAttributes&)> cond);
356 void split_all_edges();
361 void collapse_all_edges(
bool is_limit_length =
true);
389 size_t cells_before = 0;
390 size_t cells_after = 0;
391 double max_energy_before = 0.;
392 double max_energy_after = 0.;
398 size_t swap_all_edges_32();
402 size_t swap_all_edges_44();
407 size_t swap_all_edges_56();
412 size_t swap_all_faces();
415 size_t swap_all_edges_all();
417 bool prepare_surface_flip(
const Tuple& t,
const std::vector<size_t>& incident_tets);
422 return wmtk::utils::surface_topology_signature(*
this, [
this](
size_t fid) {
423 return m_face_attribute[fid].m_is_surface_fs;
426 void warn_if_surface_topology_changed(
const SurfaceTopoSignature& before,
const char* where)
429 wmtk::utils::warn_if_surface_topology_changed(before, surface_topology_signature(), where);
433 std::atomic<int> cnt_swap = 0;
434 std::atomic<int> cnt_surface_swap = 0;
435 std::atomic<int> cnt_surface_swap_32 = 0, cnt_surface_swap_44 = 0, cnt_surface_swap_56 = 0;
437 double swap_edge_44_energy(
const std::vector<std::array<size_t, 4>>& tets,
const int op_case)
439 double swap_edge_56_energy(
const std::vector<std::array<size_t, 4>>& tets,
const int op_case)
449 virtual std::shared_ptr<SampleEnvelope> smoothing_energy_envelope(
const size_t vid)
const = 0;
472 void smooth_all_vertices(
const size_t n_iters = 1);
477 bool invariants(
const std::vector<Tuple>& t)
override;
483 virtual double optimization_stop_metric()
const {
return m_params.stop_energy; }
484 virtual size_t refine_sizing_around_worst(
double max_metric) = 0;
509 virtual void write_optimization_debug_output(
const std::string& path) = 0;
510 virtual void optimization_sanity_checks_extra() {}
517 virtual bool optimization_stop_at_float()
const {
return false; }
535 return (prev - cur) <=
m_params.stuck_refine_stall_eps * (cur - optimization_stop_metric());
541 virtual bool collapse_quality_allowed(
size_t v1,
double quality,
double ring_max)
const
543 return !m_vertex_attribute.at(v1).m_is_rounded || quality <= ring_max;
545 virtual bool collapse_is_order_2_edge(
const std::array<size_t, 2>&) {
return false; }
547 collapse_after_connectivity(
size_t,
size_t,
const std::vector<std::array<size_t, 2>>&)
551 virtual void collapse_after_vertex(
size_t,
size_t) {}
566 virtual bool allow_surface_swap()
const = 0;
567 virtual bool check_surface_topology()
const = 0;
571 virtual bool swap_before_surface(
const std::vector<size_t>&,
size_t,
size_t,
size_t,
size_t)
592 enum class SwapReject :
int {
602 flip_nonmanifold_edge,
604 flip_new_face_surface,
617 app_after_side_conflict,
620 face_tracked_surface,
638 static const char* swap_reject_name(SwapReject r);
639 static const char* swap_stage_name(
SwapStage s);
640 bool swap_reject(SwapReject r)
const
642 m_swap_reject[size_t(r)].fetch_add(1, std::memory_order_relaxed);
651 std::memory_order_relaxed);
656 m_swap_stage[size_t(s)].fetch_add(1, std::memory_order_relaxed);
658 void swap_counters_reset();
662 mutable std::array<std::atomic<long>, size_t(SwapReject::COUNT)> m_swap_reject{};
663 mutable std::array<std::atomic<long>, size_t(SwapStage::COUNT)> m_swap_stage{};
672 enum class CollapseReject :
int {
675 surface_leaves_envelope,
685 app_substructure_link,
690 after_face_attribute,
691 after_coarsen_region,
694 static const char* collapse_reject_name(CollapseReject r);
695 bool collapse_reject(CollapseReject r)
const
697 m_collapse_reject[size_t(r)].fetch_add(1, std::memory_order_relaxed);
700 mutable std::array<std::atomic<long>, size_t(CollapseReject::COUNT)> m_collapse_reject{};
707 void op_event(OpKind k, OpEvent e)
const override
710 m_op_events[size_t(k)][size_t(e)].fetch_add(1, std::memory_order_relaxed);
717 static thread_local OpKind k = OpKind::COUNT;
725 void op_accounting_reset();
726 static const char* op_kind_name(OpKind k);
727 static const char* op_event_name(OpEvent e);
740 std::string before, after;
741 long before_sum = 0, after_sum = 0;
745 long op_event_count(
const OpKind k,
const OpEvent e)
const
747 return m_op_events[size_t(k)][size_t(e)].load(std::memory_order_relaxed);
752 mutable std::array<std::array<std::atomic<long>, size_t(OpEvent::COUNT)>, size_t(OpKind::COUNT)>
755 array<std::array<std::atomic<long>, size_t(SwapReject::COUNT)>, size_t(OpKind::COUNT) + 1>
756 m_swap_reject_by_kind{};
772 return after < before;
780 bool is_surface_flip =
false;
781 size_t sf_a = 0, sf_b = 0, sf_c = 0, sf_d = 0;
790 bool is_edge_on_surface =
false;
791 bool is_edge_open_boundary =
false;
792 size_t edge_order = 0;
793 std::vector<std::pair<FaceAttributes, std::array<size_t, 3>>> changed_faces;
801 double max_energy = 0.;
802 double edge_length = 0.;
803 std::vector<std::pair<FaceAttributes, std::array<size_t, 3>>> changed_faces;
804 std::vector<std::array<size_t, 3>> surface_faces;
805 std::vector<std::array<size_t, 2>> boundary_edges;
806 std::vector<size_t> changed_tids;
807 std::vector<double> changed_energies;
828 std::vector<size_t> ring;
829 std::vector<size_t> frontier;
830 std::vector<size_t> next;
831 std::vector<size_t> one_ring;
832 std::vector<uint32_t> stamp;
835 std::vector<std::pair<size_t, double>> saved_qualities;
840 const std::vector<size_t>&
856 bool is_limit_length,
858 size_t max_passes = 0,
859 bool exact_ball_lock =
false);
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:50
Tuple tuple_from_vertex(size_t vid) const
get a Tuple from global vertex index
Definition TetMesh.cpp:592
What tetwild and simwild's 3D mesh share.
Definition TetOptimizerMesh.h:46
bool m_collapse_limit_length
Definition TetOptimizerMesh.h:148
std::string op_accounting_report(OpKind k) const
Definition TetOptimizerMesh.cpp:867
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:432
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:308
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:297
optimization::NewtonCounters m_newton
Definition TetOptimizerMesh.h:144
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:631
size_t m_force_split_count
Force-splits taken in the current split pass. Diagnostic only.
Definition TetOptimizerMesh.h:155
AttributeContainerGroup m_vertex_attr_group
What p_vertex_attrs points at, so a derived class can register more.
Definition TetOptimizerMesh.h:93
bool vertex_is_on_surface(const size_t vid) const override
Is a vertex part of the substructure.
Definition TetOptimizerMesh.cpp:734
SwapStage
Definition TetOptimizerMesh.h:629
bool collapse_edge_after(const Tuple &t) override
User specified modifications and desideratas for after an edge collapse.
Definition TetOptimizerMeshCollapse.cpp:328
SwapRejectRole
Definition TetOptimizerMesh.h:735
std::unique_ptr< std::atomic< int >[]> m_high_valence_claim
Per-pass claims for the shared high-valence split gate.
Definition TetOptimizerMesh.h:163
std::vector< size_t > all_vertex_ids() const override
Every live vertex, in the mesh's own iteration order.
Definition TetOptimizerMesh.cpp:421
double region_max_quality_rel(const std::vector< size_t > &vids) const
Definition TetOptimizerMeshCollapse.cpp:546
bool m_coarsen_mode
Set for the duration of coarsen_mesh(); read-only while a pass is running.
Definition TetOptimizerMesh.h:815
bool swap_face_after(const Tuple &t) override
User specified modifications and desideratas for after a 2-3 face swap.
Definition TetOptimizerMeshSwaps.cpp:530
virtual void update_attributes()
Update the attributes of the mesh after an iteration of operations.
Definition TetOptimizerMesh.h:237
bool swap_edge_44_after(const Tuple &t) override
User specified modifications and desideratas for after a 4-4 edge swap.
Definition TetOptimizerMeshSwaps.cpp:675
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:468
virtual void split_after_vertex(size_t, bool)
Application metadata not represented by the shared vertex attributes.
Definition TetOptimizerMesh.h:564
bool surface_triangle_is_outside(const size_t a, const size_t b, const size_t c) const
Definition TetOptimizerMesh.h:316
bool swap_edge_56_after(const Tuple &t) override
User specified modifications and desideratas for after a 5-6 edge swap.
Definition TetOptimizerMeshSwaps.cpp:804
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:556
virtual double cell_quality(const size_t tid) const =0
The quality of cell tid, and how to write it.
virtual std::shared_ptr< polysolve::nonlinear::Problem > smoothing_extra_energy(const size_t) const
An extra term the application adds to this vertex's smoothing objective, or null.
Definition TetOptimizerMesh.h:464
virtual bool collapse_before_vertex(size_t, size_t, double)
Definition TetOptimizerMesh.h:540
virtual std::vector< size_t > active_vertices() const
Definition TetOptimizerMesh.cpp:496
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:137
size_t get_order_of_vertex(const size_t vid) const override
Get the order of a vertex.
Definition TetOptimizerMesh.cpp:744
std::string m_debug_pass_name
Definition TetOptimizerMesh.h:247
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:312
std::string swap_reject_report() const
One line per nonzero counter, for a per-turn log. Does not reset.
Definition TetOptimizerMeshSwaps.cpp:214
virtual bool optimization_stalled(double prev, double cur)
Whether an iteration that moved the metric from prev to cur is stalled, and the sizing refinement sho...
Definition TetOptimizerMesh.h:533
bool collapse_edge_before(const Tuple &t) override
User specified preparations and desideratas for an edge collapse before changing the connectivity.
Definition TetOptimizerMeshCollapse.cpp:97
virtual bool is_open_boundary_edge(const Tuple &e)
Whether edge e lies on the boundary of the tracked surface.
Definition TetOptimizerMesh.h:286
int m_iterations_used
Shared TetWild/SimWild outer optimization schedule.
Definition TetOptimizerMesh.h:240
virtual void log_smoothing_pass_accounting()
Definition TetOptimizerMesh.h:475
bool is_inverted_f(const Tuple &loc) const
Inversion check using only the double positions.
Definition TetOptimizerMesh.cpp:508
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:576
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:205
virtual bool optimization_bare_coarsen_passes() const
Whether the loop opens with an UNLIMITED-LENGTH collapse pass.
Definition TetOptimizerMesh.h:508
AttributeContainerGroup m_face_attr_group
What p_face_attrs points at, so a derived class can register more.
Definition TetOptimizerMesh.h:108
virtual bool split_adjust_position(size_t, const std::vector< Tuple > &)
Definition TetOptimizerMesh.h:562
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:681
bool round_vertex(const size_t vid) override
Definition TetOptimizerMesh.h:270
uint32_t m_op_epoch
Definition TetOptimizerMesh.h:516
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:761
static constexpr double MAX_ENERGY
The sentinel get_quality returns for an element AMIPS cannot score.
Definition TetOptimizerMesh.h:118
virtual std::shared_ptr< SampleEnvelope > smoothing_containment_envelope(const size_t vid) const
Envelope the resulting surface triangles are checked against.
Definition TetOptimizerMesh.cpp:414
bool split_edge_before(const Tuple &t) override
User specified preparations and desideratas for an edge split before changing the connectivity.
Definition TetOptimizerMeshSplit.cpp:94
bool swap_edge_after(const Tuple &t) override
User specified modifications and desideratas for after a 3-2 edge swap.
Definition TetOptimizerMeshSwaps.cpp:369
std::set< simplex::Edge > m_force_split_edges
Definition TetOptimizerMesh.h:153
bool swap_reject_kind_only(SwapReject r) const
Definition TetOptimizerMesh.h:647
bool smooth_before(const Tuple &t) override
User specified preparations and desideratas for smoothing a vertex.
Definition TetOptimizerMesh.cpp:327
optimization::SmoothRejectCounters m_smooth_rejects
Why smoothing attempts were refused, reported once per pass.
Definition TetOptimizerMesh.h:140
static OpKind & current_op_kind()
Definition TetOptimizerMesh.h:715
size_t collapse_all_edges_impl(bool is_limit_length, int lock_ring, size_t max_passes=0, bool exact_ball_lock=false)
Definition TetOptimizerMeshCollapse.cpp:24
size_t coarsen_mesh()
Coarsen the mesh without letting the max energy rise.
Definition TetOptimizerMeshCollapse.cpp:598
bool round(const Tuple &v)
Round a vertex position to floating point, if that inverts no incident tet.
Definition TetOptimizerMesh.cpp:576
bool op_accounting_unexplained(OpKind k) const
True when the report for k has a nonzero remainder: a refusal no counter named.
Definition TetOptimizerMesh.cpp:858
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:557
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:586
double m_s_amips
Definition TetOptimizerMesh.h:129
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:344
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:554
long op_unaccounted(OpKind k) const
Attempts minus every outcome: 0 unless an exit of the TetMesh operation reports nothing.
Definition TetOptimizerMesh.cpp:814
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:485
virtual bool swap_quality_allowed(double after, double before, bool) const
Definition TetOptimizerMesh.h:770
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:570
OptimizerParameters & m_params
Definition TetOptimizerMesh.h:122
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:349
std::shared_ptr< SampleEnvelope > m_envelope
Surface envelope: what a surface vertex is pulled toward and checked against.
Definition TetOptimizerMesh.h:125
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:182
bool vertex_is_rounded(const size_t vid) const override
Whether this vertex's double position is currently trusted.
Definition TetOptimizerMesh.h:263
bool face_is_on_surface(const size_t fid) const override
Is a face part of the substructure.
Definition TetOptimizerMesh.cpp:739
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:339
bool smooth_after(const Tuple &t) override
User specified modifications and desideratas for after smoothing a vertex.
Definition TetOptimizerMesh.cpp:341
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:29
Per-thread buffers for the coarsening composite, so it allocates nothing.
Definition TetOptimizerMesh.h:827
What coarsen_mesh() achieved, for the run report. Zeroed when the pass is off.
Definition TetOptimizerMesh.h:387
Definition TetOptimizerMesh.h:798
double region_max_rel_before
Definition TetOptimizerMesh.h:810
A kind's hook refusals by reason: the nonzero leaves as "name=n" and their sums.
Definition TetOptimizerMesh.h:739
Definition TetOptimizerMesh.h:787
Definition TetOptimizerMesh.h:776
Definition TetOptimizerMesh.h:49
size_t partition_id
Required for multi-threading.
Definition TetOptimizerMesh.h:73
size_t m_order
Definition TetOptimizerMesh.h:67
bool m_is_rounded
Definition TetOptimizerMesh.h:57
How the vertex solves of a smoothing pass ended: Newton iterations taken and the status polysolve sto...
Definition SmoothVertex.hpp:75
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