Merge branch 'printcpp' of github.com:sapir/Slic3r into sapir-printcpp

Conflicts:
	lib/Slic3r/GCode.pm
	lib/Slic3r/Print.pm
	lib/Slic3r/Print/Object.pm
	lib/Slic3r/Print/Region.pm
This commit is contained in:
Alessandro Ranellucci 2014-06-10 14:46:40 +02:00
commit ba8148f4ad
26 changed files with 1293 additions and 266 deletions

132
xs/src/Layer.cpp Normal file
View file

@ -0,0 +1,132 @@
#include "Layer.hpp"
namespace Slic3r {
LayerRegion::LayerRegion(Layer *layer, PrintRegion *region)
: _layer(layer),
_region(region)
{
}
LayerRegion::~LayerRegion()
{
}
Layer*
LayerRegion::layer()
{
return this->_layer;
}
PrintRegion*
LayerRegion::region()
{
return this->_region;
}
#ifdef SLIC3RXS
REGISTER_CLASS(LayerRegion, "Layer::Region");
#endif
Layer::Layer(int id, PrintObject *object, coordf_t height, coordf_t print_z,
coordf_t slice_z)
: _id(id),
_object(object),
upper_layer(NULL),
lower_layer(NULL),
regions(),
slicing_errors(false),
slice_z(slice_z),
print_z(print_z),
height(height),
slices()
{
}
Layer::~Layer()
{
// remove references to self
if (NULL != this->upper_layer) {
this->upper_layer->lower_layer = NULL;
}
if (NULL != this->lower_layer) {
this->lower_layer->upper_layer = NULL;
}
this->clear_regions();
}
int
Layer::id()
{
return this->_id;
}
PrintObject*
Layer::object()
{
return this->_object;
}
size_t
Layer::region_count()
{
return this->regions.size();
}
void
Layer::clear_regions()
{
for (int i = this->regions.size()-1; i >= 0; --i)
this->delete_region(i);
}
LayerRegion*
Layer::get_region(int idx)
{
return this->regions.at(idx);
}
LayerRegion*
Layer::add_region(PrintRegion* print_region)
{
LayerRegion* region = new LayerRegion(this, print_region);
this->regions.push_back(region);
return region;
}
void
Layer::delete_region(int idx)
{
LayerRegionPtrs::iterator i = this->regions.begin() + idx;
LayerRegion* item = *i;
this->regions.erase(i);
delete item;
}
#ifdef SLIC3RXS
REGISTER_CLASS(Layer, "Layer");
#endif
SupportLayer::SupportLayer(int id, PrintObject *object, coordf_t height,
coordf_t print_z, coordf_t slice_z)
: Layer(id, object, height, print_z, slice_z)
{
}
SupportLayer::~SupportLayer()
{
}
#ifdef SLIC3RXS
REGISTER_CLASS(SupportLayer, "Layer::Support");
#endif
}

View file

