From 39197ecd2dd32ae0e22cf9f19a00ea169798af24 Mon Sep 17 00:00:00 2001 From: tamasmeszaros Date: Mon, 2 Jan 2023 14:36:34 +0100 Subject: [PATCH] Caching of cgal data instead of voxel grid --- src/libslic3r/MeshBoolean.cpp | 5 +++++ src/libslic3r/MeshBoolean.hpp | 2 ++ src/libslic3r/SLAPrint.cpp | 18 +++++----------- src/libslic3r/SLAPrint.hpp | 40 +++-------------------------------- 4 files changed, 15 insertions(+), 50 deletions(-) diff --git a/src/libslic3r/MeshBoolean.cpp b/src/libslic3r/MeshBoolean.cpp index 3373616a47..c7ebcbd19e 100644 --- a/src/libslic3r/MeshBoolean.cpp +++ b/src/libslic3r/MeshBoolean.cpp @@ -315,6 +315,11 @@ bool empty(const CGALMesh &mesh) return mesh.m.is_empty(); } +CGALMeshPtr clone(const CGALMesh &m) +{ + return CGALMeshPtr{new CGALMesh{m}}; +} + } // namespace cgal } // namespace MeshBoolean diff --git a/src/libslic3r/MeshBoolean.hpp b/src/libslic3r/MeshBoolean.hpp index 4210bfe869..e20425c1c7 100644 --- a/src/libslic3r/MeshBoolean.hpp +++ b/src/libslic3r/MeshBoolean.hpp @@ -28,6 +28,8 @@ struct CGALMesh; struct CGALMeshDeleter { void operator()(CGALMesh *ptr); }; using CGALMeshPtr = std::unique_ptr; +CGALMeshPtr clone(const CGALMesh &m); + CGALMeshPtr triangle_mesh_to_cgal( const std::vector &V, const std::vector &F); diff --git a/src/libslic3r/SLAPrint.cpp b/src/libslic3r/SLAPrint.cpp index f37638fb47..0e1f8b4308 100644 --- a/src/libslic3r/SLAPrint.cpp +++ b/src/libslic3r/SLAPrint.cpp @@ -1,5 +1,6 @@ #include "SLAPrint.hpp" #include "SLAPrintSteps.hpp" +#include "CSGMesh/PerformCSGMeshBooleans.hpp" #include "Geometry.hpp" #include "Thread.hpp" @@ -1104,22 +1105,13 @@ void SLAPrint::StatusReporter::operator()(SLAPrint & p, namespace csg { -inline bool operator==(const VoxelizeParams &a, const VoxelizeParams &b) +MeshBoolean::cgal::CGALMeshPtr get_cgalmesh(const CSGPartForStep &part) { - std::hash h; - return h(a) == h(b); -} - -VoxelGridPtr get_voxelgrid(const CSGPartForStep &part, VoxelizeParams p) -{ - VoxelGridPtr &ret = part.gridcache[p]; - - if (!ret && csg::get_mesh(part)) { - p.trafo(csg::get_transform(part)); - ret = mesh_to_grid(*csg::get_mesh(part), p); + if (!part.cgalcache && csg::get_mesh(part)) { + part.cgalcache = csg::get_cgalmesh(static_cast(part)); } - return ret ? clone(*ret) : nullptr; + return part.cgalcache? clone(*part.cgalcache) : nullptr; } } // namespace csg diff --git a/src/libslic3r/SLAPrint.hpp b/src/libslic3r/SLAPrint.hpp index 6379575bf8..182c2f9111 100644 --- a/src/libslic3r/SLAPrint.hpp +++ b/src/libslic3r/SLAPrint.hpp @@ -11,6 +11,7 @@ #include "Format/SLAArchiveWriter.hpp" #include "GCode/ThumbnailData.hpp" #include "libslic3r/CSGMesh/CSGMesh.hpp" +#include "libslic3r/MeshBoolean.hpp" #include "libslic3r/OpenVDBUtils.hpp" #include @@ -49,41 +50,6 @@ enum SliceOrigin { soSupport, soModel }; } // namespace Slic3r -namespace std { - -template<> struct hash { - size_t operator() (const Slic3r::csg::VoxelizeParams &p) const { - int64_t vs = Slic3r::scaled(p.voxel_scale()) >> 10; - int64_t eb = Slic3r::scaled(p.exterior_bandwidth()) >> 10; - int64_t ib = Slic3r::scaled(p.interior_bandwidth()) >> 10; - - size_t h = 0; - boost::hash_combine(h, vs); - boost::hash_combine(h, eb); - boost::hash_combine(h, ib); - - return h; - } -}; - -template<> struct equal_to { - size_t operator() (const Slic3r::csg::VoxelizeParams &p1, - const Slic3r::csg::VoxelizeParams &p2) const { - - int64_t vs1 = Slic3r::scaled(p1.voxel_scale()) >> 10; - int64_t eb1 = Slic3r::scaled(p1.exterior_bandwidth()) >> 10; - int64_t ib1 = Slic3r::scaled(p1.interior_bandwidth()) >> 10; - - int64_t vs2 = Slic3r::scaled(p2.voxel_scale()) >> 10; - int64_t eb2 = Slic3r::scaled(p2.exterior_bandwidth()) >> 10; - int64_t ib2 = Slic3r::scaled(p2.interior_bandwidth()) >> 10; - - return vs1 == vs2 && eb1 == eb2 && ib1 == ib2; - } -}; - -} // namespace std - namespace Slic3r { // Each sla object step can hold a collection of csg operations on the @@ -95,7 +61,7 @@ namespace Slic3r { struct CSGPartForStep : public csg::CSGPart { SLAPrintObjectStep key; - mutable std::unordered_map gridcache; + mutable MeshBoolean::cgal::CGALMeshPtr cgalcache; CSGPartForStep(SLAPrintObjectStep k, CSGPart &&p = {}) : key{k}, CSGPart{std::move(p)} @@ -114,7 +80,7 @@ struct CSGPartForStep : public csg::CSGPart namespace csg { -VoxelGridPtr get_voxelgrid(const CSGPartForStep &part, VoxelizeParams p); +MeshBoolean::cgal::CGALMeshPtr get_cgalmesh(const CSGPartForStep &part); } // namespace csg