TriangleSelector: Vertices are reference-counted and garbage collected

Garbage collection is triggered automatically when more than half of all triangles are invalid
This commit is contained in:
Lukas Matena 2020-06-24 14:47:53 +02:00
parent b9321856f3
commit da6acd73e2
2 changed files with 120 additions and 57 deletions

View file

@ -33,7 +33,7 @@ public:
// stay valid, a ptr to it is saved and used.
explicit TriangleSelector(const TriangleMesh& mesh);
// Select all triangles inside the circle, subdivide where needed.
// Select all triangles fully inside the circle, subdivide where needed.
void select_patch(const Vec3f& hit, // point where to start
int facet_start, // facet that point belongs to
const Vec3f& source, // camera position (mesh coords)
@ -41,14 +41,11 @@ public:
float radius_sqr, // squared radius of the cursor
FacetSupportType new_state); // enforcer or blocker?
void unselect_all();
// Render current selection. Transformation matrices are supposed
// to be already set.
void render(ImGuiWrapper* imgui = nullptr);
// Remove all unnecessary data (such as vertices that are not needed
// because the selection has been made larger.
// Remove all unnecessary data.
void garbage_collect();
#ifdef PRUSASLICER_TRIANGLE_SELECTOR_DEBUG
@ -59,21 +56,24 @@ public:
private:
// Triangle and info about how it's split.
struct Triangle {
class Triangle {
public:
// Use TriangleSelector::push_triangle to create a new triangle.
// It increments/decrements reference counter on vertices.
Triangle(int a, int b, int c)
: verts_idxs{stl_triangle_vertex_indices(a, b, c)},
: verts_idxs{a, b, c},
state{FacetSupportType(0)},
number_of_splits{0},
special_side_idx{0},
old_number_of_splits{0}
{}
stl_triangle_vertex_indices verts_idxs;
// Indices into m_vertices.
std::array<int, 3> verts_idxs;
// Is this triangle valid or marked to remove?
// Is this triangle valid or marked to be removed?
bool valid{true};
// Children triangles (0 = no child)
// Children triangles.
std::array<int, 4> children;
// Set the division type.
@ -101,22 +101,31 @@ private:
int old_number_of_splits;
};
struct Vertex {
explicit Vertex(const stl_vertex& vert)
: v{vert},
ref_cnt{0}
{}
stl_vertex v;
int ref_cnt;
};
// Lists of vertices and triangles, both original and new
std::vector<stl_vertex> m_vertices;
std::vector<Vertex> m_vertices;
std::vector<Triangle> m_triangles;
const TriangleMesh* m_mesh;
// Number of invalid triangles (to trigger garbage collection).
int m_invalid_triangles;
// Limiting length of triangle side (squared).
float m_edge_limit_sqr = 1.f;
// Number of original vertices and triangles.
int m_orig_size_vertices;
int m_orig_size_indices;
// Limits for stopping the recursion.
float m_max_edge_length;
int m_max_recursion_depth;
// Caches for cursor position, radius and direction.
// Cache for cursor position, radius and direction.
struct Cursor {
Vec3f center;
Vec3f source;
@ -130,13 +139,14 @@ private:
bool select_triangle(int facet_idx, FacetSupportType type,
bool recursive_call = false);
bool is_point_inside_cursor(const Vec3f& point) const;
int vertices_inside(int facet_idx) const;
int vertices_inside(int facet_idx) const;
bool faces_camera(int facet) const;
void undivide_triangle(int facet_idx);
void split_triangle(int facet_idx);
void remove_useless_children(int facet_idx); // No hidden meaning. Triangles are meant.
bool is_pointer_in_triangle(int facet_idx) const;
bool is_edge_inside_cursor(int facet_idx) const;
void push_triangle(int a, int b, int c);
};