@ -2,12 +2,118 @@
#define slic3r_Layer_hpp_
#include <myinit.h>
#include "SurfaceCollection.hpp"
#include "ExtrusionEntityCollection.hpp"
#include "ExPolygonCollection.hpp"
#include "PolylineCollection.hpp"
namespace Slic3r {
typedef std::pair<coordf_t,coordf_t> t_layer_height_range;
typedef std::map<t_layer_height_range,coordf_t> t_layer_height_ranges;
class Layer;
class PrintRegion;
class PrintObject;
// TODO: make stuff private
class LayerRegion
{
friend class Layer;
public:
Layer* layer();
PrintRegion* region();
// collection of surfaces generated by slicing the original geometry
// divided by type top/bottom/internal
SurfaceCollection slices;
// collection of extrusion paths/loops filling gaps
ExtrusionEntityCollection thin_fills;
// collection of surfaces for infill generation
SurfaceCollection fill_surfaces;
// collection of expolygons representing the bridged areas (thus not
// needing support material)
ExPolygonCollection bridged;
// collection of polylines representing the unsupported bridge edges
PolylineCollection unsupported_bridge_edges;
// ordered collection of extrusion paths/loops to build all perimeters
ExtrusionEntityCollection perimeters;
// ordered collection of extrusion paths to fill surfaces
ExtrusionEntityCollection fills;
private:
Layer *_layer;
PrintRegion *_region;
LayerRegion(Layer *layer, PrintRegion *region);
~LayerRegion();
};
typedef std::vector<LayerRegion*> LayerRegionPtrs;
class Layer {
friend class PrintObject;
public:
int id();
PrintObject* object();
Layer *upper_layer;
Layer *lower_layer;
LayerRegionPtrs regions;
bool slicing_errors;
coordf_t slice_z; // Z used for slicing in unscaled coordinates
coordf_t print_z; // Z used for printing in unscaled coordinates
coordf_t height; // layer height in unscaled coordinates
// collection of expolygons generated by slicing the original geometry;
// also known as 'islands' (all regions and surface types are merged here)
ExPolygonCollection slices;
size_t region_count();
LayerRegion* get_region(int idx);
LayerRegion* add_region(PrintRegion* print_region);
protected:
int _id; // sequential number of layer, 0-based
PrintObject *_object;
Layer(int id, PrintObject *object, coordf_t height, coordf_t print_z,
coordf_t slice_z);
virtual ~Layer();
void clear_regions();
void delete_region(int idx);
};
class SupportLayer : public Layer {
friend class PrintObject;
public:
ExPolygonCollection support_islands;
ExtrusionEntityCollection support_fills;
ExtrusionEntityCollection support_interface_fills;
protected:
SupportLayer(int id, PrintObject *object, coordf_t height, coordf_t print_z,
coordf_t slice_z);
virtual ~SupportLayer();
};
}
#endif

View file

@ -0,0 +1,25 @@
#include "PlaceholderParser.hpp"
#ifdef SLIC3RXS
#include "perlglue.hpp"
#endif
namespace Slic3r {
PlaceholderParser::PlaceholderParser()
{
// TODO: port these methods to C++, then call them here
// this->apply_env_variables();
// this->update_timestamp();
}
PlaceholderParser::~PlaceholderParser()
{
}
#ifdef SLIC3RXS
REGISTER_CLASS(PlaceholderParser, "GCode::PlaceholderParser");
#endif
}

View file

@ -0,0 +1,24 @@
#ifndef slic3r_PlaceholderParser_hpp_
#define slic3r_PlaceholderParser_hpp_
#include <myinit.h>
#include <map>
#include <string>
namespace Slic3r {
class PlaceholderParser
{
public:
std::map<std::string, std::string> _single;
std::map<std::string, std::string> _multiple;
PlaceholderParser();
~PlaceholderParser();
};
}
#endif

View file

@ -243,6 +243,9 @@ Point::from_SV_check(SV* point_sv)
}
}
REGISTER_CLASS(Point3, "Point3");
#endif
void

View file

