TriangleSelector: Reusing of previously calculated triangle divisions, partial garbage collection implementation

This commit is contained in:
Lukas Matena 2020-06-24 12:24:32 +02:00
parent fb73bb1c66
commit b9321856f3
2 changed files with 117 additions and 65 deletions

View file

@ -800,51 +800,29 @@ void GLGizmoFdmSupports::on_save(cereal::BinaryOutputArchive&) const
} }
// sides_to_split==-1 : just restore previous split
void TriangleSelector::Triangle::set_division(int sides_to_split, int special_side_idx) void TriangleSelector::Triangle::set_division(int sides_to_split, int special_side_idx)
{ {
assert(sides_to_split >=0 && sides_to_split <= 3); assert(sides_to_split >=-1 && sides_to_split <= 3);
assert(special_side_idx >=-1 && special_side_idx < 3); assert(special_side_idx >=-1 && special_side_idx < 3);
// If splitting one or two sides, second argument must be provided. // If splitting one or two sides, second argument must be provided.
assert(sides_to_split != 1 || special_side_idx != -1); assert(sides_to_split != 1 || special_side_idx != -1);
assert(sides_to_split != 2 || special_side_idx != -1); assert(sides_to_split != 2 || special_side_idx != -1);
division_type = sides_to_split | ((special_side_idx != -1 ? special_side_idx : 0 ) <<2); if (sides_to_split != -1) {
} this->number_of_splits = sides_to_split;
if (sides_to_split != 0) {
assert(old_number_of_splits == 0);
this->special_side_idx = special_side_idx;
void TriangleSelector::Triangle::set_state(FacetSupportType type) this->old_number_of_splits = sides_to_split;
{ }
// If this is not a leaf-node, this makes no sense and }
// the bits are used for storing index of an edge. else {
assert(! is_split()); assert(old_number_of_splits != 0);
division_type = (int8_t(type) << 2); this->number_of_splits = old_number_of_splits;
} // indices of children should still be there.
}
int TriangleSelector::Triangle::side_to_keep() const
{
assert(number_of_split_sides() == 2);
return division_type >> 2;
}
int TriangleSelector::Triangle::side_to_split() const
{
assert(number_of_split_sides() == 1);
return division_type >> 2;
}
FacetSupportType TriangleSelector::Triangle::get_state() const
{
assert(! is_split()); // this must be leaf
return FacetSupportType(division_type >> 2);
} }
@ -938,17 +916,29 @@ bool TriangleSelector::select_triangle(int facet_idx, FacetSupportType type, boo
} }
bool TriangleSelector::split_triangle(int facet_idx) void TriangleSelector::split_triangle(int facet_idx)
{ {
if (m_triangles[facet_idx].is_split()) { if (m_triangles[facet_idx].is_split()) {
// The triangle was divided already. // The triangle is divided already.
return false; return;
} }
Triangle* tr = &m_triangles[facet_idx]; Triangle* tr = &m_triangles[facet_idx];
FacetSupportType old_type = tr->get_state(); FacetSupportType old_type = tr->get_state();
if (tr->was_split_before() != 0) {
// This triangle is not split at the moment, but was at one point
// in history. We can just restore it and resurrect its children.
tr->set_division(-1);
for (int i=0; i<=tr->number_of_split_sides(); ++i) {
m_triangles[tr->children[i]].set_state(old_type);
m_triangles[tr->children[i]].valid = true;
}
return;
}
// If we got here, we are about to actually split the triangle.
const double limit_squared = m_edge_limit_sqr; const double limit_squared = m_edge_limit_sqr;
stl_triangle_vertex_indices& facet = tr->verts_idxs; stl_triangle_vertex_indices& facet = tr->verts_idxs;
@ -964,8 +954,9 @@ bool TriangleSelector::split_triangle(int facet_idx)
side_to_keep = pt_idx; side_to_keep = pt_idx;
} }
if (sides_to_split.empty()) { if (sides_to_split.empty()) {
// This shall be unselected.
tr->set_division(0); tr->set_division(0);
return false; return;
} }
// indices of triangle vertices // indices of triangle vertices
@ -1023,11 +1014,8 @@ bool TriangleSelector::split_triangle(int facet_idx)
assert(! sides_to_split.empty() && int(sides_to_split.size()) <= 3); assert(! sides_to_split.empty() && int(sides_to_split.size()) <= 3);
for (int i=0; i<=int(sides_to_split.size()); ++i) { for (int i=0; i<=int(sides_to_split.size()); ++i) {
tr->children[i] = m_triangles.size()-1-i; tr->children[i] = m_triangles.size()-1-i;
m_triangles[tr->children[i]].parent = facet_idx;
m_triangles[tr->children[i]].set_state(old_type); m_triangles[tr->children[i]].set_state(old_type);
} }
return true;
} }
@ -1166,6 +1154,45 @@ void TriangleSelector::remove_useless_children(int facet_idx)
} }
void TriangleSelector::garbage_collect()
{
// First make a map from old to new triangle indices.
int new_idx = m_orig_size_indices;
std::vector<int> new_triangle_indices(m_triangles.size(), -1);
std::vector<bool> invalid_vertices(m_vertices.size(), false);
for (int i = m_orig_size_indices; i<int(m_triangles.size()); ++i) {
if (m_triangles[i].valid) {
new_triangle_indices[i] = new_idx;
++new_idx;
} else {
// FIXME: Decrement reference counter for the vertices.
}
}
// We can remove all invalid triangles and vertices that are no longer referenced.
m_triangles.erase(std::remove_if(m_triangles.begin()+m_orig_size_indices, m_triangles.end(),
[](const Triangle& tr) { return ! tr.valid; }),
m_triangles.end());
// Now go through all remaining triangles and update changed indices.
for (Triangle& tr : m_triangles) {
assert(tr.valid);
if (tr.is_split()) {
// There are children. Update their indices.
for (int j=0; j<=tr.number_of_split_sides(); ++j) {
assert(new_triangle_indices[tr.children[j]] != -1);
tr.children[j] = new_triangle_indices[tr.children[j]];
}
}
// If this triangle was split before, forget it.
// Children referenced in the cache are dead by now.
tr.forget_history();
}
}
TriangleSelector::TriangleSelector(const TriangleMesh& mesh) TriangleSelector::TriangleSelector(const TriangleMesh& mesh)
{ {
for (const stl_vertex& vert : mesh.its.vertices) for (const stl_vertex& vert : mesh.its.vertices)
@ -1209,6 +1236,20 @@ void TriangleSelector::render(ImGuiWrapper* imgui)
#endif #endif
} }
void TriangleSelector::set_edge_limit(float edge_limit)
{
float new_limit_sqr = std::pow(edge_limit, 2.f);
if (new_limit_sqr != m_edge_limit_sqr) {
m_edge_limit_sqr = new_limit_sqr;
// The way how triangles split may be different now, forget
// all cached splits.
garbage_collect();
}
}
#ifdef PRUSASLICER_TRIANGLE_SELECTOR_DEBUG #ifdef PRUSASLICER_TRIANGLE_SELECTOR_DEBUG
void TriangleSelector::render_debug(ImGuiWrapper* imgui) void TriangleSelector::render_debug(ImGuiWrapper* imgui)
{ {
@ -1218,28 +1259,35 @@ void TriangleSelector::render_debug(ImGuiWrapper* imgui)
imgui->text("Edge limit (mm): "); imgui->text("Edge limit (mm): ");
imgui->slider_float("", &edge_limit, 0.1f, 8.f); imgui->slider_float("", &edge_limit, 0.1f, 8.f);
set_edge_limit(edge_limit); set_edge_limit(edge_limit);
imgui->checkbox("Show triangles: ", m_show_triangles); imgui->checkbox("Show split triangles: ", m_show_triangles);
imgui->checkbox("Show invalid triangles: ", m_show_invalid);
int valid_triangles = std::count_if(m_triangles.begin(), m_triangles.end(), int valid_triangles = std::count_if(m_triangles.begin(), m_triangles.end(),
[](const Triangle& tr) { return tr.valid; }); [](const Triangle& tr) { return tr.valid; });
imgui->text("Valid triangles: " + std::to_string(valid_triangles) + imgui->text("Valid triangles: " + std::to_string(valid_triangles) +
"/" + std::to_string(m_triangles.size())); "/" + std::to_string(m_triangles.size()));
imgui->text("Number of vertices: " + std::to_string(m_vertices.size())); imgui->text("Number of vertices: " + std::to_string(m_vertices.size()));
if (imgui->button("Force garbage collection"))
garbage_collect();
imgui->end(); imgui->end();
if (m_show_triangles) { if (m_show_triangles) {
::glColor3f(0.f, 0.f, 1.f);
::glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); ::glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
::glBegin( GL_TRIANGLES); ::glBegin( GL_TRIANGLES);
for (int tr_id=0; tr_id<int(m_triangles.size()); ++tr_id) { for (int tr_id=0; tr_id<int(m_triangles.size()); ++tr_id) {
const Triangle& tr = m_triangles[tr_id]; const Triangle& tr = m_triangles[tr_id];
if (! tr.valid) if (! m_show_invalid && ! tr.valid)
continue; continue;
if (tr_id == m_orig_size_indices-1) if (tr.valid)
::glColor3f(1.f, 0.f, 0.f); ::glColor3f(1.f, 0.f, 0.f);
else
::glColor3f(1.f, 1.f, 0.f);
if (tr_id < m_orig_size_indices)
::glColor3f(0.f, 0.f, 1.f);
for (int i=0; i<3; ++i) for (int i=0; i<3; ++i)
::glVertex3f(m_vertices[tr.verts_idxs[i]][0], ::glVertex3f(m_vertices[tr.verts_idxs[i]][0],

View file

@ -27,7 +27,7 @@ class ClippingPlane;
// to recursively subdivide the triangles and make the selection finer. // to recursively subdivide the triangles and make the selection finer.
class TriangleSelector { class TriangleSelector {
public: public:
void set_edge_limit(float edge_limit) { m_edge_limit_sqr = std::pow(edge_limit, 2.f); } void set_edge_limit(float edge_limit);
// Create new object on a TriangleMesh. The referenced mesh must // Create new object on a TriangleMesh. The referenced mesh must
// stay valid, a ptr to it is saved and used. // stay valid, a ptr to it is saved and used.
@ -54,6 +54,7 @@ public:
#ifdef PRUSASLICER_TRIANGLE_SELECTOR_DEBUG #ifdef PRUSASLICER_TRIANGLE_SELECTOR_DEBUG
void render_debug(ImGuiWrapper* imgui); void render_debug(ImGuiWrapper* imgui);
bool m_show_triangles{true}; bool m_show_triangles{true};
bool m_show_invalid{false};
#endif #endif
private: private:
@ -62,16 +63,16 @@ private:
public: public:
Triangle(int a, int b, int c) Triangle(int a, int b, int c)
: verts_idxs{stl_triangle_vertex_indices(a, b, c)}, : verts_idxs{stl_triangle_vertex_indices(a, b, c)},
division_type{0} state{FacetSupportType(0)},
number_of_splits{0},
special_side_idx{0},
old_number_of_splits{0}
{} {}
stl_triangle_vertex_indices verts_idxs; stl_triangle_vertex_indices verts_idxs;
// Is this triangle valid or marked to remove? // Is this triangle valid or marked to remove?
bool valid{true}; bool valid{true};
// Index of parent triangle (-1: original)
int parent{-1};
// Children triangles (0 = no child) // Children triangles (0 = no child)
std::array<int, 4> children; std::array<int, 4> children;
@ -79,22 +80,25 @@ private:
void set_division(int sides_to_split, int special_side_idx = -1); void set_division(int sides_to_split, int special_side_idx = -1);
// Get/set current state. // Get/set current state.
void set_state(FacetSupportType state); void set_state(FacetSupportType type) { assert(! is_split()); state = type; }
FacetSupportType get_state() const; FacetSupportType get_state() const { assert(! is_split()); return state; }
// Get info on how it's split. // Get info on how it's split.
bool is_split() const { return number_of_split_sides() != 0; } bool is_split() const { return number_of_split_sides() != 0; }
int number_of_split_sides() const { return division_type & 0b11; } int number_of_split_sides() const { return number_of_splits; }
int side_to_keep() const; int side_to_keep() const { assert(number_of_split_sides() == 2); return special_side_idx; }
int side_to_split() const; int side_to_split() const { assert(number_of_split_sides() == 1); return special_side_idx; }
bool was_split_before() const { return old_number_of_splits != 0; }
void forget_history() { old_number_of_splits = 0; }
private: private:
// Bitmask encoding which sides are split. int number_of_splits;
int8_t division_type; int special_side_idx;
// bits 0, 1 : decimal 0, 1, 2 or 3 (how many sides are split) FacetSupportType state;
// bits 2, 3 (non-leaf): decimal 0, 1 or 2 identifying the special edge
// (one that splits in one-edge split or one that stays in two-edge split). // How many children were spawned during last split?
// bits 2, 3 (leaf): FacetSupportType value // Is not reset on remerging the triangle.
int old_number_of_splits;
}; };
// Lists of vertices and triangles, both original and new // Lists of vertices and triangles, both original and new
@ -129,7 +133,7 @@ private:
int vertices_inside(int facet_idx) const; int vertices_inside(int facet_idx) const;
bool faces_camera(int facet) const; bool faces_camera(int facet) const;
void undivide_triangle(int facet_idx); void undivide_triangle(int facet_idx);
bool split_triangle(int facet_idx); void split_triangle(int facet_idx);
void remove_useless_children(int facet_idx); // No hidden meaning. Triangles are meant. void remove_useless_children(int facet_idx); // No hidden meaning. Triangles are meant.
bool is_pointer_in_triangle(int facet_idx) const; bool is_pointer_in_triangle(int facet_idx) const;
bool is_edge_inside_cursor(int facet_idx) const; bool is_edge_inside_cursor(int facet_idx) const;