Refactoring

This commit is contained in:
Enrico Turri 2018-11-01 14:25:10 +01:00
parent c135f4a1f8
commit b8168d421c
6 changed files with 45 additions and 38 deletions

View file

@ -1828,7 +1828,7 @@ namespace Slic3r {
stream << " </" << COMPONENTS_TAG << ">\n"; stream << " </" << COMPONENTS_TAG << ">\n";
} }
Transform3d t = instance->world_matrix(); Transform3d t = instance->get_matrix();
build_items.emplace_back(instance_id, t); build_items.emplace_back(instance_id, t);
stream << " </" << OBJECT_TAG << ">\n"; stream << " </" << OBJECT_TAG << ">\n";

View file

@ -727,7 +727,7 @@ void ModelObject::translate_instance(size_t instance_idx, const Vec3d& vector)
invalidate_bounding_box(); invalidate_bounding_box();
} }
void ModelObject::translate(coordf_t x, coordf_t y, coordf_t z) void ModelObject::translate(double x, double y, double z)
{ {
for (ModelVolume *v : this->volumes) for (ModelVolume *v : this->volumes)
{ {
@ -918,7 +918,7 @@ double ModelObject::get_instance_min_z(size_t instance_idx) const
double min_z = DBL_MAX; double min_z = DBL_MAX;
ModelInstance* inst = instances[instance_idx]; ModelInstance* inst = instances[instance_idx];
const Transform3d& m = inst->world_matrix(true); const Transform3d& m = inst->get_matrix(true);
for (ModelVolume *v : volumes) for (ModelVolume *v : volumes)
{ {
@ -945,7 +945,7 @@ unsigned int ModelObject::check_instances_print_volume_state(const BoundingBoxf3
unsigned int inside_outside = 0; unsigned int inside_outside = 0;
for (const ModelVolume *vol : this->volumes) for (const ModelVolume *vol : this->volumes)
if (vol->is_model_part()) { if (vol->is_model_part()) {
BoundingBoxf3 bb = vol->get_convex_hull().transformed_bounding_box(model_instance->world_matrix()); BoundingBoxf3 bb = vol->get_convex_hull().transformed_bounding_box(model_instance->get_matrix());
if (print_volume.contains(bb)) if (print_volume.contains(bb))
inside_outside |= INSIDE; inside_outside |= INSIDE;
else if (print_volume.intersects(bb)) else if (print_volume.intersects(bb))
@ -1042,11 +1042,6 @@ const TriangleMesh& ModelVolume::get_convex_hull() const
return m_convex_hull; return m_convex_hull;
} }
TriangleMesh& ModelVolume::get_convex_hull()
{
return m_convex_hull;
}
ModelVolume::Type ModelVolume::type_from_string(const std::string &s) ModelVolume::Type ModelVolume::type_from_string(const std::string &s)
{ {
// Legacy support // Legacy support
@ -1113,6 +1108,17 @@ size_t ModelVolume::split(unsigned int max_extruders)
return idx; return idx;
} }
void ModelVolume::translate(double x, double y, double z)
{
translate(Vec3d(x, y, z));
}
void ModelVolume::translate(const Vec3d& displacement)
{
mesh.translate((float)displacement(0), (float)displacement(1), (float)displacement(2));
m_convex_hull.translate((float)displacement(0), (float)displacement(1), (float)displacement(2));
}
#if !ENABLE_MODELVOLUME_TRANSFORM #if !ENABLE_MODELVOLUME_TRANSFORM
void ModelInstance::set_rotation(const Vec3d& rotation) void ModelInstance::set_rotation(const Vec3d& rotation)
{ {
@ -1168,14 +1174,14 @@ void ModelInstance::set_mirror(Axis axis, double mirror)
void ModelInstance::transform_mesh(TriangleMesh* mesh, bool dont_translate) const void ModelInstance::transform_mesh(TriangleMesh* mesh, bool dont_translate) const
{ {
mesh->transform(world_matrix(dont_translate).cast<float>()); mesh->transform(get_matrix(dont_translate).cast<float>());
} }
BoundingBoxf3 ModelInstance::transform_mesh_bounding_box(const TriangleMesh* mesh, bool dont_translate) const BoundingBoxf3 ModelInstance::transform_mesh_bounding_box(const TriangleMesh* mesh, bool dont_translate) const
{ {
// Rotate around mesh origin. // Rotate around mesh origin.
TriangleMesh copy(*mesh); TriangleMesh copy(*mesh);
copy.transform(world_matrix(true, false, true, true).cast<float>()); copy.transform(get_matrix(true, false, true, true).cast<float>());
BoundingBoxf3 bbox = copy.bounding_box(); BoundingBoxf3 bbox = copy.bounding_box();
if (!empty(bbox)) { if (!empty(bbox)) {
@ -1212,12 +1218,12 @@ BoundingBoxf3 ModelInstance::transform_mesh_bounding_box(const TriangleMesh* mes
BoundingBoxf3 ModelInstance::transform_bounding_box(const BoundingBoxf3 &bbox, bool dont_translate) const BoundingBoxf3 ModelInstance::transform_bounding_box(const BoundingBoxf3 &bbox, bool dont_translate) const
{ {
return bbox.transformed(world_matrix(dont_translate)); return bbox.transformed(get_matrix(dont_translate));
} }
Vec3d ModelInstance::transform_vector(const Vec3d& v, bool dont_translate) const Vec3d ModelInstance::transform_vector(const Vec3d& v, bool dont_translate) const
{ {
return world_matrix(dont_translate) * v; return get_matrix(dont_translate) * v;
} }
void ModelInstance::transform_polygon(Polygon* polygon) const void ModelInstance::transform_polygon(Polygon* polygon) const

View file

@ -160,7 +160,7 @@ public:
void translate_instances(const Vec3d& vector); void translate_instances(const Vec3d& vector);
void translate_instance(size_t instance_idx, const Vec3d& vector); void translate_instance(size_t instance_idx, const Vec3d& vector);
void translate(const Vec3d &vector) { this->translate(vector(0), vector(1), vector(2)); } void translate(const Vec3d &vector) { this->translate(vector(0), vector(1), vector(2)); }
void translate(coordf_t x, coordf_t y, coordf_t z); void translate(double x, double y, double z);
void scale(const Vec3d &versor); void scale(const Vec3d &versor);
void scale(const double s) { this->scale(Vec3d(s, s, s)); } void scale(const double s) { this->scale(Vec3d(s, s, s)); }
void rotate(float angle, const Axis &axis); void rotate(float angle, const Axis &axis);
@ -210,13 +210,6 @@ class ModelVolume : public ModelBase
{ {
friend class ModelObject; friend class ModelObject;
// The convex hull of this model's mesh.
TriangleMesh m_convex_hull;
#if ENABLE_MODELVOLUME_TRANSFORM
Geometry::Transformation m_transformation;
#endif // ENABLE_MODELVOLUME_TRANSFORM
public: public:
std::string name; std::string name;
// The triangular model. // The triangular model.
@ -253,18 +246,22 @@ public:
// Return the number of volumes created from this one. // Return the number of volumes created from this one.
// This is useful to assign different materials to different volumes of an object. // This is useful to assign different materials to different volumes of an object.
size_t split(unsigned int max_extruders); size_t split(unsigned int max_extruders);
void translate(double x, double y, double z);
void translate(const Vec3d& displacement);
ModelMaterial* assign_unique_material(); ModelMaterial* assign_unique_material();
void calculate_convex_hull(); void calculate_convex_hull();
const TriangleMesh& get_convex_hull() const; const TriangleMesh& get_convex_hull() const;
TriangleMesh& get_convex_hull();
// Helpers for loading / storing into AMF / 3MF files. // Helpers for loading / storing into AMF / 3MF files.
static Type type_from_string(const std::string &s); static Type type_from_string(const std::string &s);
static std::string type_to_string(const Type t); static std::string type_to_string(const Type t);
#if ENABLE_MODELVOLUME_TRANSFORM #if ENABLE_MODELVOLUME_TRANSFORM
const Geometry::Transformation& get_transformation() const { return m_transformation; }
void set_transformation(const Geometry::Transformation& transformation) { m_transformation = transformation; }
const Vec3d& get_offset() const { return m_transformation.get_offset(); } const Vec3d& get_offset() const { return m_transformation.get_offset(); }
double get_offset(Axis axis) const { return m_transformation.get_offset(axis); } double get_offset(Axis axis) const { return m_transformation.get_offset(axis); }
@ -288,6 +285,8 @@ public:
void set_mirror(const Vec3d& mirror) { m_transformation.set_mirror(mirror); } void set_mirror(const Vec3d& mirror) { m_transformation.set_mirror(mirror); }
void set_mirror(Axis axis, double mirror) { m_transformation.set_mirror(axis, mirror); } void set_mirror(Axis axis, double mirror) { m_transformation.set_mirror(axis, mirror); }
const Transform3d& get_matrix(bool dont_translate = false, bool dont_rotate = false, bool dont_scale = false, bool dont_mirror = false) const { return m_transformation.get_matrix(dont_translate, dont_rotate, dont_scale, dont_mirror); }
#endif // ENABLE_MODELVOLUME_TRANSFORM #endif // ENABLE_MODELVOLUME_TRANSFORM
private: private:
@ -296,6 +295,11 @@ private:
// Is it an object to be printed, or a modifier volume? // Is it an object to be printed, or a modifier volume?
Type m_type; Type m_type;
t_model_material_id m_material_id; t_model_material_id m_material_id;
// The convex hull of this model's mesh.
TriangleMesh m_convex_hull;
#if ENABLE_MODELVOLUME_TRANSFORM
Geometry::Transformation m_transformation;
#endif // ENABLE_MODELVOLUME_TRANSFORM
ModelVolume(ModelObject *object, const TriangleMesh &mesh) : mesh(mesh), m_type(MODEL_PART), object(object) ModelVolume(ModelObject *object, const TriangleMesh &mesh) : mesh(mesh), m_type(MODEL_PART), object(object)
{ {
@ -419,9 +423,9 @@ public:
void transform_polygon(Polygon* polygon) const; void transform_polygon(Polygon* polygon) const;
#if ENABLE_MODELVOLUME_TRANSFORM #if ENABLE_MODELVOLUME_TRANSFORM
const Transform3d& world_matrix(bool dont_translate = false, bool dont_rotate = false, bool dont_scale = false, bool dont_mirror = false) const { return m_transformation.get_matrix(dont_translate, dont_rotate, dont_scale, dont_mirror); } const Transform3d& get_matrix(bool dont_translate = false, bool dont_rotate = false, bool dont_scale = false, bool dont_mirror = false) const { return m_transformation.get_matrix(dont_translate, dont_rotate, dont_scale, dont_mirror); }
#else #else
Transform3d world_matrix(bool dont_translate = false, bool dont_rotate = false, bool dont_scale = false, bool dont_mirror = false) const; Transform3d get_matrix(bool dont_translate = false, bool dont_rotate = false, bool dont_scale = false, bool dont_mirror = false) const;
#endif // ENABLE_MODELVOLUME_TRANSFORM #endif // ENABLE_MODELVOLUME_TRANSFORM
bool is_printable() const { return print_volume_state == PVS_Inside; } bool is_printable() const { return print_volume_state == PVS_Inside; }

View file

@ -411,7 +411,7 @@ void Print::add_model_object(ModelObject* model_object, int idx)
// Set the transformation matrix without translation from the first instance. // Set the transformation matrix without translation from the first instance.
if (! model_object->instances.empty()) if (! model_object->instances.empty())
object->set_trafo(model_object->instances.front()->world_matrix(true)); object->set_trafo(model_object->instances.front()->get_matrix(true));
size_t volume_id = 0; size_t volume_id = 0;
for (const ModelVolume *volume : model_object->volumes) { for (const ModelVolume *volume : model_object->volumes) {
@ -686,7 +686,7 @@ static std::vector<PrintInstances> print_objects_from_model_object(const ModelOb
trafo.copies.assign(1, Point()); trafo.copies.assign(1, Point());
for (ModelInstance *model_instance : model_object.instances) for (ModelInstance *model_instance : model_object.instances)
if (model_instance->is_printable()) { if (model_instance->is_printable()) {
trafo.trafo = model_instance->world_matrix(); trafo.trafo = model_instance->get_matrix();
// Set the Z axis of the transformation. // Set the Z axis of the transformation.
trafo.copies.front() = Point::new_scale(trafo.trafo.data()[3], trafo.trafo.data()[7]); trafo.copies.front() = Point::new_scale(trafo.trafo.data()[3], trafo.trafo.data()[7]);
trafo.trafo.data()[3] = 0; trafo.trafo.data()[3] = 0;

View file

@ -1184,7 +1184,7 @@ void GLGizmoFlatten::on_render(const GLCanvas3D::Selection& selection) const
int instance_idx = selection.get_instance_idx(); int instance_idx = selection.get_instance_idx();
if ((instance_idx != -1) && (m_model_object != nullptr)) if ((instance_idx != -1) && (m_model_object != nullptr))
{ {
Transform3d m = m_model_object->instances[instance_idx]->world_matrix(); Transform3d m = m_model_object->instances[instance_idx]->get_matrix();
m.pretranslate(dragged_offset); m.pretranslate(dragged_offset);
::glPushMatrix(); ::glPushMatrix();
::glMultMatrixd(m.data()); ::glMultMatrixd(m.data());
@ -1214,7 +1214,7 @@ void GLGizmoFlatten::on_render_for_picking(const GLCanvas3D::Selection& selectio
if ((instance_idx != -1) && (m_model_object != nullptr)) if ((instance_idx != -1) && (m_model_object != nullptr))
{ {
::glPushMatrix(); ::glPushMatrix();
::glMultMatrixd(m_model_object->instances[instance_idx]->world_matrix().data()); ::glMultMatrixd(m_model_object->instances[instance_idx]->get_matrix().data());
::glBegin(GL_POLYGON); ::glBegin(GL_POLYGON);
for (const Vec3d& vertex : m_planes[i].vertices) for (const Vec3d& vertex : m_planes[i].vertices)
{ {
@ -1439,7 +1439,7 @@ bool GLGizmoFlatten::is_plane_update_necessary() const
Vec3d GLGizmoFlatten::get_flattening_rotation() const Vec3d GLGizmoFlatten::get_flattening_rotation() const
{ {
// calculates the rotations in model space, taking in account the scaling factors // calculates the rotations in model space, taking in account the scaling factors
Eigen::Matrix<double, 3, 3, Eigen::DontAlign> m = m_model_object->instances.front()->world_matrix(true, true).matrix().block(0, 0, 3, 3).inverse().transpose(); Eigen::Matrix<double, 3, 3, Eigen::DontAlign> m = m_model_object->instances.front()->get_matrix(true, true).matrix().block(0, 0, 3, 3).inverse().transpose();
Eigen::Quaterniond q; Eigen::Quaterniond q;
Vec3d angles = Geometry::extract_euler_angles(q.setFromTwoVectors(m * m_normal, -Vec3d::UnitZ()).toRotationMatrix()); Vec3d angles = Geometry::extract_euler_angles(q.setFromTwoVectors(m * m_normal, -Vec3d::UnitZ()).toRotationMatrix());
m_normal = Vec3d::Zero(); m_normal = Vec3d::Zero();
@ -1478,7 +1478,7 @@ void GLGizmoSlaSupports::set_model_object_ptr(ModelObject* model_object)
{ {
m_starting_center = Vec3d::Zero(); m_starting_center = Vec3d::Zero();
m_model_object = model_object; m_model_object = model_object;
m_model_object_matrix = model_object->instances.front()->world_matrix(); m_model_object_matrix = model_object->instances.front()->get_matrix();
if (is_mesh_update_necessary()) if (is_mesh_update_necessary())
update_mesh(); update_mesh();
} }
@ -1560,7 +1560,7 @@ bool GLGizmoSlaSupports::is_mesh_update_necessary() const
if (m_state != On || !m_model_object || m_model_object->instances.empty()) if (m_state != On || !m_model_object || m_model_object->instances.empty())
return false; return false;
if ((m_model_object->instances.front()->world_matrix() * m_source_data.matrix.inverse() * Vec3d(1., 1., 1.) - Vec3d(1., 1., 1.)).norm() > 0.001 ) if ((m_model_object->instances.front()->get_matrix() * m_source_data.matrix.inverse() * Vec3d(1., 1., 1.) - Vec3d(1., 1., 1.)).norm() > 0.001)
return true; return true;
// following should detect direct mesh changes (can be removed after the mesh is made completely immutable): // following should detect direct mesh changes (can be removed after the mesh is made completely immutable):
@ -1588,7 +1588,7 @@ void GLGizmoSlaSupports::update_mesh()
F(i, 1) = 3*i+1; F(i, 1) = 3*i+1;
F(i, 2) = 3*i+2; F(i, 2) = 3*i+2;
} }
m_source_data.matrix = m_model_object->instances.front()->world_matrix(); m_source_data.matrix = m_model_object->instances.front()->get_matrix();
const float* first_vertex = m_model_object->volumes.front()->get_convex_hull().first_vertex(); const float* first_vertex = m_model_object->volumes.front()->get_convex_hull().first_vertex();
m_source_data.mesh_first_point = Vec3d((double)first_vertex[0], (double)first_vertex[1], (double)first_vertex[2]); m_source_data.mesh_first_point = Vec3d((double)first_vertex[0], (double)first_vertex[1], (double)first_vertex[2]);
// we'll now reload Grabbers (selection might have changed): // we'll now reload Grabbers (selection might have changed):
@ -1629,7 +1629,7 @@ Vec3f GLGizmoSlaSupports::unproject_on_mesh(const Vec2d& mouse_pos)
const Vec3f& b = m_V.row(m_F(fid, 1)); const Vec3f& b = m_V.row(m_F(fid, 1));
const Vec3f& c = m_V.row(m_F(fid, 2)); const Vec3f& c = m_V.row(m_F(fid, 2));
Vec3f point = bc(0)*a + bc(1)*b + bc(2)*c; Vec3f point = bc(0)*a + bc(1)*b + bc(2)*c;
return m_model_object->instances.front()->world_matrix().inverse().cast<float>() * point; return m_model_object->instances.front()->get_matrix().inverse().cast<float>() * point;
} }
void GLGizmoSlaSupports::clicked_on_object(const Vec2d& mouse_position) void GLGizmoSlaSupports::clicked_on_object(const Vec2d& mouse_position)

View file

@ -729,10 +729,7 @@ void ObjectList::load_part( ModelObject* model_object,
part_names.Add(new_volume->name); part_names.Add(new_volume->name);
if (delta != Vec3d::Zero()) if (delta != Vec3d::Zero())
{ new_volume->translate(delta);
new_volume->mesh.translate((float)delta(0), (float)delta(1), (float)delta(2));
new_volume->get_convex_hull().translate((float)delta(0), (float)delta(1), (float)delta(2));
}
// set a default extruder value, since user can't add it manually // set a default extruder value, since user can't add it manually
new_volume->config.set_key_value("extruder", new ConfigOptionInt(0)); new_volume->config.set_key_value("extruder", new ConfigOptionInt(0));