Merge branch 'dev' of https://github.com/prusa3d/Slic3r into dev

This commit is contained in:
Enrico Turri 2018-08-28 09:04:12 +02:00
commit 41c093a258
361 changed files with 132181 additions and 19 deletions

View file

@ -592,7 +592,7 @@ void StaticConfig::set_defaults()
t_config_option_keys StaticConfig::keys() const
{
t_config_option_keys keys;
assert(this->def != nullptr);
assert(this->def() != nullptr);
for (const auto &opt_def : this->def()->options)
if (this->option(opt_def.first) != nullptr)
keys.push_back(opt_def.first);

View file

@ -1035,7 +1035,7 @@ public:
TYPE* option(const t_config_option_key &opt_key, bool create = false)
{
ConfigOption *opt = this->optptr(opt_key, create);
assert(opt == nullptr || opt->type() == TYPE::static_type());
// assert(opt == nullptr || opt->type() == TYPE::static_type());
return (opt == nullptr || opt->type() != TYPE::static_type()) ? nullptr : static_cast<TYPE*>(opt);
}
template<typename TYPE>

View file

@ -11,6 +11,10 @@
#include <unordered_set>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/log/trivial.hpp>
#include "slic3r/IProgressIndicator.hpp"
#include "PrintExport.hpp"
//! macro used to mark string used at localization,
//! return same string
@ -1230,7 +1234,22 @@ std::string Print::output_filepath(const std::string &path)
void Print::set_status(int percent, const std::string &message)
{
printf("Print::status %d => %s\n", percent, message.c_str());
if(progressindicator) progressindicator->update(unsigned(percent), message);
else {
printf("Print::status %d => %s\n", percent, message.c_str());
std::cout.flush();
}
}
void Print::print_to_png(std::string dirpath) {
print_to<FilePrinterFormat::PNG>(*this,
dirpath,
float(this->config.bed_size_x.value),
float(this->config.bed_size_y.value),
int(this->config.pixel_width.value),
int(this->config.pixel_height.value),
float(this->config.exp_time.value),
float(this->config.exp_time_first.value));
}
// Returns extruder this eec should be printed with, according to PrintRegion config

View file

@ -227,6 +227,9 @@ private:
typedef std::vector<PrintObject*> PrintObjectPtrs;
typedef std::vector<PrintRegion*> PrintRegionPtrs;
class IProgressIndicator;
using ProgressIndicatorPtr = std::shared_ptr<IProgressIndicator>;
// The complete print tray with possibly multiple objects.
class Print
{
@ -237,7 +240,10 @@ public:
PrintObjectPtrs objects;
PrintRegionPtrs regions;
PlaceholderParser placeholder_parser;
// TODO: status_cb
ProgressIndicatorPtr progressindicator;
std::string estimated_normal_print_time;
std::string estimated_silent_print_time;
double total_used_filament, total_extruded_volume, total_cost, total_weight;
@ -322,8 +328,10 @@ public:
// Has the calculation been canceled?
bool canceled() { return m_canceled; }
void print_to_png(std::string dirpath);
private:
bool invalidate_state_by_config_options(const std::vector<t_config_option_key> &opt_keys);
PrintRegionConfig _region_config_from_model_volume(const ModelVolume &volume);

View file

@ -2122,6 +2122,52 @@ void PrintConfigDef::init_fff_params()
def->sidetext = L("mm");
def->cli = "z-offset=f";
def->default_value = new ConfigOptionFloat(0);
def = this->add("bed_size_x", coFloat);
def->label = L("Bed size X");
def->category = L("Dwarf");
def->sidetext = L("mm");
def->cli = "bed-size-x=f";
def->default_value = new ConfigOptionFloat(68.);
def = this->add("bed_size_y", coFloat);
def->label = L("Bed size Y");
def->category = L("Dwarf");
def->sidetext = L("mm");
def->cli = "bed-size-y=f";
def->default_value = new ConfigOptionFloat(120.);
def = this->add("pixel_width", coInt);
def->label = L("Picture resolution X");
def->category = L("Dwarf");
def->sidetext = L("px");
def->cli = "pixel-width=i";
def->min = 1;
def->default_value = new ConfigOptionInt(1440);
def = this->add("pixel_height", coInt);
def->label = L("Picture resolution Y");
def->category = L("Dwarf");
def->sidetext = L("px");
def->cli = "pixel-height=i";
def->min = 1;
def->default_value = new ConfigOptionInt(2560);
def = this->add("exp_time", coFloat);
def->label = L("Exposure time");
def->category = L("Dwarf");
def->sidetext = L("s");
def->cli = "exp-time=f";
def->min = 1;
def->default_value = new ConfigOptionFloat(8.);
def = this->add("exp_time_first", coFloat);
def->label = L("Exposure time first layers");
def->category = L("Dwarf");
def->sidetext = L("s");
def->cli = "exp-time-first=f";
def->min = 1;
def->default_value = new ConfigOptionFloat(35.);
}
void PrintConfigDef::init_sla_params()

View file

@ -750,6 +750,12 @@ public:
ConfigOptionFloats wiping_volumes_matrix;
ConfigOptionFloats wiping_volumes_extruders;
ConfigOptionFloat z_offset;
ConfigOptionFloat bed_size_x;
ConfigOptionFloat bed_size_y;
ConfigOptionInt pixel_width;
ConfigOptionInt pixel_height;
ConfigOptionFloat exp_time;
ConfigOptionFloat exp_time_first;
protected:
PrintConfig(int) : GCodeConfig(1) {}
@ -821,6 +827,12 @@ protected:
OPT_PTR(wiping_volumes_matrix);
OPT_PTR(wiping_volumes_extruders);
OPT_PTR(z_offset);
OPT_PTR(bed_size_x);
OPT_PTR(bed_size_y);
OPT_PTR(pixel_width);
OPT_PTR(pixel_height);
OPT_PTR(exp_time);
OPT_PTR(exp_time_first);
}
};

