Translate Model class' storage to C++.

Some code copied from xs-model branch.

Also:
* Generate ::Ref classes programatically.
* Add separate __REGISTER_CLASS macro
    (for use where forward declaration won't work, i.e. typedefs)
This commit is contained in:
Y. Sapir
2014-04-30 02:04:49 +03:00
parent c72dc13d7e
commit 05b2993769
20 changed files with 1048 additions and 218 deletions

View File

@@ -1673,6 +1673,7 @@ src/Geometry.hpp
src/Layer.hpp
src/Line.cpp
src/Line.hpp
src/Model.cpp
src/Model.hpp
src/MultiPoint.cpp
src/MultiPoint.hpp
@@ -1705,6 +1706,8 @@ src/Print.cpp
src/Print.hpp
src/PrintConfig.cpp
src/PrintConfig.hpp
src/StringMap.cpp
src/StringMap.hpp
src/Surface.cpp
src/Surface.hpp
src/SurfaceCollection.cpp
@@ -1742,6 +1745,7 @@ xsp/ExtrusionPath.xsp
xsp/Flow.xsp
xsp/Geometry.xsp
xsp/Line.xsp
xsp/Model.xsp
xsp/my.map
xsp/mytype.map
xsp/Point.xsp
@@ -1749,6 +1753,7 @@ xsp/Polygon.xsp
xsp/Polyline.xsp
xsp/PolylineCollection.xsp
xsp/Print.xsp
xsp/StringMap.xsp
xsp/Surface.xsp
xsp/SurfaceCollection.xsp
xsp/TriangleMesh.xsp

View File

@@ -13,31 +13,16 @@ use overload
'@{}' => sub { $_[0]->arrayref },
'fallback' => 1;
package Slic3r::Line::Ref;
our @ISA = 'Slic3r::Line';
sub DESTROY {}
package Slic3r::Point;
use overload
'@{}' => sub { $_[0]->arrayref },
'fallback' => 1;
package Slic3r::Point::Ref;
our @ISA = 'Slic3r::Point';
sub DESTROY {}
package Slic3r::Pointf;
use overload
'@{}' => sub { [ $_[0]->x, $_[0]->y ] }, #,
'fallback' => 1;
package Slic3r::Pointf::Ref;
our @ISA = 'Slic3r::Pointf';
sub DESTROY {}
package Slic3r::Pointf3;
use overload
'@{}' => sub { [ $_[0]->x, $_[0]->y, $_[0]->z ] }, #,
@@ -48,21 +33,11 @@ use overload
'@{}' => sub { $_[0]->arrayref },
'fallback' => 1;
package Slic3r::ExPolygon::Ref;
our @ISA = 'Slic3r::ExPolygon';
sub DESTROY {}
package Slic3r::Polyline;
use overload
'@{}' => sub { $_[0]->arrayref },
'fallback' => 1;
package Slic3r::Polyline::Ref;
our @ISA = 'Slic3r::Polyline';
sub DESTROY {}
package Slic3r::Polyline::Collection;
use overload
'@{}' => sub { $_[0]->arrayref },
@@ -73,11 +48,6 @@ use overload
'@{}' => sub { $_[0]->arrayref },
'fallback' => 1;
package Slic3r::Polygon::Ref;
our @ISA = 'Slic3r::Polygon';
sub DESTROY {}
package Slic3r::ExPolygon::Collection;
use overload
'@{}' => sub { $_[0]->arrayref },
@@ -96,11 +66,6 @@ sub new {
return $self;
}
package Slic3r::ExtrusionPath::Collection::Ref;
our @ISA = 'Slic3r::ExtrusionPath::Collection';
sub DESTROY {}
package Slic3r::ExtrusionLoop;
use overload
'@{}' => sub { $_[0]->arrayref },
@@ -130,11 +95,6 @@ sub clone {
);
}
package Slic3r::ExtrusionLoop::Ref;
our @ISA = 'Slic3r::ExtrusionLoop';
sub DESTROY {}
package Slic3r::ExtrusionPath;
use overload
'@{}' => sub { $_[0]->arrayref },
@@ -164,11 +124,6 @@ sub clone {
);
}
package Slic3r::ExtrusionPath::Ref;
our @ISA = 'Slic3r::ExtrusionPath';
sub DESTROY {}
package Slic3r::Flow;
sub new {
@@ -229,14 +184,53 @@ sub clone {
);
}
package Slic3r::Surface::Ref;
our @ISA = 'Slic3r::Surface';
sub DESTROY {}
package Slic3r::Surface::Collection;
use overload
'@{}' => sub { $_[0]->arrayref },
'fallback' => 1;
package Slic3r::StringMap;
sub to_hash {
my $self = shift;
my %tiehash;
tie %tiehash, 'Slic3r::StringMap', $self;
return \%tiehash;
}
sub TIEHASH {
my ($class, $self) = @_;
return $self;
}
package main;
for my $class (qw(
Slic3r::Config
Slic3r::Config::Print
Slic3r::ExPolygon
Slic3r::ExtrusionLoop
Slic3r::ExtrusionPath
Slic3r::ExtrusionPath::Collection
Slic3r::Geometry::BoundingBoxf3
Slic3r::Line
Slic3r::Model
Slic3r::Model::Instance
Slic3r::Model::Material
Slic3r::Model::Object
Slic3r::Model::Volume
Slic3r::Point
Slic3r::Pointf
Slic3r::Pointf3
Slic3r::Polygon
Slic3r::Polyline
Slic3r::StringMap
Slic3r::Surface
Slic3r::TriangleMesh
))
{
no strict 'refs';
my $ref_class = $class . "::Ref";
eval "package $ref_class; our \@ISA = '$class'; sub DESTROY {};";
}
1;

