Added cancellation points to pad creation step.

This commit is contained in:
tamasmeszaros 2018-12-11 15:54:54 +01:00
parent e1cea03cda
commit b31c62e044
7 changed files with 102 additions and 55 deletions

View File

@ -1,10 +1,10 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <libslic3r.h> #include <libslic3r/libslic3r.h>
#include "TriangleMesh.hpp" #include <libslic3r/TriangleMesh.hpp>
#include "SLABasePool.hpp" #include <libslic3r/SLA/SLABasePool.hpp>
#include "benchmark.h" #include <libnest2d/tools/benchmark.h>
const std::string USAGE_STR = { const std::string USAGE_STR = {
"Usage: slabasebed stlfilename.stl" "Usage: slabasebed stlfilename.stl"
@ -28,7 +28,7 @@ int main(const int argc, const char *argv[]) {
ExPolygons ground_slice; ExPolygons ground_slice;
TriangleMesh basepool; TriangleMesh basepool;
sla::ground_layer(model, ground_slice, 0.1f); sla::base_plate(model, ground_slice, 0.1f);
bench.start(); bench.start();
sla::create_base_pool(ground_slice, basepool); sla::create_base_pool(ground_slice, basepool);

View File

@ -10,6 +10,10 @@
namespace Slic3r { namespace sla { namespace Slic3r { namespace sla {
namespace {
ThrowOnCancel throw_on_cancel = [](){};
}
/// Convert the triangulation output to an intermediate mesh. /// Convert the triangulation output to an intermediate mesh.
Contour3D convert(const Polygons& triangles, coord_t z, bool dir) { 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(), 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) for(auto& p : pp.points)
if(is_upper(p)) if(is_upper(p))
rp.emplace_back(unscale(x(p), y(p), mm(cz))); rp.emplace_back(unscale(x(p), y(p), mm(cz)));
@ -231,6 +239,8 @@ Contour3D round_edges(const ExPolygon& base_plate,
if(degrees >= 90) { if(degrees >= 90) {
for(int i = 1; i <= steps; ++i) { for(int i = 1; i <= steps; ++i) {
throw_on_cancel();
ob = base_plate; ob = base_plate;
double r2 = radius_mm * radius_mm; double r2 = radius_mm * radius_mm;
@ -254,6 +264,7 @@ Contour3D round_edges(const ExPolygon& base_plate,
int tos = int(tox / stepx); int tos = int(tox / stepx);
for(int i = 1; i <= tos; ++i) { for(int i = 1; i <= tos; ++i) {
throw_on_cancel();
ob = base_plate; ob = base_plate;
double r2 = radius_mm * radius_mm; 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), std::back_inserter(punion),
[&punion, &boxindex, cc, max_dist_mm, &idx](const Point& c) [&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 dx = x(c) - x(cc), dy = y(c) - y(cc);
double l = std::sqrt(dx * dx + dy * dy); double l = std::sqrt(dx * dx + dy * dy);
double nx = dx / l, ny = dy / l; 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, void base_plate(const TriangleMesh &mesh, ExPolygons &output, float h,
float layerh) float layerh, ThrowOnCancel thrfn)
{ {
TriangleMesh m = mesh; TriangleMesh m = mesh;
TriangleMeshSlicer slicer(&m); TriangleMeshSlicer slicer(&m);
@ -431,7 +442,7 @@ void base_plate(const TriangleMesh &mesh, ExPolygons &output, float h,
heights.emplace_back(hi); heights.emplace_back(hi);
std::vector<ExPolygons> out; out.reserve(size_t(std::ceil(h/layerh))); std::vector<ExPolygons> 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(); size_t count = 0; for(auto& o : out) count += o.size();
ExPolygons tmp; tmp.reserve(count); 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, void create_base_pool(const ExPolygons &ground_layer, TriangleMesh& out,
const PoolConfig& cfg) 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) + double mdist = 2*(1.8*cfg.min_wall_thickness_mm + 4*cfg.edge_radius_mm) +
cfg.max_merge_distance_mm; cfg.max_merge_distance_mm;

View File

@ -2,6 +2,7 @@
#define SLABASEPOOL_HPP #define SLABASEPOOL_HPP
#include <vector> #include <vector>
#include <functional>
namespace Slic3r { namespace Slic3r {
@ -11,12 +12,14 @@ class TriangleMesh;
namespace sla { namespace sla {
using ExPolygons = std::vector<ExPolygon>; using ExPolygons = std::vector<ExPolygon>;
using ThrowOnCancel = std::function<void(void)>;
/// Calculate the polygon representing the silhouette from the specified height /// Calculate the polygon representing the silhouette from the specified height
void base_plate(const TriangleMesh& mesh, void base_plate(const TriangleMesh& mesh, // input mesh
ExPolygons& output, ExPolygons& output, // Output will be merged with
float zlevel = 0.1f, float zlevel = 0.1f, // Plate creation level
float layerheight = 0.05f); float layerheight = 0.05f, // The sampling height
ThrowOnCancel thrfn = [](){}); // Will be called frequently
struct PoolConfig { struct PoolConfig {
double min_wall_thickness_mm = 2; double min_wall_thickness_mm = 2;
@ -24,6 +27,8 @@ struct PoolConfig {
double max_merge_distance_mm = 50; double max_merge_distance_mm = 50;
double edge_radius_mm = 1; double edge_radius_mm = 1;
ThrowOnCancel throw_on_cancel = [](){};
inline PoolConfig() {} inline PoolConfig() {}
inline PoolConfig(double wt, double wh, double md, double er): inline PoolConfig(double wt, double wh, double md, double er):
min_wall_thickness_mm(wt), min_wall_thickness_mm(wt),
@ -35,8 +40,7 @@ struct PoolConfig {
/// Calculate the pool for the mesh for SLA printing /// Calculate the pool for the mesh for SLA printing
void create_base_pool(const ExPolygons& base_plate, void create_base_pool(const ExPolygons& base_plate,
TriangleMesh& output_mesh, TriangleMesh& output_mesh,
const PoolConfig& = PoolConfig() const PoolConfig& = PoolConfig());
);
/// TODO: Currently the base plate of the pool will have half the height of the /// 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 /// whole pool. So the carved out space has also half the height. This is not

View File

@ -58,7 +58,7 @@ struct Contour3D {
points.insert(points.end(), ctr.points.begin(), ctr.points.end()); points.insert(points.end(), ctr.points.begin(), ctr.points.end());
indices.insert(indices.end(), ctr.indices.begin(), ctr.indices.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; auto& idx = indices[n]; x(idx) += s3; y(idx) += s3; z(idx) += s3;
} }
} }

View File

@ -515,8 +515,13 @@ struct Pad {
zlevel(ground_level + sla::get_pad_elevation(pcfg)) zlevel(ground_level + sla::get_pad_elevation(pcfg))
{ {
ExPolygons basep; 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, 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); for(auto& bp : baseplate) basep.emplace_back(bp);
create_base_pool(basep, tmesh, cfg); create_base_pool(basep, tmesh, cfg);
@ -622,12 +627,19 @@ class SLASupportTree::Impl {
std::vector<Junction> m_junctions; std::vector<Junction> m_junctions;
std::vector<Bridge> m_bridges; std::vector<Bridge> m_bridges;
std::vector<CompactBridge> m_compact_bridges; std::vector<CompactBridge> m_compact_bridges;
Controller m_ctl;
Pad m_pad; Pad m_pad;
mutable TriangleMesh meshcache; mutable bool meshcache_valid; mutable TriangleMesh meshcache; mutable bool meshcache_valid;
mutable double model_height = 0; // the full height of the model mutable double model_height = 0; // the full height of the model
public: public:
double ground_level = 0; double ground_level = 0;
Impl() = default;
inline Impl(const Controller& ctl): m_ctl(ctl) {}
const Controller& ctl() const { return m_ctl; }
template<class...Args> Head& add_head(Args&&... args) { template<class...Args> Head& add_head(Args&&... args) {
m_heads.emplace_back(std::forward<Args>(args)...); m_heads.emplace_back(std::forward<Args>(args)...);
m_heads.back().id = long(m_heads.size() - 1); m_heads.back().id = long(m_heads.size() - 1);
@ -710,27 +722,38 @@ public:
meshcache = TriangleMesh(); meshcache = TriangleMesh();
for(auto& head : heads()) { for(auto& head : heads()) {
if(m_ctl.stopcondition()) break;
auto&& m = mesh(head.mesh); auto&& m = mesh(head.mesh);
meshcache.merge(m); meshcache.merge(m);
} }
for(auto& stick : pillars()) { for(auto& stick : pillars()) {
if(m_ctl.stopcondition()) break;
meshcache.merge(mesh(stick.mesh)); meshcache.merge(mesh(stick.mesh));
meshcache.merge(mesh(stick.base)); meshcache.merge(mesh(stick.base));
} }
for(auto& j : junctions()) { for(auto& j : junctions()) {
if(m_ctl.stopcondition()) break;
meshcache.merge(mesh(j.mesh)); meshcache.merge(mesh(j.mesh));
} }
for(auto& cb : compact_bridges()) { for(auto& cb : compact_bridges()) {
if(m_ctl.stopcondition()) break;
meshcache.merge(mesh(cb.mesh)); meshcache.merge(mesh(cb.mesh));
} }
for(auto& bs : bridges()) { for(auto& bs : bridges()) {
if(m_ctl.stopcondition()) break;
meshcache.merge(mesh(bs.mesh)); 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? // TODO: Is this necessary?
meshcache.repair(); meshcache.repair();
@ -1659,22 +1682,19 @@ SlicedSupports SLASupportTree::slice(float layerh, float init_layerh) const
fullmesh.merge(get_pad()); fullmesh.merge(get_pad());
TriangleMeshSlicer slicer(&fullmesh); TriangleMeshSlicer slicer(&fullmesh);
SlicedSupports ret; SlicedSupports ret;
slicer.slice(heights, &ret, m_ctl.cancelfn); slicer.slice(heights, &ret, get().ctl().cancelfn);
return ret; return ret;
} }
const TriangleMesh &SLASupportTree::add_pad(const SliceLayer& baseplate, const TriangleMesh &SLASupportTree::add_pad(const SliceLayer& baseplate,
double min_wall_thickness_mm, const PoolConfig& pcfg) const
double min_wall_height_mm,
double max_merge_distance_mm,
double edge_radius_mm) const
{ {
PoolConfig pcfg; // PoolConfig pcfg;
pcfg.min_wall_thickness_mm = min_wall_thickness_mm; // pcfg.min_wall_thickness_mm = min_wall_thickness_mm;
pcfg.min_wall_height_mm = min_wall_height_mm; // pcfg.min_wall_height_mm = min_wall_height_mm;
pcfg.max_merge_distance_mm = max_merge_distance_mm; // pcfg.max_merge_distance_mm = max_merge_distance_mm;
pcfg.edge_radius_mm = edge_radius_mm; // pcfg.edge_radius_mm = edge_radius_mm;
return m_impl->create_pad(merged_mesh(), baseplate, pcfg).tmesh; return m_impl->create_pad(merged_mesh(), baseplate, pcfg).tmesh;
} }
@ -1687,14 +1707,14 @@ SLASupportTree::SLASupportTree(const PointSet &points,
const EigenMesh3D& emesh, const EigenMesh3D& emesh,
const SupportConfig &cfg, const SupportConfig &cfg,
const Controller &ctl): 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; m_impl->ground_level = emesh.ground_level - cfg.object_elevation_mm;
generate(points, emesh, cfg, ctl); generate(points, emesh, cfg, ctl);
} }
SLASupportTree::SLASupportTree(const SLASupportTree &c): 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) SLASupportTree &SLASupportTree::operator=(const SLASupportTree &c)
{ {

View File

@ -69,6 +69,8 @@ struct SupportConfig {
double object_elevation_mm = 10; double object_elevation_mm = 10;
}; };
struct PoolConfig;
/// A Control structure for the support calculation. Consists of the status /// A Control structure for the support calculation. Consists of the status
/// indicator callback and the stop condition predicate. /// indicator callback and the stop condition predicate.
struct Controller { struct Controller {
@ -119,7 +121,6 @@ public:
class SLASupportTree { class SLASupportTree {
class Impl; class Impl;
std::unique_ptr<Impl> m_impl; std::unique_ptr<Impl> m_impl;
Controller m_ctl;
Impl& get() { return *m_impl; } Impl& get() { return *m_impl; }
const Impl& get() const { return *m_impl; } const Impl& get() const { return *m_impl; }
@ -158,10 +159,7 @@ public:
/// Adding the "pad" (base pool) under the supports /// Adding the "pad" (base pool) under the supports
const TriangleMesh& add_pad(const SliceLayer& baseplate, const TriangleMesh& add_pad(const SliceLayer& baseplate,
double min_wall_thickness_mm, const PoolConfig& pcfg) const;
double min_wall_height_mm,
double max_merge_distance_mm,
double edge_radius_mm) const;
/// Get the pad geometry /// Get the pad geometry
const TriangleMesh& get_pad() const; const TriangleMesh& get_pad() const;

View File

@ -445,7 +445,7 @@ void SLAPrint::process()
// Slicing the model object. This method is oversimplified and needs to // Slicing the model object. This method is oversimplified and needs to
// be compared with the fff slicing algorithm for verification // 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(); double lh = po.m_config.layer_height.getFloat();
TriangleMesh mesh = po.transformed_mesh(); TriangleMesh mesh = po.transformed_mesh();
@ -530,6 +530,11 @@ void SLAPrint::process()
po.m_supportdata->support_tree_ptr.reset( po.m_supportdata->support_tree_ptr.reset(
new SLASupportTree(pts, emesh, scfg, ctl)); 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&) { } catch(sla::SLASupportsStoppedException&) {
// no need to rethrow // no need to rethrow
// throw_if_canceled(); // throw_if_canceled();
@ -560,18 +565,23 @@ void SLAPrint::process()
auto&& trmesh = po.transformed_mesh(); auto&& trmesh = po.transformed_mesh();
// This call can get pretty time consuming // This call can get pretty time consuming
if(elevation < pad_h) sla::base_plate(trmesh, bp, auto thrfn = [this](){ throw_if_canceled(); };
float(pad_h), float(lh));
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 // // if the base pool (which means also the support tree) is
// done, do a refresh when indicating progress. Now the // // done, do a refresh when indicating progress. Now the
// geometries for the supports and the optional base pad are // // geometries for the supports and the optional base pad are
// ready. We can grant access for the control thread to read // // ready. We can grant access for the control thread to read
// the geometries, but first we have to update the caches: // // the geometries, but first we have to update the caches:
po.support_mesh(); /*po->pad_mesh();*/ // po.support_mesh(); /*po->pad_mesh();*/
po.throw_if_canceled();
auto rc = SlicingStatus::RELOAD_SCENE; auto rc = SlicingStatus::RELOAD_SCENE;
set_status(-1, L("Visualizing supports"), rc); 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 // 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) // 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(); 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 // For all print objects, go through its initial layers and place them
// into the layers hash // into the layers hash
@ -635,7 +645,7 @@ void SLAPrint::process()
// shortcut for empty index into the slice vectors // shortcut for empty index into the slice vectors
static const auto EMPTY_SLICE = SLAPrintObject::SliceRecord::NONE; 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]; LevelID h = levelids[i];
float fh = float(double(h) * SCALING_FACTOR); 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.clear();
po.m_supportdata->level_ids.reserve(sslices.size()); 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 a = i == 0 ? 0 : 1;
int b = i == 0 ? 0 : i - 1; int b = i == 0 ? 0 : i - 1;
LevelID h = sminZ + a * sih + b * slh; LevelID h = sminZ + a * sih + b * slh;
@ -662,7 +672,7 @@ void SLAPrint::process()
SLAPrintObject::SliceRecord& sr = po.m_slice_index[fh]; SLAPrintObject::SliceRecord& sr = po.m_slice_index[fh];
assert(sr.support_slices_idx == EMPTY_SLICE); 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; auto& levels = m_printer_input;
// Rasterizing the model objects, and their supports // Rasterizing the model objects, and their supports
auto rasterize = [this, ilh, ilhd, max_objstatus, &levels]() { auto rasterize = [this, &levels]() {
if(canceled()) return; if(canceled()) return;
// clear the rasterizer input // clear the rasterizer input
@ -688,14 +698,14 @@ void SLAPrint::process()
// now merge this object's support and object slices with the rest // now merge this object's support and object slices with the rest
// of the print object slices // 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]]; auto& lyrs = levels[gndlvl + po.m_level_ids[i]];
lyrs.emplace_back(oslices[i], po.m_instances); lyrs.emplace_back(oslices[i], po.m_instances);
} }
if(!po.m_supportdata) continue; if(!po.m_supportdata) continue;
auto& sslices = po.m_supportdata->support_slices; 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]]; auto& lyrs = levels[gndlvl + po.m_supportdata->level_ids[i]];
lyrs.emplace_back(sslices[i], po.m_instances); lyrs.emplace_back(sslices[i], po.m_instances);
} }
@ -713,8 +723,8 @@ void SLAPrint::process()
double w = printcfg.display_width.getFloat(); double w = printcfg.display_width.getFloat();
double h = printcfg.display_height.getFloat(); double h = printcfg.display_height.getFloat();
unsigned pw = printcfg.display_pixels_x.getInt(); auto pw = unsigned(printcfg.display_pixels_x.getInt());
unsigned ph = printcfg.display_pixels_y.getInt(); auto ph = unsigned(printcfg.display_pixels_y.getInt());
double lh = ocfg.layer_height.getFloat(); double lh = ocfg.layer_height.getFloat();
double exp_t = matcfg.exposure_time.getFloat(); double exp_t = matcfg.exposure_time.getFloat();
double iexp_t = matcfg.initial_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 const TriangleMesh& SLAPrintObject::support_mesh() const
{ {
if(m_config.supports_enable.getBool() && m_supportdata && 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; return EMPTY_MESH;
} }