2019-11-08 08:21:30 +00:00
|
|
|
#include <functional>
|
|
|
|
|
2019-11-12 15:53:47 +00:00
|
|
|
#include <libslic3r/OpenVDBUtils.hpp>
|
|
|
|
#include <libslic3r/TriangleMesh.hpp>
|
2019-11-11 10:41:14 +00:00
|
|
|
#include <libslic3r/SLA/Hollowing.hpp>
|
2020-06-25 11:58:51 +00:00
|
|
|
#include <libslic3r/SLA/IndexedMesh.hpp>
|
2020-01-08 16:10:11 +00:00
|
|
|
#include <libslic3r/ClipperUtils.hpp>
|
2020-01-23 09:57:51 +00:00
|
|
|
#include <libslic3r/SimplifyMesh.hpp>
|
2020-06-16 11:17:06 +00:00
|
|
|
#include <libslic3r/SLA/SupportTreeMesher.hpp>
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2019-11-08 15:51:43 +00:00
|
|
|
#include <boost/log/trivial.hpp>
|
|
|
|
|
|
|
|
#include <libslic3r/MTUtils.hpp>
|
2019-11-11 10:41:14 +00:00
|
|
|
#include <libslic3r/I18N.hpp>
|
|
|
|
|
|
|
|
//! macro used to mark string used at localization,
|
|
|
|
//! return same string
|
|
|
|
#define L(s) Slic3r::I18N::translate(s)
|
2019-11-08 15:51:43 +00:00
|
|
|
|
2019-11-08 08:21:30 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
namespace sla {
|
|
|
|
|
2019-11-08 15:51:43 +00:00
|
|
|
template<class S, class = FloatingOnly<S>>
|
|
|
|
inline void _scale(S s, TriangleMesh &m) { m.scale(float(s)); }
|
2019-11-08 08:21:30 +00:00
|
|
|
|
2019-11-08 15:51:43 +00:00
|
|
|
template<class S, class = FloatingOnly<S>>
|
2020-01-23 09:57:51 +00:00
|
|
|
inline void _scale(S s, Contour3D &m) { for (auto &p : m.points) p *= s; }
|
2019-11-08 08:21:30 +00:00
|
|
|
|
2020-01-23 09:57:51 +00:00
|
|
|
static TriangleMesh _generate_interior(const TriangleMesh &mesh,
|
|
|
|
const JobController &ctl,
|
|
|
|
double min_thickness,
|
|
|
|
double voxel_scale,
|
|
|
|
double closing_dist)
|
2019-11-08 08:21:30 +00:00
|
|
|
{
|
2020-01-23 09:57:51 +00:00
|
|
|
TriangleMesh imesh{mesh};
|
2019-11-08 08:21:30 +00:00
|
|
|
|
2019-11-08 15:51:43 +00:00
|
|
|
_scale(voxel_scale, imesh);
|
2019-11-08 08:21:30 +00:00
|
|
|
|
2019-11-08 15:51:43 +00:00
|
|
|
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);
|
2019-11-11 10:41:14 +00:00
|
|
|
|
|
|
|
if (ctl.stopcondition()) return {};
|
|
|
|
else ctl.statuscb(0, L("Hollowing"));
|
|
|
|
|
2019-11-08 15:51:43 +00:00
|
|
|
auto gridptr = mesh_to_grid(imesh, {}, out_range, in_range);
|
2019-11-08 08:21:30 +00:00
|
|
|
|
2019-11-08 15:51:43 +00:00
|
|
|
assert(gridptr);
|
2019-11-08 08:21:30 +00:00
|
|
|
|
2019-11-08 15:51:43 +00:00
|
|
|
if (!gridptr) {
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "Returned OpenVDB grid is NULL";
|
2020-01-23 09:57:51 +00:00
|
|
|
return {};
|
2019-11-08 15:51:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-11 10:41:14 +00:00
|
|
|
if (ctl.stopcondition()) return {};
|
|
|
|
else ctl.statuscb(30, L("Hollowing"));
|
|
|
|
|
2019-11-08 15:51:43 +00:00
|
|
|
if (closing_dist > .0) {
|
|
|
|
gridptr = redistance_grid(*gridptr, -(offset + D), double(in_range));
|
|
|
|
} else {
|
|
|
|
D = -offset;
|
|
|
|
}
|
|
|
|
|
2019-11-11 10:41:14 +00:00
|
|
|
if (ctl.stopcondition()) return {};
|
|
|
|
else ctl.statuscb(70, L("Hollowing"));
|
|
|
|
|
2019-11-08 15:51:43 +00:00
|
|
|
double iso_surface = D;
|
|
|
|
double adaptivity = 0.;
|
2020-01-23 09:57:51 +00:00
|
|
|
auto omesh = grid_to_mesh(*gridptr, iso_surface, adaptivity);
|
2019-11-08 15:51:43 +00:00
|
|
|
|
|
|
|
_scale(1. / voxel_scale, omesh);
|
|
|
|
|
2019-11-11 10:41:14 +00:00
|
|
|
if (ctl.stopcondition()) return {};
|
|
|
|
else ctl.statuscb(100, L("Hollowing"));
|
|
|
|
|
2019-11-08 15:51:43 +00:00
|
|
|
return omesh;
|
2019-11-08 08:21:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 15:53:47 +00:00
|
|
|
std::unique_ptr<TriangleMesh> generate_interior(const TriangleMesh & mesh,
|
|
|
|
const HollowingConfig &hc,
|
|
|
|
const JobController & ctl)
|
2019-11-08 08:21:30 +00:00
|
|
|
{
|
2020-01-23 09:57:51 +00:00
|
|
|
static const double MIN_OVERSAMPL = 3.;
|
|
|
|
static const double MAX_OVERSAMPL = 8.;
|
2019-11-08 15:51:43 +00:00
|
|
|
|
2019-11-12 15:53:47 +00:00
|
|
|
// 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.
|
2019-11-08 15:51:43 +00:00
|
|
|
//
|
|
|
|
// max 8x upscale, min is native voxel size
|
2020-01-23 09:57:51 +00:00
|
|
|
auto voxel_scale = MIN_OVERSAMPL + (MAX_OVERSAMPL - MIN_OVERSAMPL) * hc.quality;
|
|
|
|
auto meshptr = std::make_unique<TriangleMesh>(
|
2019-11-12 15:53:47 +00:00
|
|
|
_generate_interior(mesh, ctl, hc.min_thickness, voxel_scale,
|
|
|
|
hc.closing_distance));
|
2020-01-23 09:57:51 +00:00
|
|
|
|
2020-06-10 12:16:11 +00:00
|
|
|
if (meshptr && !meshptr->empty()) {
|
2020-01-23 09:57:51 +00:00
|
|
|
|
|
|
|
// 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;
|
2019-11-12 15:53:47 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 10:02:54 +00:00
|
|
|
Contour3D DrainHole::to_mesh() const
|
|
|
|
{
|
|
|
|
auto r = double(radius);
|
|
|
|
auto h = double(height);
|
2020-02-06 16:53:03 +00:00
|
|
|
sla::Contour3D hole = sla::cylinder(r, h, steps);
|
2019-12-16 10:02:54 +00:00
|
|
|
Eigen::Quaterniond q;
|
|
|
|
q.setFromTwoVectors(Vec3d{0., 0., 1.}, normal.cast<double>());
|
|
|
|
for(auto& p : hole.points) p = q * p + pos.cast<double>();
|
2020-01-09 10:19:52 +00:00
|
|
|
|
2019-12-16 10:02:54 +00:00
|
|
|
return hole;
|
|
|
|
}
|
|
|
|
|
2019-11-12 15:53:47 +00:00
|
|
|
bool DrainHole::operator==(const DrainHole &sp) const
|
|
|
|
{
|
2019-11-13 14:57:38 +00:00
|
|
|
return (pos == sp.pos) && (normal == sp.normal) &&
|
|
|
|
is_approx(radius, sp.radius) &&
|
|
|
|
is_approx(height, sp.height);
|
2019-11-08 08:21:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 14:48:52 +00:00
|
|
|
bool DrainHole::is_inside(const Vec3f& pt) const
|
|
|
|
{
|
|
|
|
Eigen::Hyperplane<float, 3> plane(normal, pos);
|
|
|
|
float dist = plane.signedDistance(pt);
|
2019-12-16 10:02:54 +00:00
|
|
|
if (dist < float(EPSILON) || dist > height)
|
2019-11-15 14:48:52 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
Eigen::ParametrizedLine<float, 3> axis(pos, normal);
|
|
|
|
if ( axis.squaredDistance(pt) < pow(radius, 2.f))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-19 13:27:05 +00:00
|
|
|
// Given a line s+dir*t, find parameter t of intersections with the hole
|
|
|
|
// and the normal (points inside the hole). Outputs through out reference,
|
|
|
|
// returns true if two intersections were found.
|
|
|
|
bool DrainHole::get_intersections(const Vec3f& s, const Vec3f& dir,
|
|
|
|
std::array<std::pair<float, Vec3d>, 2>& out)
|
|
|
|
const
|
2019-11-15 14:48:52 +00:00
|
|
|
{
|
|
|
|
assert(is_approx(normal.norm(), 1.f));
|
|
|
|
const Eigen::ParametrizedLine<float, 3> ray(s, dir.normalized());
|
|
|
|
|
2019-11-19 13:27:05 +00:00
|
|
|
for (size_t i=0; i<2; ++i)
|
2020-06-25 11:58:51 +00:00
|
|
|
out[i] = std::make_pair(sla::IndexedMesh::hit_result::infty(), Vec3d::Zero());
|
2019-11-19 13:27:05 +00:00
|
|
|
|
2019-11-15 14:48:52 +00:00
|
|
|
const float sqr_radius = pow(radius, 2.f);
|
|
|
|
|
|
|
|
// first check a bounding sphere of the hole:
|
|
|
|
Vec3f center = pos+normal*height/2.f;
|
|
|
|
float sqr_dist_limit = pow(height/2.f, 2.f) + sqr_radius ;
|
|
|
|
if (ray.squaredDistance(center) > sqr_dist_limit)
|
2019-11-19 13:27:05 +00:00
|
|
|
return false;
|
2019-11-15 14:48:52 +00:00
|
|
|
|
|
|
|
// The line intersects the bounding sphere, look for intersections with
|
|
|
|
// bases of the cylinder.
|
|
|
|
|
|
|
|
size_t found = 0; // counts how many intersections were found
|
|
|
|
Eigen::Hyperplane<float, 3> base;
|
|
|
|
if (! is_approx(ray.direction().dot(normal), 0.f)) {
|
|
|
|
for (size_t i=1; i<=1; --i) {
|
|
|
|
Vec3f cylinder_center = pos+i*height*normal;
|
2019-11-26 10:36:09 +00:00
|
|
|
if (i == 0) {
|
|
|
|
// The hole base can be identical to mesh surface if it is flat
|
|
|
|
// let's better move the base outward a bit
|
|
|
|
cylinder_center -= EPSILON*normal;
|
|
|
|
}
|
2019-11-15 14:48:52 +00:00
|
|
|
base = Eigen::Hyperplane<float, 3>(normal, cylinder_center);
|
|
|
|
Vec3f intersection = ray.intersectionPoint(base);
|
|
|
|
// Only accept the point if it is inside the cylinder base.
|
|
|
|
if ((cylinder_center-intersection).squaredNorm() < sqr_radius) {
|
2019-11-19 13:27:05 +00:00
|
|
|
out[found].first = ray.intersectionParameter(base);
|
|
|
|
out[found].second = (i==0 ? 1. : -1.) * normal.cast<double>();
|
2019-11-15 14:48:52 +00:00
|
|
|
++found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// In case the line was perpendicular to the cylinder axis, previous
|
|
|
|
// block was skipped, but base will later be assumed to be valid.
|
2019-11-26 10:36:09 +00:00
|
|
|
base = Eigen::Hyperplane<float, 3>(normal, pos-EPSILON*normal);
|
2019-11-15 14:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// In case there is still an intersection to be found, check the wall
|
|
|
|
if (found != 2 && ! is_approx(std::abs(ray.direction().dot(normal)), 1.f)) {
|
|
|
|
// Project the ray onto the base plane
|
|
|
|
Vec3f proj_origin = base.projection(ray.origin());
|
|
|
|
Vec3f proj_dir = base.projection(ray.origin()+ray.direction())-proj_origin;
|
|
|
|
// save how the parameter scales and normalize the projected direction
|
|
|
|
float par_scale = proj_dir.norm();
|
|
|
|
proj_dir = proj_dir/par_scale;
|
|
|
|
Eigen::ParametrizedLine<float, 3> projected_ray(proj_origin, proj_dir);
|
|
|
|
// Calculate point on the secant that's closest to the center
|
|
|
|
// and its distance to the circle along the projected line
|
|
|
|
Vec3f closest = projected_ray.projection(pos);
|
|
|
|
float dist = sqrt((sqr_radius - (closest-pos).squaredNorm()));
|
|
|
|
// Unproject both intersections on the original line and check
|
|
|
|
// they are on the cylinder and not past it:
|
|
|
|
for (int i=-1; i<=1 && found !=2; i+=2) {
|
|
|
|
Vec3f isect = closest + i*dist * projected_ray.direction();
|
2020-01-17 15:01:49 +00:00
|
|
|
Vec3f to_isect = isect-proj_origin;
|
|
|
|
float par = to_isect.norm() / par_scale;
|
|
|
|
if (to_isect.normalized().dot(proj_dir.normalized()) < 0.f)
|
|
|
|
par *= -1.f;
|
2019-11-19 13:27:05 +00:00
|
|
|
Vec3d hit_normal = (pos-isect).normalized().cast<double>();
|
2019-11-15 14:48:52 +00:00
|
|
|
isect = ray.pointAt(par);
|
|
|
|
// check that the intersection is between the base planes:
|
|
|
|
float vert_dist = base.signedDistance(isect);
|
|
|
|
if (vert_dist > 0.f && vert_dist < height) {
|
2019-11-19 13:27:05 +00:00
|
|
|
out[found].first = par;
|
|
|
|
out[found].second = hit_normal;
|
2019-11-15 14:48:52 +00:00
|
|
|
++found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If only one intersection was found, it is some corner case,
|
|
|
|
// no intersection will be returned:
|
2019-11-19 13:27:05 +00:00
|
|
|
if (found != 2)
|
|
|
|
return false;
|
2019-11-15 14:48:52 +00:00
|
|
|
|
|
|
|
// Sort the intersections:
|
2019-11-19 13:27:05 +00:00
|
|
|
if (out[0].first > out[1].first)
|
|
|
|
std::swap(out[0], out[1]);
|
2019-11-15 14:48:52 +00:00
|
|
|
|
2019-11-19 13:27:05 +00:00
|
|
|
return true;
|
2019-11-15 14:48:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 16:10:11 +00:00
|
|
|
void cut_drainholes(std::vector<ExPolygons> & obj_slices,
|
|
|
|
const std::vector<float> &slicegrid,
|
|
|
|
float closing_radius,
|
|
|
|
const sla::DrainHoles & holes,
|
|
|
|
std::function<void(void)> thr)
|
|
|
|
{
|
|
|
|
TriangleMesh mesh;
|
2020-01-09 10:19:52 +00:00
|
|
|
for (const sla::DrainHole &holept : holes)
|
|
|
|
mesh.merge(sla::to_triangle_mesh(holept.to_mesh()));
|
2020-01-08 16:10:11 +00:00
|
|
|
|
|
|
|
if (mesh.empty()) return;
|
|
|
|
|
|
|
|
mesh.require_shared_vertices();
|
|
|
|
|
|
|
|
TriangleMeshSlicer slicer(&mesh);
|
|
|
|
|
|
|
|
std::vector<ExPolygons> hole_slices;
|
2020-02-08 20:36:29 +00:00
|
|
|
slicer.slice(slicegrid, SlicingMode::Regular, closing_radius, &hole_slices, thr);
|
2020-01-08 16:10:11 +00:00
|
|
|
|
|
|
|
if (obj_slices.size() != hole_slices.size())
|
|
|
|
BOOST_LOG_TRIVIAL(warning)
|
|
|
|
<< "Sliced object and drain-holes layer count does not match!";
|
|
|
|
|
|
|
|
size_t until = std::min(obj_slices.size(), hole_slices.size());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < until; ++i)
|
|
|
|
obj_slices[i] = diff_ex(obj_slices[i], hole_slices[i]);
|
|
|
|
}
|
|
|
|
|
2020-08-18 09:41:14 +00:00
|
|
|
void hollow_mesh(TriangleMesh &mesh, const HollowingConfig &cfg)
|
|
|
|
{
|
|
|
|
std::unique_ptr<Slic3r::TriangleMesh> inter_ptr =
|
|
|
|
Slic3r::sla::generate_interior(mesh);
|
|
|
|
|
|
|
|
if (inter_ptr) mesh.merge(*inter_ptr);
|
|
|
|
mesh.require_shared_vertices();
|
|
|
|
}
|
|
|
|
|
2019-11-08 08:21:30 +00:00
|
|
|
}} // namespace Slic3r::sla
|