hollowing params renamed, filtering generalized

This commit is contained in:
tamasmeszaros 2019-11-08 09:21:30 +01:00
parent bc3d22348a
commit 4b08865809
12 changed files with 111 additions and 72 deletions

View File

@ -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)

View File

@ -2,7 +2,7 @@
#include "OpenVDBUtils.hpp"
#include <openvdb/tools/MeshToVolume.h>
#include <openvdb/tools/VolumeToMesh.h>
#include <openvdb/tools/Filter.h>
#include <openvdb/tools/TopologyToLevelSet.h>
#include <boost/log/trivial.hpp>
#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<openvdb::FloatGrid>{grid}.gaussian(width, count);
}
}
template<class Mesh>
remove_cvref_t<Mesh> _hollowed_interior(Mesh &&mesh,
double min_thickness,
double accuracy,
double smoothing)
double quality,
HollowingFilter filt)
{
using MMesh = remove_cvref_t<Mesh>;
MMesh imesh{std::forward<Mesh>(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<Mesh> _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<Mesh> _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

View File

@ -18,13 +18,15 @@ TriangleMesh volumeToMesh(const openvdb::FloatGrid &grid,
double adaptivity = 0.0,
bool relaxDisorientedTriangles = true);
using HollowingFilter = std::function<void(openvdb::FloatGrid& grid, double thickness, double scale)>;
// 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

View File

@ -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. "

View File

@ -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);
}
};

View File

@ -0,0 +1,55 @@
#include "Hollowing.hpp"
#include <openvdb/tools/Filter.h>
#include <openvdb/tools/LevelSetRebuild.h>
#include <openvdb/tools/LevelSetFilter.h>
#include <functional>
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<openvdb::FloatGrid>{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<openvdb::FloatGrid> 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

View File

@ -0,0 +1,17 @@
#ifndef HOLLOWINGFILTER_HPP
#define HOLLOWINGFILTER_HPP
#include <libslic3r/OpenVDBUtils.hpp>
namespace Slic3r {
namespace sla {
TriangleMesh generate_interior(const TriangleMesh &mesh,
double min_thickness,
double quality = 0.5,
double flatness = 0.5);
}
}
#endif // HOLLOWINGFILTER_H

View File

@ -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<t_conf
for (const t_config_option_key &opt_key : opt_keys) {
if ( opt_key == "hollowing_enable"
|| opt_key == "hollowing_min_thickness"
|| opt_key == "hollowing_accuracy"
|| opt_key == "hollowing_smoothness"
|| opt_key == "hollowing_quality"
|| opt_key == "hollowing_flatness"
) {
steps.emplace_back(slaposHollowing);
} else if (

View File

@ -17,7 +17,7 @@
#include "slic3r/GUI/Plater.hpp"
#include "slic3r/GUI/PresetBundle.hpp"
#include "libslic3r/SLAPrint.hpp"
#include "libslic3r/OpenVDBUtils.hpp"
#include "libslic3r/SLA/Hollowing.hpp"
namespace Slic3r {
@ -597,7 +597,8 @@ void GLGizmoHollow::on_update(const UpdateData& data)
void GLGizmoHollow::hollow_mesh()
{
TriangleMesh cavity = hollowed_interior(*m_mesh, double(m_offset),
TriangleMesh cavity = sla::generate_interior(*m_mesh,
double(m_offset),
double(m_accuracy),
double(m_smoothness));

View File

@ -498,8 +498,8 @@ const std::vector<std::string>& 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",

View File

@ -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")));

View File

@ -2,7 +2,7 @@
#include <fstream>
#include <catch2/catch.hpp>
#include "libslic3r/OpenVDBUtils.hpp"
#include "libslic3r/SLA/Hollowing.hpp"
#include <openvdb/tools/Filter.h>
#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<openvdb::FloatGrid>{*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();