wip: reorganizing things to support accurate pad creation. Also more accurate progress indication.

This commit is contained in:
tamasmeszaros 2018-11-15 18:05:47 +01:00
parent 9bb04ff15a
commit 810fcc2221
5 changed files with 90 additions and 73 deletions

View file

@ -192,7 +192,7 @@ public:
// Calls a registered callback to update the status, or print out the default message.
void set_status(int percent, const std::string &message) {
if (m_status_callback) m_status_callback(percent, message);
else printf("%d => %s\n", percent, message.c_str());
/*else */printf("%d => %s\n", percent, message.c_str());
}
typedef std::function<void()> cancel_callback_type;

View file

@ -9,6 +9,7 @@
#include "SLASpatIndex.hpp"
#include "SLABasePool.hpp"
#include <libnest2d/tools/benchmark.h>
#include "ClipperUtils.hpp"
#include "Model.hpp"
@ -510,14 +511,16 @@ struct Pad {
Pad() {}
Pad(const TriangleMesh& object_support_mesh,
const ExPolygons& baseplate,
double ground_level,
const PoolConfig& cfg) : zlevel(ground_level)
{
ExPolygons basep;
base_plate(object_support_mesh, basep);
for(auto& bp : baseplate) basep.emplace_back(bp);
union_ex(basep);
create_base_pool(basep, tmesh, cfg);
tmesh.translate(0, 0, float(zlevel));
std::cout << "pad ground level " << zlevel << std::endl;
}
};
@ -563,6 +566,10 @@ EigenMesh3D to_eigenmesh(const TriangleMesh& tmesh) {
const stl_file& stl = tmesh.stl;
EigenMesh3D outmesh;
auto&& bb = tmesh.bounding_box();
outmesh.ground_level += bb.min(Z);
auto& V = outmesh.V;
auto& F = outmesh.F;
@ -586,28 +593,7 @@ EigenMesh3D to_eigenmesh(const TriangleMesh& tmesh) {
}
EigenMesh3D to_eigenmesh(const ModelObject& modelobj) {
auto&& rmesh = modelobj.raw_mesh();
// we need to transform the raw mesh...
// currently all the instances share the same x and y rotation and scaling
// so we have to extract those from e.g. the first instance and apply to the
// raw mesh. This is also true for the support points.
// BUT: when the support structure is spawned for each instance than it has
// to omit the X, Y rotation and scaling as those have been already applied
// or apply an inverse transformation on the support structure after it
// has been created.
// auto rot = modelobj.instances.front()->get_rotation();
// auto scaling = modelobj.instances.front()->get_scaling_factor();
// rmesh.rotate(float(rot(X)), Axis::X);
// rmesh.rotate(float(rot(Y)), Axis::Y);
// rmesh.scale(scaling);
auto&& ret = to_eigenmesh(rmesh);
auto&& bb = rmesh.bounding_box();
ret.ground_level = bb.min(Z);
return ret;
return to_eigenmesh(modelobj.raw_mesh());
}
EigenMesh3D to_eigenmesh(const Model& model) {
@ -767,8 +753,9 @@ public:
}
const Pad& create_pad(const TriangleMesh& object_supports,
const ExPolygons& baseplate,
const PoolConfig& cfg) {
m_pad = Pad(object_supports, ground_level, cfg);
m_pad = Pad(object_supports, baseplate, ground_level, cfg);
return m_pad;
}
@ -1014,7 +1001,6 @@ bool SLASupportTree::generate(const PointSet &points,
using Result = SLASupportTree::Impl;
Result& result = *m_impl;
result.ground_level = mesh.ground_level;
enum Steps {
BEGIN,
@ -1283,7 +1269,7 @@ bool SLASupportTree::generate(const PointSet &points,
const double hbr = cfg.head_back_radius_mm;
const double pradius = cfg.pillar_radius_mm;
const double maxbridgelen = cfg.max_bridge_length_mm;
const double gndlvl = emesh.ground_level - cfg.object_elevation_mm;
const double gndlvl = emesh.ground_level;
ClusterEl cl_centroids;
cl_centroids.reserve(gnd_clusters.size());
@ -1759,7 +1745,8 @@ SlicedSupports SLASupportTree::slice(float layerh, float init_layerh) const
return ret;
}
const TriangleMesh &SLASupportTree::add_pad(double min_wall_thickness_mm,
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
@ -1771,7 +1758,7 @@ const TriangleMesh &SLASupportTree::add_pad(double 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(mm, pcfg).tmesh;
return m_impl->create_pad(mm, baseplate, pcfg).tmesh;
}
const TriangleMesh &SLASupportTree::get_pad() const
@ -1791,6 +1778,7 @@ SLASupportTree::SLASupportTree(const PointSet &points,
const SupportConfig &cfg,
const Controller &ctl): m_impl(new Impl())
{
m_impl->ground_level = emesh.ground_level - cfg.object_elevation_mm;
generate(points, emesh, cfg, ctl);
}

View file

@ -160,7 +160,8 @@ public:
SlicedSupports slice(float layerh, float init_layerh = -1.0) const;
/// Adding the "pad" (base pool) under the supports
const TriangleMesh& add_pad(double min_wall_thickness_mm,
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;

View file

@ -1,5 +1,6 @@
#include "SLAPrint.hpp"
#include "SLA/SLASupportTree.hpp"
#include "SLA/SLABasePool.hpp"
#include <tbb/parallel_for.h>
//#include <tbb/spin_mutex.h>//#include "tbb/mutex.h"
@ -27,12 +28,12 @@ namespace {
const std::array<unsigned, slaposCount> OBJ_STEP_LEVELS =
{
0,
20,
30,
50,
70,
80,
100
90
};
const std::array<std::string, slaposCount> OBJ_STEP_LABELS =
@ -47,8 +48,9 @@ const std::array<std::string, slaposCount> OBJ_STEP_LABELS =
const std::array<unsigned, slapsCount> PRINT_STEP_LEVELS =
{
// This is after processing all the Print objects, so we start from 50%
50, // slapsRasterize
100, // slapsValidate
90, // slapsValidate
};
const std::array<std::string, slapsCount> PRINT_STEP_LABELS =
@ -118,8 +120,6 @@ void SLAPrint::process()
{
using namespace sla;
std::cout << "SLA Processing triggered" << std::endl;
// Assumption: at this point the print objects should be populated only with
// the model objects we have to process and the instances are also filtered
@ -131,9 +131,7 @@ void SLAPrint::process()
auto slice_model = [ilh](SLAPrintObject& po) {
auto lh = float(po.m_config.layer_height.getFloat());
ModelObject *o = po.m_model_object;
TriangleMesh&& mesh = o->raw_mesh();
TriangleMesh mesh = po.transformed_mesh();
TriangleMeshSlicer slicer(&mesh);
auto bb3d = mesh.bounding_box();
@ -150,11 +148,10 @@ void SLAPrint::process()
ModelObject& mo = *po.m_model_object;
if(!mo.sla_support_points.empty()) {
po.m_supportdata.reset(new SLAPrintObject::SupportData());
po.m_supportdata->emesh = sla::to_eigenmesh(mo);
po.m_supportdata->support_points = sla::support_points(mo);
po.m_supportdata->emesh = sla::to_eigenmesh(po.transformed_mesh());
std::cout << "support points copied "
<< po.m_supportdata->support_points.rows() << std::endl;
// TODO: transform support points appropriately
po.m_supportdata->support_points = sla::support_points(mo);
}
// for(SLAPrintObject *po : pobjects) {
@ -195,17 +192,19 @@ void SLAPrint::process()
// and before the supports had been sliced. (or the slicing has to be
// repeated)
// if(po.is_step_done(slaposSupportTree) &&
// po.m_supportdata &&
// po.m_supportdata->support_tree_ptr)
// {
// double wt = po.m_config.pad_wall_thickness.getFloat();
// double h = po.m_config.pad_wall_height.getFloat();
// double md = po.m_config.pad_max_merge_distance.getFloat();
// double er = po.m_config.pad_edge_radius.getFloat();
if(po.is_step_done(slaposSupportTree) &&
po.m_supportdata &&
po.m_supportdata->support_tree_ptr)
{
double wt = po.m_config.pad_wall_thickness.getFloat();
double h = po.m_config.pad_wall_height.getFloat();
double md = po.m_config.pad_max_merge_distance.getFloat();
double er = po.m_config.pad_edge_radius.getFloat();
// po.m_supportdata->support_tree_ptr->add_pad(wt, h, md, er);
// }
sla::ExPolygons bp;
sla::base_plate(po.transformed_mesh(), bp);
po.m_supportdata->support_tree_ptr->add_pad(bp, wt, h, md, er);
}
};
// Slicing the support geometries similarly to the model slicing procedure.
@ -216,13 +215,12 @@ void SLAPrint::process()
if(sd && sd->support_tree_ptr) {
auto lh = float(po.m_config.layer_height.getFloat());
sd->support_slices = sd->support_tree_ptr->slice(lh, ilh);
std::cout << "support slice count " << sd->support_slices.size() << std::endl;
}
};
// Rasterizing the model objects, and their supports
auto rasterize = [this, ilh]() {
using Layer = ExPolygons;
using Layer = sla::ExPolygons;
using LayerCopies = std::vector<SLAPrintObject::Instance>;
struct LayerRef {
std::reference_wrapper<const Layer> lref;
@ -240,7 +238,7 @@ void SLAPrint::process()
// into the layers hash
for(SLAPrintObject *o : m_objects) {
double lh = o->m_config.layer_height.getFloat();
std::vector<ExPolygons> & oslices = o->m_model_slices;
SlicedModel & oslices = o->m_model_slices;
for(int i = 0; i < oslices.size(); ++i) {
double h = ilh + i * lh;
long long lyridx = static_cast<long long>(scale_(h));
@ -248,11 +246,8 @@ void SLAPrint::process()
lyrs.emplace_back(oslices[i], o->m_instances);
}
std::cout << "model slice count at rasterization: " << oslices.size() << std::endl;
if(o->m_supportdata) { // deal with the support slices if present
auto& sslices = o->m_supportdata->support_slices;
std::cout << "support slice count at rasterization: " << o->m_supportdata->support_slices.size() << std::endl;
for(int i = 0; i < sslices.size(); ++i) {
double h = ilh + i * lh;
@ -263,12 +258,12 @@ void SLAPrint::process()
}
}
if(canceled()) return;
// collect all the keys
std::vector<long long> keys; keys.reserve(levels.size());
for(auto& e : levels) keys.emplace_back(e.first);
std::cout << "levels count at rasterization " << levels.size() << std::endl;
{ // create a raster printer for the current print parameters
// I don't know any better
auto& ocfg = m_objects.front()->m_config;
@ -291,14 +286,21 @@ void SLAPrint::process()
auto lvlcnt = unsigned(levels.size());
printer.layers(lvlcnt);
// TODO exclusive progress indication for this step would be good
// as it is the longest of all. It would require synchronization
// in the parallel processing.
// procedure to process one height level. This will run in parallel
auto lvlfn = [&keys, &levels, &printer](unsigned level_id) {
auto lvlfn = [this, &keys, &levels, &printer](unsigned level_id) {
if(canceled()) return;
LayerRefs& lrange = levels[keys[level_id]];
// Switch to the appropriate layer in the printer
printer.begin_layer(level_id);
for(auto& lyrref : lrange) { // for all layers in the current level
if(canceled()) break;
const Layer& sl = lyrref.lref; // get the layer reference
const LayerCopies& copies = lyrref.copies;
@ -316,6 +318,9 @@ void SLAPrint::process()
printer.finish_layer(level_id);
};
// last minute escape
if(canceled()) return;
// Sequential version (for testing)
// for(unsigned l = 0; l < lvlcnt; ++l) process_level(l);
@ -351,6 +356,11 @@ void SLAPrint::process()
[](){} // validate
};
const unsigned min_objstatus = 0;
const unsigned max_objstatus = PRINT_STEP_LEVELS[slapsRasterize];
const size_t objcount = m_objects.size();
const double ostepd = (max_objstatus - min_objstatus) / (objcount * 100.0);
for(SLAPrintObject * po : m_objects) {
for(size_t s = 0; s < pobj_program.size(); ++s) {
auto currentstep = objectsteps[s];
@ -361,8 +371,9 @@ void SLAPrint::process()
throw_if_canceled();
if(po->m_stepmask[s] && !po->is_step_done(currentstep)) {
set_status(OBJ_STEP_LEVELS[currentstep],
OBJ_STEP_LABELS[currentstep]);
unsigned st = OBJ_STEP_LEVELS[currentstep];
st = unsigned(min_objstatus + st * ostepd);
set_status(st, OBJ_STEP_LABELS[currentstep]);
po->set_started(currentstep);
pobj_program[s](*po);
@ -375,7 +386,7 @@ void SLAPrint::process()
slapsRasterize, slapsValidate
};
// TODO: enable rasterizing
// this would disable the rasterization step
// m_stepmask[slapsRasterize] = false;
for(size_t s = 0; s < print_program.size(); ++s) {
@ -402,7 +413,6 @@ SLAPrintObject::SLAPrintObject(SLAPrint *print, ModelObject *model_object):
m_model_object(model_object),
m_stepmask(slaposCount, true)
{
}
SLAPrintObject::~SLAPrintObject() {}
@ -414,22 +424,33 @@ TriangleMesh SLAPrintObject::support_mesh() const
if(m_supportdata && m_supportdata->support_tree_ptr)
m_supportdata->support_tree_ptr->merged_mesh(trm);
// TODO: is this necessary?
trm.repair();
std::cout << "support mesh united and returned" << std::endl;
return trm;
// return make_cube(10., 10., 10.);
}
TriangleMesh SLAPrintObject::pad_mesh() const
{
if(!m_supportdata || !m_supportdata->support_tree_ptr) {
std::cout << "Empty pad mesh returned.." << std::endl;
return TriangleMesh();
}
if(!m_supportdata || !m_supportdata->support_tree_ptr) return {};
// FIXME: pad mesh is empty here for some reason.
return m_supportdata->support_tree_ptr->get_pad();
}
const TriangleMesh &SLAPrintObject::transformed_mesh() const {
// we need to transform the raw mesh...
// currently all the instances share the same x and y rotation and scaling
// so we have to extract those from e.g. the first instance and apply to the
// raw mesh. This is also true for the support points.
// BUT: when the support structure is spawned for each instance than it has
// to omit the X, Y rotation and scaling as those have been already applied
// or apply an inverse transformation on the support structure after it
// has been created.
if(m_trmesh_valid) return m_transformed_rmesh;
m_transformed_rmesh = m_model_object->raw_mesh();
m_transformed_rmesh.transform(m_trafo);
m_trmesh_valid = true;
}
} // namespace Slic3r

View file

@ -57,6 +57,9 @@ public:
// Support mesh is only valid if this->is_step_done(slaposPad) is true.
TriangleMesh pad_mesh() const;
// This will return the transformed mesh which is cached
const TriangleMesh& transformed_mesh() const;
// I refuse to grantee copying (Tamas)
SLAPrintObject(const SLAPrintObject&) = delete;
SLAPrintObject& operator=(const SLAPrintObject&) = delete;
@ -71,7 +74,7 @@ protected:
void config_apply(const ConfigBase &other, bool ignore_nonexistent = false) { this->m_config.apply(other, ignore_nonexistent); }
void config_apply_only(const ConfigBase &other, const t_config_option_keys &keys, bool ignore_nonexistent = false)
{ this->m_config.apply_only(other, keys, ignore_nonexistent); }
void set_trafo(const Transform3d& trafo) { m_trafo = trafo; }
void set_trafo(const Transform3d& trafo) { m_trafo = trafo; m_trmesh_valid = false; }
bool set_instances(const std::vector<Instance> &instances);
// Invalidates the step, and its depending steps in SLAPrintObject and SLAPrint.
@ -90,6 +93,10 @@ private:
std::vector<bool> m_stepmask;
std::vector<ExPolygons> m_model_slices;
// Caching the transformed (m_trafo) raw mesh of the object
mutable TriangleMesh m_transformed_rmesh;
mutable bool m_trmesh_valid = false;
class SupportData;
std::unique_ptr<SupportData> m_supportdata;
};