diff --git a/sandboxes/slabasebed/slabasebed.cpp b/sandboxes/slabasebed/slabasebed.cpp index 255ce2cc3..9804ea3c9 100644 --- a/sandboxes/slabasebed/slabasebed.cpp +++ b/sandboxes/slabasebed/slabasebed.cpp @@ -1,10 +1,10 @@ #include #include -#include -#include "TriangleMesh.hpp" -#include "SLABasePool.hpp" -#include "benchmark.h" +#include +#include +#include +#include const std::string USAGE_STR = { "Usage: slabasebed stlfilename.stl" @@ -28,7 +28,7 @@ int main(const int argc, const char *argv[]) { ExPolygons ground_slice; TriangleMesh basepool; - sla::ground_layer(model, ground_slice, 0.1f); + sla::base_plate(model, ground_slice, 0.1f); bench.start(); sla::create_base_pool(ground_slice, basepool); diff --git a/src/libslic3r/SLA/SLABasePool.cpp b/src/libslic3r/SLA/SLABasePool.cpp index 6ecc63576..6f28caf71 100644 --- a/src/libslic3r/SLA/SLABasePool.cpp +++ b/src/libslic3r/SLA/SLABasePool.cpp @@ -10,6 +10,10 @@ namespace Slic3r { namespace sla { +namespace { +ThrowOnCancel throw_on_cancel = [](){}; +} + /// Convert the triangulation output to an intermediate mesh. Contour3D convert(const Polygons& triangles, coord_t z, bool dir) { @@ -60,9 +64,13 @@ Contour3D walls(const ExPolygon& floor_plate, const ExPolygon& ceiling, }); }; + auto& thr = throw_on_cancel; + std::for_each(tri.begin(), tri.end(), - [&rp, &rpi, &poly, &idx, is_upper, fz, cz](const Polygon& pp) + [&rp, &rpi, thr, &idx, is_upper, fz, cz](const Polygon& pp) { + thr(); // may throw if cancellation was requested + for(auto& p : pp.points) if(is_upper(p)) rp.emplace_back(unscale(x(p), y(p), mm(cz))); @@ -231,6 +239,8 @@ Contour3D round_edges(const ExPolygon& base_plate, if(degrees >= 90) { for(int i = 1; i <= steps; ++i) { + throw_on_cancel(); + ob = base_plate; double r2 = radius_mm * radius_mm; @@ -254,6 +264,7 @@ Contour3D round_edges(const ExPolygon& base_plate, int tos = int(tox / stepx); for(int i = 1; i <= tos; ++i) { + throw_on_cancel(); ob = base_plate; double r2 = radius_mm * radius_mm; @@ -385,7 +396,7 @@ ExPolygons concave_hull(const ExPolygons& polys, double max_dist_mm = 50) std::back_inserter(punion), [&punion, &boxindex, cc, max_dist_mm, &idx](const Point& c) { - + throw_on_cancel(); double dx = x(c) - x(cc), dy = y(c) - y(cc); double l = std::sqrt(dx * dx + dy * dy); double nx = dx / l, ny = dy / l; @@ -419,7 +430,7 @@ ExPolygons concave_hull(const ExPolygons& polys, double max_dist_mm = 50) } void base_plate(const TriangleMesh &mesh, ExPolygons &output, float h, - float layerh) + float layerh, ThrowOnCancel thrfn) { TriangleMesh m = mesh; TriangleMeshSlicer slicer(&m); @@ -431,7 +442,7 @@ void base_plate(const TriangleMesh &mesh, ExPolygons &output, float h, heights.emplace_back(hi); std::vector out; out.reserve(size_t(std::ceil(h/layerh))); - slicer.slice(heights, &out, [](){}); + slicer.slice(heights, &out, thrfn); size_t count = 0; for(auto& o : out) count += o.size(); ExPolygons tmp; tmp.reserve(count); @@ -444,6 +455,8 @@ void base_plate(const TriangleMesh &mesh, ExPolygons &output, float h, void create_base_pool(const ExPolygons &ground_layer, TriangleMesh& out, const PoolConfig& cfg) { + throw_on_cancel = cfg.throw_on_cancel; + double mdist = 2*(1.8*cfg.min_wall_thickness_mm + 4*cfg.edge_radius_mm) + cfg.max_merge_distance_mm; diff --git a/src/libslic3r/SLA/SLABasePool.hpp b/src/libslic3r/SLA/SLABasePool.hpp index 132a7aeac..07a76265a 100644 --- a/src/libslic3r/SLA/SLABasePool.hpp +++ b/src/libslic3r/SLA/SLABasePool.hpp @@ -2,6 +2,7 @@ #define SLABASEPOOL_HPP #include +#include namespace Slic3r { @@ -11,12 +12,14 @@ class TriangleMesh; namespace sla { using ExPolygons = std::vector; +using ThrowOnCancel = std::function; /// Calculate the polygon representing the silhouette from the specified height -void base_plate(const TriangleMesh& mesh, - ExPolygons& output, - float zlevel = 0.1f, - float layerheight = 0.05f); +void base_plate(const TriangleMesh& mesh, // input mesh + ExPolygons& output, // Output will be merged with + float zlevel = 0.1f, // Plate creation level + float layerheight = 0.05f, // The sampling height + ThrowOnCancel thrfn = [](){}); // Will be called frequently struct PoolConfig { double min_wall_thickness_mm = 2; @@ -24,6 +27,8 @@ struct PoolConfig { double max_merge_distance_mm = 50; double edge_radius_mm = 1; + ThrowOnCancel throw_on_cancel = [](){}; + inline PoolConfig() {} inline PoolConfig(double wt, double wh, double md, double er): min_wall_thickness_mm(wt), @@ -35,8 +40,7 @@ struct PoolConfig { /// Calculate the pool for the mesh for SLA printing void create_base_pool(const ExPolygons& base_plate, TriangleMesh& output_mesh, - const PoolConfig& = PoolConfig() - ); + const PoolConfig& = PoolConfig()); /// TODO: Currently the base plate of the pool will have half the height of the /// whole pool. So the carved out space has also half the height. This is not diff --git a/src/libslic3r/SLA/SLABoilerPlate.hpp b/src/libslic3r/SLA/SLABoilerPlate.hpp index 9a79c8e58..1436be17f 100644 --- a/src/libslic3r/SLA/SLABoilerPlate.hpp +++ b/src/libslic3r/SLA/SLABoilerPlate.hpp @@ -58,7 +58,7 @@ struct Contour3D { points.insert(points.end(), ctr.points.begin(), ctr.points.end()); indices.insert(indices.end(), ctr.indices.begin(), ctr.indices.end()); - for(auto n = s; n < indices.size(); n++) { + for(size_t n = s; n < indices.size(); n++) { auto& idx = indices[n]; x(idx) += s3; y(idx) += s3; z(idx) += s3; } } diff --git a/src/libslic3r/SLA/SLASupportTree.cpp b/src/libslic3r/SLA/SLASupportTree.cpp index f66351bdb..6cab763cd 100644 --- a/src/libslic3r/SLA/SLASupportTree.cpp +++ b/src/libslic3r/SLA/SLASupportTree.cpp @@ -515,8 +515,13 @@ struct Pad { zlevel(ground_level + sla::get_pad_elevation(pcfg)) { ExPolygons basep; + cfg.throw_on_cancel(); + + // The 0.1f is the layer height with which the mesh is sampled and then + // the layers are unified into one vector of polygons. base_plate(object_support_mesh, basep, - float(cfg.min_wall_height_mm)/*,layer_height*/); + float(cfg.min_wall_height_mm), 0.1f, pcfg.throw_on_cancel); + for(auto& bp : baseplate) basep.emplace_back(bp); create_base_pool(basep, tmesh, cfg); @@ -622,12 +627,19 @@ class SLASupportTree::Impl { std::vector m_junctions; std::vector m_bridges; std::vector m_compact_bridges; + Controller m_ctl; + Pad m_pad; mutable TriangleMesh meshcache; mutable bool meshcache_valid; mutable double model_height = 0; // the full height of the model public: double ground_level = 0; + Impl() = default; + inline Impl(const Controller& ctl): m_ctl(ctl) {} + + const Controller& ctl() const { return m_ctl; } + template Head& add_head(Args&&... args) { m_heads.emplace_back(std::forward(args)...); m_heads.back().id = long(m_heads.size() - 1); @@ -710,27 +722,38 @@ public: meshcache = TriangleMesh(); for(auto& head : heads()) { + if(m_ctl.stopcondition()) break; auto&& m = mesh(head.mesh); meshcache.merge(m); } for(auto& stick : pillars()) { + if(m_ctl.stopcondition()) break; meshcache.merge(mesh(stick.mesh)); meshcache.merge(mesh(stick.base)); } for(auto& j : junctions()) { + if(m_ctl.stopcondition()) break; meshcache.merge(mesh(j.mesh)); } for(auto& cb : compact_bridges()) { + if(m_ctl.stopcondition()) break; meshcache.merge(mesh(cb.mesh)); } for(auto& bs : bridges()) { + if(m_ctl.stopcondition()) break; meshcache.merge(mesh(bs.mesh)); } + if(m_ctl.stopcondition()) { + // In case of failure we have to return an empty mesh + meshcache = TriangleMesh(); + return meshcache; + } + // TODO: Is this necessary? meshcache.repair(); @@ -1659,22 +1682,19 @@ SlicedSupports SLASupportTree::slice(float layerh, float init_layerh) const fullmesh.merge(get_pad()); TriangleMeshSlicer slicer(&fullmesh); SlicedSupports ret; - slicer.slice(heights, &ret, m_ctl.cancelfn); + slicer.slice(heights, &ret, get().ctl().cancelfn); return ret; } const TriangleMesh &SLASupportTree::add_pad(const SliceLayer& baseplate, - double min_wall_thickness_mm, - double min_wall_height_mm, - double max_merge_distance_mm, - double edge_radius_mm) const + const PoolConfig& pcfg) const { - PoolConfig pcfg; - pcfg.min_wall_thickness_mm = min_wall_thickness_mm; - pcfg.min_wall_height_mm = min_wall_height_mm; - pcfg.max_merge_distance_mm = max_merge_distance_mm; - pcfg.edge_radius_mm = edge_radius_mm; +// PoolConfig pcfg; +// pcfg.min_wall_thickness_mm = min_wall_thickness_mm; +// pcfg.min_wall_height_mm = min_wall_height_mm; +// pcfg.max_merge_distance_mm = max_merge_distance_mm; +// pcfg.edge_radius_mm = edge_radius_mm; return m_impl->create_pad(merged_mesh(), baseplate, pcfg).tmesh; } @@ -1687,14 +1707,14 @@ SLASupportTree::SLASupportTree(const PointSet &points, const EigenMesh3D& emesh, const SupportConfig &cfg, const Controller &ctl): - m_impl(new Impl()), m_ctl(ctl) + m_impl(new Impl(ctl)) { m_impl->ground_level = emesh.ground_level - cfg.object_elevation_mm; generate(points, emesh, cfg, ctl); } SLASupportTree::SLASupportTree(const SLASupportTree &c): - m_impl(new Impl(*c.m_impl)), m_ctl(c.m_ctl) {} + m_impl(new Impl(*c.m_impl)) {} SLASupportTree &SLASupportTree::operator=(const SLASupportTree &c) { diff --git a/src/libslic3r/SLA/SLASupportTree.hpp b/src/libslic3r/SLA/SLASupportTree.hpp index 9213101f3..90162d6b5 100644 --- a/src/libslic3r/SLA/SLASupportTree.hpp +++ b/src/libslic3r/SLA/SLASupportTree.hpp @@ -69,6 +69,8 @@ struct SupportConfig { double object_elevation_mm = 10; }; +struct PoolConfig; + /// A Control structure for the support calculation. Consists of the status /// indicator callback and the stop condition predicate. struct Controller { @@ -119,7 +121,6 @@ public: class SLASupportTree { class Impl; std::unique_ptr m_impl; - Controller m_ctl; Impl& get() { return *m_impl; } const Impl& get() const { return *m_impl; } @@ -158,10 +159,7 @@ public: /// Adding the "pad" (base pool) under the supports const TriangleMesh& add_pad(const SliceLayer& baseplate, - double min_wall_thickness_mm, - double min_wall_height_mm, - double max_merge_distance_mm, - double edge_radius_mm) const; + const PoolConfig& pcfg) const; /// Get the pad geometry const TriangleMesh& get_pad() const; diff --git a/src/libslic3r/SLAPrint.cpp b/src/libslic3r/SLAPrint.cpp index 3cb4920a5..9eb26fe75 100644 --- a/src/libslic3r/SLAPrint.cpp +++ b/src/libslic3r/SLAPrint.cpp @@ -445,7 +445,7 @@ void SLAPrint::process() // Slicing the model object. This method is oversimplified and needs to // be compared with the fff slicing algorithm for verification - auto slice_model = [this, ilh, ilhd](SLAPrintObject& po) { + auto slice_model = [this, ilh](SLAPrintObject& po) { double lh = po.m_config.layer_height.getFloat(); TriangleMesh mesh = po.transformed_mesh(); @@ -530,6 +530,11 @@ void SLAPrint::process() po.m_supportdata->support_tree_ptr.reset( new SLASupportTree(pts, emesh, scfg, ctl)); + // Create the unified mesh + auto rc = SlicingStatus::RELOAD_SCENE; + set_status(-1, L("Visualizing supports")); + po.m_supportdata->support_tree_ptr->merged_mesh(); + set_status(-1, L("Visualizing supports"), rc); } catch(sla::SLASupportsStoppedException&) { // no need to rethrow // throw_if_canceled(); @@ -560,18 +565,23 @@ void SLAPrint::process() auto&& trmesh = po.transformed_mesh(); // This call can get pretty time consuming - if(elevation < pad_h) sla::base_plate(trmesh, bp, - float(pad_h), float(lh)); + auto thrfn = [this](){ throw_if_canceled(); }; - po.m_supportdata->support_tree_ptr->add_pad(bp, wt, h, md, er); + if(elevation < pad_h) + sla::base_plate(trmesh, bp, float(pad_h), float(lh), + thrfn); + + pcfg.throw_on_cancel = thrfn; + po.m_supportdata->support_tree_ptr->add_pad(bp, pcfg); } - // if the base pool (which means also the support tree) is - // done, do a refresh when indicating progress. Now the - // geometries for the supports and the optional base pad are - // ready. We can grant access for the control thread to read - // the geometries, but first we have to update the caches: - po.support_mesh(); /*po->pad_mesh();*/ +// // if the base pool (which means also the support tree) is +// // done, do a refresh when indicating progress. Now the +// // geometries for the supports and the optional base pad are +// // ready. We can grant access for the control thread to read +// // the geometries, but first we have to update the caches: +// po.support_mesh(); /*po->pad_mesh();*/ + po.throw_if_canceled(); auto rc = SlicingStatus::RELOAD_SCENE; set_status(-1, L("Visualizing supports"), rc); }; @@ -589,9 +599,9 @@ void SLAPrint::process() // We have the layer polygon collection but we need to unite them into // an index where the key is the height level in discrete levels (clipper) - auto index_slices = [this, ilh, ilhd](SLAPrintObject& po) { + auto index_slices = [ilhd](SLAPrintObject& po) { po.m_slice_index.clear(); - auto sih = LevelID(scale_(ilh)); + auto sih = LevelID(scale_(ilhd)); // For all print objects, go through its initial layers and place them // into the layers hash @@ -635,7 +645,7 @@ void SLAPrint::process() // shortcut for empty index into the slice vectors static const auto EMPTY_SLICE = SLAPrintObject::SliceRecord::NONE; - for(int i = 0; i < oslices.size(); ++i) { + for(size_t i = 0; i < oslices.size(); ++i) { LevelID h = levelids[i]; float fh = float(double(h) * SCALING_FACTOR); @@ -652,7 +662,7 @@ void SLAPrint::process() po.m_supportdata->level_ids.clear(); po.m_supportdata->level_ids.reserve(sslices.size()); - for(int i = 0; i < sslices.size(); ++i) { + for(int i = 0; i < int(sslices.size()); ++i) { int a = i == 0 ? 0 : 1; int b = i == 0 ? 0 : i - 1; LevelID h = sminZ + a * sih + b * slh; @@ -662,7 +672,7 @@ void SLAPrint::process() SLAPrintObject::SliceRecord& sr = po.m_slice_index[fh]; assert(sr.support_slices_idx == EMPTY_SLICE); - sr.support_slices_idx = i; + sr.support_slices_idx = SLAPrintObject::SliceRecord::Idx(i); } } }; @@ -670,7 +680,7 @@ void SLAPrint::process() auto& levels = m_printer_input; // Rasterizing the model objects, and their supports - auto rasterize = [this, ilh, ilhd, max_objstatus, &levels]() { + auto rasterize = [this, &levels]() { if(canceled()) return; // clear the rasterizer input @@ -688,14 +698,14 @@ void SLAPrint::process() // now merge this object's support and object slices with the rest // of the print object slices - for(int i = 0; i < oslices.size(); ++i) { + for(size_t i = 0; i < oslices.size(); ++i) { auto& lyrs = levels[gndlvl + po.m_level_ids[i]]; lyrs.emplace_back(oslices[i], po.m_instances); } if(!po.m_supportdata) continue; auto& sslices = po.m_supportdata->support_slices; - for(int i = 0; i < sslices.size(); ++i) { + for(size_t i = 0; i < sslices.size(); ++i) { auto& lyrs = levels[gndlvl + po.m_supportdata->level_ids[i]]; lyrs.emplace_back(sslices[i], po.m_instances); } @@ -713,8 +723,8 @@ void SLAPrint::process() double w = printcfg.display_width.getFloat(); double h = printcfg.display_height.getFloat(); - unsigned pw = printcfg.display_pixels_x.getInt(); - unsigned ph = printcfg.display_pixels_y.getInt(); + auto pw = unsigned(printcfg.display_pixels_x.getInt()); + auto ph = unsigned(printcfg.display_pixels_y.getInt()); double lh = ocfg.layer_height.getFloat(); double exp_t = matcfg.exposure_time.getFloat(); double iexp_t = matcfg.initial_exposure_time.getFloat(); @@ -1098,7 +1108,9 @@ TriangleMesh SLAPrintObject::get_mesh(SLAPrintObjectStep step) const const TriangleMesh& SLAPrintObject::support_mesh() const { if(m_config.supports_enable.getBool() && m_supportdata && - m_supportdata->support_tree_ptr) return m_supportdata->support_tree_ptr->merged_mesh(); + m_supportdata->support_tree_ptr) { + return m_supportdata->support_tree_ptr->merged_mesh(); + } return EMPTY_MESH; }