View file

@ -0,0 +1,389 @@
#ifndef PRINTEXPORT_HPP
#define PRINTEXPORT_HPP
#include "Print.hpp"
// For png export of the sliced model
#include <fstream>
#include <sstream>
#include <wx/stdstream.h>
#include <wx/wfstream.h>
#include <wx/zipstrm.h>
#include <boost/log/trivial.hpp>
#include "Rasterizer/Rasterizer.hpp"
#include <tbb/parallel_for.h>
#include <tbb/spin_mutex.h>//#include "tbb/mutex.h"
namespace Slic3r {
enum class FilePrinterFormat {
PNG,
SVG
};
/*
* Interface for a file printer of the slices. Implementation can be an SVG
* or PNG printer or any other format.
*
* The format argument specifies the output format of the printer and it enables
* different implementations of this class template for each supported format.
*
*/
template<FilePrinterFormat format>
class FilePrinter {
public:
void printConfig(const Print&);
// Draw an ExPolygon which is a polygon inside a slice on the specified layer.
void drawPolygon(const ExPolygon& p, unsigned lyr);
// Tell the printer how many layers should it consider.
void layers(unsigned layernum);
// Get the number of layers in the print.
unsigned layers() const;
/* Switch to a particular layer. If there where less layers then the
* specified layer number than an appropriate number of layers will be
* allocated in the printer.
*/
void beginLayer(unsigned layer);
// Allocate a new layer on top of the last and switch to it.
void beginLayer();
/*
* Finish the selected layer. It means that no drawing is allowed on that
* layer anymore. This fact can be used to prepare the file system output
* data like png comprimation and so on.
*/
void finishLayer(unsigned layer);
// Finish the top layer.
void finishLayer();
// Save all the layers into the file (or dir) specified in the path argument
void save(const std::string& path);
// Save only the selected layer to the file specified in path argument.
void saveLayer(unsigned lyr, const std::string& path);
};
// Implementation for PNG raster output
// Be aware that if a large number of layers are allocated, it can very well
// exhaust the available memory especially on 32 bit platform.
template<> class FilePrinter<FilePrinterFormat::PNG> {
struct Layer {
Raster first;
std::stringstream second;
Layer() {}
Layer(const Layer&) = delete;
Layer(Layer&& m):
first(std::move(m.first))/*, second(std::move(m.second))*/ {}
};
// We will save the compressed PNG data into stringstreams which can be done
// in parallel. Later we can write every layer to the disk sequentially.
std::vector<Layer> layers_rst_;
Raster::Resolution res_;
Raster::PixelDim pxdim_;
const Print *print_ = nullptr;
double exp_time_s_ = .0, exp_time_first_s_ = .0;
std::string createIniContent(const std::string& projectname) {
double layer_height = print_?
print_->default_object_config.layer_height.getFloat() :
0.05;
using std::string;
using std::to_string;
auto expt_str = to_string(exp_time_s_);
auto expt_first_str = to_string(exp_time_first_s_);
auto stepnum_str = to_string(static_cast<unsigned>(800*layer_height));
auto layerh_str = to_string(layer_height);
return string(
"action = print\n"
"jobDir = ") + projectname + "\n" +
"expTime = " + expt_str + "\n"
"expTimeFirst = " + expt_first_str + "\n"
"stepNum = " + stepnum_str + "\n"
"wifiOn = 1\n"
"tiltSlow = 60\n"
"tiltFast = 15\n"
"numFade = 10\n"
"startdelay = 0\n"
"layerHeight = " + layerh_str + "\n"
"noteInfo = "
"expTime="+expt_str+"+resinType=generic+layerHeight="
+layerh_str+"+printer=DWARF3\n";
}
// Change this to TOP_LEFT if you want correct PNG orientation
static const Raster::Origin ORIGIN = Raster::Origin::BOTTOM_LEFT;
public:
inline FilePrinter(double width_mm, double height_mm,
unsigned width_px, unsigned height_px,
double exp_time, double exp_time_first):
res_(width_px, height_px),
pxdim_(width_mm/width_px, height_mm/height_px),
exp_time_s_(exp_time),
exp_time_first_s_(exp_time_first)
{
}
FilePrinter(const FilePrinter& ) = delete;
FilePrinter(FilePrinter&& m):
layers_rst_(std::move(m.layers_rst_)),
res_(m.res_),
pxdim_(m.pxdim_) {}
inline void layers(unsigned cnt) { if(cnt > 0) layers_rst_.resize(cnt); }
inline unsigned layers() const { return layers_rst_.size(); }
void printConfig(const Print& printconf) { print_ = &printconf; }
inline void drawPolygon(const ExPolygon& p, unsigned lyr) {
assert(lyr < layers_rst_.size());
layers_rst_[lyr].first.draw(p);
}
inline void beginLayer(unsigned lyr) {
if(layers_rst_.size() <= lyr) layers_rst_.resize(lyr+1);
layers_rst_[lyr].first.reset(res_, pxdim_, ORIGIN);
}
inline void beginLayer() {
layers_rst_.emplace_back();
layers_rst_.front().first.reset(res_, pxdim_, ORIGIN);
}
inline void finishLayer(unsigned lyr_id) {
assert(lyr_id < layers_rst_.size());
layers_rst_[lyr_id].first.save(layers_rst_[lyr_id].second,
Raster::Compression::PNG);
layers_rst_[lyr_id].first.reset();
}
inline void finishLayer() {
if(!layers_rst_.empty()) {
layers_rst_.back().first.save(layers_rst_.back().second,
Raster::Compression::PNG);
layers_rst_.back().first.reset();
}
}
inline void save(const std::string& path) {
wxFileName filepath(path);
wxFFileOutputStream zipfile(path);
std::string project = filepath.GetName().ToStdString();
if(!zipfile.IsOk()) {
BOOST_LOG_TRIVIAL(error) << "Can't create zip file for layers! "
<< path;
return;
}
wxZipOutputStream zipstream(zipfile);
wxStdOutputStream pngstream(zipstream);
zipstream.PutNextEntry("config.ini");
pngstream << createIniContent(project);
for(unsigned i = 0; i < layers_rst_.size(); i++) {
if(layers_rst_[i].second.rdbuf()->in_avail() > 0) {
char lyrnum[6];
std::sprintf(lyrnum, "%.5d", i);
auto zfilename = project + lyrnum + ".png";
zipstream.PutNextEntry(zfilename);
pngstream << layers_rst_[i].second.rdbuf();
layers_rst_[i].second.str("");
}
}
zipstream.Close();
zipfile.Close();
}
void saveLayer(unsigned lyr, const std::string& path) {
unsigned i = lyr;
assert(i < layers_rst_.size());
char lyrnum[6];
std::sprintf(lyrnum, "%.5d", lyr);
std::string loc = path + "layer" + lyrnum + ".png";
std::fstream out(loc, std::fstream::out | std::fstream::binary);
if(out.good()) {
layers_rst_[i].first.save(out, Raster::Compression::PNG);
} else {
BOOST_LOG_TRIVIAL(error) << "Can't create file for layer";
}
out.close();
layers_rst_[i].first.reset();
}
};
// Let's shadow this eigen interface
inline coord_t px(const Point& p) { return p(0); }
inline coord_t py(const Point& p) { return p(1); }
inline coordf_t px(const Vec2d& p) { return p(0); }
inline coordf_t py(const Vec2d& p) { return p(1); }
template<FilePrinterFormat format, class...Args>
void print_to(Print& print,
std::string dirpath,
double width_mm,
double height_mm,
Args&&...args)
{
std::string& dir = dirpath;
// This map will hold the layers sorted by z coordinate. Layers on the
// same height (from different objects) will be mapped to the same key and
// rasterized to the same image.
std::map<long long, LayerPtrs> layers;
auto& objects = print.objects;
// Merge the sliced layers with the support layers
std::for_each(objects.begin(), objects.end(), [&layers](PrintObject *o) {
for(auto l : o->layers) {
auto& lyrs = layers[static_cast<long long>(scale_(l->print_z))];
lyrs.push_back(l);
}
for(auto l : o->support_layers) {
auto& lyrs = layers[static_cast<long long>(scale_(l->print_z))];
lyrs.push_back(l);
}
});
auto print_bb = print.bounding_box();
Vec2d punsc = unscale(print_bb.size());
// If the print does not fit into the print area we should cry about it.
if(px(punsc) > width_mm || py(punsc) > height_mm) {
BOOST_LOG_TRIVIAL(warning) << "Warning: Print will not fit!" << "\n"
<< "Width needed: " << px(punsc) << "\n"
<< "Height needed: " << py(punsc) << "\n";
}
// Offset for centering the print onto the print area
auto cx = scale_(width_mm)/2 - (px(print_bb.center()) - px(print_bb.min));
auto cy = scale_(height_mm)/2 - (py(print_bb.center()) - py(print_bb.min));
// Create the actual printer, forward any additional arguments to it.
FilePrinter<format> printer(width_mm, height_mm,
std::forward<Args>(args)...);
printer.printConfig(print);
printer.layers(layers.size()); // Allocate space for all the layers
int st_prev = 0;
const std::string jobdesc = "Rasterizing and compressing sliced layers";
tbb::spin_mutex m;
std::vector<long long> keys;
keys.reserve(layers.size());
for(auto& e : layers) keys.push_back(e.first);
int initstatus = print.progressindicator? print.progressindicator->state()
: 0;
print.set_status(initstatus, jobdesc);
// Method that prints one layer
auto process_layer = [&layers, &keys, &printer, &st_prev, &m,
&jobdesc, print_bb, dir, cx, cy, &print, initstatus]
(unsigned layer_id)
{
LayerPtrs lrange = layers[keys[layer_id]];
printer.beginLayer(layer_id); // Switch to the appropriate layer
for(Layer *lp : lrange) {
Layer& l = *lp;
ExPolygonCollection slices = l.slices; // Copy the layer slices
// Sort the polygons in the layer
std::stable_sort(slices.expolygons.begin(), slices.expolygons.end(),
[](const ExPolygon& a, const ExPolygon& b) {
return a.contour.contains(b.contour.first_point()) ? false :
true;
});
// Draw all the polygons in the slice to the actual layer.
std::for_each(l.object()->_shifted_copies.begin(),
l.object()->_shifted_copies.end(),
[&] (Point d)
{
std::for_each(slices.expolygons.begin(),
slices.expolygons.end(),
[&] (ExPolygon slice)
{
slice.translate(px(d), py(d));
slice.translate(-px(print_bb.min) + cx,
-py(print_bb.min) + cy);
printer.drawPolygon(slice, layer_id);
});
});
/*if(print.has_support_material() && layer_id > 0) {
BOOST_LOG_TRIVIAL(warning) << "support material for layer "
<< layer_id
<< " defined but export is "
"not yet implemented.";
}*/
}
printer.finishLayer(layer_id); // Finish the layer for later saving it.
auto st = static_cast<int>(layer_id*80.0/layers.size());
m.lock();
if( st - st_prev > 10) {
print.set_status(initstatus + st, jobdesc);
st_prev = st;
}
m.unlock();
// printer.saveLayer(layer_id, dir); We could save the layer immediately
};
// Print all the layers in parallel
tbb::parallel_for<size_t, decltype(process_layer)>(0,
layers.size(),
process_layer);
// Sequential version (for testing)
// for(unsigned l = 0; l < layers.size(); ++l) process_layer(l);
// print.set_status(100, jobdesc);
// Save the print into the file system.
print.set_status(initstatus + 90, "Writing layers to disk");
printer.save(dir);
print.set_status(initstatus + 100, "Writing layers completed");
}
}
#endif // PRINTEXPORT_HPP

