Enhanced hollowing scheme, closing distance working as expected.

This commit is contained in:
tamasmeszaros 2019-11-08 16:51:43 +01:00
parent 4b08865809
commit ac8eab5fa8
10 changed files with 160 additions and 165 deletions

View File

@ -2,9 +2,9 @@
#include "OpenVDBUtils.hpp" #include "OpenVDBUtils.hpp"
#include <openvdb/tools/MeshToVolume.h> #include <openvdb/tools/MeshToVolume.h>
#include <openvdb/tools/VolumeToMesh.h> #include <openvdb/tools/VolumeToMesh.h>
#include <openvdb/tools/TopologyToLevelSet.h> #include <openvdb/tools/LevelSetRebuild.h>
#include <boost/log/trivial.hpp>
#include "MTUtils.hpp" //#include "MTUtils.hpp"
namespace Slic3r { namespace Slic3r {
@ -58,7 +58,7 @@ void Contour3DDataAdapter::getIndexSpacePoint(size_t n,
// docs say it should be called ones. It does a mutex lock-unlock sequence all // docs say it should be called ones. It does a mutex lock-unlock sequence all
// even if was called previously. // even if was called previously.
openvdb::FloatGrid::Ptr meshToVolume(const TriangleMesh &mesh, openvdb::FloatGrid::Ptr mesh_to_grid(const TriangleMesh &mesh,
const openvdb::math::Transform &tr, const openvdb::math::Transform &tr,
float exteriorBandWidth, float exteriorBandWidth,
float interiorBandWidth, float interiorBandWidth,
@ -70,7 +70,7 @@ openvdb::FloatGrid::Ptr meshToVolume(const TriangleMesh &mesh,
interiorBandWidth, flags); interiorBandWidth, flags);
} }
static openvdb::FloatGrid::Ptr meshToVolume(const sla::Contour3D &mesh, openvdb::FloatGrid::Ptr mesh_to_grid(const sla::Contour3D &mesh,
const openvdb::math::Transform &tr, const openvdb::math::Transform &tr,
float exteriorBandWidth, float exteriorBandWidth,
float interiorBandWidth, float interiorBandWidth,
@ -82,13 +82,8 @@ static openvdb::FloatGrid::Ptr meshToVolume(const sla::Contour3D &mesh,
flags); flags);
} }
inline Vec3f to_vec3f(const openvdb::Vec3s &v) { return Vec3f{v.x(), v.y(), v.z()}; }
inline Vec3d to_vec3d(const openvdb::Vec3s &v) { return to_vec3f(v).cast<double>(); }
inline Vec3i to_vec3i(const openvdb::Vec3I &v) { return Vec3i{int(v[0]), int(v[1]), int(v[2])}; }
inline Vec4i to_vec4i(const openvdb::Vec4I &v) { return Vec4i{int(v[0]), int(v[1]), int(v[2]), int(v[3])}; }
template<class Grid> template<class Grid>
sla::Contour3D __volumeToMesh(const Grid &grid, sla::Contour3D _volumeToMesh(const Grid &grid,
double isovalue, double isovalue,
double adaptivity, double adaptivity,
bool relaxDisorientedTriangles) bool relaxDisorientedTriangles)
@ -114,97 +109,27 @@ sla::Contour3D __volumeToMesh(const Grid &grid,
return ret; return ret;
} }
template<class Mesh = sla::Contour3D> inline TriangleMesh grid_to_mesh(const openvdb::FloatGrid &grid,
Mesh _volumeToMesh(const openvdb::FloatGrid &grid,
double isovalue = 0.0,
double adaptivity = 0.0,
bool relaxDisorientedTriangles = true);
template<> inline
TriangleMesh _volumeToMesh<TriangleMesh>(const openvdb::FloatGrid &grid,
double isovalue, double isovalue,
double adaptivity, double adaptivity,
bool relaxDisorientedTriangles) bool relaxDisorientedTriangles)
{ {
return to_triangle_mesh(__volumeToMesh(grid, isovalue, adaptivity, return to_triangle_mesh(
relaxDisorientedTriangles)); _volumeToMesh(grid, isovalue, adaptivity, relaxDisorientedTriangles));
} }
template<> inline sla::Contour3D grid_to_contour3d(const openvdb::FloatGrid &grid,
sla::Contour3D _volumeToMesh<sla::Contour3D>(const openvdb::FloatGrid &grid, double isovalue,
double isovalue, double adaptivity,
double adaptivity, bool relaxDisorientedTriangles)
bool relaxDisorientedTriangles)
{ {
return __volumeToMesh(grid, isovalue, adaptivity, return _volumeToMesh(grid, isovalue, adaptivity,
relaxDisorientedTriangles); relaxDisorientedTriangles);
} }
TriangleMesh volumeToMesh(const openvdb::FloatGrid &grid, openvdb::FloatGrid::Ptr redistance_grid(const openvdb::FloatGrid &grid, double iso, double er, double ir)
double isovalue,
double adaptivity,
bool relaxDisorientedTriangles)
{ {
return _volumeToMesh<TriangleMesh>(grid, isovalue, adaptivity, return openvdb::tools::levelSetRebuild(grid, float(iso), float(er), float(ir));
relaxDisorientedTriangles);
}
template<class S, class = FloatingOnly<S>>
inline void _scale(S s, TriangleMesh &m) { m.scale(float(s)); }
template<class S, class = FloatingOnly<S>>
inline void _scale(S s, sla::Contour3D &m)
{
for (auto &p : m.points) p *= s;
}
template<class Mesh>
remove_cvref_t<Mesh> _hollowed_interior(Mesh &&mesh,
double min_thickness,
double quality,
HollowingFilter filt)
{
using MMesh = remove_cvref_t<Mesh>;
MMesh imesh{std::forward<Mesh>(mesh)};
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 + QUALITY_COEFF * quality); // max 8x upscale, min is native voxel size
_scale(scale, imesh);
double offset = scale * min_thickness;
float range = float(std::max(2 * offset, scale));
auto gridptr = meshToVolume(imesh, {}, 0.1f * float(offset), range);
assert(gridptr);
if (!gridptr) {
BOOST_LOG_TRIVIAL(error) << "Returned OpenVDB grid is NULL";
return MMesh{};
}
if (filt) filt(*gridptr, min_thickness, scale);
double iso_surface = -offset;
double adaptivity = 0.;
auto omesh = _volumeToMesh<MMesh>(*gridptr, iso_surface, adaptivity);
_scale(1. / scale, omesh);
return omesh;
}
TriangleMesh hollowed_interior(const TriangleMesh &mesh,
double min_thickness,
double quality,
HollowingFilter filt)
{
return _hollowed_interior(mesh, min_thickness, quality, filt);
} }
} // namespace Slic3r } // namespace Slic3r