343
xs/src/Model.cpp Normal file
View File

@@ -0,0 +1,343 @@
#include "Model.hpp"
#ifdef SLIC3RXS
#include "perlglue.hpp"
#endif
namespace Slic3r {
Model::Model()
{
}
Model::Model(const Model &other)
: materials(),
objects()
{
objects.reserve(other.objects.size());
for (ModelMaterialMap::const_iterator i = other.materials.begin();
i != other.materials.end(); ++i)
{
ModelMaterial *copy = new ModelMaterial(*i->second);
copy->model = this;
materials[i->first] = copy;
}
for (ModelObjectPtrs::const_iterator i = other.objects.begin();
i != other.objects.end(); ++i)
{
ModelObject *copy = new ModelObject(**i);
copy->model = this;
objects.push_back(copy);
}
}
Model::~Model()
{
delete_all_objects();
delete_all_materials();
}
ModelObject *
Model::add_object(std::string input_file, DynamicPrintConfig *config,
t_layer_height_ranges layer_height_ranges, Point origin_translation)
{
ModelObject *object = new ModelObject(this, input_file, config,
layer_height_ranges, origin_translation);
this->objects.push_back(object);
return object;
}
void
Model::delete_object(size_t idx)
{
ModelObjectPtrs::iterator i = this->objects.begin() + idx;
delete *i;
this->objects.erase(i);
}
void
Model::delete_all_objects()
{
for (ModelObjectPtrs::iterator i = this->objects.begin();
i != this->objects.end(); ++i)
{
delete *i;
}
objects.clear();
}
void
Model::delete_all_materials()
{
for (ModelMaterialMap::iterator i = this->materials.begin();
i != this->materials.end(); ++i)
{
delete i->second;
}
this->materials.clear();
}
ModelMaterial *
Model::set_material(t_model_material_id material_id,
const t_model_material_attributes &attributes)
{
ModelMaterialMap::iterator i = this->materials.find(material_id);
ModelMaterial *mat;
if (i == this->materials.end()) {
mat = this->materials[material_id] = new ModelMaterial(this);
} else {
mat = i->second;
}
mat->apply(attributes);
return mat;
}
/*
void
Model::duplicate_objects_grid(unsigned int x, unsigned int y, coordf_t distance)
{
if (this->objects.size() > 1) throw "Grid duplication is not supported with multiple objects";
if (this->objects.empty()) throw "No objects!";
ModelObject* object = this->objects.front();
object->clear_instances();
BoundingBoxf3 bb;
object->bounding_box(&bb);
Sizef3 size = bb.size();
for (unsigned int x_copy = 1; x_copy <= x; ++x_copy) {
for (unsigned int y_copy = 1; y_copy <= y; ++y_copy) {
ModelInstance* instance = object->add_instance();
instance->offset.x = (size.x + distance) * (x_copy-1);
instance->offset.y = (size.y + distance) * (y_copy-1);
}
}
}
*/
bool
Model::has_objects_with_no_instances() const
{
for (ModelObjectPtrs::const_iterator i = this->objects.begin();
i != this->objects.end(); ++i)
{
if ((*i)->instances.empty()) {
return true;
}
}
return false;
}
#ifdef SLIC3RXS
REGISTER_CLASS(Model, "Model");
#endif
ModelMaterial::ModelMaterial(Model *model)
: model(model),
attributes(),
config()
{
}
void
ModelMaterial::apply(const t_model_material_attributes &attributes)
{
this->attributes.insert(attributes.begin(), attributes.end());
}
#ifdef SLIC3RXS
REGISTER_CLASS(ModelMaterial, "Model::Material");
#endif
ModelObject::ModelObject(Model *model, std::string input_file,
DynamicPrintConfig *config, t_layer_height_ranges layer_height_ranges,
Point origin_translation)
: model(model),
input_file(input_file),
config(*config),
layer_height_ranges(layer_height_ranges),
origin_translation(origin_translation),
_bounding_box_valid(false)
{
}
ModelObject::ModelObject(const ModelObject &other)
: model(other.model),
input_file(other.input_file),
instances(),
volumes(),
config(other.config),
layer_height_ranges(other.layer_height_ranges),
origin_translation(other.origin_translation),
_bounding_box(other._bounding_box),
_bounding_box_valid(other._bounding_box_valid)
{
volumes.reserve(other.volumes.size());
instances.reserve(other.instances.size());
for (ModelVolumePtrs::const_iterator i = other.volumes.begin();
i != other.volumes.end(); ++i)
{
ModelVolume *v = new ModelVolume(**i);
v->object = this;
volumes.push_back(v);
}
for (ModelInstancePtrs::const_iterator i = other.instances.begin();
i != other.instances.end(); ++i)
{
ModelInstance *in = new ModelInstance(**i);
in->object = this;
instances.push_back(in);
}
}
ModelObject::~ModelObject()
{
this->clear_volumes();
this->clear_instances();
}
ModelVolume *
ModelObject::add_volume(t_model_material_id material_id,
TriangleMesh *mesh, bool modifier)
{
ModelVolume *v = new ModelVolume(this, material_id, mesh, modifier);
this->volumes.push_back(v);
this->invalidate_bounding_box();
return v;
}
void
ModelObject::delete_volume(size_t idx)
{
ModelVolumePtrs::iterator i = this->volumes.begin() + idx;
delete *i;
this->volumes.erase(i);
this->invalidate_bounding_box();
}
void
ModelObject::clear_volumes()
{
for (ModelVolumePtrs::iterator i = this->volumes.begin();
i != this->volumes.end(); ++i)
{
delete *i;
}
this->volumes.clear();
}
ModelInstance *
ModelObject::add_instance(double rotation, double scaling_factor,
Pointf offset)
{
ModelInstance *i = new ModelInstance(
this, rotation, scaling_factor, offset);
this->instances.push_back(i);
this->invalidate_bounding_box();
return i;
}
void
ModelObject::delete_last_instance()
{
delete this->instances.back();
this->instances.pop_back();
this->invalidate_bounding_box();
}
void
ModelObject::clear_instances()
{
for (ModelInstancePtrs::iterator i = this->instances.begin();
i != this->instances.end(); ++i)
{
delete *i;
}
this->instances.clear();
}
void
ModelObject::invalidate_bounding_box()
{
this->_bounding_box_valid = false;
}
#ifdef SLIC3RXS
REGISTER_CLASS(ModelObject, "Model::Object");
SV*
ModelObject::to_SV_ref() {
SV* sv = newSV(0);
sv_setref_pv( sv, perl_class_name_ref(this), this );
return sv;
}
#endif
ModelVolume::ModelVolume(ModelObject *object, t_model_material_id material_id,
TriangleMesh *mesh, bool modifier)
: object(object),
material_id(material_id),
mesh(*mesh),
modifier(modifier)
{
}
ModelVolume::~ModelVolume()
{
}
#ifdef SLIC3RXS
REGISTER_CLASS(ModelVolume, "Model::Volume");
SV*
ModelVolume::to_SV_ref() {
SV* sv = newSV(0);
sv_setref_pv( sv, perl_class_name_ref(this), this );
return sv;
}
#endif
ModelInstance::ModelInstance(ModelObject *object, double rotation,
double scaling_factor, Pointf offset)
: object(object),
rotation(rotation),
scaling_factor(scaling_factor),
offset(offset)
{
}
ModelInstance::~ModelInstance()
{
}
#ifdef SLIC3RXS
REGISTER_CLASS(ModelInstance, "Model::Instance");
SV*
ModelInstance::to_SV_ref() {
SV* sv = newSV(0);
sv_setref_pv( sv, perl_class_name_ref(this), this );
return sv;
}
#endif
}

View File

@@ -2,7 +2,7 @@
#define slic3r_Model_hpp_
#include <myinit.h>
#include "Config.hpp"
#include "PrintConfig.hpp"
#include "Layer.hpp"
#include "Point.hpp"
#include "TriangleMesh.hpp"
@@ -22,30 +22,40 @@ typedef std::string t_model_material_id;
typedef std::string t_model_material_attribute;
typedef std::map<t_model_material_attribute,std::string> t_model_material_attributes;
typedef std::map<t_model_material_id,ModelMaterial*> ModelMaterialMap;
typedef std::vector<ModelObject*> ModelObjectPtrs;
typedef std::vector<ModelVolume*> ModelVolumePtrs;
typedef std::vector<ModelInstance*> ModelInstancePtrs;
class Model
{
public:
std::map<t_model_material_id,ModelMaterial*> materials;
std::vector<ModelObject*> objects;
ModelMaterialMap materials;
ModelObjectPtrs objects;
Model();
Model(const Model &other);
~Model();
ModelObject* add_object(const ModelObject &object);
void delete_object(size_t obj_idx);
ModelObject *add_object(std::string input_file, DynamicPrintConfig *config,
t_layer_height_ranges layer_height_ranges, Point origin_translation);
void delete_object(size_t idx);
void delete_all_objects();
void set_material(t_model_material_id material_id, const t_model_material_attributes &attributes);
void duplicate_objects_grid(coordf_t x, coordf_t y, coordf_t distance);
void duplicate_objects(size_t copies_num, coordf_t distance, const BoundingBox &bb);
void arrange_objects(coordf_t distance, const BoundingBox &bb);
void duplicate(size_t copies_num, coordf_t distance, const BoundingBox &bb);
void delete_all_materials();
ModelMaterial *set_material(t_model_material_id material_id,
const t_model_material_attributes &attributes);
// void duplicate_objects_grid(unsigned int x, unsigned int y, coordf_t distance);
// void duplicate_objects(size_t copies_num, coordf_t distance, const BoundingBox &bb);
// void arrange_objects(coordf_t distance, const BoundingBox &bb);
// void duplicate(size_t copies_num, coordf_t distance, const BoundingBox &bb);
bool has_objects_with_no_instances() const;
void bounding_box(BoundingBox* bb) const;
void align_to_origin();
void center_instances_around_point(const Pointf &point);
void translate(coordf_t x, coordf_t y, coordf_t z);
void mesh(TriangleMesh* mesh) const;
void split_meshes();
std::string get_material_name(t_model_material_id material_id);
// void bounding_box(BoundingBox* bb) const;
// void align_to_origin();
// void center_instances_around_point(const Pointf &point);
// void translate(coordf_t x, coordf_t y, coordf_t z);
// void mesh(TriangleMesh* mesh) const;
// void split_meshes();
// std::string get_material_name(t_model_material_id material_id);
private:
void _arrange(const std::vector<Size> &sizes, coordf_t distance, const BoundingBox &bb) const;
@@ -56,7 +66,10 @@ class ModelMaterial
public:
Model* model;
t_model_material_attributes attributes;
DynamicConfig config;
DynamicPrintConfig config;
ModelMaterial(Model *model);
void apply(const t_model_material_attributes &attributes);
};
class ModelObject
@@ -64,16 +77,31 @@ class ModelObject
public:
Model* model;
std::string input_file;
std::vector<ModelInstance*> instances;
std::vector<ModelVolume*> volumes;
DynamicConfig config;
ModelInstancePtrs instances;
ModelVolumePtrs volumes;
DynamicPrintConfig config;
t_layer_height_ranges layer_height_ranges;
Point origin_translation;
BoundingBoxf3 _bounding_box;
bool _bounding_box_valid;
ModelObject();
ModelObject(Model *model, std::string input_file, DynamicPrintConfig *config,
t_layer_height_ranges layer_height_ranges, Point origin_translation);
ModelObject(const ModelObject &other);
~ModelObject();
ModelInstance* add_instance(const ModelInstance &instance);
ModelVolume* add_volume(const ModelVolume &volume);
ModelVolume *add_volume(t_model_material_id material_id,
TriangleMesh *mesh, bool modifier);
void delete_volume(size_t idx);
void clear_volumes();
ModelInstance *add_instance(double rotation=0, double scaling_factor=1,
Pointf offset=Pointf(0, 0));
void delete_last_instance();
void clear_instances();
void invalidate_bounding_box();
void raw_mesh(TriangleMesh* mesh) const;
void mesh(TriangleMesh* mesh) const;
void instance_bounding_box(size_t instance_idx, BoundingBox* bb) const;
@@ -84,8 +112,11 @@ class ModelObject
size_t facets_count() const;
bool needed_repair() const;
#ifdef SLIC3RXS
SV* to_SV_ref();
#endif
private:
BoundingBox bb;
void update_bounding_box();
};
@@ -95,18 +126,35 @@ class ModelVolume
ModelObject* object;
t_model_material_id material_id;
TriangleMesh mesh;
bool modifier;
ModelVolume(ModelObject *object, t_model_material_id material_id,
TriangleMesh *mesh, bool modifier);
~ModelVolume();
#ifdef SLIC3RXS
SV* to_SV_ref();
#endif
};
class ModelInstance
{
public:
ModelObject* object;
double rotation;
double rotation; // around mesh center point
double scaling_factor;
Pointf offset;
Pointf offset; // in unscaled coordinates
ModelInstance(ModelObject *object, double rotation, double scaling_factor,
Pointf offset);
~ModelInstance();
void transform_mesh(TriangleMesh* mesh, bool dont_translate) const;
void transform_polygon(Polygon* polygon) const;
#ifdef SLIC3RXS
SV* to_SV_ref();
#endif
};
}

13
xs/src/StringMap.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include "StringMap.hpp"
#ifdef SLIC3RXS
#include "perlglue.hpp"
#endif
namespace Slic3r {
#ifdef SLIC3RXS
__REGISTER_CLASS(StringMap, "StringMap");
#endif
}

16
xs/src/StringMap.hpp Normal file
View File

@@ -0,0 +1,16 @@
#ifndef slic3r_StringMap_hpp_
#define slic3r_StringMap_hpp_
#include <myinit.h>
#include <map>
#include <string>
namespace Slic3r {
// this is just for XSPP, because it chokes on the template typename
typedef std::map<std::string, std::string> StringMap;
}
#endif

View File

@@ -8,12 +8,17 @@ struct ClassTraits {
static const char* name;
static const char* name_ref;
};
#define REGISTER_CLASS(cname,perlname) \
class cname; \
// use this for typedefs for which the forward prototype
// in REGISTER_CLASS won't work
#define __REGISTER_CLASS(cname, perlname) \
template <>const char* ClassTraits<cname>::name = "Slic3r::" perlname; \
template <>const char* ClassTraits<cname>::name_ref = "Slic3r::" perlname "::Ref";
#define REGISTER_CLASS(cname,perlname) \
class cname; \
__REGISTER_CLASS(cname, perlname);
template<class T>
const char* perl_class_name(const T*) { return ClassTraits<T>::name; }
template<class T>

216
xs/xsp/Model.xsp Normal file
View File

@@ -0,0 +1,216 @@
%module{Slic3r::XS};
%{
#include <myinit.h>
#include "Model.hpp"
#include "PrintConfig.hpp"
#include "StringMap.hpp"
#include "perlglue.hpp"
%}
%name{Slic3r::Model} class Model {
Model();
~Model();
Clone<Model> clone()
%code%{ RETVAL = THIS; %};
Ref<ModelObject> _add_object(std::string input_file,
DynamicPrintConfig *config,
t_layer_height_ranges layer_height_ranges,
Point *origin_translation)
%code%{
RETVAL = THIS->add_object(input_file, config, layer_height_ranges,
*origin_translation);
%};
void delete_object(size_t idx);
void delete_all_objects();
void delete_all_materials();
%name{_set_material} Ref<ModelMaterial> set_material(t_model_material_id material_id,
StringMap attributes)
%code%{ RETVAL = THIS->set_material(material_id, attributes); %};
Ref<ModelMaterial> get_material(t_model_material_id material_id)
%code%{
ModelMaterialMap::iterator i = THIS->materials.find(material_id);
if (i == THIS->materials.end()) {
XSRETURN_UNDEF;
}
RETVAL = i->second;
%};
bool has_material(t_model_material_id material_id) const
%code%{
RETVAL = (THIS->materials.find(material_id) != THIS->materials.end());
%};
std::vector<std::string> material_names() const
%code%{
for (ModelMaterialMap::iterator i = THIS->materials.begin();
i != THIS->materials.end(); ++i)
{
RETVAL.push_back(i->first);
}
%};
size_t material_count() const
%code%{ RETVAL = THIS->materials.size(); %};
// void duplicate_objects_grid(coordf_t x, coordf_t y, coordf_t distance);
// void duplicate_objects(size_t copies_num, coordf_t distance, const BoundingBox &bb);
// void arrange_objects(coordf_t distance, const BoundingBox &bb);
// void duplicate(size_t copies_num, coordf_t distance, const BoundingBox &bb);
bool has_objects_with_no_instances() const;
// void bounding_box(BoundingBox* bb) const;
// void center_instances_around_point(const Pointf &point);
// void align_instances_to_origin();
// void translate(coordf_t x, coordf_t y, coordf_t z);
// void mesh(TriangleMesh* mesh) const;
// void split_meshes();
// std::string get_material_name(t_model_material_id material_id);
ModelObjectPtrs *objects()
%code%{
if (THIS->objects.empty()) {
XSRETURN_UNDEF;
}
RETVAL = &THIS->objects;
%};
};
%name{Slic3r::Model::Material} class ModelMaterial {
~ModelMaterial();
Ref<Model> model()
%code%{ RETVAL = THIS->model; %};
Ref<StringMap> _attributes()
%code%{ RETVAL = &THIS->attributes; %};
Ref<DynamicPrintConfig> config()
%code%{ RETVAL = &THIS->config; %};
};
%name{Slic3r::Model::Object} class ModelObject {
ModelObject(Model *model, std::string input_file,
DynamicPrintConfig *config, t_layer_height_ranges layer_height_ranges,
Point *origin_translation)
%code%{
RETVAL = new ModelObject(model, input_file, config,
layer_height_ranges, *origin_translation);
%};
~ModelObject();
ModelVolumePtrs *volumes()
%code%{
if (THIS->volumes.empty()) {
XSRETURN_UNDEF;
}
RETVAL = &THIS->volumes;
%};
ModelInstancePtrs *instances()
%code%{
if (THIS->instances.empty()) {
XSRETURN_UNDEF;
}
RETVAL = &THIS->instances;
%};
void invalidate_bounding_box();
Ref<BoundingBoxf3> _bounding_box(BoundingBoxf3 *new_bbox = NULL)
%code{%
if (NULL != new_bbox) {
THIS->_bounding_box = *new_bbox;
THIS->_bounding_box_valid = true;
}
if (!THIS->_bounding_box_valid) {
XSRETURN_UNDEF;
}
RETVAL = &THIS->_bounding_box;
%};
%name{_add_volume} Ref<ModelVolume> add_volume(
t_model_material_id material_id, TriangleMesh *mesh, bool modifier)
%code%{ RETVAL = THIS->add_volume(material_id, mesh, modifier); %};
void delete_volume(size_t idx);
void clear_volumes();
%name{_add_instance} Ref<ModelInstance> add_instance(
double rotation, double scaling_factor, std::vector<double> offset)
%code%{
RETVAL = THIS->add_instance(rotation, scaling_factor,
Pointf(offset[0], offset[1]));
%};
void delete_last_instance();
void clear_instances();
std::string input_file()
%code%{ RETVAL = THIS->input_file; %};
void set_input_file(std::string value)
%code%{ THIS->input_file = value; %};
Ref<DynamicPrintConfig> config()
%code%{ RETVAL = &THIS->config; %};
Ref<Model> model()
%code%{ RETVAL = THIS->model; %};
t_layer_height_ranges layer_height_ranges()
%code%{ RETVAL = THIS->layer_height_ranges; %};
Clone<Point> origin_translation()
%code%{ RETVAL = THIS->origin_translation; %};
};
%name{Slic3r::Model::Volume} class ModelVolume {
~ModelVolume();
Ref<ModelObject> object()
%code%{ RETVAL = THIS->object; %};
t_model_material_id material_id()
%code%{ RETVAL = THIS->material_id; %};
Ref<TriangleMesh> mesh()
%code%{ RETVAL = &THIS->mesh; %};
bool modifier()
%code%{ RETVAL = THIS->modifier; %};
};
%name{Slic3r::Model::Instance} class ModelInstance {
~ModelInstance();
Ref<ModelObject> object()
%code%{ RETVAL = THIS->object; %};
double rotation()
%code%{ RETVAL = THIS->rotation; %};
double scaling_factor()
%code%{ RETVAL = THIS->scaling_factor; %};
Clone<Pointf> offset()
%code%{ RETVAL = THIS->offset; %};
void set_rotation(double val)
%code%{ THIS->rotation = val; %};
void set_scaling_factor(double val)
%code%{ THIS->scaling_factor = val; %};
void set_offset(Pointf *offset)
%code%{ THIS->offset = *offset; %};
};