View file

@ -0,0 +1,214 @@
#include "Rasterizer.hpp"
#include <ExPolygon.hpp>
#include <cstdint>
// For rasterizing
#include <agg/agg_basics.h>
#include <agg/agg_rendering_buffer.h>
#include <agg/agg_pixfmt_gray.h>
#include <agg/agg_pixfmt_rgb.h>
#include <agg/agg_renderer_base.h>
#include <agg/agg_renderer_scanline.h>
#include <agg/agg_scanline_p.h>
#include <agg/agg_rasterizer_scanline_aa.h>
#include <agg/agg_path_storage.h>
// For png compression
#include <png/writer.hpp>
namespace Slic3r {
class Raster::Impl {
public:
using TPixelRenderer = agg::pixfmt_gray8; // agg::pixfmt_rgb24;
using TRawRenderer = agg::renderer_base<TPixelRenderer>;
using TPixel = TPixelRenderer::color_type;
using TRawBuffer = agg::rendering_buffer;
using TBuffer = std::vector<TPixelRenderer::pixel_type>;
using TRendererAA = agg::renderer_scanline_aa_solid<TRawRenderer>;
static const TPixel ColorWhite;
static const TPixel ColorBlack;
using Origin = Raster::Origin;
private:
Raster::Resolution resolution_;
Raster::PixelDim pxdim_;
TBuffer buf_;
TRawBuffer rbuf_;
TPixelRenderer pixfmt_;
TRawRenderer raw_renderer_;
TRendererAA renderer_;
Origin o_;
std::function<void(agg::path_storage&)> flipy_ = [](agg::path_storage&) {};
public:
inline Impl(const Raster::Resolution& res, const Raster::PixelDim &pd,
Origin o):
resolution_(res), pxdim_(pd),
buf_(res.pixels()),
rbuf_(reinterpret_cast<TPixelRenderer::value_type*>(buf_.data()),
res.width_px, res.height_px,
res.width_px*TPixelRenderer::num_components),
pixfmt_(rbuf_),
raw_renderer_(pixfmt_),
renderer_(raw_renderer_),
o_(o)
{
renderer_.color(ColorWhite);
// If we would like to play around with gamma
// ras.gamma(agg::gamma_power(1.0));
clear();
if(o_ == Origin::TOP_LEFT) flipy_ = [this](agg::path_storage& path) {
path.flip_y(0, resolution_.height_px);
};
}
void draw(const ExPolygon &poly) {
agg::rasterizer_scanline_aa<> ras;
agg::scanline_p8 scanlines;
auto&& path = to_path(poly.contour);
flipy_(path);
ras.add_path(path);
for(auto h : poly.holes) {
auto&& holepath = to_path(h);
flipy_(holepath);
ras.add_path(holepath);
}
agg::render_scanlines(ras, scanlines, renderer_);
}
inline void clear() {
raw_renderer_.clear(ColorBlack);
}
inline TBuffer& buffer() { return buf_; }
inline const Raster::Resolution resolution() { return resolution_; }
inline Origin origin() const /*noexcept*/ { return o_; }
private:
double getPx(const Point& p) {
return p(0) * SCALING_FACTOR/pxdim_.w_mm;
}
double getPy(const Point& p) {
return p(1) * SCALING_FACTOR/pxdim_.h_mm;
}
agg::path_storage to_path(const Polygon& poly) {
agg::path_storage path;
auto it = poly.points.begin();
path.move_to(getPx(*it), getPy(*it));
while(++it != poly.points.end())
path.line_to(getPx(*it), getPy(*it));
path.line_to(getPx(poly.points.front()), getPy(poly.points.front()));
return path;
}
};
const Raster::Impl::TPixel Raster::Impl::ColorWhite = Raster::Impl::TPixel(255);
const Raster::Impl::TPixel Raster::Impl::ColorBlack = Raster::Impl::TPixel(0);
Raster::Raster(const Resolution &r, const PixelDim &pd, Origin o):
impl_(new Impl(r, pd, o)) {}
Raster::Raster() {}
Raster::~Raster() {}
Raster::Raster(Raster &&m):
impl_(std::move(m.impl_)) {}
void Raster::reset(const Raster::Resolution &r, const Raster::PixelDim &pd)
{
// Free up the unnecessary memory and make sure it stays clear after
// an exception
auto o = impl_? impl_->origin() : Origin::TOP_LEFT;
reset(r, pd, o);
}
void Raster::reset(const Raster::Resolution &r, const Raster::PixelDim &pd,
Raster::Origin o)
{
impl_.reset();
impl_.reset(new Impl(r, pd, o));
}
void Raster::reset()
{
impl_.reset();
}
Raster::Resolution Raster::resolution() const
{
if(impl_) return impl_->resolution();
return Resolution(0, 0);
}
void Raster::clear()
{
assert(impl_);
impl_->clear();
}
void Raster::draw(const ExPolygon &poly)
{
assert(impl_);
impl_->draw(poly);
}
void Raster::save(std::ostream& stream, Compression comp)
{
assert(impl_);
switch(comp) {
case Compression::PNG: {
png::writer<std::ostream> wr(stream);
wr.set_bit_depth(8);
wr.set_color_type(png::color_type_gray);
wr.set_width(resolution().width_px);
wr.set_height(resolution().height_px);
wr.set_compression_type(png::compression_type_default);
wr.write_info();
auto& b = impl_->buffer();
auto ptr = reinterpret_cast<png::byte*>( b.data() );
unsigned stride =
sizeof(Impl::TBuffer::value_type) * resolution().width_px;
for(unsigned r = 0; r < resolution().height_px; r++, ptr+=stride) {
wr.write_row(ptr);
}
break;
}
case Compression::RAW: {
stream << "P5 "
<< impl_->resolution().width_px << " "
<< impl_->resolution().height_px << " "
<< "255 ";
stream.write(reinterpret_cast<const char*>(impl_->buffer().data()),
impl_->buffer().size()*sizeof(Impl::TBuffer::value_type));
}
}
}
}