View File

@ -7,26 +7,37 @@
namespace Slic3r { namespace Slic3r {
openvdb::FloatGrid::Ptr meshToVolume(const TriangleMesh & mesh, inline Vec3f to_vec3f(const openvdb::Vec3s &v) { return Vec3f{v.x(), v.y(), v.z()}; }
inline Vec3d to_vec3d(const openvdb::Vec3s &v) { return to_vec3f(v).cast<double>(); }
inline Vec3i to_vec3i(const openvdb::Vec3I &v) { return Vec3i{int(v[0]), int(v[1]), int(v[2])}; }
inline Vec4i to_vec4i(const openvdb::Vec4I &v) { return Vec4i{int(v[0]), int(v[1]), int(v[2]), int(v[3])}; }
openvdb::FloatGrid::Ptr mesh_to_grid(const TriangleMesh & mesh,
const openvdb::math::Transform &tr = {}, const openvdb::math::Transform &tr = {},
float exteriorBandWidth = 3.0f, float exteriorBandWidth = 3.0f,
float interiorBandWidth = 3.0f, float interiorBandWidth = 3.0f,
int flags = 0); int flags = 0);
TriangleMesh volumeToMesh(const openvdb::FloatGrid &grid, openvdb::FloatGrid::Ptr mesh_to_grid(const sla::Contour3D & mesh,
const openvdb::math::Transform &tr = {},
float exteriorBandWidth = 3.0f,
float interiorBandWidth = 3.0f,
int flags = 0);
sla::Contour3D grid_to_contour3d(const openvdb::FloatGrid &grid,
double isovalue,
double adaptivity,
bool relaxDisorientedTriangles = true);
TriangleMesh grid_to_mesh(const openvdb::FloatGrid &grid,
double isovalue = 0.0, double isovalue = 0.0,
double adaptivity = 0.0, double adaptivity = 0.0,
bool relaxDisorientedTriangles = true); bool relaxDisorientedTriangles = true);
using HollowingFilter = std::function<void(openvdb::FloatGrid& grid, double thickness, double scale)>; openvdb::FloatGrid::Ptr redistance_grid(const openvdb::FloatGrid &grid,
double iso,
// Generate an interior for any solid geometry maintaining a given minimum double ext_range = 3.,
// wall thickness. The returned mesh has triangles with normals facing inside double int_range = 3.);
// the mesh so the result can be directly merged with the input to finish the
// hollowing.
TriangleMesh hollowed_interior(const TriangleMesh &mesh, double min_thickness,
double quality = 0.5,
HollowingFilter filt = nullptr);
} // namespace Slic3r } // namespace Slic3r

View File

@ -2856,15 +2856,13 @@ void PrintConfigDef::init_sla_params()
def->mode = comExpert; def->mode = comExpert;
def->set_default_value(new ConfigOptionFloat(0.5)); def->set_default_value(new ConfigOptionFloat(0.5));
def = this->add("hollowing_flatness", coFloat); def = this->add("hollowing_closing_distance", coFloat);
def->label = L("Hollowing smoothness"); def->label = L("Hollowing closing distance");
def->category = L("Hollowing"); def->category = L("Hollowing");
def->tooltip = L("The cavity shape is a smoothed version of the outside original shape. " def->tooltip = L("");
"Possible values span from 0 to 1 and control the amount of surface smoothing.");
def->min = 0; def->min = 0;
def->max = 1;
def->mode = comExpert; def->mode = comExpert;
def->set_default_value(new ConfigOptionFloat(0.5)); def->set_default_value(new ConfigOptionFloat(2.0));
} }
void PrintConfigDef::handle_legacy(t_config_option_key &opt_key, std::string &value) void PrintConfigDef::handle_legacy(t_config_option_key &opt_key, std::string &value)

