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();
236 int m_debug_print_counter = 0;
237 void mesh_improvement(
int max_its = 80);
238 std::tuple<double, double> local_operations(
239 const std::array<int, 4>& ops,
240 bool collapse_limit_length =
true);
254 return m_vertex_attribute.at(vid).m_is_rounded;
262 bool is_edge_on_surface(
const Tuple& loc);
263 bool is_edge_on_bbox(
const Tuple& loc);
298 const std::array<size_t, 3>& vids)
const
308 if (!env)
return false;
309 const auto& VA = m_vertex_attribute;
310 return env->is_outside({{VA[a].m_posf, VA[b].m_posf, VA[c].m_posf}});
317 std::vector<std::array<size_t, 3>> get_faces_by_condition(
320 void output_faces(std::string file, std::function<
bool(
const FaceAttributes&)> cond);
345 void split_all_edges();
350 void collapse_all_edges(
bool is_limit_length =
true);
378 size_t cells_before = 0;
379 size_t cells_after = 0;
380 double max_energy_before = 0.;
381 double max_energy_after = 0.;
387 size_t swap_all_edges_32();
391 size_t swap_all_edges_44();
396 size_t swap_all_edges_56();
401 size_t swap_all_faces();
404 size_t swap_all_edges_all();
406 bool prepare_surface_flip(
const Tuple& t,
const std::vector<size_t>& incident_tets);
411 return wmtk::utils::surface_topology_signature(*
this, [
this](
size_t fid) {
412 return m_face_attribute[fid].m_is_surface_fs;
415 void warn_if_surface_topology_changed(
const SurfaceTopoSignature& before,
const char* where)
418 wmtk::utils::warn_if_surface_topology_changed(before, surface_topology_signature(), where);
422 std::atomic<int> cnt_swap = 0;
423 std::atomic<int> cnt_surface_swap = 0;
424 std::atomic<int> cnt_surface_swap_32 = 0, cnt_surface_swap_44 = 0, cnt_surface_swap_56 = 0;
426 double swap_edge_44_energy(
const std::vector<std::array<size_t, 4>>& tets,
const int op_case)
428 double swap_edge_56_energy(
const std::vector<std::array<size_t, 4>>& tets,
const int op_case)
438 virtual std::shared_ptr<SampleEnvelope> smoothing_energy_envelope(
const size_t vid)
const = 0;
461 void smooth_all_vertices(
const size_t n_iters = 1);
463 bool invariants(
const std::vector<Tuple>& t)
override;
469 virtual double optimization_stop_metric()
const {
return m_params.stop_energy; }
470 virtual size_t refine_sizing_around_worst(
double max_metric) = 0;
495 virtual void write_optimization_debug_output(
const std::string& path) = 0;
496 virtual void optimization_sanity_checks_extra() {}
503 virtual bool optimization_stop_at_float()
const {
return false; }
521 return (prev - cur) <=
m_params.stuck_refine_stall_eps * (cur - optimization_stop_metric());
527 virtual bool collapse_quality_allowed(
size_t v1,
double quality,
double ring_max)
const
529 return !m_vertex_attribute.at(v1).m_is_rounded || quality <= ring_max;
531 virtual bool collapse_is_order_2_edge(
const std::array<size_t, 2>&) {
return false; }
533 collapse_after_connectivity(
size_t,
size_t,
const std::vector<std::array<size_t, 2>>&)
537 virtual void collapse_after_vertex(
size_t,
size_t) {}
552 virtual bool allow_surface_swap()
const = 0;
553 virtual bool check_surface_topology()
const = 0;
557 virtual bool swap_before_surface(
const std::vector<size_t>&,
size_t,
size_t,
size_t,
size_t)
569 bool is_surface_flip =
false;
570 size_t sf_a = 0, sf_b = 0, sf_c = 0, sf_d = 0;
579 bool is_edge_on_surface =
false;
580 bool is_edge_open_boundary =
false;
581 size_t edge_order = 0;
582 std::vector<std::pair<FaceAttributes, std::array<size_t, 3>>> changed_faces;
590 double max_energy = 0.;
591 double edge_length = 0.;
592 std::vector<std::pair<FaceAttributes, std::array<size_t, 3>>> changed_faces;
593 std::vector<std::array<size_t, 3>> surface_faces;
594 std::vector<std::array<size_t, 2>> boundary_edges;
595 std::vector<size_t> changed_tids;
596 std::vector<double> changed_energies;
617 std::vector<size_t> ring;
618 std::vector<size_t> frontier;
619 std::vector<size_t> next;
620 std::vector<size_t> one_ring;
621 std::vector<uint32_t> stamp;
624 std::vector<std::pair<size_t, double>> saved_qualities;
629 const std::vector<size_t>&
645 bool is_limit_length,
647 size_t max_passes = 0,
648 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: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:422
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:297
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:296
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:724
bool collapse_edge_after(const Tuple &t) override
User specified modifications and desideratas for after an edge collapse.
Definition TetOptimizerMeshCollapse.cpp:325
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:411
double region_max_quality_rel(const std::vector< size_t > &vids) const
Definition TetOptimizerMeshCollapse.cpp:515
bool m_coarsen_mode
Set for the duration of coarsen_mesh(); read-only while a pass is running.
Definition TetOptimizerMesh.h:604
bool swap_face_after(const Tuple &t) override
User specified modifications and desideratas for after a 2-3 face swap.
Definition TetOptimizerMeshSwaps.cpp:442
virtual void update_attributes()
Update the attributes of the mesh after an iteration of operations.
Definition TetOptimizerMesh.h:232
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:550
bool surface_triangle_is_outside(const size_t a, const size_t b, const size_t c) const
Definition TetOptimizerMesh.h:305
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:542
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:453
virtual bool collapse_before_vertex(size_t, size_t, double)
Definition TetOptimizerMesh.h:526
virtual std::vector< size_t > active_vertices() const
Definition TetOptimizerMesh.cpp:486
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:734
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:311
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:519
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:275
int m_iterations_used
Shared TetWild/SimWild outer optimization schedule.
Definition TetOptimizerMesh.h:235
bool is_inverted_f(const Tuple &loc) const
Inversion check using only the double positions.
Definition TetOptimizerMesh.cpp:498
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:562
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
virtual bool optimization_bare_coarsen_passes() const
Whether the loop opens with an UNLIMITED-LENGTH collapse pass.
Definition TetOptimizerMesh.h:494
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:548
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:671
bool round_vertex(const size_t vid) override
Definition TetOptimizerMesh.h:259
uint32_t m_op_epoch
Definition TetOptimizerMesh.h:502
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:404
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: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:326
optimization::SmoothRejectCounters m_smooth_rejects
Why smoothing attempts were refused, reported once per pass.
Definition TetOptimizerMesh.h:139
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:567
bool round(const Tuple &v)
Round a vertex position to floating point, if that inverts no incident tet.
Definition TetOptimizerMesh.cpp:566
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:526
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:555
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:333
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:540
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:454
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:556
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:182
bool vertex_is_rounded(const size_t vid) const override
Whether this vertex's double position is currently trusted.
Definition TetOptimizerMesh.h:252
bool face_is_on_surface(const size_t fid) const override
Is a face part of the substructure.
Definition TetOptimizerMesh.cpp:729
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:340
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:616
What coarsen_mesh() achieved, for the run report. Zeroed when the pass is off.
Definition TetOptimizerMesh.h:376
Definition TetOptimizerMesh.h:587
double region_max_rel_before
Definition TetOptimizerMesh.h:599
Definition TetOptimizerMesh.h:576
Definition TetOptimizerMesh.h:565
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