From f33e9bf6095eb4d54fce5946a75e2780fb1502d5 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Mon, 15 Apr 2019 21:58:13 +0200 Subject: [PATCH] TriangleMeshSlicer is now initialized by const-pointer to the mesh, responsibility for calling require_shared_vertices is left to the caller --- src/libslic3r/Model.cpp | 1 + src/libslic3r/PrintObject.cpp | 2 ++ src/libslic3r/SLA/SLABasePool.cpp | 1 + src/libslic3r/SLA/SLASupportTree.cpp | 2 ++ src/libslic3r/SLAPrint.cpp | 1 + src/libslic3r/TriangleMesh.cpp | 6 ++++-- src/libslic3r/TriangleMesh.hpp | 12 +++++------- src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp | 2 ++ 8 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/libslic3r/Model.cpp b/src/libslic3r/Model.cpp index 6b16855e8..5a0558977 100644 --- a/src/libslic3r/Model.cpp +++ b/src/libslic3r/Model.cpp @@ -1189,6 +1189,7 @@ ModelObjectPtrs ModelObject::cut(size_t instance, coordf_t z, bool keep_upper, b volume->mesh.transform(instance_matrix * volume_matrix, true); // Perform cut + volume->mesh.require_shared_vertices(); // TriangleMeshSlicer needs this TriangleMeshSlicer tms(&volume->mesh); tms.cut(float(z), &upper_mesh, &lower_mesh); diff --git a/src/libslic3r/PrintObject.cpp b/src/libslic3r/PrintObject.cpp index 0b51f36ec..bb03d1e9d 100644 --- a/src/libslic3r/PrintObject.cpp +++ b/src/libslic3r/PrintObject.cpp @@ -1813,6 +1813,7 @@ std::vector PrintObject::_slice_volumes(const std::vector &z, TriangleMeshSlicer mslicer; const Print *print = this->print(); auto callback = TriangleMeshSlicer::throw_on_cancel_callback_type([print](){print->throw_if_canceled();}); + mesh.require_shared_vertices(); // TriangleMeshSlicer needs this mslicer.init(&mesh, callback); mslicer.slice(z, float(m_config.slice_closing_radius.value), &layers, callback); m_print->throw_if_canceled(); @@ -1840,6 +1841,7 @@ std::vector PrintObject::_slice_volume(const std::vector &z, TriangleMeshSlicer mslicer; const Print *print = this->print(); auto callback = TriangleMeshSlicer::throw_on_cancel_callback_type([print](){print->throw_if_canceled();}); + mesh.require_shared_vertices(); // TriangleMeshSlicer needs this mslicer.init(&mesh, callback); mslicer.slice(z, float(m_config.slice_closing_radius.value), &layers, callback); m_print->throw_if_canceled(); diff --git a/src/libslic3r/SLA/SLABasePool.cpp b/src/libslic3r/SLA/SLABasePool.cpp index 2aabaa590..94b6c7d9b 100644 --- a/src/libslic3r/SLA/SLABasePool.cpp +++ b/src/libslic3r/SLA/SLABasePool.cpp @@ -552,6 +552,7 @@ void base_plate(const TriangleMesh &mesh, ExPolygons &output, float h, float layerh, ThrowOnCancel thrfn) { TriangleMesh m = mesh; + m.require_shared_vertices(); // TriangleMeshSlicer needs this TriangleMeshSlicer slicer(&m); auto bb = mesh.bounding_box(); diff --git a/src/libslic3r/SLA/SLASupportTree.cpp b/src/libslic3r/SLA/SLASupportTree.cpp index bb0e5e007..37a6bae4b 100644 --- a/src/libslic3r/SLA/SLASupportTree.cpp +++ b/src/libslic3r/SLA/SLASupportTree.cpp @@ -2231,6 +2231,7 @@ SlicedSupports SLASupportTree::slice(float layerh, float init_layerh) const TriangleMesh fullmesh = m_impl->merged_mesh(); fullmesh.merge(get_pad()); + fullmesh.require_shared_vertices(); // TriangleMeshSlicer needs this TriangleMeshSlicer slicer(&fullmesh); SlicedSupports ret; slicer.slice(heights, 0.f, &ret, get().ctl().cancelfn); @@ -2243,6 +2244,7 @@ SlicedSupports SLASupportTree::slice(const std::vector &heights, { TriangleMesh fullmesh = m_impl->merged_mesh(); fullmesh.merge(get_pad()); + fullmesh.require_shared_vertices(); // TriangleMeshSlicer needs this TriangleMeshSlicer slicer(&fullmesh); SlicedSupports ret; slicer.slice(heights, cr, &ret, get().ctl().cancelfn); diff --git a/src/libslic3r/SLAPrint.cpp b/src/libslic3r/SLAPrint.cpp index e5a574463..b7191970d 100644 --- a/src/libslic3r/SLAPrint.cpp +++ b/src/libslic3r/SLAPrint.cpp @@ -706,6 +706,7 @@ void SLAPrint::process() po.m_model_height_levels.emplace_back(it->slice_level()); } + mesh.require_shared_vertices(); // TriangleMeshSlicer needs this TriangleMeshSlicer slicer(&mesh); po.m_model_slices.clear(); diff --git a/src/libslic3r/TriangleMesh.cpp b/src/libslic3r/TriangleMesh.cpp index 2d603661d..f449ac2b4 100644 --- a/src/libslic3r/TriangleMesh.cpp +++ b/src/libslic3r/TriangleMesh.cpp @@ -607,10 +607,12 @@ void TriangleMesh::require_shared_vertices() BOOST_LOG_TRIVIAL(trace) << "TriangleMeshSlicer::require_shared_vertices - end"; } -void TriangleMeshSlicer::init(TriangleMesh *_mesh, throw_on_cancel_callback_type throw_on_cancel) +void TriangleMeshSlicer::init(const TriangleMesh *_mesh, throw_on_cancel_callback_type throw_on_cancel) { mesh = _mesh; - _mesh->require_shared_vertices(); + if (! mesh->has_shared_vertices()) + throw std::invalid_argument("TriangleMeshSlicer was passed a mesh without shared vertices."); + throw_on_cancel(); facets_edges.assign(_mesh->stl.stats.number_of_facets * 3, -1); v_scaled_shared.assign(_mesh->stl.v_shared, _mesh->stl.v_shared + _mesh->stl.stats.shared_vertices); diff --git a/src/libslic3r/TriangleMesh.hpp b/src/libslic3r/TriangleMesh.hpp index 4bf5ce039..60ddcca08 100644 --- a/src/libslic3r/TriangleMesh.hpp +++ b/src/libslic3r/TriangleMesh.hpp @@ -67,18 +67,17 @@ public: TriangleMesh convex_hull_3d() const; void reset_repair_stats(); bool needed_repair() const; + void require_shared_vertices(); + bool has_shared_vertices() const { return stl.v_shared != NULL; } size_t facets_count() const { return this->stl.stats.number_of_facets; } bool empty() const { return this->facets_count() == 0; } - bool is_splittable() const; stl_file stl; bool repaired; - + private: - void require_shared_vertices(); std::deque find_unvisited_neighbors(std::vector &facet_visited) const; - friend class TriangleMeshSlicer; }; enum FacetEdgeType { @@ -159,9 +158,8 @@ class TriangleMeshSlicer public: typedef std::function throw_on_cancel_callback_type; TriangleMeshSlicer() : mesh(nullptr) {} - // Not quite nice, but the constructor and init() methods require non-const mesh pointer to be able to call mesh->require_shared_vertices() - TriangleMeshSlicer(TriangleMesh* mesh) { this->init(mesh, [](){}); } - void init(TriangleMesh *mesh, throw_on_cancel_callback_type throw_on_cancel); + TriangleMeshSlicer(const TriangleMesh* mesh) { this->init(mesh, [](){}); } + void init(const TriangleMesh *mesh, throw_on_cancel_callback_type throw_on_cancel); void slice(const std::vector &z, std::vector* layers, throw_on_cancel_callback_type throw_on_cancel) const; void slice(const std::vector &z, const float closing_radius, std::vector* layers, throw_on_cancel_callback_type throw_on_cancel) const; enum FacetSliceType { diff --git a/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp b/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp index 569684a15..af27c2f9f 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp @@ -178,6 +178,7 @@ void GLGizmoSlaSupports::render_clipping_plane(const Selection& selection, const // The timestamp has changed - stash the mesh and initialize the TMS. m_supports_mesh = print_object->support_mesh(); m_supports_tms.reset(new TriangleMeshSlicer); + m_supports_mesh.require_shared_vertices(); // TriangleMeshSlicer needs this m_supports_tms->init(const_cast(&m_supports_mesh), [](){}); m_old_timestamp = timestamp; } @@ -411,6 +412,7 @@ void GLGizmoSlaSupports::update_mesh() Eigen::MatrixXi& F = m_F; // This mesh does not account for the possible Z up SLA offset. m_mesh = m_model_object->raw_mesh(); + m_mesh.require_shared_vertices(); // TriangleMeshSlicer needs this const stl_file& stl = m_mesh.stl; V.resize(3 * stl.stats.number_of_facets, 3); F.resize(stl.stats.number_of_facets, 3);