Merge branch 'sapir-modelcpp'

This commit is contained in:
Alessandro Ranellucci 2014-05-08 11:13:21 +02:00
commit 6e207d3830
17 changed files with 911 additions and 222 deletions

233
xs/xsp/Model.xsp Normal file
View file

@ -0,0 +1,233 @@
%module{Slic3r::XS};
%{
#include <myinit.h>
#include "Model.hpp"
#include "PrintConfig.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,
Pointf* origin_translation)
%code%{
RETVAL = THIS->add_object(input_file, *config, layer_height_ranges,
*origin_translation);
%};
void delete_object(size_t idx);
void clear_objects();
void delete_material(t_model_material_id material_id);
void clear_materials();
%name{_set_material} Ref<ModelMaterial> set_material(t_model_material_id material_id)
%code%{ RETVAL = THIS->set_material(material_id); %};
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<DynamicPrintConfig> config()
%code%{ RETVAL = &THIS->config; %};
std::string get_attribute(std::string name)
%code%{ if (THIS->attributes.find(name) != THIS->attributes.end()) RETVAL = THIS->attributes[name]; %};
void set_attribute(std::string name, std::string value)
%code%{ THIS->attributes[name] = value; %};
%{
SV*
ModelMaterial::attributes()
CODE:
HV* hv = newHV();
for (t_model_material_attributes::const_iterator attr = THIS->attributes.begin(); attr != THIS->attributes.end(); ++attr) {
(void)hv_store( hv, attr->first.c_str(), attr->first.length(), newSVpv(attr->second.c_str(), attr->second.length()), 0 );
}
RETVAL = (SV*)newRV_noinc((SV*)hv);
OUTPUT:
RETVAL
%}
};
%name{Slic3r::Model::Object} class ModelObject {
ModelObject(Model* model, std::string input_file,
DynamicPrintConfig* config, t_layer_height_ranges layer_height_ranges,
Pointf* 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<Pointf> 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; %};
};

View file

@ -1,6 +1,12 @@
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
BoundingBox* O_OBJECT_SLIC3R
Ref<BoundingBox> O_OBJECT_SLIC3R_T
@ -11,6 +17,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 +90,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
@ -104,12 +132,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
@ -141,6 +173,53 @@ T_ARRAYREF
${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]},
\"$var\");
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
@ -172,6 +251,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);
@ -181,3 +270,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,16 @@
%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{SV*};
%typemap{AV*};
%typemap{Point*};
@ -25,6 +29,7 @@
%typemap{Ref<BoundingBoxf3>}{simple};
%typemap{Clone<BoundingBoxf3>}{simple};
%typemap{DynamicPrintConfig*};
%typemap{Ref<DynamicPrintConfig>}{simple};
%typemap{PrintObjectConfig*};
%typemap{PrintRegionConfig*};
%typemap{PrintConfig*};
@ -73,8 +78,33 @@
%typemap{ExtrusionPaths};
%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};