Slicing object and rasterization generates output zip. Needs testing.

This commit is contained in:
tamasmeszaros 2018-11-13 17:33:03 +01:00
parent 95419370e1
commit ffe6862626
5 changed files with 321 additions and 149 deletions

View file

@ -1,6 +1,9 @@
#include "SLAPrint.hpp"
#include "SLA/SLASupportTree.hpp"
#include <tbb/parallel_for.h>
//#include <tbb/spin_mutex.h>//#include "tbb/mutex.h"
#include "I18N.hpp"
//! macro used to mark string used at localization,
@ -9,12 +12,15 @@
namespace Slic3r {
using SlicedModel = SlicedSupports;
using SupportTreePtr = std::unique_ptr<sla::SLASupportTree>;
class SLAPrintObject::SupportData {
public:
sla::EigenMesh3D emesh; // index-triangle representation
sla::PointSet support_points; // all the support points (manual/auto)
std::unique_ptr<sla::SLASupportTree> support_tree_ptr; // the supports
SlicedSupports slice_cache; // sliced supports
sla::EigenMesh3D emesh; // index-triangle representation
sla::PointSet support_points; // all the support points (manual/auto)
SupportTreePtr support_tree_ptr; // the supports
SlicedSupports support_slices; // sliced supports
};
namespace {
@ -88,8 +94,14 @@ SLAPrint::ApplyStatus SLAPrint::apply(const Model &model,
m_model.assign_copy(model);
// Generate new SLAPrintObjects.
for (ModelObject *model_object : m_model.objects) {
//TODO
m_objects.emplace_back(new SLAPrintObject(this, model_object));
auto po = new SLAPrintObject(this, model_object);
m_objects.emplace_back(po);
for(ModelInstance *oinst : model_object->instances) {
Point tr = Point::new_scale(oinst->get_offset()(X),
oinst->get_offset()(Y));
auto rotZ = float(oinst->get_rotation()(Z));
po->m_instances.emplace_back(tr, rotZ);
}
}
}
@ -105,19 +117,33 @@ void SLAPrint::process()
// 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
auto slice_model = [](const SLAPrintObject&) {
// shortcut to initial layer height
auto ilh = float(m_material_config.initial_layer_height.getFloat());
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();
TriangleMeshSlicer slicer(&mesh);
auto bb3d = mesh.bounding_box();
auto H = bb3d.max(Z) - bb3d.min(Z);
std::vector<float> heights = {ilh};
for(float h = ilh; h < H; h += lh) heights.emplace_back(h);
auto& layers = po.m_model_slices;
slicer.slice(heights, &layers, [](){});
};
auto support_points = [](const SLAPrintObject&) {
auto support_points = [](SLAPrintObject&) {
// for(SLAPrintObject *po : pobjects) {
// TODO: calculate automatic support points
// po->m_supportdata->slice_cache contains the slices at this point
//}
};
auto support_tree = [this](const SLAPrintObject& po) {
auto support_tree = [this](SLAPrintObject& po) {
auto& emesh = po.m_supportdata->emesh;
auto& pts = po.m_supportdata->support_points; // nowhere filled yet
auto& supportd = *po.m_supportdata;
@ -141,19 +167,112 @@ void SLAPrint::process()
}
};
auto base_pool = [](const SLAPrintObject&) {
auto base_pool = [](SLAPrintObject&) {
};
auto slice_supports = [](const SLAPrintObject&) {
auto slice_supports = [](SLAPrintObject&) {
};
auto rasterize = []() {
auto rasterize = [this, ilh]() {
using Layer = ExPolygons;
using LayerCopies = std::vector<SLAPrintObject::Instance>;
struct LayerRef {
std::reference_wrapper<const Layer> lref;
std::reference_wrapper<const LayerCopies> copies;
LayerRef(const Layer& lyr, const LayerCopies& cp) :
lref(std::cref(lyr)), copies(std::cref(cp)) {}
};
using LayerRefs = std::vector<LayerRef>;
// layers according to quantized height levels
std::map<long long, LayerRefs> levels;
// For all print objects, go through its initial layers and place them
// into the layers hash
long long initlyridx = static_cast<long long>(scale_(ilh));
for(SLAPrintObject *o : m_objects) {
auto& oslices = o->m_model_slices;
auto& firstlyr = oslices.front();
auto& initlevel = levels[initlyridx];
initlevel.emplace_back(firstlyr, o->m_instances);
double lh = o->m_config.layer_height.getFloat();
size_t li = 1;
for(auto lit = std::next(oslices.begin());
lit != oslices.end();
++lit)
{
double h = ilh + li++ * lh;
long long lyridx = static_cast<long long>(scale_(h));
auto& lyrs = levels[lyridx];
lyrs.emplace_back(*lit, o->m_instances);
}
}
// collect all the keys
std::vector<long long> keys; keys.reserve(levels.size());
for(auto& e : levels) keys.emplace_back(e.first);
{ // create a raster printer for the current print parameters
// I don't know any better
auto& ocfg = m_objects.front()->m_config;
auto& matcfg = m_material_config;
auto& printcfg = m_printer_config;
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();
double lh = ocfg.layer_height.getFloat();
double exp_t = matcfg.exposure_time.getFloat();
double iexp_t = matcfg.initial_exposure_time.getFloat();
m_printer.reset(new SLAPrinter(w, h, pw, ph, lh, exp_t, iexp_t));
}
// Allocate space for all the layers
SLAPrinter& printer = *m_printer;
printer.layers(unsigned(levels.size()));
// procedure to process one height level. This will run in parallel
auto process_level = [&keys, &levels, &printer](unsigned level_id) {
LayerRefs& lrange = levels[keys[level_id]];
for(auto& lyrref : lrange) { // for all layers in the current level
const Layer& l = lyrref.lref; // get the layer reference
const LayerCopies& copies = lyrref.copies;
ExPolygonCollection sl = l;
// Switch to the appropriate layer in the printer
printer.begin_layer(level_id);
// Draw all the polygons in the slice to the actual layer.
for(auto& cp : copies) {
for(ExPolygon slice : sl.expolygons) {
slice.translate(cp.shift(X), cp.shift(Y));
slice.rotate(cp.rotation);
printer.draw_polygon(slice, level_id);
}
}
// Finish the layer for later saving it.
printer.finish_layer(level_id);
}
};
// Sequential version (for testing)
// for(unsigned l = 0; l < levels.size(); ++l) process_level(l);
// Print all the layers in parallel
tbb::parallel_for<size_t, decltype(process_level)>(0,
levels.size(),
process_level);
};
using slaposFn = std::function<void(const SLAPrintObject&)>;
using slaposFn = std::function<void(SLAPrintObject&)>;
using slapsFn = std::function<void(void)>;
std::array<SLAPrintObjectStep, slaposCount> objectsteps = {
@ -168,7 +287,7 @@ void SLAPrint::process()
std::array<slaposFn, slaposCount> pobj_program =
{
slice_model,
[](const SLAPrintObject&){}, // slaposSupportIslands now empty
[](SLAPrintObject&){}, // slaposSupportIslands now empty
support_points,
support_tree,
base_pool,
@ -229,11 +348,6 @@ void SLAPrint::render_supports(SLASupportRenderer &renderer)
std::cout << "Would show the SLA supports" << std::endl;
}
void SLAPrint::export_raster(const std::string &fname)
{
std::cout << "Would export the SLA raster" << std::endl;
}
SLAPrintObject::SLAPrintObject(SLAPrint *print, ModelObject *model_object):
Inherited(print),
m_model_object(model_object),
@ -242,6 +356,7 @@ SLAPrintObject::SLAPrintObject(SLAPrint *print, ModelObject *model_object):
{
m_supportdata->emesh = sla::to_eigenmesh(*m_model_object);
m_supportdata->support_points = sla::support_points(*m_model_object);
std::cout << "support points copied " << m_supportdata->support_points.rows() << std::endl;
}