WIP: Reworked slicing

1) Slicing code moved to TriangleMeshSlicer.cpp,hpp from TriangleMesh.cpp,hpp
2) Refactored to use as little as possible of admesh.
This commit is contained in:
Vojtech Bubnik 2021-05-17 20:25:59 +02:00
parent 68d2427a34
commit 308d6b7809
24 changed files with 1828 additions and 1594 deletions

View File

@ -135,7 +135,7 @@ struct stl_file {
std::vector<stl_facet> facet_start;
std::vector<stl_neighbors> neighbors_start;
// Statistics
stl_stats stats;
stl_stats stats;
};
struct indexed_triangle_set
@ -149,9 +149,9 @@ struct indexed_triangle_set
}
std::vector<stl_triangle_vertex_indices> indices;
std::vector<stl_vertex> vertices;
std::vector<stl_vertex> vertices;
//FIXME add normals once we get rid of the stl_file from TriangleMesh completely.
//std::vector<stl_normal> normals
//std::vector<stl_normal> normals
};
extern bool stl_open(stl_file *stl, const char *file);

View File

@ -105,6 +105,15 @@ struct OutRec {
//------------------------------------------------------------------------------
inline IntPoint IntPoint2d(cInt x, cInt y)
{
return IntPoint(x, y
#ifdef CLIPPERLIB_USE_XYZ
, 0
#endif // CLIPPERLIB_USE_XYZ
);
}
inline cInt Round(double val)
{
return static_cast<cInt>((val < 0) ? (val - 0.5) : (val + 0.5));
@ -2243,7 +2252,7 @@ void Clipper::ProcessHorizontal(TEdge *horzEdge)
while (maxIt != m_Maxima.end() && *maxIt < e->Curr.x())
{
if (horzEdge->OutIdx >= 0 && !IsOpen)
AddOutPt(horzEdge, IntPoint(*maxIt, horzEdge->Bot.y()));
AddOutPt(horzEdge, IntPoint2d(*maxIt, horzEdge->Bot.y()));
++maxIt;
}
}
@ -2252,7 +2261,7 @@ void Clipper::ProcessHorizontal(TEdge *horzEdge)
while (maxRit != m_Maxima.rend() && *maxRit > e->Curr.x())
{
if (horzEdge->OutIdx >= 0 && !IsOpen)
AddOutPt(horzEdge, IntPoint(*maxRit, horzEdge->Bot.y()));
AddOutPt(horzEdge, IntPoint2d(*maxRit, horzEdge->Bot.y()));
++maxRit;
}
}
@ -2297,12 +2306,12 @@ void Clipper::ProcessHorizontal(TEdge *horzEdge)
if(dir == dLeftToRight)
{
IntPoint Pt = IntPoint(e->Curr.x(), horzEdge->Curr.y());
IntPoint Pt = IntPoint2d(e->Curr.x(), horzEdge->Curr.y());
IntersectEdges(horzEdge, e, Pt);
}
else
{
IntPoint Pt = IntPoint(e->Curr.x(), horzEdge->Curr.y());
IntPoint Pt = IntPoint2d(e->Curr.x(), horzEdge->Curr.y());
IntersectEdges( e, horzEdge, Pt);
}
TEdge* eNext = (dir == dLeftToRight) ? e->NextInAEL : e->PrevInAEL;
@ -3372,14 +3381,14 @@ void ClipperOffset::AddPath(const Path& path, JoinType joinType, EndType endType
//if this path's lowest pt is lower than all the others then update m_lowest
if (endType != etClosedPolygon) return;
if (m_lowest.x() < 0)
m_lowest = IntPoint(m_polyNodes.ChildCount() - 1, k);
m_lowest = IntPoint2d(m_polyNodes.ChildCount() - 1, k);
else
{
IntPoint ip = m_polyNodes.Childs[(int)m_lowest.x()]->Contour[(int)m_lowest.y()];
if (newNode->Contour[k].y() > ip.y() ||
(newNode->Contour[k].y() == ip.y() &&
newNode->Contour[k].x() < ip.x()))
m_lowest = IntPoint(m_polyNodes.ChildCount() - 1, k);
m_lowest = IntPoint2d(m_polyNodes.ChildCount() - 1, k);
}
}
//------------------------------------------------------------------------------
@ -3427,10 +3436,10 @@ void ClipperOffset::Execute(Paths& solution, double delta)
{
IntRect r = clpr.GetBounds();
Path outer(4);
outer[0] = IntPoint(r.left - 10, r.bottom + 10);
outer[1] = IntPoint(r.right + 10, r.bottom + 10);
outer[2] = IntPoint(r.right + 10, r.top - 10);
outer[3] = IntPoint(r.left - 10, r.top - 10);
outer[0] = IntPoint2d(r.left - 10, r.bottom + 10);
outer[1] = IntPoint2d(r.right + 10, r.bottom + 10);
outer[2] = IntPoint2d(r.right + 10, r.top - 10);
outer[3] = IntPoint2d(r.left - 10, r.top - 10);
clpr.AddPath(outer, ptSubject, true);
clpr.ReverseSolution(true);
@ -3457,10 +3466,10 @@ void ClipperOffset::Execute(PolyTree& solution, double delta)
{
IntRect r = clpr.GetBounds();
Path outer(4);
outer[0] = IntPoint(r.left - 10, r.bottom + 10);
outer[1] = IntPoint(r.right + 10, r.bottom + 10);
outer[2] = IntPoint(r.right + 10, r.top - 10);
outer[3] = IntPoint(r.left - 10, r.top - 10);
outer[0] = IntPoint2d(r.left - 10, r.bottom + 10);
outer[1] = IntPoint2d(r.right + 10, r.bottom + 10);
outer[2] = IntPoint2d(r.right + 10, r.top - 10);
outer[3] = IntPoint2d(r.left - 10, r.top - 10);
clpr.AddPath(outer, ptSubject, true);
clpr.ReverseSolution(true);
@ -3536,7 +3545,7 @@ void ClipperOffset::DoOffset(double delta)
double X = 1.0, Y = 0.0;
for (cInt j = 1; j <= steps; j++)
{
m_destPoly.push_back(IntPoint(
m_destPoly.push_back(IntPoint2d(
Round(m_srcPoly[0].x() + X * delta),
Round(m_srcPoly[0].y() + Y * delta)));
double X2 = X;
@ -3549,7 +3558,7 @@ void ClipperOffset::DoOffset(double delta)
double X = -1.0, Y = -1.0;
for (int j = 0; j < 4; ++j)
{
m_destPoly.push_back(IntPoint(
m_destPoly.push_back(IntPoint2d(
Round(m_srcPoly[0].x() + X * delta),
Round(m_srcPoly[0].y() + Y * delta)));
if (X < 0) X = 1;
@ -3604,9 +3613,9 @@ void ClipperOffset::DoOffset(double delta)
if (node.m_endtype == etOpenButt)
{
int j = len - 1;
pt1 = IntPoint(Round(m_srcPoly[j].x() + m_normals[j].x() * delta), Round(m_srcPoly[j].y() + m_normals[j].y() * delta));
pt1 = IntPoint2d(Round(m_srcPoly[j].x() + m_normals[j].x() * delta), Round(m_srcPoly[j].y() + m_normals[j].y() * delta));
m_destPoly.push_back(pt1);
pt1 = IntPoint(Round(m_srcPoly[j].x() - m_normals[j].x() * delta), Round(m_srcPoly[j].y() - m_normals[j].y() * delta));
pt1 = IntPoint2d(Round(m_srcPoly[j].x() - m_normals[j].x() * delta), Round(m_srcPoly[j].y() - m_normals[j].y() * delta));
m_destPoly.push_back(pt1);
}
else
@ -3631,9 +3640,9 @@ void ClipperOffset::DoOffset(double delta)
if (node.m_endtype == etOpenButt)
{
pt1 = IntPoint(Round(m_srcPoly[0].x() - m_normals[0].x() * delta), Round(m_srcPoly[0].y() - m_normals[0].y() * delta));
pt1 = IntPoint2d(Round(m_srcPoly[0].x() - m_normals[0].x() * delta), Round(m_srcPoly[0].y() - m_normals[0].y() * delta));
m_destPoly.push_back(pt1);
pt1 = IntPoint(Round(m_srcPoly[0].x() + m_normals[0].x() * delta), Round(m_srcPoly[0].y() + m_normals[0].y() * delta));
pt1 = IntPoint2d(Round(m_srcPoly[0].x() + m_normals[0].x() * delta), Round(m_srcPoly[0].y() + m_normals[0].y() * delta));
m_destPoly.push_back(pt1);
}
else
@ -3661,7 +3670,7 @@ void ClipperOffset::OffsetPoint(int j, int& k, JoinType jointype)
double cosA = (m_normals[k].x() * m_normals[j].x() + m_normals[j].y() * m_normals[k].y() );
if (cosA > 0) // angle => 0 degrees
{
m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].x() + m_normals[k].x() * m_delta),
m_destPoly.push_back(IntPoint2d(Round(m_srcPoly[j].x() + m_normals[k].x() * m_delta),
Round(m_srcPoly[j].y() + m_normals[k].y() * m_delta)));
return;
}
@ -3672,10 +3681,10 @@ void ClipperOffset::OffsetPoint(int j, int& k, JoinType jointype)
if (m_sinA * m_delta < 0)
{
m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].x() + m_normals[k].x() * m_delta),
m_destPoly.push_back(IntPoint2d(Round(m_srcPoly[j].x() + m_normals[k].x() * m_delta),
Round(m_srcPoly[j].y() + m_normals[k].y() * m_delta)));
m_destPoly.push_back(m_srcPoly[j]);
m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].x() + m_normals[j].x() * m_delta),
m_destPoly.push_back(IntPoint2d(Round(m_srcPoly[j].x() + m_normals[j].x() * m_delta),
Round(m_srcPoly[j].y() + m_normals[j].y() * m_delta)));
}
else
@ -3699,10 +3708,10 @@ void ClipperOffset::DoSquare(int j, int k)
{
double dx = std::tan(std::atan2(m_sinA,
m_normals[k].x() * m_normals[j].x() + m_normals[k].y() * m_normals[j].y()) / 4);
m_destPoly.push_back(IntPoint(
m_destPoly.push_back(IntPoint2d(
Round(m_srcPoly[j].x() + m_delta * (m_normals[k].x() - m_normals[k].y() * dx)),
Round(m_srcPoly[j].y() + m_delta * (m_normals[k].y() + m_normals[k].x() * dx))));
m_destPoly.push_back(IntPoint(
m_destPoly.push_back(IntPoint2d(
Round(m_srcPoly[j].x() + m_delta * (m_normals[j].x() + m_normals[j].y() * dx)),
Round(m_srcPoly[j].y() + m_delta * (m_normals[j].y() - m_normals[j].x() * dx))));
}
@ -3711,7 +3720,7 @@ void ClipperOffset::DoSquare(int j, int k)
void ClipperOffset::DoMiter(int j, int k, double r)
{
double q = m_delta / r;
m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].x() + (m_normals[k].x() + m_normals[j].x()) * q),
m_destPoly.push_back(IntPoint2d(Round(m_srcPoly[j].x() + (m_normals[k].x() + m_normals[j].x()) * q),
Round(m_srcPoly[j].y() + (m_normals[k].y() + m_normals[j].y()) * q)));
}
//------------------------------------------------------------------------------
@ -3725,14 +3734,14 @@ void ClipperOffset::DoRound(int j, int k)
double X = m_normals[k].x(), Y = m_normals[k].y(), X2;
for (int i = 0; i < steps; ++i)
{
m_destPoly.push_back(IntPoint(
m_destPoly.push_back(IntPoint2d(
Round(m_srcPoly[j].x() + X * m_delta),
Round(m_srcPoly[j].y() + Y * m_delta)));
X2 = X;
X = X * m_cos - m_sin * Y;
Y = X2 * m_sin + Y * m_cos;
}
m_destPoly.push_back(IntPoint(
m_destPoly.push_back(IntPoint2d(
Round(m_srcPoly[j].x() + m_normals[j].x() * m_delta),
Round(m_srcPoly[j].y() + m_normals[j].y() * m_delta)));
}
@ -4001,7 +4010,7 @@ void Minkowski(const Path& poly, const Path& path,
Path p;
p.reserve(polyCnt);
for (size_t j = 0; j < poly.size(); ++j)
p.push_back(IntPoint(path[i].x() + poly[j].x(), path[i].y() + poly[j].y()));
p.push_back(IntPoint2d(path[i].x() + poly[j].x(), path[i].y() + poly[j].y()));
pp.push_back(p);
}
else
@ -4010,7 +4019,7 @@ void Minkowski(const Path& poly, const Path& path,
Path p;
p.reserve(polyCnt);
for (size_t j = 0; j < poly.size(); ++j)
p.push_back(IntPoint(path[i].x() - poly[j].x(), path[i].y() - poly[j].y()));
p.push_back(IntPoint2d(path[i].x() - poly[j].x(), path[i].y() - poly[j].y()));
pp.push_back(p);
}
@ -4045,7 +4054,7 @@ void TranslatePath(const Path& input, Path& output, const IntPoint& delta)
//precondition: input != output
output.resize(input.size());
for (size_t i = 0; i < input.size(); ++i)
output[i] = IntPoint(input[i].x() + delta.x(), input[i].y() + delta.y());
output[i] = IntPoint2d(input[i].x() + delta.x(), input[i].y() + delta.y());
}
//------------------------------------------------------------------------------

View File

@ -198,6 +198,8 @@ add_library(libslic3r STATIC
Tesselate.hpp
TriangleMesh.cpp
TriangleMesh.hpp
TriangleMeshSlicer.cpp
TriangleMeshSlicer.hpp
TriangulateWall.hpp
TriangulateWall.cpp
utils.cpp

View File

@ -3,6 +3,7 @@
#include "ModelArrange.hpp"
#include "Geometry.hpp"
#include "MTUtils.hpp"
#include "TriangleMeshSlicer.hpp"
#include "TriangleSelector.hpp"
#include "Format/AMF.hpp"

View File

@ -10,6 +10,7 @@
#include "Surface.hpp"
#include "Slicing.hpp"
#include "Tesselate.hpp"
#include "TriangleMeshSlicer.hpp"
#include "Utils.hpp"
#include "Fill/FillAdaptive.hpp"
#include "Format/STL.hpp"
@ -2221,9 +2222,8 @@ std::vector<ExPolygons> PrintObject::slice_volumes(
auto callback = TriangleMeshSlicer::throw_on_cancel_callback_type([print](){print->throw_if_canceled();});
// TriangleMeshSlicer needs shared vertices, also this calls the repair() function.
mesh.require_shared_vertices();
TriangleMeshSlicer mslicer;
mslicer.init(&mesh, callback);
mslicer.slice(z, mode, slicing_mode_normal_below_layer, mode_below, float(m_config.slice_closing_radius.value), &layers, callback);
MeshSlicingParamsExtended params { { mode, slicing_mode_normal_below_layer, mode_below }, float(m_config.slice_closing_radius.value) };
slice_mesh(mesh, z, params, layers, callback);
m_print->throw_if_canceled();
}
}
@ -2245,13 +2245,14 @@ std::vector<ExPolygons> PrintObject::slice_volume(const std::vector<float> &z, S
// apply XY shift
mesh.translate(- unscale<float>(m_center_offset.x()), - unscale<float>(m_center_offset.y()), 0);
// perform actual slicing
TriangleMeshSlicer mslicer;
const Print *print = this->print();
auto callback = TriangleMeshSlicer::throw_on_cancel_callback_type([print](){print->throw_if_canceled();});
// TriangleMeshSlicer needs the shared vertices.
mesh.require_shared_vertices();
mslicer.init(&mesh, callback);
mslicer.slice(z, mode, float(m_config.slice_closing_radius.value), &layers, callback);
MeshSlicingParamsExtended params;
params.mode = mode;
params.closing_radius = float(m_config.slice_closing_radius.value);
slice_mesh(mesh, z, params, layers, callback);
m_print->throw_if_canceled();
}
}

View File

@ -2,6 +2,7 @@
#include <libslic3r/OpenVDBUtils.hpp>
#include <libslic3r/TriangleMesh.hpp>
#include <libslic3r/TriangleMeshSlicer.hpp>
#include <libslic3r/SLA/Hollowing.hpp>
#include <libslic3r/SLA/IndexedMesh.hpp>
#include <libslic3r/ClipperUtils.hpp>
@ -296,10 +297,8 @@ void cut_drainholes(std::vector<ExPolygons> & obj_slices,
mesh.require_shared_vertices();
TriangleMeshSlicer slicer(&mesh);
std::vector<ExPolygons> hole_slices;
slicer.slice(slicegrid, SlicingMode::Regular, closing_radius, &hole_slices, thr);
slice_mesh(mesh, slicegrid, closing_radius, hole_slices, thr);
if (obj_slices.size() != hole_slices.size())
BOOST_LOG_TRIVIAL(warning)

View File

@ -2,6 +2,7 @@
#include <libslic3r/SLA/SpatIndex.hpp>
#include <libslic3r/SLA/BoostAdapter.hpp>
#include <libslic3r/SLA/Contour3D.hpp>
#include <libslic3r/TriangleMeshSlicer.hpp>
#include "ConcaveHull.hpp"
@ -476,10 +477,9 @@ void pad_blueprint(const TriangleMesh & mesh,
ThrowOnCancel thrfn)
{
if (mesh.empty()) return;
TriangleMeshSlicer slicer(&mesh);
auto out = reserve_vector<ExPolygons>(heights.size());
slicer.slice(heights, SlicingMode::Regular, 0.f, &out, thrfn);
slice_mesh(mesh, heights, out, thrfn);
size_t count = 0;
for(auto& o : out) count += o.size();

View File

@ -12,6 +12,7 @@
#include <libslic3r/MTUtils.hpp>
#include <libslic3r/ClipperUtils.hpp>
#include <libslic3r/Model.hpp>
#include <libslic3r/TriangleMeshSlicer.hpp>
#include <libnest2d/optimizers/nlopt/genetic.hpp>
#include <libnest2d/optimizers/nlopt/subplex.hpp>
@ -44,9 +45,7 @@ std::vector<ExPolygons> SupportTree::slice(
if (!sup_mesh.empty()) {
slices.emplace_back();
TriangleMeshSlicer sup_slicer(&sup_mesh);
sup_slicer.slice(grid, SlicingMode::Regular, cr, &slices.back(), ctl().cancelfn);
slice_mesh(sup_mesh, grid, cr, slices.back(), ctl().cancelfn);
}
if (!pad_mesh.empty()) {
@ -59,8 +58,7 @@ std::vector<ExPolygons> SupportTree::slice(
auto padgrid = reserve_vector<float>(size_t(cap > 0 ? cap : 0));
std::copy(grid.begin(), maxzit, std::back_inserter(padgrid));
TriangleMeshSlicer pad_slicer(&pad_mesh);
pad_slicer.slice(padgrid, SlicingMode::Regular, cr, &slices.back(), ctl().cancelfn);
slice_mesh(pad_mesh, padgrid, cr, slices.back(), ctl().cancelfn);
}
size_t len = grid.size();

View File

@ -3,6 +3,7 @@
#include <libslic3r/Exception.hpp>
#include <libslic3r/SLAPrintSteps.hpp>
#include <libslic3r/MeshBoolean.hpp>
#include <libslic3r/TriangleMeshSlicer.hpp>
// Need the cylinder method for the the drainholes in hollowing step
#include <libslic3r/SLA/SupportTreeBuilder.hpp>
@ -198,7 +199,7 @@ static std::vector<bool> create_exclude_mask(
std::vector<bool> exclude_mask(its.indices.size(), false);
std::vector< std::vector<size_t> > neighbor_index =
create_neighbor_index(its);
create_vertex_faces_index(its);
auto exclude_neighbors = [&neighbor_index, &exclude_mask](const Vec3i &face)
{
@ -470,13 +471,11 @@ void SLAPrint::Steps::slice_model(SLAPrintObject &po)
for(auto it = slindex_it; it != po.m_slice_index.end(); ++it)
po.m_model_height_levels.emplace_back(it->slice_level());
TriangleMeshSlicer slicer(&mesh);
po.m_model_slices.clear();
float closing_r = float(po.config().slice_closing_radius.value);
auto thr = [this]() { m_print->throw_if_canceled(); };
auto &slice_grid = po.m_model_height_levels;
slicer.slice(slice_grid, SlicingMode::Regular, closing_r, &po.m_model_slices, thr);
slice_mesh(mesh, slice_grid, closing_r, po.m_model_slices, thr);
sla::Interior *interior = po.m_hollowing_data ?
po.m_hollowing_data->interior.get() :
@ -486,9 +485,8 @@ void SLAPrint::Steps::slice_model(SLAPrintObject &po)
TriangleMesh interiormesh = sla::get_mesh(*interior);
interiormesh.repaired = false;
interiormesh.repair(true);
TriangleMeshSlicer interior_slicer(&interiormesh);
std::vector<ExPolygons> interior_slices;
interior_slicer.slice(slice_grid, SlicingMode::Regular, closing_r, &interior_slices, thr);
slice_mesh(interiormesh, slice_grid, closing_r, interior_slices, thr);
sla::ccr::for_each(size_t(0), interior_slices.size(),
[&po, &interior_slices] (size_t i) {

View File

@ -83,12 +83,6 @@ void SVG::draw(const Lines &lines, std::string stroke, coordf_t stroke_width)
this->draw(l, stroke, stroke_width);
}
void SVG::draw(const IntersectionLines &lines, std::string stroke)
{
for (const IntersectionLine &il : lines)
this->draw((Line)il, stroke);
}
void SVG::draw(const ExPolygon &expolygon, std::string fill, const float fill_opacity)
{
this->fill = fill;

View File

@ -43,8 +43,7 @@ public:
void draw(const Line &line, std::string stroke = "black", coordf_t stroke_width = 0);
void draw(const ThickLine &line, const std::string &fill, const std::string &stroke, coordf_t stroke_width = 0);
void draw(const Lines &lines, std::string stroke = "black", coordf_t stroke_width = 0);
void draw(const IntersectionLines &lines, std::string stroke = "black");
void draw(const ExPolygon &expolygon, std::string fill = "grey", const float fill_opacity=1.f);
void draw_outline(const ExPolygon &polygon, std::string stroke_outer = "black", std::string stroke_holes = "blue", coordf_t stroke_width = 0);
void draw(const ExPolygons &expolygons, std::string fill = "grey", const float fill_opacity=1.f);

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,6 @@
#include <admesh/stl.h>
#include <functional>
#include <vector>
#include <boost/thread.hpp>
#include "BoundingBox.hpp"
#include "Line.hpp"
#include "Point.hpp"
@ -24,7 +23,7 @@ public:
TriangleMesh() : repaired(false) {}
TriangleMesh(const Pointf3s &points, const std::vector<Vec3i> &facets);
explicit TriangleMesh(const indexed_triangle_set &M);
void clear() { this->stl.clear(); this->its.clear(); this->repaired = false; }
void clear() { this->stl.clear(); this->its.clear(); this->repaired = false; }
bool ReadSTLFile(const char* input_file) { return stl_open(&stl, input_file); }
bool write_ascii(const char* output_file) { return stl_write_ascii(&this->stl, output_file, ""); }
bool write_binary(const char* output_file) { return stl_write_binary(&this->stl, output_file, ""); }
@ -47,7 +46,7 @@ public:
void mirror_y() { this->mirror(Y); }
void mirror_z() { this->mirror(Z); }
void transform(const Transform3d& t, bool fix_left_handed = false);
void transform(const Matrix3d& t, bool fix_left_handed = false);
void transform(const Matrix3d& t, bool fix_left_handed = false);
void align_to_origin();
void rotate(double angle, Point* center);
TriangleMeshPtrs split() const;
@ -62,7 +61,7 @@ public:
// Return the size of the mesh in coordinates.
Vec3d size() const { return stl.stats.size.cast<double>(); }
/// Return the center of the related bounding box.
Vec3d center() const { return this->bounding_box().center(); }
Vec3d center() const { return this->bounding_box().center(); }
// Returns the convex hull of this TriangleMesh
TriangleMesh convex_hull_3d() const;
// Slice this mesh at the provided Z levels and return the vector
@ -78,8 +77,8 @@ public:
size_t memsize() const;
// Release optional data from the mesh if the object is on the Undo / Redo stack only. Returns the amount of memory released.
size_t release_optional();
// Restore optional data possibly released by release_optional().
void restore_optional();
// Restore optional data possibly released by release_optional().
void restore_optional();
stl_file stl;
indexed_triangle_set its;
@ -92,160 +91,16 @@ private:
// Create an index of faces belonging to each vertex. The returned vector can
// be indexed with vertex indices and contains a list of face indices for each
// vertex.
std::vector< std::vector<size_t> >
create_neighbor_index(const indexed_triangle_set &its);
std::vector<std::vector<size_t>> create_vertex_faces_index(const indexed_triangle_set &its);
enum FacetEdgeType {
// A general case, the cutting plane intersect a face at two different edges.
feGeneral,
// Two vertices are aligned with the cutting plane, the third vertex is below the cutting plane.
feTop,
// Two vertices are aligned with the cutting plane, the third vertex is above the cutting plane.
feBottom,
// All three vertices of a face are aligned with the cutting plane.
feHorizontal
};
class IntersectionReference
{
public:
IntersectionReference() : point_id(-1), edge_id(-1) {}
IntersectionReference(int point_id, int edge_id) : point_id(point_id), edge_id(edge_id) {}
// Where is this intersection point located? On mesh vertex or mesh edge?
// Only one of the following will be set, the other will remain set to -1.
// Index of the mesh vertex.
int point_id;
// Index of the mesh edge.
int edge_id;
};
class IntersectionPoint : public Point, public IntersectionReference
{
public:
IntersectionPoint() {}
IntersectionPoint(int point_id, int edge_id, const Point &pt) : IntersectionReference(point_id, edge_id), Point(pt) {}
IntersectionPoint(const IntersectionReference &ir, const Point &pt) : IntersectionReference(ir), Point(pt) {}
// Inherits coord_t x, y
};
class IntersectionLine : public Line
{
public:
IntersectionLine() : a_id(-1), b_id(-1), edge_a_id(-1), edge_b_id(-1), edge_type(feGeneral), flags(0) {}
bool skip() const { return (this->flags & SKIP) != 0; }
void set_skip() { this->flags |= SKIP; }
bool is_seed_candidate() const { return (this->flags & NO_SEED) == 0 && ! this->skip(); }
void set_no_seed(bool set) { if (set) this->flags |= NO_SEED; else this->flags &= ~NO_SEED; }
// Inherits Point a, b
// For each line end point, either {a,b}_id or {a,b}edge_a_id is set, the other is left to -1.
// Vertex indices of the line end points.
int a_id;
int b_id;
// Source mesh edges of the line end points.
int edge_a_id;
int edge_b_id;
// feGeneral, feTop, feBottom, feHorizontal
FacetEdgeType edge_type;
// Used by TriangleMeshSlicer::slice() to skip duplicate edges.
enum {
// Triangle edge added, because it has no neighbor.
EDGE0_NO_NEIGHBOR = 0x001,
EDGE1_NO_NEIGHBOR = 0x002,
EDGE2_NO_NEIGHBOR = 0x004,
// Triangle edge added, because it makes a fold with another horizontal edge.
EDGE0_FOLD = 0x010,
EDGE1_FOLD = 0x020,
EDGE2_FOLD = 0x040,
// The edge cannot be a seed of a greedy loop extraction (folds are not safe to become seeds).
NO_SEED = 0x100,
SKIP = 0x200,
};
uint32_t flags;
};
typedef std::vector<IntersectionLine> IntersectionLines;
typedef std::vector<IntersectionLine*> IntersectionLinePtrs;
enum class SlicingMode : uint32_t {
// Regular slicing, maintain all contours and their orientation.
Regular,
// Maintain all contours, orient all contours CCW, therefore all holes are being closed.
Positive,
// Orient all contours CCW and keep only the contour with the largest area.
// This mode is useful for slicing complex objects in vase mode.
PositiveLargestContour,
};
class TriangleMeshSlicer
{
public:
typedef std::function<void()> throw_on_cancel_callback_type;
TriangleMeshSlicer() : mesh(nullptr) {}
TriangleMeshSlicer(const TriangleMesh* mesh) { this->init(mesh, [](){}); }
void init(const TriangleMesh *mesh, throw_on_cancel_callback_type throw_on_cancel);
void slice(
const std::vector<float> &z, SlicingMode mode, size_t alternate_mode_first_n_layers, SlicingMode alternate_mode,
std::vector<Polygons>* layers, throw_on_cancel_callback_type throw_on_cancel) const;
void slice(const std::vector<float> &z, SlicingMode mode, std::vector<Polygons>* layers, throw_on_cancel_callback_type throw_on_cancel) const
{ return this->slice(z, mode, 0, mode, layers, throw_on_cancel); }
void slice(
const std::vector<float> &z, SlicingMode mode, size_t alternate_mode_first_n_layers, SlicingMode alternate_mode, const float closing_radius,
std::vector<ExPolygons>* layers, throw_on_cancel_callback_type throw_on_cancel) const;
void slice(const std::vector<float> &z, SlicingMode mode, const float closing_radius,
std::vector<ExPolygons>* layers, throw_on_cancel_callback_type throw_on_cancel) const
{ this->slice(z, mode, 0, mode, closing_radius, layers, throw_on_cancel); }
enum FacetSliceType {
NoSlice = 0,
Slicing = 1,
Cutting = 2
};
FacetSliceType slice_facet(float slice_z, const stl_facet &facet, const int facet_idx,
const float min_z, const float max_z, IntersectionLine *line_out) const;
void cut(float z, TriangleMesh* upper, TriangleMesh* lower) const;
void set_up_direction(const Vec3f& up);
private:
const TriangleMesh *mesh;
// Map from a facet to an edge index.
std::vector<int> facets_edges;
// Scaled copy of this->mesh->stl.v_shared
std::vector<stl_vertex> v_scaled_shared;
// Quaternion that will be used to rotate every facet before the slicing
Eigen::Quaternion<float, Eigen::DontAlign> m_quaternion;
// Whether or not the above quaterion should be used
bool m_use_quaternion = false;
void _slice_do(size_t facet_idx, std::vector<IntersectionLines>* lines, boost::mutex* lines_mutex, const std::vector<float> &z) const;
void make_loops(std::vector<IntersectionLine> &lines, Polygons* loops) const;
void make_expolygons(const Polygons &loops, const float closing_radius, ExPolygons* slices) const;
void make_expolygons_simple(std::vector<IntersectionLine> &lines, ExPolygons* slices) const;
void make_expolygons(std::vector<IntersectionLine> &lines, const float closing_radius, ExPolygons* slices) const;
};
inline void slice_mesh(
const TriangleMesh & mesh,
const std::vector<float> & z,
std::vector<Polygons> & layers,
TriangleMeshSlicer::throw_on_cancel_callback_type thr = nullptr)
{
if (mesh.empty()) return;
TriangleMeshSlicer slicer(&mesh);
slicer.slice(z, SlicingMode::Regular, &layers, thr);
}
inline void slice_mesh(
const TriangleMesh & mesh,
const std::vector<float> & z,
std::vector<ExPolygons> & layers,
float closing_radius,
TriangleMeshSlicer::throw_on_cancel_callback_type thr = nullptr)
{
if (mesh.empty()) return;
TriangleMeshSlicer slicer(&mesh);
slicer.slice(z, SlicingMode::Regular, closing_radius, &layers, thr);
}
// Map from a facet edge to a neighbor face index or -1 if no neighbor exists.
std::vector<int> create_face_neighbors_index(const indexed_triangle_set &its);
std::vector<int> create_face_neighbors_index(const indexed_triangle_set &its, std::function<void()> throw_on_cancel_callback);
// Remove degenerate faces, return number of faces removed.
int its_remove_degenerate_faces(indexed_triangle_set &its, bool shrink_to_fit = true);
// Remove vertices, which none of the faces references. Return number of freed vertices.
int its_compactify_vertices(indexed_triangle_set &its, bool shrink_to_fit = true);
void its_shrink_to_fit(indexed_triangle_set &its);
TriangleMesh make_cube(double x, double y, double z);
@ -259,21 +114,21 @@ TriangleMesh make_sphere(double rho, double fa=(2*PI/360));
// Serialization through the Cereal library
#include <cereal/access.hpp>
namespace cereal {
template <class Archive> struct specialize<Archive, Slic3r::TriangleMesh, cereal::specialization::non_member_load_save> {};
template<class Archive> void load(Archive &archive, Slic3r::TriangleMesh &mesh) {
template <class Archive> struct specialize<Archive, Slic3r::TriangleMesh, cereal::specialization::non_member_load_save> {};
template<class Archive> void load(Archive &archive, Slic3r::TriangleMesh &mesh) {
stl_file &stl = mesh.stl;
stl.stats.type = inmemory;
archive(stl.stats.number_of_facets, stl.stats.original_num_facets);
archive(stl.stats.number_of_facets, stl.stats.original_num_facets);
stl_allocate(&stl);
archive.loadBinary((char*)stl.facet_start.data(), stl.facet_start.size() * 50);
archive.loadBinary((char*)stl.facet_start.data(), stl.facet_start.size() * 50);
stl_get_size(&stl);
mesh.repair();
}
template<class Archive> void save(Archive &archive, const Slic3r::TriangleMesh &mesh) {
const stl_file& stl = mesh.stl;
archive(stl.stats.number_of_facets, stl.stats.original_num_facets);
archive.saveBinary((char*)stl.facet_start.data(), stl.facet_start.size() * 50);
}
}
template<class Archive> void save(Archive &archive, const Slic3r::TriangleMesh &mesh) {
const stl_file& stl = mesh.stl;
archive(stl.stats.number_of_facets, stl.stats.original_num_facets);
archive.saveBinary((char*)stl.facet_start.data(), stl.facet_start.size() * 50);
}
}
#endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,141 @@
#ifndef slic3r_TriangleMeshSlicer_hpp_
#define slic3r_TriangleMeshSlicer_hpp_
#include "libslic3r.h"
#include <admesh/stl.h>
#include <functional>
#include <vector>
#include <boost/thread.hpp>
#include "BoundingBox.hpp"
#include "Line.hpp"
#include "Point.hpp"
#include "Polygon.hpp"
#include "ExPolygon.hpp"
namespace Slic3r {
class TriangleMesh;
enum class SlicingMode : uint32_t {
// Regular slicing, maintain all contours and their orientation.
Regular,
// Maintain all contours, orient all contours CCW, therefore all holes are being closed.
Positive,
// Orient all contours CCW and keep only the contour with the largest area.
// This mode is useful for slicing complex objects in vase mode.
PositiveLargestContour,
};
struct MeshSlicingParams
{
SlicingMode mode { SlicingMode::Regular };
// For vase mode: below this layer a different slicing mode will be used to produce a single contour.
// 0 = ignore.
size_t slicing_mode_normal_below_layer { 0 };
// Mode to apply below slicing_mode_normal_below_layer. Ignored if slicing_mode_nromal_below_layer == 0.
SlicingMode mode_below { SlicingMode::Regular };
};
struct MeshSlicingParamsExtended : public MeshSlicingParams
{
// Morphological closing operation when creating output expolygons.
float closing_radius { 0 };
// Positive offset applied when creating output expolygons.
float extra_offset { 0 };
// Resolution for contour simplification.
// 0 = don't simplify.
double resolution { 0 };
// Transformation of the object owning the ModelVolume.
// Transform3d object_trafo;
};
class TriangleMeshSlicer
{
public:
using throw_on_cancel_callback_type = std::function<void()>;
TriangleMeshSlicer() = default;
TriangleMeshSlicer(const TriangleMesh *mesh) { this->init(mesh, []{}); }
TriangleMeshSlicer(const indexed_triangle_set *its) { this->init(its, []{}); }
void init(const TriangleMesh *mesh, throw_on_cancel_callback_type throw_on_cancel);
void init(const indexed_triangle_set *its, throw_on_cancel_callback_type);
void slice(
const std::vector<float> &z,
const MeshSlicingParams &params,
std::vector<Polygons> *layers,
throw_on_cancel_callback_type throw_on_cancel = []{}) const;
void slice(
// Where to slice.
const std::vector<float> &z,
const MeshSlicingParamsExtended &params,
std::vector<ExPolygons> *layers,
throw_on_cancel_callback_type throw_on_cancel = []{}) const;
void cut(float z, indexed_triangle_set *upper, indexed_triangle_set *lower) const;
void cut(float z, TriangleMesh* upper, TriangleMesh* lower) const;
void set_up_direction(const Vec3f& up);
private:
const indexed_triangle_set *m_its { nullptr };
// const TriangleMesh *mesh { nullptr };
// Map from a facet to an edge index.
std::vector<int> facets_edges;
// Scaled copy of this->mesh->stl.v_shared
std::vector<stl_vertex> v_scaled_shared;
// Quaternion that will be used to rotate every facet before the slicing
Eigen::Quaternion<float, Eigen::DontAlign> m_quaternion;
// Whether or not the above quaterion should be used
bool m_use_quaternion = false;
};
inline void slice_mesh(
const TriangleMesh &mesh,
const std::vector<float> &z,
std::vector<Polygons> &layers,
TriangleMeshSlicer::throw_on_cancel_callback_type thr = []{})
{
if (! mesh.empty()) {
TriangleMeshSlicer slicer(&mesh);
slicer.slice(z, MeshSlicingParams{}, &layers, thr);
}
}
inline void slice_mesh(
const TriangleMesh &mesh,
const std::vector<float> &z,
const MeshSlicingParamsExtended &params,
std::vector<ExPolygons> &layers,
TriangleMeshSlicer::throw_on_cancel_callback_type thr = []{})
{
if (! mesh.empty()) {
TriangleMeshSlicer slicer(&mesh);
slicer.slice(z, params, &layers, thr);
}
}
inline void slice_mesh(
const TriangleMesh &mesh,
const std::vector<float> &z,
float closing_radius,
std::vector<ExPolygons> &layers,
TriangleMeshSlicer::throw_on_cancel_callback_type thr = []{})
{
MeshSlicingParamsExtended params;
params.closing_radius = closing_radius;
slice_mesh(mesh, z, params, layers);
}
inline void slice_mesh(
const TriangleMesh &mesh,
const std::vector<float> &z,
std::vector<ExPolygons> &layers,
TriangleMeshSlicer::throw_on_cancel_callback_type thr = []{})
{
slice_mesh(mesh, z, MeshSlicingParamsExtended{}, layers);
}
}
#endif // slic3r_TriangleMeshSlicer_hpp_

View File

@ -5,6 +5,8 @@
#include <condition_variable>
#include <mutex>
#include <boost/thread.hpp>
#include <wx/event.h>
#include "libslic3r/PrintBase.hpp"

View File

@ -2,6 +2,7 @@
#include "libslic3r/Tesselate.hpp"
#include "libslic3r/TriangleMesh.hpp"
#include "libslic3r/TriangleMeshSlicer.hpp"
#include "libslic3r/ClipperUtils.hpp"
#include "slic3r/GUI/Camera.hpp"
@ -83,16 +84,17 @@ void MeshClipper::recalculate_triangles()
// Now do the cutting
std::vector<ExPolygons> list_of_expolys;
m_tms->set_up_direction(up.cast<float>());
m_tms->slice(std::vector<float>{height_mesh}, SlicingMode::Regular, 0.f, &list_of_expolys, [](){});
m_tms->slice(std::vector<float>{height_mesh}, MeshSlicingParamsExtended{}, &list_of_expolys);
if (m_negative_mesh && !m_negative_mesh->empty()) {
TriangleMeshSlicer negative_tms{m_negative_mesh};
negative_tms.set_up_direction(up.cast<float>());
std::vector<ExPolygons> neg_polys;
negative_tms.slice(std::vector<float>{height_mesh}, SlicingMode::Regular, 0.f, &neg_polys, [](){});
negative_tms.slice(std::vector<float>{height_mesh}, MeshSlicingParamsExtended{}, &neg_polys);
list_of_expolys.front() = diff_ex(list_of_expolys.front(), neg_polys.front());
}
m_triangles2d = triangulate_expolygons_2f(list_of_expolys[0], m_trafo.get_matrix().matrix().determinant() < 0.);
// Rotate the cut into world coords:

View File

@ -3,6 +3,7 @@
#include "libslic3r/Point.hpp"
#include "libslic3r/Geometry.hpp"
#include "libslic3r/TriangleMeshSlicer.hpp"
#include "libslic3r/SLA/IndexedMesh.hpp"
#include "admesh/stl.h"
@ -12,9 +13,6 @@
namespace Slic3r {
class TriangleMesh;
class TriangleMeshSlicer;
namespace GUI {
struct Camera;

View File

@ -27,6 +27,7 @@
#include <boost/filesystem.hpp>
#include <boost/nowide/convert.hpp>
#include <boost/nowide/cstdio.hpp>
#include <boost/thread.hpp>
#include "libslic3r/Model.hpp"
#include "libslic3r/Print.hpp"

View File

@ -1,6 +1,7 @@
#include <catch2/catch.hpp>
#include "libslic3r/TriangleMesh.hpp"
#include "libslic3r/TriangleMeshSlicer.hpp"
#include "libslic3r/Point.hpp"
#include "libslic3r/Config.hpp"
#include "libslic3r/Model.hpp"

View File

@ -13,6 +13,7 @@
#include <libslic3r/SVG.hpp>
#include <libslic3r/ClipperUtils.hpp>
#include <libslic3r/TriangleMeshSlicer.hpp>
#include <libslic3r/TriangulateWall.hpp>
#include <libslic3r/Tesselate.hpp>
#include <libslic3r/SlicesToTriangleMesh.hpp>
@ -320,7 +321,7 @@ static void recreate_object_from_rasters(const std::string &objname, float lh) {
bb = mesh.bounding_box();
std::vector<ExPolygons> layers;
slice_mesh(mesh, grid(float(bb.min.z()) + lh, float(bb.max.z()), lh), layers, 0.f, []{});
slice_mesh(mesh, grid(float(bb.min.z()) + lh, float(bb.max.z()), lh), layers);
sla::RasterBase::Resolution res{2560, 1440};
double disp_w = 120.96;

View File

@ -6,6 +6,7 @@
#include "sla_test_utils.hpp"
#include <libslic3r/TriangleMeshSlicer.hpp>
#include <libslic3r/SLA/SupportTreeMesher.hpp>
#include <libslic3r/SLA/Concurrency.hpp>
@ -48,9 +49,7 @@ TEST_CASE("Support point generator should be deterministic if seeded",
sla::SupportPointGenerator::Config autogencfg;
autogencfg.head_diameter = float(2 * supportcfg.head_front_radius_mm);
sla::SupportPointGenerator point_gen{emesh, autogencfg, [] {}, [](int) {}};
TriangleMeshSlicer slicer{&mesh};
auto bb = mesh.bounding_box();
double zmin = bb.min.z();
double zmax = bb.max.z();
@ -59,7 +58,7 @@ TEST_CASE("Support point generator should be deterministic if seeded",
auto slicegrid = grid(float(gnd), float(zmax), layer_h);
std::vector<ExPolygons> slices;
slicer.slice(slicegrid, SlicingMode::Regular, CLOSING_RADIUS, &slices, []{});
slice_mesh(mesh, slicegrid, CLOSING_RADIUS, slices);
point_gen.seed(0);
point_gen.execute(slices, slicegrid);

View File

@ -1,4 +1,5 @@
#include "sla_test_utils.hpp"
#include "libslic3r/TriangleMeshSlicer.hpp"
#include "libslic3r/SLA/AGGRaster.hpp"
void test_support_model_collision(const std::string &obj_filename,
@ -94,8 +95,6 @@ void test_supports(const std::string &obj_filename,
mesh.require_shared_vertices();
}
TriangleMeshSlicer slicer{&mesh};
auto bb = mesh.bounding_box();
double zmin = bb.min.z();
double zmax = bb.max.z();
@ -103,7 +102,7 @@ void test_supports(const std::string &obj_filename,
auto layer_h = 0.05f;
out.slicegrid = grid(float(gnd), float(zmax), layer_h);
slicer.slice(out.slicegrid, SlicingMode::Regular, CLOSING_RADIUS, &out.model_slices, []{});
slice_mesh(mesh, out.slicegrid, CLOSING_RADIUS, out.model_slices);
sla::cut_drainholes(out.model_slices, out.slicegrid, CLOSING_RADIUS, drainholes, []{});
// Create the special index-triangle mesh with spatial indexing which
@ -470,7 +469,7 @@ sla::SupportPoints calc_support_pts(
std::vector<ExPolygons> slices;
auto bb = cast<float>(mesh.bounding_box());
std::vector<float> heights = grid(bb.min.z(), bb.max.z(), 0.1f);
slice_mesh(mesh, heights, slices, CLOSING_RADIUS, [] {});
slice_mesh(mesh, heights, CLOSING_RADIUS, slices);
// Prepare the support point calculator
sla::IndexedMesh emesh{mesh};

View File

@ -3,6 +3,7 @@
%{
#include <xsinit.h>
#include "libslic3r/TriangleMesh.hpp"
#include "libslic3r/TriangleMeshSlicer.hpp"
%}
%name{Slic3r::TriangleMesh} class TriangleMesh {
@ -181,8 +182,7 @@ TriangleMesh::slice(z)
std::vector<float> z_f = cast<float>(z);
std::vector<ExPolygons> layers;
TriangleMeshSlicer mslicer(THIS);
mslicer.slice(z_f, SlicingMode::Regular, 0.049f, &layers, [](){});
slice_mesh(*THIS, z_f, 0.049f, layers);
AV* layers_av = newAV();
size_t len = layers.size();