View file

@ -0,0 +1,86 @@
#ifndef RASTERIZER_HPP
#define RASTERIZER_HPP
#include <ostream>
#include <memory>
namespace Slic3r {
class ExPolygon;
/**
* @brief Raster captures an anti-aliased monochrome canvas where vectorial
* polygons can be rasterized. Fill color is always white and the background is
* black. Contours are anti-aliased.
*
* It also supports saving the raster data into a standard output stream in raw
* or PNG format.
*/
class Raster {
class Impl;
std::unique_ptr<Impl> impl_;
public:
/// Supported compression types
enum class Compression {
RAW, //!> Uncompressed pixel data
PNG //!> PNG compression
};
enum class Origin {
TOP_LEFT,
BOTTOM_LEFT
};
/// Type that represents a resolution in pixels.
struct Resolution {
unsigned width_px;
unsigned height_px;
inline Resolution(unsigned w, unsigned h): width_px(w), height_px(h) {}
inline unsigned pixels() const /*noexcept*/ {
return width_px * height_px;
}
};
/// Types that represents the dimension of a pixel in millimeters.
struct PixelDim {
double w_mm;
double h_mm;
inline PixelDim(double px_width_mm, double px_height_mm ):
w_mm(px_width_mm), h_mm(px_height_mm) {}
};
/// Constructor taking the resolution and the pixel dimension.
explicit Raster(const Resolution& r, const PixelDim& pd,
Origin o = Origin::BOTTOM_LEFT );
Raster();
Raster(const Raster& cpy) = delete;
Raster& operator=(const Raster& cpy) = delete;
Raster(Raster&& m);
~Raster();
/// Reallocated everything for the given resolution and pixel dimension.
void reset(const Resolution& r, const PixelDim& pd);
void reset(const Resolution& r, const PixelDim& pd, Origin o);
/**
* Release the allocated resources. Drawing in this state ends in
* unspecified behaviour.
*/
void reset();
/// Get the resolution of the raster.
Resolution resolution() const;
/// Clear the raster with black color.
void clear();
/// Draw a polygon with holes.
void draw(const ExPolygon& poly);
/// Save the raster on the specified stream.
void save(std::ostream& stream, Compression comp = Compression::RAW);
};
}
#endif // RASTERIZER_HPP