@ -1,4 +1,5 @@
#include "Print.hpp"
#include "BoundingBox.hpp"
namespace Slic3r {
@ -44,4 +45,275 @@ PrintState::invalidate_all()
REGISTER_CLASS(PrintState, "Print::State");
#endif
PrintRegion::PrintRegion(Print* print)
: config(), _print(print)
{
}
PrintRegion::~PrintRegion()
{
}
Print*
PrintRegion::print()
{
return this->_print;
}
PrintConfig &
PrintRegion::print_config()
{
return this->_print->config;
}
#ifdef SLIC3RXS
REGISTER_CLASS(PrintRegion, "Print::Region");
#endif
PrintObject::PrintObject(Print* print, int id, ModelObject* model_object,
const BoundingBoxf3 &modobj_bbox)
: _print(print),
_id(id),
_model_object(model_object)
{
region_volumes.resize(this->_print->regions.size());
// Compute the translation to be applied to our meshes so that we work with smaller coordinates
{
// Translate meshes so that our toolpath generation algorithms work with smaller
// XY coordinates; this translation is an optimization and not strictly required.
// A cloned mesh will be aligned to 0 before slicing in _slice_region() since we
// don't assume it's already aligned and we don't alter the original position in model.
// We store the XY translation so that we can place copies correctly in the output G-code
// (copies are expressed in G-code coordinates and this translation is not publicly exposed).
this->_copies_shift = Point(
scale_(modobj_bbox.min.x), scale_(modobj_bbox.min.y));
// TODO: $self->_trigger_copies;
// Scale the object size and store it
Pointf3 size = modobj_bbox.size();
this->size = Point3(scale_(size.x), scale_(size.y), scale_(size.z));
}
}
PrintObject::~PrintObject()
{
}
Print*
PrintObject::print()
{
return this->_print;
}
int
PrintObject::id()
{
return this->_id;
}
ModelObject*
PrintObject::model_object()
{
return this->_model_object;
}
void
PrintObject::add_region_volume(int region_id, int volume_id)
{
if (region_id >= region_volumes.size()) {
region_volumes.resize(region_id + 1);
}
region_volumes[region_id].push_back(volume_id);
}
size_t
PrintObject::layer_count()
{
return this->layers.size();
}
void
PrintObject::clear_layers()
{
for (int i = this->layers.size()-1; i >= 0; --i)
this->delete_layer(i);
}
Layer*
PrintObject::get_layer(int idx)
{
return this->layers.at(idx);
}
Layer*
PrintObject::add_layer(int id, coordf_t height, coordf_t print_z,
coordf_t slice_z)
{
Layer* layer = new Layer(id, this, height, print_z, slice_z);
layers.push_back(layer);
return layer;
}
void
PrintObject::delete_layer(int idx)
{
LayerPtrs::iterator i = this->layers.begin() + idx;
Layer* item = *i;
this->layers.erase(i);
delete item;
}
size_t
PrintObject::support_layer_count()
{
return this->support_layers.size();
}
void
PrintObject::clear_support_layers()
{
for (int i = this->support_layers.size()-1; i >= 0; --i)
this->delete_support_layer(i);
}
SupportLayer*
PrintObject::get_support_layer(int idx)
{
return this->support_layers.at(idx);
}
SupportLayer*
PrintObject::add_support_layer(int id, coordf_t height, coordf_t print_z,
coordf_t slice_z)
{
SupportLayer* layer = new SupportLayer(id, this, height, print_z, slice_z);
support_layers.push_back(layer);
return layer;
}
void
PrintObject::delete_support_layer(int idx)
{
SupportLayerPtrs::iterator i = this->support_layers.begin() + idx;
SupportLayer* item = *i;
this->support_layers.erase(i);
delete item;
}
#ifdef SLIC3RXS
REGISTER_CLASS(PrintObject, "Print::Object");
#endif
Print::Print()
: total_used_filament(0),
total_extruded_volume(0)
{
}
Print::~Print()
{
clear_objects();
clear_regions();
}
void
Print::clear_objects()
{
for (int i = this->objects.size()-1; i >= 0; --i)
this->delete_object(i);
this->clear_regions();
this->_state.invalidate(psSkirt);
this->_state.invalidate(psBrim);
}
PrintObject*
Print::get_object(int idx)
{
return objects.at(idx);
}
PrintObject*
Print::add_object(ModelObject *model_object,
const BoundingBoxf3 &modobj_bbox)
{
PrintObject *object = new PrintObject(this,
this->objects.size(), model_object, modobj_bbox);
objects.push_back(object);
return object;
}
PrintObject*
Print::set_new_object(size_t idx, ModelObject *model_object,
const BoundingBoxf3 &modobj_bbox)
{
if (idx < 0 || idx >= this->objects.size()) throw "bad idx";
PrintObjectPtrs::iterator old_it = this->objects.begin() + idx;
delete *old_it;
PrintObject *object = new PrintObject(this, idx, model_object, modobj_bbox);
this->objects[idx] = object;
return object;
}
void
Print::delete_object(int idx)
{
PrintObjectPtrs::iterator i = this->objects.begin() + idx;
PrintObject* item = *i;
this->objects.erase(i);
delete item;
// TODO: purge unused regions
this->_state.invalidate(psSkirt);
this->_state.invalidate(psBrim);
}
void
Print::clear_regions()
{
for (int i = this->regions.size()-1; i >= 0; --i)
this->delete_region(i);
}
PrintRegion*
Print::get_region(int idx)
{
return regions.at(idx);
}
PrintRegion*
Print::add_region()
{
PrintRegion *region = new PrintRegion(this);
regions.push_back(region);
return region;
}
void
Print::delete_region(int idx)
{
PrintRegionPtrs::iterator i = this->regions.begin() + idx;
PrintRegion* item = *i;
this->regions.erase(i);
delete item;
}
#ifdef SLIC3RXS
REGISTER_CLASS(Print, "Print");
#endif
}

