Skip to content

Commit

Permalink
Fix wrong boundingbox (don't rotate the boudingbox, rotate the object!)
Browse files Browse the repository at this point in the history
  • Loading branch information
supermerill committed Oct 7, 2021
1 parent 5abd2fc commit de554d5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
15 changes: 8 additions & 7 deletions src/libslic3r/GCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
2 changes: 1 addition & 1 deletion src/libslic3r/GCode/SeamPlacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
22 changes: 10 additions & 12 deletions src/libslic3r/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/libslic3r/Model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit de554d5

Please sign in to comment.