mirror of https://github.com/vitalif/Slic3r
Initial work for porting Model to XS
parent
f308a46cd5
commit
6010428044
|
@ -20,18 +20,6 @@ sub read_from_file {
|
||||||
return $model;
|
return $model;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub merge {
|
|
||||||
my $class = shift;
|
|
||||||
my @models = @_;
|
|
||||||
|
|
||||||
my $new_model = ref($class)
|
|
||||||
? $class
|
|
||||||
: $class->new;
|
|
||||||
|
|
||||||
$new_model->add_object($_) for map @{$_->objects}, @models;
|
|
||||||
return $new_model;
|
|
||||||
}
|
|
||||||
|
|
||||||
sub add_object {
|
sub add_object {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,84 @@
|
||||||
|
#include "Model.hpp"
|
||||||
|
|
||||||
|
namespace Slic3r {
|
||||||
|
|
||||||
|
Model::~Model()
|
||||||
|
{
|
||||||
|
for (t_model_materials::iterator it = this->materials.begin(); it != this->materials.end(); ++it)
|
||||||
|
delete it->second;
|
||||||
|
for (t_model_objects::iterator it = this->objects.begin(); it != this->objects.end(); ++it)
|
||||||
|
delete *it;
|
||||||
|
}
|
||||||
|
|
||||||
|
ModelObject*
|
||||||
|
Model::add_object()
|
||||||
|
{
|
||||||
|
ModelObject* retval = new ModelObject(this);
|
||||||
|
this->objects.push_back(retval);
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
ModelObject*
|
||||||
|
Model::add_object(const ModelObject &object)
|
||||||
|
{
|
||||||
|
ModelObject* retval = new ModelObject(this, object);
|
||||||
|
this->objects.push_back(retval);
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
Model::delete_object(size_t obj_idx)
|
||||||
|
{
|
||||||
|
this->objects.erase(this->objects.begin() + obj_idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
Model::delete_all_objects()
|
||||||
|
{
|
||||||
|
this->objects.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
ModelMaterial*
|
||||||
|
Model::set_material(t_model_material_id material_id, const t_model_material_attributes &attributes)
|
||||||
|
{
|
||||||
|
ModelMaterial* material;
|
||||||
|
|
||||||
|
// create material if it does not exist
|
||||||
|
t_model_materials::iterator itm = this->materials.find(material_id);
|
||||||
|
if (itm == this->materials.end()) {
|
||||||
|
material = new ModelMaterial(this);
|
||||||
|
this->materials[material_id] = material;
|
||||||
|
} else {
|
||||||
|
material = *itm;
|
||||||
|
}
|
||||||
|
|
||||||
|
// apply attributes
|
||||||
|
for (t_model_material_attributes::const_iterator it = attributes.begin(); it != attributes.end(); ++it) {
|
||||||
|
material->attributes[it->first] = it->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
return material;
|
||||||
|
}
|
||||||
|
|
||||||
|
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";
|
||||||
|
|
||||||
|
ModelObject* object = this->objects.front();
|
||||||
|
object->instances.clear();
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -24,22 +24,26 @@ typedef std::map<t_model_material_attribute,std::string> t_model_material_attrib
|
||||||
|
|
||||||
class Model
|
class Model
|
||||||
{
|
{
|
||||||
public:
|
typedef std::map<t_model_material_id,ModelMaterial*> t_model_materials;
|
||||||
std::map<t_model_material_id,ModelMaterial*> materials;
|
typedef std::vector<ModelObject*> t_model_objects;
|
||||||
std::vector<ModelObject*> objects;
|
|
||||||
|
|
||||||
Model();
|
public:
|
||||||
|
t_model_materials materials;
|
||||||
|
t_model_objects objects;
|
||||||
|
|
||||||
|
Model() {};
|
||||||
~Model();
|
~Model();
|
||||||
|
ModelObject* add_object();
|
||||||
ModelObject* add_object(const ModelObject &object);
|
ModelObject* add_object(const ModelObject &object);
|
||||||
void delete_object(size_t obj_idx);
|
void delete_object(size_t obj_idx);
|
||||||
void delete_all_objects();
|
void delete_all_objects();
|
||||||
void set_material(t_model_material_id material_id, const t_model_material_attributes &attributes);
|
ModelMaterial* 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_grid(unsigned int x, unsigned int y, coordf_t distance);
|
||||||
void duplicate_objects(size_t copies_num, coordf_t distance, const BoundingBox &bb);
|
void duplicate_objects(size_t copies_num, coordf_t distance, const BoundingBoxf &bb);
|
||||||
void arrange_objects(coordf_t distance, const BoundingBox &bb);
|
void arrange_objects(coordf_t distance, const BoundingBoxf &bb);
|
||||||
void duplicate(size_t copies_num, coordf_t distance, const BoundingBox &bb);
|
void duplicate(size_t copies_num, coordf_t distance, const BoundingBoxf &bb);
|
||||||
bool has_objects_with_no_instances() const;
|
bool has_objects_with_no_instances() const;
|
||||||
void bounding_box(BoundingBox* bb) const;
|
void bounding_box(BoundingBoxf3* bb) const;
|
||||||
void align_to_origin();
|
void align_to_origin();
|
||||||
void center_instances_around_point(const Pointf &point);
|
void center_instances_around_point(const Pointf &point);
|
||||||
void translate(coordf_t x, coordf_t y, coordf_t z);
|
void translate(coordf_t x, coordf_t y, coordf_t z);
|
||||||
|
@ -48,35 +52,44 @@ class Model
|
||||||
std::string get_material_name(t_model_material_id material_id);
|
std::string get_material_name(t_model_material_id material_id);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void _arrange(const std::vector<Size> &sizes, coordf_t distance, const BoundingBox &bb) const;
|
void _arrange(const std::vector<Size> &sizes, coordf_t distance, const BoundingBoxf &bb, Pointfs* positions) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ModelMaterial
|
class ModelMaterial
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Model* model;
|
ModelMaterial(Model* _model) : model(_model) {};
|
||||||
t_model_material_attributes attributes;
|
t_model_material_attributes attributes;
|
||||||
DynamicConfig config;
|
DynamicConfig config;
|
||||||
|
Model* getModel() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Model* model;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ModelObject
|
class ModelObject
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Model* model;
|
|
||||||
std::string input_file;
|
std::string input_file;
|
||||||
std::vector<ModelInstance*> instances;
|
std::vector<ModelInstance*> instances;
|
||||||
std::vector<ModelVolume*> volumes;
|
std::vector<ModelVolume*> volumes;
|
||||||
DynamicConfig config;
|
DynamicConfig config;
|
||||||
t_layer_height_ranges layer_height_ranges;
|
t_layer_height_ranges layer_height_ranges;
|
||||||
|
Vector origin_translation; // translation vector applied by center_around_origin()
|
||||||
|
|
||||||
ModelObject();
|
ModelObject(Model* _model) : model(_model) {};
|
||||||
|
ModelObject(Model* _model, const ModelObject &object);
|
||||||
~ModelObject();
|
~ModelObject();
|
||||||
|
ModelInstance* add_instance();
|
||||||
ModelInstance* add_instance(const ModelInstance &instance);
|
ModelInstance* add_instance(const ModelInstance &instance);
|
||||||
|
ModelVolume* add_volume();
|
||||||
ModelVolume* add_volume(const ModelVolume &volume);
|
ModelVolume* add_volume(const ModelVolume &volume);
|
||||||
|
void delete_volume(size_t volume_idx);
|
||||||
void delete_last_instance();
|
void delete_last_instance();
|
||||||
void raw_mesh(TriangleMesh* mesh) const;
|
void raw_mesh(TriangleMesh* mesh) const;
|
||||||
void mesh(TriangleMesh* mesh) const;
|
void mesh(TriangleMesh* mesh) const;
|
||||||
void instance_bounding_box(size_t instance_idx, BoundingBox* bb) const;
|
void bounding_box(BoundingBoxf3* bb);
|
||||||
|
void instance_bounding_box(size_t instance_idx, BoundingBoxf3* bb) const;
|
||||||
void center_around_origin();
|
void center_around_origin();
|
||||||
void translate(coordf_t x, coordf_t y, coordf_t z);
|
void translate(coordf_t x, coordf_t y, coordf_t z);
|
||||||
size_t materials_count() const;
|
size_t materials_count() const;
|
||||||
|
@ -85,28 +98,38 @@ class ModelObject
|
||||||
bool needed_repair() const;
|
bool needed_repair() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BoundingBox bb;
|
Model* model;
|
||||||
|
BoundingBoxf bb;
|
||||||
void update_bounding_box();
|
void update_bounding_box();
|
||||||
};
|
};
|
||||||
|
|
||||||
class ModelVolume
|
class ModelVolume
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ModelObject* object;
|
ModelVolume(ModelObject* _object) : object(_object), modifier(false) {};
|
||||||
t_model_material_id material_id;
|
t_model_material_id material_id;
|
||||||
TriangleMesh mesh;
|
TriangleMesh mesh;
|
||||||
|
bool modifier;
|
||||||
|
|
||||||
|
ModelMaterial* assign_unique_material();
|
||||||
|
|
||||||
|
private:
|
||||||
|
ModelObject* object;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ModelInstance
|
class ModelInstance
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ModelObject* object;
|
|
||||||
double rotation;
|
double rotation;
|
||||||
double scaling_factor;
|
double scaling_factor;
|
||||||
Pointf offset;
|
Pointf offset;
|
||||||
|
|
||||||
|
ModelInstance(ModelObject* _object) : object(_object), rotation(0), scaling_factor(1) {};
|
||||||
void transform_mesh(TriangleMesh* mesh, bool dont_translate) const;
|
void transform_mesh(TriangleMesh* mesh, bool dont_translate) const;
|
||||||
void transform_polygon(Polygon* polygon) const;
|
void transform_polygon(Polygon* polygon) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
ModelObject* object;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue