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

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};