From 4b088658096ad38937795a9153ffd8406ce827a3 Mon Sep 17 00:00:00 2001 From: tamasmeszaros Date: Fri, 8 Nov 2019 09:21:30 +0100 Subject: [PATCH] hollowing params renamed, filtering generalized --- src/libslic3r/CMakeLists.txt | 2 + src/libslic3r/OpenVDBUtils.cpp | 31 ++++---------- src/libslic3r/OpenVDBUtils.hpp | 6 ++- src/libslic3r/PrintConfig.cpp | 4 +- src/libslic3r/PrintConfig.hpp | 8 ++-- src/libslic3r/SLA/Hollowing.cpp | 55 +++++++++++++++++++++++++ src/libslic3r/SLA/Hollowing.hpp | 17 ++++++++ src/libslic3r/SLAPrint.cpp | 12 +++--- src/slic3r/GUI/Gizmos/GLGizmoHollow.cpp | 11 ++--- src/slic3r/GUI/Preset.cpp | 4 +- src/slic3r/GUI/Tab.cpp | 4 +- tests/libslic3r/test_hollowing.cpp | 29 +------------ 12 files changed, 111 insertions(+), 72 deletions(-) create mode 100644 src/libslic3r/SLA/Hollowing.cpp create mode 100644 src/libslic3r/SLA/Hollowing.hpp diff --git a/src/libslic3r/CMakeLists.txt b/src/libslic3r/CMakeLists.txt index e4b6ccd10..e28917b5f 100644 --- a/src/libslic3r/CMakeLists.txt +++ b/src/libslic3r/CMakeLists.txt @@ -204,6 +204,8 @@ add_library(libslic3r STATIC SLA/SLARasterWriter.cpp SLA/ConcaveHull.hpp SLA/ConcaveHull.cpp + SLA/Hollowing.hpp + SLA/Hollowing.cpp ) encoding_check(libslic3r) diff --git a/src/libslic3r/OpenVDBUtils.cpp b/src/libslic3r/OpenVDBUtils.cpp index 53f9d5ae9..91ccdd5c4 100644 --- a/src/libslic3r/OpenVDBUtils.cpp +++ b/src/libslic3r/OpenVDBUtils.cpp @@ -2,7 +2,7 @@ #include "OpenVDBUtils.hpp" #include #include -#include +#include #include #include "MTUtils.hpp" @@ -158,35 +158,22 @@ inline void _scale(S s, sla::Contour3D &m) for (auto &p : m.points) p *= s; } -static void filter_grid(openvdb::FloatGrid &grid, double scale, double gain) -{ - static const double ROUNDNESS_COEFF = 1.; - - // Filtering: - if (gain > 0.) { - double rounding = ROUNDNESS_COEFF * gain; - int width = int(rounding * scale); - int count = 1; - openvdb::tools::Filter{grid}.gaussian(width, count); - } -} - template remove_cvref_t _hollowed_interior(Mesh &&mesh, double min_thickness, - double accuracy, - double smoothing) + double quality, + HollowingFilter filt) { using MMesh = remove_cvref_t; MMesh imesh{std::forward(mesh)}; - static const double ACCURACY_COEFF = 7.; + static const double QUALITY_COEFF = 7.; // I can't figure out how to increase the grid resolution through openvdb API // so the model will be scaled up before conversion and the result scaled // down. Voxels have a unit size. If I set voxelSize smaller, it scales // the whole geometry down, and doesn't increase the number of voxels. - auto scale = (1.0 + ACCURACY_COEFF * accuracy); // max 8x upscale, min is native voxel size + auto scale = (1.0 + QUALITY_COEFF * quality); // max 8x upscale, min is native voxel size _scale(scale, imesh); @@ -201,7 +188,7 @@ remove_cvref_t _hollowed_interior(Mesh &&mesh, return MMesh{}; } - filter_grid(*gridptr, scale, smoothing); + if (filt) filt(*gridptr, min_thickness, scale); double iso_surface = -offset; double adaptivity = 0.; @@ -214,10 +201,10 @@ remove_cvref_t _hollowed_interior(Mesh &&mesh, TriangleMesh hollowed_interior(const TriangleMesh &mesh, double min_thickness, - double accuracy, - double smoothing) + double quality, + HollowingFilter filt) { - return _hollowed_interior(mesh, min_thickness, accuracy, smoothing); + return _hollowed_interior(mesh, min_thickness, quality, filt); } } // namespace Slic3r diff --git a/src/libslic3r/OpenVDBUtils.hpp b/src/libslic3r/OpenVDBUtils.hpp index 95b95db7c..bd52e81ee 100644 --- a/src/libslic3r/OpenVDBUtils.hpp +++ b/src/libslic3r/OpenVDBUtils.hpp @@ -18,13 +18,15 @@ TriangleMesh volumeToMesh(const openvdb::FloatGrid &grid, double adaptivity = 0.0, bool relaxDisorientedTriangles = true); +using HollowingFilter = std::function; + // Generate an interior for any solid geometry maintaining a given minimum // wall thickness. The returned mesh has triangles with normals facing inside // the mesh so the result can be directly merged with the input to finish the // hollowing. -// TODO: The thicknes is not strictly maintained due to the used gaussian filter TriangleMesh hollowed_interior(const TriangleMesh &mesh, double min_thickness, - double accuracy = 0.5, double smoothing = 0.5); + double quality = 0.5, + HollowingFilter filt = nullptr); } // namespace Slic3r diff --git a/src/libslic3r/PrintConfig.cpp b/src/libslic3r/PrintConfig.cpp index e621d4b34..96b84cf81 100644 --- a/src/libslic3r/PrintConfig.cpp +++ b/src/libslic3r/PrintConfig.cpp @@ -2847,7 +2847,7 @@ void PrintConfigDef::init_sla_params() def->mode = comSimple; def->set_default_value(new ConfigOptionFloat(4)); - def = this->add("hollowing_accuracy", coFloat); + def = this->add("hollowing_quality", coFloat); def->label = L("Hollowing accuracy"); def->category = L("Hollowing"); def->tooltip = L("Performance vs accuracy of calculation. Lower values may produce unwanted artifacts."); @@ -2856,7 +2856,7 @@ void PrintConfigDef::init_sla_params() def->mode = comExpert; def->set_default_value(new ConfigOptionFloat(0.5)); - def = this->add("hollowing_smoothness", coFloat); + def = this->add("hollowing_flatness", coFloat); def->label = L("Hollowing smoothness"); def->category = L("Hollowing"); def->tooltip = L("The cavity shape is a smoothed version of the outside original shape. " diff --git a/src/libslic3r/PrintConfig.hpp b/src/libslic3r/PrintConfig.hpp index d07999c88..9ed63e5d4 100644 --- a/src/libslic3r/PrintConfig.hpp +++ b/src/libslic3r/PrintConfig.hpp @@ -1099,11 +1099,11 @@ public: ConfigOptionFloat hollowing_min_thickness; // Indirectly controls the voxel size (resolution) used by openvdb - ConfigOptionFloat hollowing_accuracy; + ConfigOptionFloat hollowing_quality; // Indirectly controls the amount of filtering used to blur geometry // features in the created cavity. - ConfigOptionFloat hollowing_smoothness; + ConfigOptionFloat hollowing_flatness; protected: void initialize(StaticCacheBase &cache, const char *base_ptr) @@ -1143,8 +1143,8 @@ protected: OPT_PTR(pad_object_connector_penetration); OPT_PTR(hollowing_enable); OPT_PTR(hollowing_min_thickness); - OPT_PTR(hollowing_accuracy); - OPT_PTR(hollowing_smoothness); + OPT_PTR(hollowing_quality); + OPT_PTR(hollowing_flatness); } }; diff --git a/src/libslic3r/SLA/Hollowing.cpp b/src/libslic3r/SLA/Hollowing.cpp new file mode 100644 index 000000000..430900313 --- /dev/null +++ b/src/libslic3r/SLA/Hollowing.cpp @@ -0,0 +1,55 @@ +#include "Hollowing.hpp" +#include +#include +#include +#include + +namespace Slic3r { +namespace sla { + +namespace { + +void filter_grid_sla(openvdb::FloatGrid &grid, double scale, double /*thickness*/, double flatness) +{ + static const double ROUNDNESS_COEFF = 1.; + + // Filtering: + if (flatness > 0.) { + double rounding = ROUNDNESS_COEFF * flatness; + int width = int(rounding * scale); + int count = 1; + openvdb::tools::Filter{grid}.gaussian(width, count); + } +} +// openvdb::tools::levelSetRebuild(grid, -float(thickness * 2)); +// filter_grid_sla(grid, scale, thickness, flatness); + +// openvdb::tools::levelSetRebuild(grid, float(thickness)); + + +void redist_grid_sla(openvdb::FloatGrid &grid, double scale, double thickness, double flatness) +{ +// openvdb::tools::levelSetRebuild(grid, -float(scale * thickness)); + + + openvdb::tools::LevelSetFilter filt{grid}; + +// filt.gaussian(int(flatness * scale)); + +// openvdb::tools::levelSetRebuild(grid, float(scale * thickness)); + //grid = openvdb::tools::topologyToLevelSet(grid); +} + +} + +TriangleMesh generate_interior(const TriangleMesh &mesh, + double min_thickness, + double quality, + double flatness) +{ + namespace plc = std::placeholders; + auto filt = std::bind(filter_grid_sla, plc::_1, plc::_2, plc::_3, flatness); + return hollowed_interior(mesh, min_thickness, quality, filt); +} + +}} // namespace Slic3r::sla diff --git a/src/libslic3r/SLA/Hollowing.hpp b/src/libslic3r/SLA/Hollowing.hpp new file mode 100644 index 000000000..6abbe9f99 --- /dev/null +++ b/src/libslic3r/SLA/Hollowing.hpp @@ -0,0 +1,17 @@ +#ifndef HOLLOWINGFILTER_HPP +#define HOLLOWINGFILTER_HPP + +#include + +namespace Slic3r { +namespace sla { + +TriangleMesh generate_interior(const TriangleMesh &mesh, + double min_thickness, + double quality = 0.5, + double flatness = 0.5); + +} +} + +#endif // HOLLOWINGFILTER_H diff --git a/src/libslic3r/SLAPrint.cpp b/src/libslic3r/SLAPrint.cpp index d2200bcfb..b150a4f76 100644 --- a/src/libslic3r/SLAPrint.cpp +++ b/src/libslic3r/SLAPrint.cpp @@ -3,7 +3,7 @@ #include "SLA/SLAPad.hpp" #include "SLA/SLAAutoSupports.hpp" #include "ClipperUtils.hpp" -#include "OpenVDBUtils.hpp" +#include "SLA/Hollowing.hpp" #include "Geometry.hpp" #include "MTUtils.hpp" @@ -776,10 +776,10 @@ void SLAPrint::process() po.m_hollowing_data.reset(new SLAPrintObject::HollowingData()); double thickness = po.m_config.hollowing_min_thickness.getFloat(); - double accuracy = po.m_config.hollowing_accuracy.getFloat(); - double blur = po.m_config.hollowing_smoothness.getFloat(); + double accuracy = po.m_config.hollowing_quality.getFloat(); + double blur = po.m_config.hollowing_flatness.getFloat(); po.m_hollowing_data->interior = - hollowed_interior(po.transformed_mesh(), thickness, accuracy, blur); + generate_interior(po.transformed_mesh(), thickness, accuracy, blur); if (po.m_hollowing_data->interior.empty()) BOOST_LOG_TRIVIAL(warning) << "Hollowed interior is empty!"; @@ -1754,8 +1754,8 @@ bool SLAPrintObject::invalidate_state_by_config_options(const std::vector& Preset::sla_print_options() "pad_object_connector_penetration", "hollowing_enable", "hollowing_min_thickness", - "hollowing_accuracy", - "hollowing_smoothness", + "hollowing_quality", + "hollowing_flatness", "output_filename_format", "default_sla_print_profile", "compatible_printers", diff --git a/src/slic3r/GUI/Tab.cpp b/src/slic3r/GUI/Tab.cpp index d20e0ee6c..333384802 100644 --- a/src/slic3r/GUI/Tab.cpp +++ b/src/slic3r/GUI/Tab.cpp @@ -3568,8 +3568,8 @@ void TabSLAPrint::build() optgroup = page->new_optgroup(_(L("Hollowing"))); optgroup->append_single_option_line("hollowing_enable"); optgroup->append_single_option_line("hollowing_min_thickness"); - optgroup->append_single_option_line("hollowing_accuracy"); - optgroup->append_single_option_line("hollowing_smoothness"); + optgroup->append_single_option_line("hollowing_quality"); + optgroup->append_single_option_line("hollowing_flatness"); page = add_options_page(_(L("Advanced")), "wrench"); optgroup = page->new_optgroup(_(L("Slicing"))); diff --git a/tests/libslic3r/test_hollowing.cpp b/tests/libslic3r/test_hollowing.cpp index 9a2ea2e72..4264774a3 100644 --- a/tests/libslic3r/test_hollowing.cpp +++ b/tests/libslic3r/test_hollowing.cpp @@ -2,7 +2,7 @@ #include #include -#include "libslic3r/OpenVDBUtils.hpp" +#include "libslic3r/SLA/Hollowing.hpp" #include #include "libslic3r/Format/OBJ.hpp" @@ -22,38 +22,13 @@ static Slic3r::TriangleMesh load_model(const std::string &obj_filename) return mesh; } -//static Slic3r::TriangleMesh hollowed_interior(const Slic3r::TriangleMesh &mesh, -// double min_thickness) -//{ -// Slic3r::sla::Contour3D imesh = Slic3r::sla::Contour3D{mesh}; - -// double scale = std::max(1.0, 3. / min_thickness); -// double offset = scale * min_thickness; -// float range = float(std::max(2 * offset, scale)); - -// for (auto &p : imesh.points) p *= scale; -// auto ptr = Slic3r::meshToVolume(imesh, {}, 0.1f * float(offset), range); - -// REQUIRE(ptr); - -// openvdb::tools::Filter{*ptr}.gaussian(int(scale), 1); - -// double iso_surface = -offset; -// double adaptivity = 0.; -// Slic3r::sla::Contour3D omesh = Slic3r::volumeToMesh(*ptr, iso_surface, adaptivity); - -// for (auto &p : omesh.points) p /= scale; - -// return to_triangle_mesh(omesh); -//} - TEST_CASE("Negative 3D offset should produce smaller object.", "[Hollowing]") { Slic3r::TriangleMesh in_mesh = load_model("20mm_cube.obj"); Benchmark bench; bench.start(); - Slic3r::TriangleMesh out_mesh = hollowed_interior(in_mesh, 0.5); + Slic3r::TriangleMesh out_mesh = Slic3r::sla::generate_interior(in_mesh, 0.5); bench.stop();