add mesh simplification.

SPE-1072 
Working but flipped normals with the interior.
Testing on treefrog passed
Oversampling for hollowed mesh should not be less than 3x
Flip back normals after simplify and remove redundant test code.
This commit is contained in:
tamasmeszaros 2020-01-23 10:57:51 +01:00
parent 63b0eec5a9
commit f8a5796ca5
11 changed files with 875 additions and 44 deletions

View file

@ -7,6 +7,7 @@
#include <libslic3r/SLA/EigenMesh3D.hpp>
#include <libslic3r/SLA/SupportTreeBuilder.hpp>
#include <libslic3r/ClipperUtils.hpp>
#include <libslic3r/SimplifyMesh.hpp>
#include <boost/log/trivial.hpp>
@ -24,41 +25,15 @@ 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, Contour3D &m)
{
for (auto &p : m.points) p *= s;
}
inline void _scale(S s, Contour3D &m) { for (auto &p : m.points) p *= s; }
template<class Mesh>
remove_cvref_t<Mesh> _grid_to_mesh(const openvdb::FloatGrid &grid,
double isosurf,
double adapt);
template<>
TriangleMesh _grid_to_mesh<TriangleMesh>(const openvdb::FloatGrid &grid,
double isosurf,
double adapt)
static TriangleMesh _generate_interior(const TriangleMesh &mesh,
const JobController &ctl,
double min_thickness,
double voxel_scale,
double closing_dist)
{
return grid_to_mesh(grid, isosurf, adapt);
}
template<>
Contour3D _grid_to_mesh<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,
const JobController &ctl,
double min_thickness,
double voxel_scale,
double closing_dist)
{
using MMesh = remove_cvref_t<Mesh>;
MMesh imesh{std::forward<Mesh>(mesh)};
TriangleMesh imesh{mesh};
_scale(voxel_scale, imesh);
@ -76,7 +51,7 @@ remove_cvref_t<Mesh> _generate_interior(Mesh &&mesh,
if (!gridptr) {
BOOST_LOG_TRIVIAL(error) << "Returned OpenVDB grid is NULL";
return MMesh{};
return {};
}
if (ctl.stopcondition()) return {};
@ -93,7 +68,7 @@ remove_cvref_t<Mesh> _generate_interior(Mesh &&mesh,
double iso_surface = D;
double adaptivity = 0.;
auto omesh = _grid_to_mesh<MMesh>(*gridptr, iso_surface, adaptivity);
auto omesh = grid_to_mesh(*gridptr, iso_surface, adaptivity);
_scale(1. / voxel_scale, omesh);
@ -107,7 +82,8 @@ std::unique_ptr<TriangleMesh> generate_interior(const TriangleMesh & mesh,
const HollowingConfig &hc,
const JobController & ctl)
{
static const double MAX_OVERSAMPL = 7.;
static const double MIN_OVERSAMPL = 3.;
static const double MAX_OVERSAMPL = 8.;
// 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
@ -116,10 +92,27 @@ std::unique_ptr<TriangleMesh> generate_interior(const TriangleMesh & mesh,
// voxels.
//
// max 8x upscale, min is native voxel size
auto voxel_scale = (1.0 + MAX_OVERSAMPL * hc.quality);
return std::make_unique<TriangleMesh>(
auto voxel_scale = MIN_OVERSAMPL + (MAX_OVERSAMPL - MIN_OVERSAMPL) * hc.quality;
auto meshptr = std::make_unique<TriangleMesh>(
_generate_interior(mesh, ctl, hc.min_thickness, voxel_scale,
hc.closing_distance));
if (meshptr) {
// This flips the normals to be outward facing...
meshptr->require_shared_vertices();
indexed_triangle_set its = std::move(meshptr->its);
Slic3r::simplify_mesh(its);
// flip normals back...
for (stl_triangle_vertex_indices &ind : its.indices)
std::swap(ind(0), ind(2));
*meshptr = Slic3r::TriangleMesh{its};
}
return meshptr;
}
Contour3D DrainHole::to_mesh() const