Simplify_slices rewritten to C++, parallelized.

Added some move methods to Surface class.
This commit is contained in:
bubnikv 2017-03-08 13:43:49 +01:00
parent 4331f38912
commit 52b76930aa
5 changed files with 62 additions and 37 deletions

View file

@ -51,9 +51,8 @@ sub slice {
warn $warning if (defined($warning) && $warning ne ''); warn $warning if (defined($warning) && $warning ne '');
# simplify slices if required # simplify slices if required
if ($self->print->config->resolution) { $self->_simplify_slices(scale($self->print->config->resolution));
$self->_simplify_slices(scale($self->print->config->resolution)); if ($self->print->config->resolution);
}
die "No layers were detected. You might want to repair your STL file(s) or check their size or thickness and retry.\n" die "No layers were detected. You might want to repair your STL file(s) or check their size or thickness and retry.\n"
if !@{$self->layers}; if !@{$self->layers};
@ -644,18 +643,6 @@ sub combine_infill {
} }
} }
# Simplify the sliced model, if "resolution" configuration parameter > 0.
# The simplification is problematic, because it simplifies the slices independent from each other,
# which makes the simplified discretization visible on the object surface.
sub _simplify_slices {
my ($self, $distance) = @_;
foreach my $layer (@{$self->layers}) {
$layer->slices->simplify($distance);
$_->slices->simplify($distance) for @{$layer->regions};
}
}
# Used by t/support.t and by GCode.pm to export support line width as a comment. # Used by t/support.t and by GCode.pm to export support line width as a comment.
# To be removed. # To be removed.
sub support_material_flow { sub support_material_flow {

View file

@ -169,6 +169,7 @@ public:
void _slice(); void _slice();
std::string _fix_slicing_errors(); std::string _fix_slicing_errors();
void _simplify_slices(double distance);
bool has_support_material() const; bool has_support_material() const;
void detect_surfaces_type(); void detect_surfaces_type();
void process_external_surfaces(); void process_external_surfaces();

View file

@ -1121,7 +1121,7 @@ void PrintObject::_slice()
} }
} }
// remove last layer(s) if empty BOOST_LOG_TRIVIAL(debug) << "Slicing objects - removing top empty layers";
while (! this->layers.empty()) { while (! this->layers.empty()) {
const Layer *layer = this->layers.back(); const Layer *layer = this->layers.back();
for (size_t region_id = 0; region_id < this->print()->regions.size(); ++ region_id) for (size_t region_id = 0; region_id < this->print()->regions.size(); ++ region_id)
@ -1284,6 +1284,25 @@ std::string PrintObject::_fix_slicing_errors()
"however you might want to check the results or repair the input file and retry.\n"; "however you might want to check the results or repair the input file and retry.\n";
} }
// Simplify the sliced model, if "resolution" configuration parameter > 0.
// The simplification is problematic, because it simplifies the slices independent from each other,
// which makes the simplified discretization visible on the object surface.
void PrintObject::_simplify_slices(double distance)
{
BOOST_LOG_TRIVIAL(debug) << "Slicing objects - siplifying slices in parallel - begin";
tbb::parallel_for(
tbb::blocked_range<size_t>(0, this->layers.size()),
[this, distance](const tbb::blocked_range<size_t>& range) {
for (size_t layer_idx = range.begin(); layer_idx < range.end(); ++ layer_idx) {
Layer *layer = this->layers[layer_idx];
for (size_t region_idx = 0; region_idx < layer->regions.size(); ++ region_idx)
layer->regions[region_idx]->slices.simplify(distance);
layer->slices.simplify(distance);
}
});
BOOST_LOG_TRIVIAL(debug) << "Slicing objects - siplifying slices in parallel - end";
}
void void
PrintObject::_make_perimeters() PrintObject::_make_perimeters()
{ {

View file

@ -45,18 +45,34 @@ public:
{}; {};
Surface(const Surface &other, const ExPolygon &_expolygon) Surface(const Surface &other, const ExPolygon &_expolygon)
: surface_type(other.surface_type), expolygon(_expolygon), : surface_type(other.surface_type), expolygon(_expolygon),
thickness(other.thickness), thickness_layers(other.thickness_layers), bridge_angle(other.bridge_angle), extra_perimeters(other.extra_perimeters) thickness(other.thickness), thickness_layers(other.thickness_layers),
bridge_angle(other.bridge_angle), extra_perimeters(other.extra_perimeters)
{};
Surface(Surface &&rhs)
: surface_type(rhs.surface_type), expolygon(std::move(rhs.expolygon)),
thickness(rhs.thickness), thickness_layers(rhs.thickness_layers),
bridge_angle(rhs.bridge_angle), extra_perimeters(rhs.extra_perimeters)
{}; {};
#if SLIC3R_CPPVER >= 11
Surface(SurfaceType _surface_type, const ExPolygon &&_expolygon) Surface(SurfaceType _surface_type, const ExPolygon &&_expolygon)
: surface_type(_surface_type), expolygon(std::move(_expolygon)), : surface_type(_surface_type), expolygon(std::move(_expolygon)),
thickness(-1), thickness_layers(1), bridge_angle(-1), extra_perimeters(0) thickness(-1), thickness_layers(1), bridge_angle(-1), extra_perimeters(0)
{}; {};
Surface(const Surface &other, const ExPolygon &&_expolygon) Surface(const Surface &other, const ExPolygon &&_expolygon)
: surface_type(other.surface_type), expolygon(std::move(_expolygon)), : surface_type(other.surface_type), expolygon(std::move(_expolygon)),
thickness(other.thickness), thickness_layers(other.thickness_layers), bridge_angle(other.bridge_angle), extra_perimeters(other.extra_perimeters) thickness(other.thickness), thickness_layers(other.thickness_layers),
bridge_angle(other.bridge_angle), extra_perimeters(other.extra_perimeters)
{}; {};
#endif
Surface& operator=(Surface &&rhs)
{
surface_type = rhs.surface_type;
expolygon = std::move(rhs.expolygon);
thickness = rhs.thickness;
thickness_layers = rhs.thickness_layers;
bridge_angle = rhs.bridge_angle;
extra_perimeters = rhs.extra_perimeters;
}
operator Polygons() const; operator Polygons() const;
double area() const; double area() const;
bool empty() const { return expolygon.empty(); } bool empty() const { return expolygon.empty(); }
@ -79,9 +95,9 @@ inline Polygons to_polygons(const Surfaces &src)
Polygons polygons; Polygons polygons;
polygons.reserve(num); polygons.reserve(num);
for (Surfaces::const_iterator it = src.begin(); it != src.end(); ++it) { for (Surfaces::const_iterator it = src.begin(); it != src.end(); ++it) {
polygons.push_back(it->expolygon.contour); polygons.emplace_back(it->expolygon.contour);
for (Polygons::const_iterator ith = it->expolygon.holes.begin(); ith != it->expolygon.holes.end(); ++ith) for (Polygons::const_iterator ith = it->expolygon.holes.begin(); ith != it->expolygon.holes.end(); ++ith)
polygons.push_back(*ith); polygons.emplace_back(*ith);
} }
return polygons; return polygons;
} }
@ -94,9 +110,9 @@ inline Polygons to_polygons(const SurfacesPtr &src)
Polygons polygons; Polygons polygons;
polygons.reserve(num); polygons.reserve(num);
for (SurfacesPtr::const_iterator it = src.begin(); it != src.end(); ++it) { for (SurfacesPtr::const_iterator it = src.begin(); it != src.end(); ++it) {
polygons.push_back((*it)->expolygon.contour); polygons.emplace_back((*it)->expolygon.contour);
for (Polygons::const_iterator ith = (*it)->expolygon.holes.begin(); ith != (*it)->expolygon.holes.end(); ++ith) for (Polygons::const_iterator ith = (*it)->expolygon.holes.begin(); ith != (*it)->expolygon.holes.end(); ++ith)
polygons.push_back(*ith); polygons.emplace_back(*ith);
} }
return polygons; return polygons;
} }
@ -106,7 +122,7 @@ inline ExPolygons to_expolygons(const Surfaces &src)
ExPolygons expolygons; ExPolygons expolygons;
expolygons.reserve(src.size()); expolygons.reserve(src.size());
for (Surfaces::const_iterator it = src.begin(); it != src.end(); ++it) for (Surfaces::const_iterator it = src.begin(); it != src.end(); ++it)
expolygons.push_back(it->expolygon); expolygons.emplace_back(it->expolygon);
return expolygons; return expolygons;
} }
@ -125,7 +141,7 @@ inline ExPolygons to_expolygons(const SurfacesPtr &src)
ExPolygons expolygons; ExPolygons expolygons;
expolygons.reserve(src.size()); expolygons.reserve(src.size());
for (SurfacesPtr::const_iterator it = src.begin(); it != src.end(); ++it) for (SurfacesPtr::const_iterator it = src.begin(); it != src.end(); ++it)
expolygons.push_back((*it)->expolygon); expolygons.emplace_back((*it)->expolygon);
return expolygons; return expolygons;
} }
@ -151,7 +167,7 @@ inline void polygons_append(Polygons &dst, const Surfaces &src)
{ {
dst.reserve(dst.size() + number_polygons(src)); dst.reserve(dst.size() + number_polygons(src));
for (Surfaces::const_iterator it = src.begin(); it != src.end(); ++ it) { for (Surfaces::const_iterator it = src.begin(); it != src.end(); ++ it) {
dst.push_back(it->expolygon.contour); dst.emplace_back(it->expolygon.contour);
dst.insert(dst.end(), it->expolygon.holes.begin(), it->expolygon.holes.end()); dst.insert(dst.end(), it->expolygon.holes.begin(), it->expolygon.holes.end());
} }
} }
@ -160,7 +176,7 @@ inline void polygons_append(Polygons &dst, Surfaces &&src)
{ {
dst.reserve(dst.size() + number_polygons(src)); dst.reserve(dst.size() + number_polygons(src));
for (Surfaces::iterator it = src.begin(); it != src.end(); ++ it) { for (Surfaces::iterator it = src.begin(); it != src.end(); ++ it) {
dst.push_back(std::move(it->expolygon.contour)); dst.emplace_back(std::move(it->expolygon.contour));
std::move(std::begin(it->expolygon.holes), std::end(it->expolygon.holes), std::back_inserter(dst)); std::move(std::begin(it->expolygon.holes), std::end(it->expolygon.holes), std::back_inserter(dst));
it->expolygon.holes.clear(); it->expolygon.holes.clear();
} }
@ -171,7 +187,7 @@ inline void polygons_append(Polygons &dst, const SurfacesPtr &src)
{ {
dst.reserve(dst.size() + number_polygons(src)); dst.reserve(dst.size() + number_polygons(src));
for (SurfacesPtr::const_iterator it = src.begin(); it != src.end(); ++ it) { for (SurfacesPtr::const_iterator it = src.begin(); it != src.end(); ++ it) {
dst.push_back((*it)->expolygon.contour); dst.emplace_back((*it)->expolygon.contour);
dst.insert(dst.end(), (*it)->expolygon.holes.begin(), (*it)->expolygon.holes.end()); dst.insert(dst.end(), (*it)->expolygon.holes.begin(), (*it)->expolygon.holes.end());
} }
} }
@ -180,7 +196,7 @@ inline void polygons_append(Polygons &dst, SurfacesPtr &&src)
{ {
dst.reserve(dst.size() + number_polygons(src)); dst.reserve(dst.size() + number_polygons(src));
for (SurfacesPtr::const_iterator it = src.begin(); it != src.end(); ++ it) { for (SurfacesPtr::const_iterator it = src.begin(); it != src.end(); ++ it) {
dst.push_back(std::move((*it)->expolygon.contour)); dst.emplace_back(std::move((*it)->expolygon.contour));
std::move(std::begin((*it)->expolygon.holes), std::end((*it)->expolygon.holes), std::back_inserter(dst)); std::move(std::begin((*it)->expolygon.holes), std::end((*it)->expolygon.holes), std::back_inserter(dst));
(*it)->expolygon.holes.clear(); (*it)->expolygon.holes.clear();
} }
@ -190,14 +206,14 @@ inline void polygons_append(Polygons &dst, SurfacesPtr &&src)
inline void surfaces_append(Surfaces &dst, const ExPolygons &src, SurfaceType surfaceType) inline void surfaces_append(Surfaces &dst, const ExPolygons &src, SurfaceType surfaceType)
{ {
dst.reserve(dst.size() + src.size()); dst.reserve(dst.size() + src.size());
for (ExPolygons::const_iterator it = src.begin(); it != src.end(); ++ it) for (const ExPolygon &expoly : src)
dst.push_back(Surface(surfaceType, *it)); dst.emplace_back(Surface(surfaceType, expoly));
} }
inline void surfaces_append(Surfaces &dst, const ExPolygons &src, const Surface &surfaceTempl) inline void surfaces_append(Surfaces &dst, const ExPolygons &src, const Surface &surfaceTempl)
{ {
dst.reserve(dst.size() + number_polygons(src)); dst.reserve(dst.size() + number_polygons(src));
for (ExPolygons::const_iterator it = src.begin(); it != src.end(); ++ it) for (const ExPolygon &expoly : src)
dst.push_back(Surface(surfaceTempl, *it)); dst.emplace_back(Surface(surfaceTempl, expoly));
} }
inline void surfaces_append(Surfaces &dst, const Surfaces &src) inline void surfaces_append(Surfaces &dst, const Surfaces &src)
{ {
@ -207,8 +223,8 @@ inline void surfaces_append(Surfaces &dst, const Surfaces &src)
inline void surfaces_append(Surfaces &dst, ExPolygons &&src, SurfaceType surfaceType) inline void surfaces_append(Surfaces &dst, ExPolygons &&src, SurfaceType surfaceType)
{ {
dst.reserve(dst.size() + src.size()); dst.reserve(dst.size() + src.size());
for (ExPolygons::const_iterator it = src.begin(); it != src.end(); ++ it) for (ExPolygon &expoly : src)
dst.push_back(Surface(surfaceType, std::move(*it))); dst.emplace_back(Surface(surfaceType, std::move(expoly)));
src.clear(); src.clear();
} }
@ -216,9 +232,10 @@ inline void surfaces_append(Surfaces &dst, ExPolygons &&src, const Surface &surf
{ {
dst.reserve(dst.size() + number_polygons(src)); dst.reserve(dst.size() + number_polygons(src));
for (ExPolygons::const_iterator it = src.begin(); it != src.end(); ++ it) for (ExPolygons::const_iterator it = src.begin(); it != src.end(); ++ it)
dst.push_back(Surface(surfaceTempl, std::move(*it))); dst.emplace_back(Surface(surfaceTempl, std::move(*it)));
src.clear(); src.clear();
} }
inline void surfaces_append(Surfaces &dst, Surfaces &&src) inline void surfaces_append(Surfaces &dst, Surfaces &&src)
{ {
if (dst.empty()) { if (dst.empty()) {

View file

@ -113,6 +113,7 @@ _constant()
void _slice(); void _slice();
std::string _fix_slicing_errors(); std::string _fix_slicing_errors();
void _simplify_slices(double distance);
void detect_surfaces_type(); void detect_surfaces_type();
void process_external_surfaces(); void process_external_surfaces();
void discover_vertical_shells(); void discover_vertical_shells();