diff --git a/src/libslic3r/Model.cpp b/src/libslic3r/Model.cpp index a9835eaa19..b5b9a008d0 100644 --- a/src/libslic3r/Model.cpp +++ b/src/libslic3r/Model.cpp @@ -1467,7 +1467,11 @@ int ModelVolume::extruder_id() const bool ModelVolume::is_splittable() const { - return mesh.stl.stats.number_of_parts > 1; + // the call mesh.is_splittable() is expensive, so cache the value to calculate it only once + if (m_is_splittable == -1) + m_is_splittable = (int)mesh.is_splittable(); + + return m_is_splittable == 1; } void ModelVolume::center_geometry() diff --git a/src/libslic3r/Model.hpp b/src/libslic3r/Model.hpp index 919e7715c0..8a48f8ee96 100644 --- a/src/libslic3r/Model.hpp +++ b/src/libslic3r/Model.hpp @@ -420,6 +420,12 @@ private: TriangleMesh m_convex_hull; Geometry::Transformation m_transformation; + // flag to optimize the checking if the volume is splittable + // -1 -> is unknown value (before first cheking) + // 0 -> is not splittable + // 1 -> is splittable + mutable int m_is_splittable{ -1 }; + ModelVolume(ModelObject *object, const TriangleMesh &mesh) : mesh(mesh), m_type(ModelVolumeType::MODEL_PART), object(object) { if (mesh.stl.stats.number_of_facets > 1) diff --git a/src/libslic3r/TriangleMesh.cpp b/src/libslic3r/TriangleMesh.cpp index 1baf3cee30..7595688603 100644 --- a/src/libslic3r/TriangleMesh.cpp +++ b/src/libslic3r/TriangleMesh.cpp @@ -338,6 +338,19 @@ void TriangleMesh::rotate(double angle, Point* center) this->translate(c(0), c(1), 0); } +/** + * Calculates whether or not the mesh is splittable. + */ +bool TriangleMesh::is_splittable() const +{ + std::vector visited; + find_unvisited_neighbors(visited); + + // Try finding an unvisited facet. If there are none, the mesh is not splittable. + auto it = std::find(visited.begin(), visited.end(), false); + return it != visited.end(); +} + /** * Visit all unvisited neighboring facets that are reachable from the first unvisited facet, * and return them. diff --git a/src/libslic3r/TriangleMesh.hpp b/src/libslic3r/TriangleMesh.hpp index e538b76937..b204a9a3ec 100644 --- a/src/libslic3r/TriangleMesh.hpp +++ b/src/libslic3r/TriangleMesh.hpp @@ -68,6 +68,7 @@ public: size_t facets_count() const { return this->stl.stats.number_of_facets; } bool empty() const { return this->facets_count() == 0; } + bool is_splittable() const; std::deque find_unvisited_neighbors(std::vector &facet_visited) const; stl_file stl;