59
xs/xsp/StringMap.xsp Normal file
View File

@@ -0,0 +1,59 @@
%module{Slic3r::XS};
%{
#include <myinit.h>
#include "StringMap.hpp"
#include "perlglue.hpp"
%}
%name{Slic3r::StringMap} class StringMap {
std::string FETCH(std::string key)
%code%{
StringMap::iterator i = THIS->find(key);
if (i == THIS->end()) {
XSRETURN_UNDEF;
}
RETVAL = i->second;
%};
void STORE(std::string key, std::string val)
%code%{ (*THIS)[key] = val; %};
void DELETE(std::string key)
%code%{ THIS->erase(key); %};
void CLEAR()
%code%{ THIS->clear(); %};
bool EXISTS(std::string key)
%code%{ RETVAL = (THIS->find(key) != THIS->end()); %};
std::string FIRSTKEY()
%code%{
StringMap::iterator i = THIS->begin();
if (i == THIS->end()) {
XSRETURN_UNDEF;
} else {
RETVAL = i->first;
}
%};
std::string NEXTKEY(std::string lastkey)
%code%{
StringMap::iterator i = THIS->find(lastkey);
if (i == THIS->end()) {
XSRETURN_UNDEF;
}
++i;
if (i == THIS->end()) {
XSRETURN_UNDEF;
} else {
RETVAL = i->first;
}
%};
bool SCALAR()
%code%{ RETVAL = !THIS->empty(); %};
};

View File

