From de554d577b0b4dc9a0562db41440abe5510dc4d2 Mon Sep 17 00:00:00 2001 From: supermerill Date: Thu, 7 Oct 2021 18:34:06 +0200 Subject: [PATCH] Fix wrong boundingbox (don't rotate the boudingbox, rotate the object!) supermerill/SuperSlicer#1612 --- src/libslic3r/GCode.cpp | 15 ++++++++------- src/libslic3r/GCode/SeamPlacer.cpp | 2 +- src/libslic3r/Model.cpp | 22 ++++++++++------------ src/libslic3r/Model.hpp | 3 +-- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index cf534732def..0ded4402b38 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -1218,19 +1218,20 @@ void GCode::_do_export(Print& print, FILE* file, ThumbnailsGeneratorCallback thu if (pos_dot != std::string::npos && pos_dot > 0) object_name = object_name.substr(0, pos_dot); //get bounding box for the instance - BoundingBoxf3 raw_bbox = print_object->model_object()->raw_mesh_bounding_box(); - BoundingBoxf3 m_bounding_box = print_instance.model_instance->transform_bounding_box(raw_bbox); + //BoundingBoxf3 raw_bbox = print_object->model_object()->raw_mesh_bounding_box(); + //BoundingBoxf3 bounding_box;// = print_instance.model_instance->transform_bounding_box(raw_bbox); + BoundingBoxf3 bounding_box = print_object->model_object()->instance_bounding_box(*print_instance.model_instance, false); if (global_bounding_box.size().norm() == 0) { - global_bounding_box = m_bounding_box; + global_bounding_box = bounding_box; } else { - global_bounding_box.merge(m_bounding_box); + global_bounding_box.merge(bounding_box); } if (this->config().gcode_label_objects) { _write_format(file, "; object:{\"name\":\"%s\",\"id\":\"%s id:%d copy %d\",\"object_center\":[%f,%f,%f],\"boundingbox_center\":[%f,%f,%f],\"boundingbox_size\":[%f,%f,%f]}\n", object_name.c_str(), print_object->model_object()->name.c_str(), this->m_ordered_objects.size() - 1, copy_id, - m_bounding_box.center().x(), m_bounding_box.center().y(), 0., - m_bounding_box.center().x(), m_bounding_box.center().y(), m_bounding_box.center().z(), - m_bounding_box.size().x(), m_bounding_box.size().y(), m_bounding_box.size().z() + bounding_box.center().x(), bounding_box.center().y(), 0., + bounding_box.center().x(), bounding_box.center().y(), bounding_box.center().z(), + bounding_box.size().x(), bounding_box.size().y(), bounding_box.size().z() ); } copy_id++; diff --git a/src/libslic3r/GCode/SeamPlacer.cpp b/src/libslic3r/GCode/SeamPlacer.cpp index 3425de4a12d..00a8800d33e 100644 --- a/src/libslic3r/GCode/SeamPlacer.cpp +++ b/src/libslic3r/GCode/SeamPlacer.cpp @@ -349,7 +349,7 @@ Point SeamPlacer::get_seam(const Layer& layer, SeamPosition seam_position, Point nearest = polygon.point_projection(xy_lambda); Vec3d polygon_3dpoint{ unscaled(nearest.x()), unscaled(nearest.y()), (double)layer.print_z }; double test_lambda_dist = (polygon_3dpoint - test_lambda_pos).norm(); - double sphere_radius = po->model_object()->instances.front()->transform_bounding_box(v->mesh().bounding_box(), true).size().x() / 2; + double sphere_radius = po->model_object()->instance_bounding_box(0, true).size().x() / 2; //if (test_lambda_dist > sphere_radius) // continue; diff --git a/src/libslic3r/Model.cpp b/src/libslic3r/Model.cpp index 11098c97253..6d460d52401 100644 --- a/src/libslic3r/Model.cpp +++ b/src/libslic3r/Model.cpp @@ -805,10 +805,9 @@ const BoundingBoxf3& ModelObject::bounding_box() const { if (! m_bounding_box_valid) { m_bounding_box_valid = true; - BoundingBoxf3 raw_bbox = this->raw_mesh_bounding_box(); m_bounding_box.reset(); - for (const ModelInstance *i : this->instances) - m_bounding_box.merge(i->transform_bounding_box(raw_bbox)); + for (size_t i = 0; i < instances.size(); ++i) + m_bounding_box.merge(instance_bounding_box(i, false)); } return m_bounding_box; } @@ -927,16 +926,20 @@ const BoundingBoxf3& ModelObject::raw_bounding_box() const return m_raw_bounding_box; } -// This returns an accurate snug bounding box of the transformed object instance, without the translation applied. +// This returns an accurate snug bounding box of the transformed object instance, with or without the translation applied. BoundingBoxf3 ModelObject::instance_bounding_box(size_t instance_idx, bool dont_translate) const +{ + return instance_bounding_box(*this->instances[instance_idx], dont_translate); +} +BoundingBoxf3 ModelObject::instance_bounding_box(const ModelInstance & instance, bool dont_translate) const { BoundingBoxf3 bb; - const Transform3d& inst_matrix = this->instances[instance_idx]->get_transformation().get_matrix(dont_translate); - for (ModelVolume *v : this->volumes) + const Transform3d& inst_matrix = instance.get_transformation().get_matrix(dont_translate); + for (ModelVolume* v : this->volumes) { if (v->is_model_part()) bb.merge(v->mesh().transformed_bounding_box(inst_matrix * v->get_matrix())); - } + } return bb; } @@ -1925,11 +1928,6 @@ BoundingBoxf3 ModelInstance::transform_mesh_bounding_box(const TriangleMesh& mes return bbox; } -BoundingBoxf3 ModelInstance::transform_bounding_box(const BoundingBoxf3 &bbox, bool dont_translate) const -{ - return bbox.transformed(get_matrix(dont_translate)); -} - Vec3d ModelInstance::transform_vector(const Vec3d& v, bool dont_translate) const { return get_matrix(dont_translate) * v; diff --git a/src/libslic3r/Model.hpp b/src/libslic3r/Model.hpp index 2190080c8fd..d3aee9ec166 100644 --- a/src/libslic3r/Model.hpp +++ b/src/libslic3r/Model.hpp @@ -306,6 +306,7 @@ class ModelObject final : public ObjectBase const BoundingBoxf3& raw_bounding_box() const; // A snug bounding box around the transformed non-modifier object volumes. BoundingBoxf3 instance_bounding_box(size_t instance_idx, bool dont_translate = false) const; + BoundingBoxf3 instance_bounding_box(const ModelInstance& instance, bool dont_translate = false) const; // A snug bounding box of non-transformed (non-rotated, non-scaled, non-translated) sum of non-modifier object volumes. const BoundingBoxf3& raw_mesh_bounding_box() const; // A snug bounding box of non-transformed (non-rotated, non-scaled, non-translated) sum of all object volumes. @@ -876,8 +877,6 @@ class ModelInstance final : public ObjectBase void transform_mesh(TriangleMesh* mesh, bool dont_translate = false) const; // Calculate a bounding box of a transformed mesh. To be called on an external mesh. BoundingBoxf3 transform_mesh_bounding_box(const TriangleMesh& mesh, bool dont_translate = false) const; - // Transform an external bounding box. - BoundingBoxf3 transform_bounding_box(const BoundingBoxf3 &bbox, bool dont_translate = false) const; // Transform an external vector. Vec3d transform_vector(const Vec3d& v, bool dont_translate = false) const; // To be called on an external polygon. It does not translate the polygon, only rotates and scales.