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

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>