@@ -1,6 +1,17 @@
coordf_t T_NV
std::vector<Points::size_type> T_STD_VECTOR_INT
std::vector<size_t> T_STD_VECTOR_INT
t_config_option_key T_STD_STRING
t_model_material_id T_STD_STRING
t_layer_height_ranges T_LAYER_HEIGHT_RANGES
StringMap T_STRING_MAP
StringMap* O_OBJECT_SLIC3R
Ref<StringMap> O_OBJECT_SLIC3R_T
BoundingBox* O_OBJECT_SLIC3R
Ref<BoundingBox> O_OBJECT_SLIC3R_T
@@ -11,6 +22,7 @@ Ref<BoundingBoxf3> O_OBJECT_SLIC3R_T
Clone<BoundingBoxf3> O_OBJECT_SLIC3R_T
DynamicPrintConfig* O_OBJECT_SLIC3R
Ref<DynamicPrintConfig> O_OBJECT_SLIC3R_T
PrintObjectConfig* O_OBJECT_SLIC3R
PrintRegionConfig* O_OBJECT_SLIC3R
PrintConfig* O_OBJECT_SLIC3R
@@ -83,6 +95,27 @@ SurfaceCollection* O_OBJECT_SLIC3R
Extruder* O_OBJECT_SLIC3R
Model* O_OBJECT_SLIC3R
Ref<Model> O_OBJECT_SLIC3R_T
Clone<Model> O_OBJECT_SLIC3R_T
ModelMaterial* O_OBJECT_SLIC3R
Ref<ModelMaterial> O_OBJECT_SLIC3R_T
Clone<ModelMaterial> O_OBJECT_SLIC3R_T
ModelObject* O_OBJECT_SLIC3R
Ref<ModelObject> O_OBJECT_SLIC3R_T
Clone<ModelObject> O_OBJECT_SLIC3R_T
ModelVolume* O_OBJECT_SLIC3R
Ref<ModelVolume> O_OBJECT_SLIC3R_T
Clone<ModelVolume> O_OBJECT_SLIC3R_T
ModelInstance* O_OBJECT_SLIC3R
Ref<ModelInstance> O_OBJECT_SLIC3R_T
Clone<ModelInstance> O_OBJECT_SLIC3R_T
ExtrusionRole T_UV
FlowRole T_UV
PrintStep T_UV
@@ -103,12 +136,16 @@ Surfaces T_ARRAYREF
# by reference and marked ::Ref because they're contained in another
# Perl object
Polygons* T_ARRAYREF_PTR
ModelObjectPtrs* T_PTR_ARRAYREF_PTR
ModelVolumePtrs* T_PTR_ARRAYREF_PTR
ModelInstancePtrs* T_PTR_ARRAYREF_PTR
# we return these types whenever we want the items to be returned
# by reference and not marked ::Ref because they're newly allocated
# and not referenced by any Perl object
TriangleMeshPtrs T_PTR_ARRAYREF
INPUT
O_OBJECT_SLIC3R
@@ -140,6 +177,75 @@ T_ARRAYREF
${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]},
\"$var\");
# http://www.perlmonks.org/?node_id=853194
T_STRING_MAP
{
HV *hv;
HE *he;
std::map<std::string, std::string> t_sm;
if(SvROK($arg) && SvTYPE(SvRV($arg)) == SVt_PVHV) {
hv = (HV *)SvRV($arg);
hv_iterinit(hv);
} else {
warn(\"${Package}::$func_name() -- $var is not a hash reference\");
XSRETURN_UNDEF;
}
while((he = hv_iternext(hv)) != NULL) {
SV *svkey = HeSVKEY_force(he);
SV *svval = HeVAL(he);
t_sm[SvPV_nolen(svkey)] = SvPV_nolen(svval);
}
$var = t_sm;
}
T_LAYER_HEIGHT_RANGES
{
if (!SvROK($arg) || SvTYPE(SvRV($arg)) != SVt_PVAV) {
Perl_croak(aTHX_ \"%s: %s is not an array reference\",
${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]},
\"$var\");
}
AV* av = (AV*)SvRV($arg);
const unsigned int len = av_len(av)+1;
t_layer_height_ranges tmp_ranges;
for (unsigned int i = 0; i < len; i++) {
SV* elem = *av_fetch(av, i, 0);
if (!SvROK(elem) || SvTYPE(SvRV(elem)) != SVt_PVAV) {
Perl_croak(
aTHX_ \"%s: %s contains something that is not an array reference\",
${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]},
\"$var\");
}
AV* elemAV = (AV*)SvRV(elem);
if (av_len(elemAV) + 1 != 3) {
Perl_croak(
aTHX_ \"%s: %s contains an array that isn't 3 elements long\",
${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]},
\"$var\");
}
coordf_t vals[3];
for (unsigned int j = 0; j < 3; ++j) {
SV *elem_elem = *av_fetch(elemAV, j, 0);
if (!SvNOK(elem_elem)) {
Perl_croak(
aTHX_ \"%s: layer ranges and heights must be numbers\",
${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]});
}
vals[j] = SvNV(elem_elem);
}
tmp_ranges[t_layer_height_range(vals[0], vals[1])] = vals[2];
}
$var = tmp_ranges;
}
OUTPUT
# return object from pointer
O_OBJECT_SLIC3R
@@ -171,6 +277,16 @@ T_ARRAYREF_PTR
av_store(av, i++, it->to_SV_ref());
}
T_PTR_ARRAYREF_PTR
AV* av = newAV();
$arg = newRV_noinc((SV*)av);
sv_2mortal($arg);
av_extend(av, $var->size()-1);
int i = 0;
for (${ my $t = $type; $t =~ s/\*$//; \$t }::iterator it = $var->begin(); it != $var->end(); ++it) {
av_store(av, i++, (*it)->to_SV_ref());
}
T_PTR_ARRAYREF
AV* av = newAV();
$arg = newRV_noinc((SV*)av);
@@ -180,3 +296,30 @@ T_PTR_ARRAYREF
for (${type}::iterator it = $var.begin(); it != $var.end(); ++it) {
av_store(av, i++, (*it)->to_SV());
}
T_LAYER_HEIGHT_RANGES
AV* av = newAV();
$arg = newRV_noinc((SV*)av);
sv_2mortal($arg);
av_extend(av, $var.size() - 1);
// map is sorted, so we can just copy it in order
int i = 0;
for (${type}::iterator it = $var.begin(); it != $var.end(); ++it) {
const coordf_t range_values[] = {
it->first.first, // key's first = minz
it->first.second, // key's second = maxz
it->second, // value = height
};
AV *rangeAV = newAV();
SV *rangeAV_ref = newRV_noinc((SV*)rangeAV);
sv_2mortal(rangeAV_ref);
av_extend(rangeAV, 2);
for (int j = 0; j < 3; ++j) {
SV *val = sv_newmortal();
sv_setnv(val, range_values[j]);
av_store(rangeAV, j, val);
}
av_store(av, i++, (SV*)rangeAV_ref);
}

View File

@@ -1,12 +1,19 @@
%typemap{bool}{simple};
%typemap{size_t}{simple};
%typemap{coordf_t}{simple};
%typemap{std::string};
%typemap{t_config_option_key};
%typemap{t_model_material_id};
%typemap{std::vector<int>};
%typemap{std::vector<size_t>};
%typemap{std::vector<unsigned int>*};
%typemap{std::vector<double>};
%typemap{std::vector<double>*};
%typemap{std::vector<std::string>};
%typemap{t_layer_height_ranges};
%typemap{StringMap};
%typemap{StringMap*};
%typemap{Ref<StringMap>}{simple};
%typemap{SV*};
%typemap{AV*};
%typemap{Point*};
@@ -25,6 +32,7 @@
%typemap{Ref<BoundingBoxf3>}{simple};
%typemap{Clone<BoundingBoxf3>}{simple};
%typemap{DynamicPrintConfig*};
%typemap{Ref<DynamicPrintConfig>}{simple};
%typemap{PrintObjectConfig*};
%typemap{PrintRegionConfig*};
%typemap{PrintConfig*};
@@ -72,8 +80,33 @@
%typemap{ExPolygons};
%typemap{Surfaces};
%typemap{Polygons*};
%typemap{TriangleMesh*};
%typemap{TriangleMeshPtrs};
%typemap{Extruder*};
%typemap{Model*};
%typemap{Ref<Model>}{simple};
%typemap{Clone<Model>}{simple};
%typemap{ModelMaterial*};
%typemap{Ref<ModelMaterial>}{simple};
%typemap{Clone<ModelMaterial>}{simple};
%typemap{ModelObject*};
%typemap{Ref<ModelObject>}{simple};
%typemap{Clone<ModelObject>}{simple};
%typemap{ModelObjectPtrs*};
%typemap{Ref<ModelObjectPtrs>}{simple};
%typemap{Clone<ModelObjectPtrs>}{simple};
%typemap{ModelVolume*};
%typemap{Ref<ModelVolume>}{simple};
%typemap{Clone<ModelVolume>}{simple};
%typemap{ModelVolumePtrs*};
%typemap{Ref<ModelVolumePtrs>}{simple};
%typemap{Clone<ModelVolumePtrs>}{simple};
%typemap{ModelInstance*};
%typemap{Ref<ModelInstance>}{simple};
%typemap{Clone<ModelInstance>}{simple};
%typemap{ModelInstancePtrs*};
%typemap{Ref<ModelInstancePtrs>}{simple};
%typemap{Clone<ModelInstancePtrs>}{simple};
%typemap{SurfaceType}{parsed}{
%cpp_type{SurfaceType};