2014-04-29 23:04:49 +00:00
|
|
|
#include "Model.hpp"
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2014-05-06 22:58:29 +00:00
|
|
|
Model::Model() {}
|
2014-04-29 23:04:49 +00:00
|
|
|
|
|
|
|
Model::Model(const Model &other)
|
|
|
|
{
|
2014-05-09 12:24:35 +00:00
|
|
|
// copy materials
|
|
|
|
for (ModelMaterialMap::const_iterator i = other.materials.begin(); i != other.materials.end(); ++i)
|
|
|
|
this->add_material(i->first, *i->second);
|
|
|
|
|
|
|
|
// copy objects
|
|
|
|
this->objects.reserve(other.objects.size());
|
|
|
|
for (ModelObjectPtrs::const_iterator i = other.objects.begin(); i != other.objects.end(); ++i)
|
|
|
|
this->add_object(**i);
|
2014-04-29 23:04:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 11:33:43 +00:00
|
|
|
Model& Model::operator= (Model other)
|
|
|
|
{
|
|
|
|
this->swap(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Model::swap(Model &other)
|
|
|
|
{
|
|
|
|
std::swap(this->materials, other.materials);
|
|
|
|
std::swap(this->objects, other.objects);
|
|
|
|
}
|
|
|
|
|
2014-04-29 23:04:49 +00:00
|
|
|
Model::~Model()
|
|
|
|
{
|
2014-05-06 22:58:29 +00:00
|
|
|
this->clear_objects();
|
|
|
|
this->clear_materials();
|
2014-04-29 23:04:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-06 22:58:29 +00:00
|
|
|
ModelObject*
|
2014-05-09 12:24:35 +00:00
|
|
|
Model::add_object()
|
|
|
|
{
|
|
|
|
ModelObject* new_object = new ModelObject(this);
|
|
|
|
this->objects.push_back(new_object);
|
|
|
|
return new_object;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelObject*
|
|
|
|
Model::add_object(const ModelObject &other)
|
2014-04-29 23:04:49 +00:00
|
|
|
{
|
2014-05-09 12:24:35 +00:00
|
|
|
ModelObject* new_object = new ModelObject(this, other);
|
|
|
|
this->objects.push_back(new_object);
|
|
|
|
return new_object;
|
2014-04-29 23:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Model::delete_object(size_t idx)
|
|
|
|
{
|
|
|
|
ModelObjectPtrs::iterator i = this->objects.begin() + idx;
|
|
|
|
delete *i;
|
|
|
|
this->objects.erase(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-06 22:58:29 +00:00
|
|
|
Model::clear_objects()
|
2014-04-29 23:04:49 +00:00
|
|
|
{
|
2014-05-08 11:33:43 +00:00
|
|
|
// int instead of size_t because it can be -1 when vector is empty
|
|
|
|
for (int i = this->objects.size()-1; i >= 0; --i)
|
2014-05-06 22:58:29 +00:00
|
|
|
this->delete_object(i);
|
2014-04-29 23:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-06 22:58:29 +00:00
|
|
|
Model::delete_material(t_model_material_id material_id)
|
2014-04-29 23:04:49 +00:00
|
|
|
{
|
2014-05-06 22:58:29 +00:00
|
|
|
ModelMaterialMap::iterator i = this->materials.find(material_id);
|
|
|
|
if (i != this->materials.end()) {
|
2014-04-29 23:04:49 +00:00
|
|
|
delete i->second;
|
2014-05-06 22:58:29 +00:00
|
|
|
this->materials.erase(i);
|
2014-04-29 23:04:49 +00:00
|
|
|
}
|
2014-05-06 22:58:29 +00:00
|
|
|
}
|
2014-04-29 23:04:49 +00:00
|
|
|
|
2014-05-06 22:58:29 +00:00
|
|
|
void
|
|
|
|
Model::clear_materials()
|
|
|
|
{
|
|
|
|
while (!this->materials.empty())
|
|
|
|
this->delete_material( this->materials.begin()->first );
|
2014-04-29 23:04:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-09 12:24:35 +00:00
|
|
|
ModelMaterial*
|
|
|
|
Model::add_material(t_model_material_id material_id)
|
2014-04-29 23:04:49 +00:00
|
|
|
{
|
2014-05-09 12:24:35 +00:00
|
|
|
ModelMaterial* material = this->get_material(material_id);
|
|
|
|
if (material == NULL) {
|
|
|
|
material = this->materials[material_id] = new ModelMaterial(this);
|
|
|
|
}
|
|
|
|
return material;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelMaterial*
|
|
|
|
Model::add_material(t_model_material_id material_id, const ModelMaterial &other)
|
|
|
|
{
|
|
|
|
// delete existing material if any
|
|
|
|
ModelMaterial* material = this->get_material(material_id);
|
|
|
|
if (material != NULL) {
|
|
|
|
delete material;
|
|
|
|
}
|
2014-05-06 22:22:56 +00:00
|
|
|
|
2014-05-09 12:24:35 +00:00
|
|
|
// set new material
|
|
|
|
material = new ModelMaterial(this, other);
|
|
|
|
this->materials[material_id] = material;
|
|
|
|
return material;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelMaterial*
|
|
|
|
Model::get_material(t_model_material_id material_id)
|
|
|
|
{
|
|
|
|
ModelMaterialMap::iterator i = this->materials.find(material_id);
|
2014-04-29 23:04:49 +00:00
|
|
|
if (i == this->materials.end()) {
|
2014-05-09 12:24:35 +00:00
|
|
|
return NULL;
|
2014-04-29 23:04:49 +00:00
|
|
|
} else {
|
2014-05-09 12:24:35 +00:00
|
|
|
return i->second;
|
2014-04-29 23:04:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2014-05-06 22:22:56 +00:00
|
|
|
ModelMaterial::ModelMaterial(Model *model) : model(model) {}
|
2014-05-09 12:24:35 +00:00
|
|
|
ModelMaterial::ModelMaterial(Model *model, const ModelMaterial &other)
|
|
|
|
: model(model), config(other.config), attributes(other.attributes)
|
|
|
|
{}
|
2014-04-29 23:04:49 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
ModelMaterial::apply(const t_model_material_attributes &attributes)
|
|
|
|
{
|
|
|
|
this->attributes.insert(attributes.begin(), attributes.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef SLIC3RXS
|
|
|
|
REGISTER_CLASS(ModelMaterial, "Model::Material");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-05-09 12:24:35 +00:00
|
|
|
ModelObject::ModelObject(Model *model)
|
|
|
|
: model(model)
|
|
|
|
{}
|
2014-04-29 23:04:49 +00:00
|
|
|
|
2014-05-09 12:24:35 +00:00
|
|
|
ModelObject::ModelObject(Model *model, const ModelObject &other)
|
|
|
|
: model(model),
|
2014-07-12 09:20:57 +00:00
|
|
|
name(other.name),
|
2014-04-29 23:04:49 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
|
2014-05-09 12:24:35 +00:00
|
|
|
this->volumes.reserve(other.volumes.size());
|
|
|
|
for (ModelVolumePtrs::const_iterator i = other.volumes.begin(); i != other.volumes.end(); ++i)
|
|
|
|
this->add_volume(**i);
|
2014-04-29 23:04:49 +00:00
|
|
|
|
2014-05-09 12:24:35 +00:00
|
|
|
this->instances.reserve(other.instances.size());
|
|
|
|
for (ModelInstancePtrs::const_iterator i = other.instances.begin(); i != other.instances.end(); ++i)
|
|
|
|
this->add_instance(**i);
|
2014-04-29 23:04:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 11:33:43 +00:00
|
|
|
ModelObject& ModelObject::operator= (ModelObject other)
|
|
|
|
{
|
|
|
|
this->swap(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ModelObject::swap(ModelObject &other)
|
|
|
|
{
|
|
|
|
std::swap(this->input_file, other.input_file);
|
|
|
|
std::swap(this->instances, other.instances);
|
|
|
|
std::swap(this->volumes, other.volumes);
|
|
|
|
std::swap(this->config, other.config);
|
|
|
|
std::swap(this->layer_height_ranges, other.layer_height_ranges);
|
|
|
|
std::swap(this->origin_translation, other.origin_translation);
|
|
|
|
std::swap(this->_bounding_box, other._bounding_box);
|
|
|
|
std::swap(this->_bounding_box_valid, other._bounding_box_valid);
|
|
|
|
}
|
|
|
|
|
2014-04-29 23:04:49 +00:00
|
|
|
ModelObject::~ModelObject()
|
|
|
|
{
|
|
|
|
this->clear_volumes();
|
|
|
|
this->clear_instances();
|
|
|
|
}
|
|
|
|
|
2014-05-09 12:24:35 +00:00
|
|
|
ModelVolume*
|
|
|
|
ModelObject::add_volume(const TriangleMesh &mesh)
|
|
|
|
{
|
|
|
|
ModelVolume* v = new ModelVolume(this, mesh);
|
|
|
|
this->volumes.push_back(v);
|
|
|
|
this->invalidate_bounding_box();
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelVolume*
|
|
|
|
ModelObject::add_volume(const ModelVolume &other)
|
2014-04-29 23:04:49 +00:00
|
|
|
{
|
2014-05-09 12:24:35 +00:00
|
|
|
ModelVolume* v = new ModelVolume(this, other);
|
2014-04-29 23:04:49 +00:00
|
|
|
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()
|
|
|
|
{
|
2014-05-08 11:33:43 +00:00
|
|
|
// int instead of size_t because it can be -1 when vector is empty
|
|
|
|
for (int i = this->volumes.size()-1; i >= 0; --i)
|
2014-05-06 22:58:29 +00:00
|
|
|
this->delete_volume(i);
|
2014-04-29 23:04:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-09 12:24:35 +00:00
|
|
|
ModelInstance*
|
|
|
|
ModelObject::add_instance()
|
2014-04-29 23:04:49 +00:00
|
|
|
{
|
2014-05-09 12:24:35 +00:00
|
|
|
ModelInstance* i = new ModelInstance(this);
|
|
|
|
this->instances.push_back(i);
|
|
|
|
this->invalidate_bounding_box();
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelInstance*
|
|
|
|
ModelObject::add_instance(const ModelInstance &other)
|
|
|
|
{
|
|
|
|
ModelInstance* i = new ModelInstance(this, other);
|
2014-04-29 23:04:49 +00:00
|
|
|
this->instances.push_back(i);
|
|
|
|
this->invalidate_bounding_box();
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-06 22:58:29 +00:00
|
|
|
ModelObject::delete_instance(size_t idx)
|
2014-04-29 23:04:49 +00:00
|
|
|
{
|
2014-05-06 22:58:29 +00:00
|
|
|
ModelInstancePtrs::iterator i = this->instances.begin() + idx;
|
|
|
|
delete *i;
|
|
|
|
this->instances.erase(i);
|
2014-04-29 23:04:49 +00:00
|
|
|
this->invalidate_bounding_box();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-06 22:58:29 +00:00
|
|
|
ModelObject::delete_last_instance()
|
2014-04-29 23:04:49 +00:00
|
|
|
{
|
2014-05-06 22:58:29 +00:00
|
|
|
this->delete_instance(this->instances.size() - 1);
|
|
|
|
}
|
2014-04-29 23:04:49 +00:00
|
|
|
|
2014-05-06 22:58:29 +00:00
|
|
|
void
|
|
|
|
ModelObject::clear_instances()
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < this->instances.size(); ++i)
|
|
|
|
this->delete_instance(i);
|
2014-04-29 23:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ModelObject::invalidate_bounding_box()
|
|
|
|
{
|
|
|
|
this->_bounding_box_valid = false;
|
|
|
|
}
|
|
|
|
|
2014-08-03 18:33:16 +00:00
|
|
|
void
|
|
|
|
ModelObject::raw_mesh(TriangleMesh* mesh) const
|
|
|
|
{
|
|
|
|
for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) {
|
|
|
|
if ((*v)->modifier) continue;
|
|
|
|
mesh->merge((*v)->mesh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-29 23:04:49 +00:00
|
|
|
#ifdef SLIC3RXS
|
|
|
|
REGISTER_CLASS(ModelObject, "Model::Object");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-05-09 12:24:35 +00:00
|
|
|
ModelVolume::ModelVolume(ModelObject* object, const TriangleMesh &mesh)
|
|
|
|
: object(object), mesh(mesh), modifier(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
ModelVolume::ModelVolume(ModelObject* object, const ModelVolume &other)
|
2014-07-12 09:20:57 +00:00
|
|
|
: object(object), name(other.name), mesh(other.mesh), config(other.config),
|
|
|
|
modifier(other.modifier)
|
2014-05-10 14:59:17 +00:00
|
|
|
{
|
|
|
|
this->material_id(other.material_id());
|
|
|
|
}
|
|
|
|
|
|
|
|
t_model_material_id
|
|
|
|
ModelVolume::material_id() const
|
|
|
|
{
|
|
|
|
return this->_material_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ModelVolume::material_id(t_model_material_id material_id)
|
|
|
|
{
|
|
|
|
this->_material_id = material_id;
|
|
|
|
|
|
|
|
// ensure this->_material_id references an existing material
|
|
|
|
(void)this->object->get_model()->add_material(material_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelMaterial*
|
|
|
|
ModelVolume::material() const
|
|
|
|
{
|
|
|
|
return this->object->get_model()->get_material(this->_material_id);
|
|
|
|
}
|
2014-04-29 23:04:49 +00:00
|
|
|
|
2014-08-03 18:33:16 +00:00
|
|
|
ModelMaterial*
|
|
|
|
ModelVolume::assign_unique_material()
|
|
|
|
{
|
|
|
|
Model* model = this->get_object()->get_model();
|
|
|
|
|
|
|
|
this->_material_id = 1 + model->materials.size();
|
|
|
|
return model->add_material(this->_material_id);
|
|
|
|
}
|
|
|
|
|
2014-04-29 23:04:49 +00:00
|
|
|
#ifdef SLIC3RXS
|
|
|
|
REGISTER_CLASS(ModelVolume, "Model::Volume");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-05-09 12:24:35 +00:00
|
|
|
ModelInstance::ModelInstance(ModelObject *object)
|
|
|
|
: object(object), rotation(0), scaling_factor(1)
|
|
|
|
{}
|
|
|
|
|
|
|
|
ModelInstance::ModelInstance(ModelObject *object, const ModelInstance &other)
|
|
|
|
: object(object), rotation(other.rotation), scaling_factor(other.scaling_factor), offset(other.offset)
|
|
|
|
{}
|
2014-04-29 23:04:49 +00:00
|
|
|
|
2014-08-03 18:33:16 +00:00
|
|
|
void
|
|
|
|
ModelInstance::transform_mesh(TriangleMesh* mesh, bool dont_translate) const
|
|
|
|
{
|
|
|
|
mesh->rotate_z(this->rotation); // rotate around mesh origin
|
|
|
|
mesh->scale(this->scaling_factor); // scale around mesh origin
|
|
|
|
if (!dont_translate)
|
|
|
|
mesh->translate(this->offset.x, this->offset.y, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ModelInstance::transform_polygon(Polygon* polygon) const
|
|
|
|
{
|
|
|
|
polygon->rotate(this->rotation, Point(0,0)); // rotate around polygon origin
|
|
|
|
polygon->scale(this->scaling_factor); // scale around polygon origin
|
|
|
|
}
|
|
|
|
|
2014-04-29 23:04:49 +00:00
|
|
|
#ifdef SLIC3RXS
|
|
|
|
REGISTER_CLASS(ModelInstance, "Model::Instance");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|