View file

@ -0,0 +1,526 @@
#include <functional>
#include <numeric>
#include "SLABasePool.hpp"
#include "ExPolygon.hpp"
#include "TriangleMesh.hpp"
#include "ClipperUtils.hpp"
#include "boost/log/trivial.hpp"
//#include "SVG.hpp"
namespace Slic3r { namespace sla {
namespace {
using coord_t = Point::coord_type;
/// get the scaled clipper units for a millimeter value
inline coord_t mm(double v) { return coord_t(v/SCALING_FACTOR); }
/// Get x and y coordinates (because we are eigenizing...)
inline coord_t x(const Point& p) { return p(0); }
inline coord_t y(const Point& p) { return p(1); }
inline coord_t& x(Point& p) { return p(0); }
inline coord_t& y(Point& p) { return p(1); }
inline coordf_t x(const Vec3d& p) { return p(0); }
inline coordf_t y(const Vec3d& p) { return p(1); }
inline coordf_t z(const Vec3d& p) { return p(2); }
inline coordf_t& x(Vec3d& p) { return p(0); }
inline coordf_t& y(Vec3d& p) { return p(1); }
inline coordf_t& z(Vec3d& p) { return p(2); }
inline coord_t& x(Vec3crd& p) { return p(0); }
inline coord_t& y(Vec3crd& p) { return p(1); }
inline coord_t& z(Vec3crd& p) { return p(2); }
inline coord_t x(const Vec3crd& p) { return p(0); }
inline coord_t y(const Vec3crd& p) { return p(1); }
inline coord_t z(const Vec3crd& p) { return p(2); }
inline void triangulate(const ExPolygon& expoly, Polygons& triangles) {
expoly.triangulate_p2t(&triangles);
}
inline Polygons triangulate(const ExPolygon& expoly) {
Polygons tri; triangulate(expoly, tri); return tri;
}
using Indices = std::vector<Vec3crd>;
/// Intermediate struct for a 3D mesh
struct Contour3D {
Pointf3s points;
Indices indices;
void merge(const Contour3D& ctr) {
auto s3 = coord_t(points.size());
auto s = coord_t(indices.size());
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++) {
auto& idx = indices[n]; x(idx) += s3; y(idx) += s3; z(idx) += s3;
}
}
};
/// Convert the triangulation output to an intermediate mesh.
inline Contour3D convert(const Polygons& triangles, coord_t z, bool dir) {
Pointf3s points;
points.reserve(3*triangles.size());
Indices indices;
indices.reserve(points.size());
for(auto& tr : triangles) {
auto c = coord_t(points.size()), b = c++, a = c++;
if(dir) indices.emplace_back(a, b, c);
else indices.emplace_back(c, b, a);
for(auto& p : tr.points) {
points.emplace_back(unscale(x(p), y(p), z));
}
}
return {points, indices};
}
/// Only a debug function to generate top and bottom plates from a 2D shape.
/// It is not used in the algorithm directly.
inline Contour3D roofs(const ExPolygon& poly, coord_t z_distance) {
Polygons triangles = triangulate(poly);
auto lower = convert(triangles, 0, false);
auto upper = convert(triangles, z_distance, true);
lower.merge(upper);
return lower;
}
inline Contour3D walls(const ExPolygon& floor_plate, const ExPolygon& ceiling,
double floor_z_mm, double ceiling_z_mm) {
using std::transform; using std::back_inserter;
ExPolygon poly;
poly.contour.points = floor_plate.contour.points;
poly.holes.emplace_back(ceiling.contour);
auto& h = poly.holes.front();
std::reverse(h.points.begin(), h.points.end());
Polygons tri = triangulate(poly);
Contour3D ret;
ret.points.reserve(tri.size() * 3);
double fz = floor_z_mm;
double cz = ceiling_z_mm;
auto& rp = ret.points;
auto& rpi = ret.indices;
ret.indices.reserve(tri.size() * 3);
coord_t idx = 0;
auto hlines = h.lines();
auto is_upper = [&hlines](const Point& p) {
return std::any_of(hlines.begin(), hlines.end(),
[&p](const Line& l) {
return l.distance_to(p) < mm(0.01);
});
};
std::for_each(tri.begin(), tri.end(),
[&rp, &rpi, &poly, &idx, is_upper, fz, cz](const Polygon& pp)
{
for(auto& p : pp.points)
if(is_upper(p))
rp.emplace_back(unscale(x(p), y(p), mm(cz)));
else rp.emplace_back(unscale(x(p), y(p), mm(fz)));
coord_t a = idx++, b = idx++, c = idx++;
if(fz > cz) rpi.emplace_back(c, b, a);
else rpi.emplace_back(a, b, c);
});
return ret;
}
/// Mesh from an existing contour.
inline TriangleMesh mesh(const Contour3D& ctour) {
return {ctour.points, ctour.indices};
}
/// Mesh from an evaporating 3D contour
inline TriangleMesh mesh(Contour3D&& ctour) {
return {std::move(ctour.points), std::move(ctour.indices)};
}
/// Offsetting with clipper and smoothing the edges into a curvature.
inline void offset(ExPolygon& sh, coord_t distance) {
using ClipperLib::ClipperOffset;
using ClipperLib::jtRound;
using ClipperLib::etClosedPolygon;
using ClipperLib::Paths;
using ClipperLib::Path;
auto&& ctour = Slic3rMultiPoint_to_ClipperPath(sh.contour);
auto&& holes = Slic3rMultiPoints_to_ClipperPaths(sh.holes);
// If the input is not at least a triangle, we can not do this algorithm
if(ctour.size() < 3 ||
std::any_of(holes.begin(), holes.end(),
[](const Path& p) { return p.size() < 3; })
) {
BOOST_LOG_TRIVIAL(error) << "Invalid geometry for offsetting!";
return;
}
ClipperOffset offs;
offs.ArcTolerance = 0.01*mm(1);
Paths result;
offs.AddPath(ctour, jtRound, etClosedPolygon);
offs.AddPaths(holes, jtRound, etClosedPolygon);
offs.Execute(result, static_cast<double>(distance));
// Offsetting reverts the orientation and also removes the last vertex
// so boost will not have a closed polygon.
bool found_the_contour = false;
sh.holes.clear();
for(auto& r : result) {
if(ClipperLib::Orientation(r)) {
// We don't like if the offsetting generates more than one contour
// but throwing would be an overkill. Instead, we should warn the
// caller about the inability to create correct geometries
if(!found_the_contour) {
auto rr = ClipperPath_to_Slic3rPolygon(r);
sh.contour.points.swap(rr.points);
found_the_contour = true;
} else {
BOOST_LOG_TRIVIAL(warning)
<< "Warning: offsetting result is invalid!";
}
} else {
// TODO If there are multiple contours we can't be sure which hole
// belongs to the first contour. (But in this case the situation is
// bad enough to let it go...)
sh.holes.emplace_back(ClipperPath_to_Slic3rPolygon(r));
}
}
}
template<class ExP, class D>
inline Contour3D round_edges(const ExPolygon& base_plate,
double radius_mm,
double degrees,
double ceilheight_mm,
bool dir,
ExP&& last_offset = ExP(), D&& last_height = D())
{
auto ob = base_plate;
auto ob_prev = ob;
double wh = ceilheight_mm, wh_prev = wh;
Contour3D curvedwalls;
const size_t steps = 6; // steps for 180 degrees
degrees = std::fmod(degrees, 180);
const int portion = int(steps*degrees / 90);
const double ystep_mm = radius_mm/steps;
coord_t s = dir? 1 : -1;
double xxprev = 0;
for(int i = 0; i < portion; i++) {
ob = base_plate;
// The offset is given by the equation: x = sqrt(r^2 - y^2)
// which can be derived from the circle equation. y is the current
// height for which the offset is calculated and x is the offset itself
// r is the radius of the circle that is used to smooth the edges
double r2 = radius_mm * radius_mm;
double y2 = steps*ystep_mm - i*ystep_mm;
y2 *= y2;
double xx = sqrt(r2 - y2);
offset(ob, s*mm(xx));
wh = ceilheight_mm - i*ystep_mm;
Contour3D pwalls;
if(xxprev < xx) pwalls = walls(ob, ob_prev, wh, wh_prev);
else pwalls = walls(ob_prev, ob, wh_prev, wh);
curvedwalls.merge(pwalls);
ob_prev = ob;
wh_prev = wh;
xxprev = xx;
}
last_offset = std::move(ob);
last_height = wh;
return curvedwalls;
}
/// Generating the concave part of the 3D pool with the bottom plate and the
/// side walls.
inline Contour3D inner_bed(const ExPolygon& poly, double depth_mm,
double begin_h_mm = 0) {
Polygons triangles = triangulate(poly);
coord_t depth = mm(depth_mm);
coord_t begin_h = mm(begin_h_mm);
auto bottom = convert(triangles, -depth + begin_h, false);
auto lines = poly.lines();
// Generate outer walls
auto fp = [](const Point& p, Point::coord_type z) {
return unscale(x(p), y(p), z);
};
for(auto& l : lines) {
auto s = coord_t(bottom.points.size());
bottom.points.emplace_back(fp(l.a, -depth + begin_h));
bottom.points.emplace_back(fp(l.b, -depth + begin_h));
bottom.points.emplace_back(fp(l.a, begin_h));
bottom.points.emplace_back(fp(l.b, begin_h));
bottom.indices.emplace_back(s + 3, s + 1, s);
bottom.indices.emplace_back(s + 2, s + 3, s);
}
return bottom;
}
/// Unification of polygons (with clipper) preserving holes as well.
inline ExPolygons unify(const ExPolygons& shapes) {
ExPolygons retv;
bool closed = true;
bool valid = true;
ClipperLib::Clipper clipper;
for(auto& path : shapes) {
auto clipperpath = Slic3rMultiPoint_to_ClipperPath(path.contour);
valid &= clipper.AddPath(clipperpath, ClipperLib::ptSubject, closed);
auto clipperholes = Slic3rMultiPoints_to_ClipperPaths(path.holes);
for(auto& hole : clipperholes) {
valid &= clipper.AddPath(hole, ClipperLib::ptSubject, closed);
}
}
if(!valid) BOOST_LOG_TRIVIAL(warning) << "Unification of invalid shapes!";
ClipperLib::PolyTree result;
clipper.Execute(ClipperLib::ctUnion, result, ClipperLib::pftNonZero);
retv.reserve(static_cast<size_t>(result.Total()));
// Now we will recursively traverse the polygon tree and serialize it
// into an ExPolygon with holes. The polygon tree has the clipper-ish
// PolyTree structure which alternates its nodes as contours and holes
// A "declaration" of function for traversing leafs which are holes
std::function<void(ClipperLib::PolyNode*, ExPolygon&)> processHole;
// Process polygon which calls processHoles which than calls processPoly
// again until no leafs are left.
auto processPoly = [&retv, &processHole](ClipperLib::PolyNode *pptr) {
ExPolygon poly;
poly.contour.points = ClipperPath_to_Slic3rPolygon(pptr->Contour);
for(auto h : pptr->Childs) { processHole(h, poly); }
retv.push_back(poly);
};
// Body of the processHole function
processHole = [&processPoly](ClipperLib::PolyNode *pptr, ExPolygon& poly)
{
poly.holes.emplace_back();
poly.holes.back().points = ClipperPath_to_Slic3rPolygon(pptr->Contour);
for(auto c : pptr->Childs) processPoly(c);
};
// Wrapper for traversing.
auto traverse = [&processPoly] (ClipperLib::PolyNode *node)
{
for(auto ch : node->Childs) {
processPoly(ch);
}
};
// Here is the actual traverse
traverse(&result);
return retv;
}
inline Point centroid(Points& pp) {
Point c;
switch(pp.size()) {
case 0: break;
case 1: c = pp.front(); break;
case 2: c = (pp[0] + pp[1]) / 2; break;
default: {
Polygon p;
p.points.swap(pp);
c = p.centroid();
pp.swap(p.points);
break;
}
}
return c;
}
inline Point centroid(const ExPolygon& poly) {
return poly.contour.centroid();
}
/// A fake concave hull that is constructed by connecting separate shapes
/// with explicit bridges. Bridges are generated from each shape's centroid
/// to the center of the "scene" which is the centroid calculated from the shape
/// centroids (a star is created...)
inline ExPolygons concave_hull(const ExPolygons& polys, double max_dist_mm = 0)
{
if(polys.empty()) return ExPolygons();
ExPolygons punion = unify(polys); // could be redundant
if(punion.size() == 1) return punion;
// We get the centroids of all the islands in the 2D slice
Points centroids; centroids.reserve(punion.size());
std::transform(punion.begin(), punion.end(), std::back_inserter(centroids),
[](const ExPolygon& poly) { return centroid(poly); });
// Centroid of the centroids of islands. This is where the additional
// connector sticks are routed.
Point cc = centroid(centroids);
punion.reserve(punion.size() + centroids.size());
std::transform(centroids.begin(), centroids.end(),
std::back_inserter(punion),
[cc, max_dist_mm](const Point& c) {
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;
if(l < max_dist_mm) return ExPolygon();
ExPolygon r;
auto& ctour = r.contour.points;
ctour.reserve(3);
ctour.emplace_back(cc);
Point d(coord_t(mm(1)*nx), coord_t(mm(1)*ny));
ctour.emplace_back(c + Point( -y(d), x(d) ));
ctour.emplace_back(c + Point( y(d), -x(d) ));
offset(r, mm(1));
return r;
});
punion = unify(punion);
if(punion.size() != 1)
BOOST_LOG_TRIVIAL(error) << "Cannot generate correct SLA base pool!";
return punion;
}
}
void ground_layer(const TriangleMesh &mesh, ExPolygons &output, float h)
{
TriangleMesh m = mesh;
TriangleMeshSlicer slicer(&m);
std::vector<ExPolygons> tmp;
slicer.slice({h}, &tmp);
output = tmp.front();
}
void create_base_pool(const ExPolygons &ground_layer, TriangleMesh& out,
double min_wall_thickness_mm,
double min_wall_height_mm)
{
auto concavehs = concave_hull(ground_layer);
for(ExPolygon& concaveh : concavehs) {
if(concaveh.contour.points.empty()) return;
concaveh.holes.clear();
BoundingBox bb(concaveh);
coord_t w = x(bb.max) - x(bb.min);
coord_t h = y(bb.max) - y(bb.min);
auto wall_thickness = coord_t(std::pow((w+h)*0.1, 0.8));
const coord_t WALL_THICKNESS = mm(min_wall_thickness_mm) +
wall_thickness;
const coord_t WALL_DISTANCE = coord_t(0.3*WALL_THICKNESS);
const coord_t HEIGHT = mm(min_wall_height_mm);
auto outer_base = concaveh;
offset(outer_base, WALL_THICKNESS+WALL_DISTANCE);
auto inner_base = outer_base;
offset(inner_base, -WALL_THICKNESS);
inner_base.holes.clear(); outer_base.holes.clear();
ExPolygon top_poly;
top_poly.contour = outer_base.contour;
top_poly.holes.emplace_back(inner_base.contour);
auto& tph = top_poly.holes.back().points;
std::reverse(tph.begin(), tph.end());
Contour3D pool;
ExPolygon ob = outer_base; double wh = 0;
auto curvedwalls = round_edges(ob,
1, // radius 1 mm
170, // 170 degrees
0, // z position of the input plane
true,
ob, wh);
pool.merge(curvedwalls);
ExPolygon ob_contr = ob;
ob_contr.holes.clear();
auto pwalls = walls(ob_contr, inner_base, wh, -min_wall_height_mm);
pool.merge(pwalls);
Polygons top_triangles, bottom_triangles;
triangulate(top_poly, top_triangles);
triangulate(inner_base, bottom_triangles);
auto top_plate = convert(top_triangles, 0, false);
auto bottom_plate = convert(bottom_triangles, -HEIGHT, true);
ob = inner_base; wh = 0;
curvedwalls = round_edges(ob,
1, // radius 1 mm
90, // 170 degrees
0, // z position of the input plane
false,
ob, wh);
pool.merge(curvedwalls);
auto innerbed = inner_bed(ob, min_wall_height_mm/2 + wh, wh);
pool.merge(top_plate);
pool.merge(bottom_plate);
pool.merge(innerbed);
out.merge(mesh(pool));
}
}
}
}