View file

@ -3,9 +3,19 @@
#include <myinit.h>
#include <set>
#include <vector>
#include "PrintConfig.hpp"
#include "Point.hpp"
#include "Layer.hpp"
#include "PlaceholderParser.hpp"
namespace Slic3r {
class Print;
class ModelObject;
enum PrintStep {
psInitExtruders, psSlice, psPerimeters, prPrepareInfill,
psInfill, psSupportMaterial, psSkirt, psBrim,
@ -26,6 +36,134 @@ class PrintState
void invalidate_all();
};
// TODO: make stuff private
// A PrintRegion object represents a group of volumes to print
// sharing the same config (including the same assigned extruder(s))
class PrintRegion
{
friend class Print;
public:
PrintRegionConfig config;
Print* print();
PrintConfig &print_config();
private:
Print* _print;
PrintRegion(Print* print);
virtual ~PrintRegion();
};
typedef std::vector<Layer*> LayerPtrs;
typedef std::vector<SupportLayer*> SupportLayerPtrs;
class BoundingBoxf3; // TODO: for temporary constructor parameter
class PrintObject
{
friend class Print;
public:
// vector of (vectors of volume ids), indexed by region_id
std::vector<std::vector<int> > region_volumes;
Points copies; // Slic3r::Point objects in scaled G-code coordinates
PrintObjectConfig config;
t_layer_height_ranges layer_height_ranges;
Point3 size; // XYZ in scaled coordinates
// scaled coordinates to add to copies (to compensate for the alignment
// operated when creating the object but still preserving a coherent API
// for external callers)
Point _copies_shift;
// Slic3r::Point objects in scaled G-code coordinates in our coordinates
Points _shifted_copies;
LayerPtrs layers;
SupportLayerPtrs support_layers;
// TODO: Fill* fill_maker => (is => 'lazy');
PrintState _state;
Print* print();
int id();
ModelObject* model_object();
// adds region_id, too, if necessary
void add_region_volume(int region_id, int volume_id);
size_t layer_count();
void clear_layers();
Layer* get_layer(int idx);
Layer* add_layer(int id, coordf_t height, coordf_t print_z,
coordf_t slice_z);
void delete_layer(int idx);
size_t support_layer_count();
void clear_support_layers();
SupportLayer* get_support_layer(int idx);
SupportLayer* add_support_layer(int id, coordf_t height, coordf_t print_z,
coordf_t slice_z);
void delete_support_layer(int idx);
private:
Print* _print;
int _id;
ModelObject* _model_object;
// TODO: call model_object->get_bounding_box() instead of accepting
// parameter
PrintObject(Print* print, int id, ModelObject* model_object,
const BoundingBoxf3 &modobj_bbox);
virtual ~PrintObject();
};
typedef std::vector<PrintObject*> PrintObjectPtrs;
typedef std::vector<PrintRegion*> PrintRegionPtrs;
class Print
{
public:
PrintConfig config;
PrintObjectConfig default_object_config;
PrintRegionConfig default_region_config;
PrintObjectPtrs objects;
PrintRegionPtrs regions;
PlaceholderParser placeholder_parser;
// TODO: status_cb
double total_used_filament;
double total_extruded_volume;
PrintState _state;
// ordered collection of extrusion paths to build skirt loops
ExtrusionEntityCollection skirt;
// ordered collection of extrusion paths to build a brim
ExtrusionEntityCollection brim;
Print();
virtual ~Print();
void clear_objects();
PrintObject* get_object(int idx);
PrintObject* add_object(ModelObject *model_object,
const BoundingBoxf3 &modobj_bbox);
PrintObject* set_new_object(size_t idx, ModelObject *model_object,
const BoundingBoxf3 &modobj_bbox);
void delete_object(int idx);
PrintRegion* get_region(int idx);
PrintRegion* add_region();
private:
void clear_regions();
void delete_region(int idx);
};
}
#endif