View File

@ -1101,9 +1101,8 @@ public:
// Indirectly controls the voxel size (resolution) used by openvdb // Indirectly controls the voxel size (resolution) used by openvdb
ConfigOptionFloat hollowing_quality; ConfigOptionFloat hollowing_quality;
// Indirectly controls the amount of filtering used to blur geometry // Indirectly controls the minimum size of created cavities.
// features in the created cavity. ConfigOptionFloat hollowing_closing_distance;
ConfigOptionFloat hollowing_flatness;
protected: protected:
void initialize(StaticCacheBase &cache, const char *base_ptr) void initialize(StaticCacheBase &cache, const char *base_ptr)
@ -1144,7 +1143,7 @@ protected:
OPT_PTR(hollowing_enable); OPT_PTR(hollowing_enable);
OPT_PTR(hollowing_min_thickness); OPT_PTR(hollowing_min_thickness);
OPT_PTR(hollowing_quality); OPT_PTR(hollowing_quality);
OPT_PTR(hollowing_flatness); OPT_PTR(hollowing_closing_distance);
} }
}; };

View File

@ -1,55 +1,122 @@
#include "Hollowing.hpp"
#include <openvdb/tools/Filter.h>
#include <openvdb/tools/LevelSetRebuild.h>
#include <openvdb/tools/LevelSetFilter.h>
#include <functional> #include <functional>
//#include <openvdb/tools/Filter.h>
#include <boost/log/trivial.hpp>
#include "Hollowing.hpp"
#include <libslic3r/MTUtils.hpp>
namespace Slic3r { namespace Slic3r {
namespace sla { namespace sla {
namespace { //namespace {
void filter_grid_sla(openvdb::FloatGrid &grid, double scale, double /*thickness*/, double flatness) //void filter_grid_sla(openvdb::FloatGrid::Ptr &grid, double scale, double /*thickness*/, double flatness, double closing_dist)
//{
// 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);
// }
//}
//}
template<class S, class = FloatingOnly<S>>
inline void _scale(S s, TriangleMesh &m) { m.scale(float(s)); }
template<class S, class = FloatingOnly<S>>
inline void _scale(S s, sla::Contour3D &m)
{ {
static const double ROUNDNESS_COEFF = 1.; for (auto &p : m.points) p *= s;
}
// Filtering: template<class Mesh>
if (flatness > 0.) { remove_cvref_t<Mesh> _grid_to_mesh(const openvdb::FloatGrid &grid,
double rounding = ROUNDNESS_COEFF * flatness; double isosurf,
int width = int(rounding * scale); double adapt);
int count = 1;
openvdb::tools::Filter<openvdb::FloatGrid>{grid}.gaussian(width, count); template<>
TriangleMesh _grid_to_mesh<TriangleMesh>(const openvdb::FloatGrid &grid,
double isosurf,
double adapt)
{
return grid_to_mesh(grid, isosurf, adapt);
}
template<>
sla::Contour3D _grid_to_mesh<sla::Contour3D>(const openvdb::FloatGrid &grid,
double isosurf,
double adapt)
{
return grid_to_contour3d(grid, isosurf, adapt);
}
template<class Mesh>
remove_cvref_t<Mesh> _generate_interior(Mesh &&mesh,
double min_thickness,
double voxel_scale,
double closing_dist)
{
// namespace plc = std::placeholders;
// auto filt = std::bind(redist_grid_sla, plc::_1, plc::_2, plc::_3, flatness, closing_dist);
// return hollowed_interior(mesh, min_thickness, quality, filt);
using MMesh = remove_cvref_t<Mesh>;
MMesh imesh{std::forward<Mesh>(mesh)};
_scale(voxel_scale, imesh);
double offset = voxel_scale * min_thickness;
double D = voxel_scale * closing_dist;
float out_range = 0.1f * float(offset);
float in_range = 1.1f * float(offset + D);
auto gridptr = mesh_to_grid(imesh, {}, out_range, in_range);
assert(gridptr);
if (!gridptr) {
BOOST_LOG_TRIVIAL(error) << "Returned OpenVDB grid is NULL";
return MMesh{};
} }
}
// openvdb::tools::levelSetRebuild(grid, -float(thickness * 2));
// filter_grid_sla(grid, scale, thickness, flatness);
// openvdb::tools::levelSetRebuild(grid, float(thickness)); if (closing_dist > .0) {
gridptr = redistance_grid(*gridptr, -(offset + D), double(in_range));
} else {
D = -offset;
}
// openvdb::tools::Filter<openvdb::FloatGrid> filt{*gridptr};
// filt.offset(float(offset + D));
void redist_grid_sla(openvdb::FloatGrid &grid, double scale, double thickness, double flatness) double iso_surface = D;
{ double adaptivity = 0.;
// openvdb::tools::levelSetRebuild(grid, -float(scale * thickness)); auto omesh = _grid_to_mesh<MMesh>(*gridptr, iso_surface, adaptivity);
_scale(1. / voxel_scale, omesh);
openvdb::tools::LevelSetFilter<openvdb::FloatGrid> filt{grid}; return omesh;
// filt.gaussian(int(flatness * scale));
// openvdb::tools::levelSetRebuild(grid, float(scale * thickness));
//grid = openvdb::tools::topologyToLevelSet(grid);
}
} }
TriangleMesh generate_interior(const TriangleMesh &mesh, TriangleMesh generate_interior(const TriangleMesh &mesh,
double min_thickness, double min_thickness,
double quality, double quality,
double flatness) double closing_dist)
{ {
namespace plc = std::placeholders; static const double MAX_OVERSAMPL = 7.;
auto filt = std::bind(filter_grid_sla, plc::_1, plc::_2, plc::_3, flatness);
return hollowed_interior(mesh, min_thickness, quality, filt); // 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.
//
// max 8x upscale, min is native voxel size
auto voxel_scale = (1.0 + MAX_OVERSAMPL * quality);
return _generate_interior(mesh, min_thickness, voxel_scale, closing_dist);
} }
}} // namespace Slic3r::sla }} // namespace Slic3r::sla

View File

@ -776,10 +776,10 @@ void SLAPrint::process()
po.m_hollowing_data.reset(new SLAPrintObject::HollowingData()); po.m_hollowing_data.reset(new SLAPrintObject::HollowingData());
double thickness = po.m_config.hollowing_min_thickness.getFloat(); double thickness = po.m_config.hollowing_min_thickness.getFloat();
double accuracy = po.m_config.hollowing_quality.getFloat(); double quality = po.m_config.hollowing_quality.getFloat();
double blur = po.m_config.hollowing_flatness.getFloat(); double closing_d = po.m_config.hollowing_closing_distance.getFloat();
po.m_hollowing_data->interior = po.m_hollowing_data->interior =
generate_interior(po.transformed_mesh(), thickness, accuracy, blur); generate_interior(po.transformed_mesh(), thickness, quality, closing_d);
if (po.m_hollowing_data->interior.empty()) if (po.m_hollowing_data->interior.empty())
BOOST_LOG_TRIVIAL(warning) << "Hollowed interior is empty!"; BOOST_LOG_TRIVIAL(warning) << "Hollowed interior is empty!";
@ -1755,7 +1755,7 @@ bool SLAPrintObject::invalidate_state_by_config_options(const std::vector<t_conf
if ( opt_key == "hollowing_enable" if ( opt_key == "hollowing_enable"
|| opt_key == "hollowing_min_thickness" || opt_key == "hollowing_min_thickness"
|| opt_key == "hollowing_quality" || opt_key == "hollowing_quality"
|| opt_key == "hollowing_flatness" || opt_key == "hollowing_closing_distance"
) { ) {
steps.emplace_back(slaposHollowing); steps.emplace_back(slaposHollowing);
} else if ( } else if (

View File

@ -600,7 +600,7 @@ void GLGizmoHollow::hollow_mesh()
TriangleMesh cavity = sla::generate_interior(*m_mesh, TriangleMesh cavity = sla::generate_interior(*m_mesh,
double(m_offset), double(m_offset),
double(m_accuracy), double(m_accuracy),
double(m_smoothness)); double(m_closing_d));
if (cavity.empty()) return; if (cavity.empty()) return;
@ -753,7 +753,6 @@ RENDER_AGAIN:
// m_imgui->text(" "); // vertical gap // m_imgui->text(" "); // vertical gap
m_imgui->text("Offset: "); m_imgui->text("Offset: ");
ImGui::SameLine(); ImGui::SameLine();
ImGui::SliderFloat(" ", &m_offset, 0.f, 10.f, "%.1f"); ImGui::SliderFloat(" ", &m_offset, 0.f, 10.f, "%.1f");
@ -763,10 +762,9 @@ RENDER_AGAIN:
ImGui::SameLine(); ImGui::SameLine();
ImGui::SliderFloat(" ", &m_accuracy, 0.f, 1.f, "%.1f"); ImGui::SliderFloat(" ", &m_accuracy, 0.f, 1.f, "%.1f");
// TODO: only in expert mode: m_imgui->text("Closing distance: ");
m_imgui->text("Smoothness: ");
ImGui::SameLine(); ImGui::SameLine();
ImGui::SliderFloat(" ", &m_smoothness, 0.f, 1.f, "%.1f"); ImGui::SliderFloat(" ", &m_closing_d, 0.f, 20.f, "%.1f");
} }
else { // not in editing mode: else { // not in editing mode:
m_imgui->text(m_desc.at("minimal_distance")); m_imgui->text(m_desc.at("minimal_distance"));

View File

@ -109,19 +109,16 @@ private:
std::vector<sla::SupportPoint> m_normal_cache; // to restore after discarding changes or undo/redo std::vector<sla::SupportPoint> m_normal_cache; // to restore after discarding changes or undo/redo
float m_offset = 2.0f; float m_offset = 2.0f;
float m_accuracy = 0.5f;
float m_closing_d = 2.f;
float m_clipping_plane_distance = 0.f; float m_clipping_plane_distance = 0.f;
std::unique_ptr<ClippingPlane> m_clipping_plane; std::unique_ptr<ClippingPlane> m_clipping_plane;
float m_accuracy = 0.5f;
// This map holds all translated description texts, so they can be easily referenced during layout calculations // This map holds all translated description texts, so they can be easily referenced during layout calculations
// etc. When language changes, GUI is recreated and this class constructed again, so the change takes effect. // etc. When language changes, GUI is recreated and this class constructed again, so the change takes effect.
std::map<std::string, wxString> m_desc; std::map<std::string, wxString> m_desc;
float m_smoothness = 0.5f;
GLSelectionRectangle m_selection_rectangle; GLSelectionRectangle m_selection_rectangle;
bool m_wait_for_up_event = false; bool m_wait_for_up_event = false;

View File

@ -499,7 +499,7 @@ const std::vector<std::string>& Preset::sla_print_options()
"hollowing_enable", "hollowing_enable",
"hollowing_min_thickness", "hollowing_min_thickness",
"hollowing_quality", "hollowing_quality",
"hollowing_flatness", "hollowing_closing_distance",
"output_filename_format", "output_filename_format",
"default_sla_print_profile", "default_sla_print_profile",
"compatible_printers", "compatible_printers",

View File

@ -3569,7 +3569,7 @@ void TabSLAPrint::build()
optgroup->append_single_option_line("hollowing_enable"); optgroup->append_single_option_line("hollowing_enable");
optgroup->append_single_option_line("hollowing_min_thickness"); optgroup->append_single_option_line("hollowing_min_thickness");
optgroup->append_single_option_line("hollowing_quality"); optgroup->append_single_option_line("hollowing_quality");
optgroup->append_single_option_line("hollowing_flatness"); optgroup->append_single_option_line("hollowing_closing_distance");
page = add_options_page(_(L("Advanced")), "wrench"); page = add_options_page(_(L("Advanced")), "wrench");
optgroup = page->new_optgroup(_(L("Slicing"))); optgroup = page->new_optgroup(_(L("Slicing")));