View file

@ -0,0 +1,31 @@
#ifndef SLASUPPORTPOOL_HPP
#define SLASUPPORTPOOL_HPP
#include <vector>
namespace Slic3r {
class ExPolygon;
class TriangleMesh;
namespace sla {
using ExPolygons = std::vector<ExPolygon>;
/// Calculate the polygon representing the slice of the lowest layer of mesh
void ground_layer(const TriangleMesh& mesh,
ExPolygons& output,
float height = 0.1f);
/// Calculate the pool for the mesh for SLA printing
void create_base_pool(const ExPolygons& ground_layer,
TriangleMesh& output_mesh,
double min_wall_thickness_mm = 4,
double min_wall_height_mm = 5
);
}
}
#endif // SLASUPPORTPOOL_HPP

View file

@ -858,7 +858,7 @@ TriangleMeshSlicer::slice(const std::vector<float> &z, std::vector<ExPolygons>*
#ifdef SLIC3R_TRIANGLEMESH_DEBUG
printf("Layer " PRINTF_ZU " (slice_z = %.2f):\n", layer_id, z[layer_id]);
#endif
this->make_expolygons(layers_p[layer_id], &(*layers)[layer_id]);
this->make_expolygons(layers_p[layer_id], &(*layers)[layer_id]);
}
});
BOOST_LOG_TRIVIAL(debug) << "TriangleMeshSlicer::make_expolygons in parallel - end";
@ -1205,8 +1205,8 @@ void TriangleMeshSlicer::make_loops(std::vector<IntersectionLine> &lines, Polygo
if ((ip1.edge_id != -1 && ip1.edge_id == ip2.edge_id) ||
(ip1.point_id != -1 && ip1.point_id == ip2.point_id)) {
// The current loop is complete. Add it to the output.
assert(opl.points.front().point_id == opl.points.back().point_id);
assert(opl.points.front().edge_id == opl.points.back().edge_id);
/*assert(opl.points.front().point_id == opl.points.back().point_id);
assert(opl.points.front().edge_id == opl.points.back().edge_id);*/
// Remove the duplicate last point.
opl.points.pop_back();
if (opl.points.size() >= 3) {