Skip to content
Permalink
Browse files

created a bounding hull for each object

  • Loading branch information...
3bhady committed Jun 7, 2019
1 parent a0a2055 commit ce678e817fb82642c68da1bd545a3bbddcee8325
Showing with 100 additions and 0 deletions.
  1. +23 −0 xs/src/libslic3r/BoundingBox.cpp
  2. +11 −0 xs/src/libslic3r/BoundingBox.hpp
  3. +59 −0 xs/src/libslic3r/Model.cpp
  4. +7 −0 xs/src/libslic3r/Model.hpp
@@ -1,4 +1,5 @@
#include "BoundingBox.hpp"
#include "Geometry.hpp"
#include <algorithm>

namespace Slic3r {
@@ -268,4 +269,26 @@ BoundingBoxBase<PointClass>::contains(const PointClass &point) const
template bool BoundingBoxBase<Point>::contains(const Point &point) const;
template bool BoundingBoxBase<Pointf>::contains(const Pointf &point) const;

//BoundingHull::BoundingHull(const std::vector<Pointf> &pointsf): BoundingBoxBase<Pointf>(pointsf){
// Points points = to_points<Pointf>(pointsf);
// for(int i = 0 ;i <points.size(); i++){
// printf("%d\n",points[i].x );
// }
//}
void BoundingHull::merge(std::vector<Pointf> points){
this->original_points.insert(this->original_points.end(), points.begin(), points.end());
}
void BoundingHull::merge(BoundingHull bh){
this->original_points.insert(this->original_points.end(), bh.original_points.begin(), bh.original_points.end());
}
void BoundingHull::update_hull() {
Points points;
for(int i = 0; i< this->original_points.size(); i++){
points.push_back(Point(ceil(this->original_points[i].x),ceil(this->original_points[i].y)));
}
this->hull = Slic3r::Geometry::convex_hull(points);
printf("hell yeah!");
}
}


@@ -104,7 +104,18 @@ inline bool operator!=(const BoundingBoxBase<VT> &bb1, const BoundingBoxBase<VT>
{
return !(bb1 == bb2);
}
class BoundingHull : public BoundingBoxBase<Pointf> {
public:
Polygon hull;
std::vector<Pointf> original_points;
BoundingHull() : BoundingBoxBase<Pointf>() {};
// BoundingHull(const std::vector<Pointf> &pointsf);
void merge(std::vector<Pointf> points);
void merge(BoundingHull bh);
void update_hull();

};
}


#endif
@@ -302,6 +302,12 @@ Model::_arrange(const Pointfs &sizes, coordf_t dist, const BoundingBoxf* bb, Poi
bool
Model::arrange_objects(coordf_t dist, const BoundingBoxf* bb)
{
std::vector<BoundingHull> shapes;
for(const ModelObject* o : this->objects){
for(size_t i = 0; i< o->instances.size(); ++i){
shapes.push_back(o->instance_bounding_hull(i));
}
}
// get the (transformed) size of each instance so that we take
// into account their different transformations when packing
Pointfs instance_sizes;
@@ -642,6 +648,17 @@ ModelObject::raw_bounding_box() const
return bb;
}

// TODO: mhady: create instance_bounding_hull here to reterun specific bounding hull for each instance
BoundingHull
ModelObject::instance_bounding_hull(size_t instance_idx) const{
BoundingHull bh;
for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) {
if ((*v)->modifier) continue;
bh.merge(this->instances[instance_idx]->transform_mesh_bounding_hull(&(*v)->mesh));
bh.update_hull();
}
return bh;
}
// this returns the bounding box of the *transformed* given instance
BoundingBoxf3
ModelObject::instance_bounding_box(size_t instance_idx) const
@@ -1093,6 +1110,48 @@ ModelInstance::transform_mesh(TriangleMesh* mesh, bool dont_translate) const

}

//mhady: TODO: create a transform_mesh_bounding_hull here
std::vector<Pointf> ModelInstance::transform_mesh_bounding_hull(const TriangleMesh* mesh ) const{
// rotate around mesh origin
double c = cos(this->rotation);
double s = sin(this->rotation);
double cx = cos(this->x_rotation);
double sx = sin(this->x_rotation);
double cy = cos(this->y_rotation);
double sy = sin(this->y_rotation);
std::vector<Pointf> points;
for (int i = 0; i < mesh->stl.stats.number_of_facets; ++ i) {
const stl_facet &facet = mesh->stl.facet_start[i];
for (int j = 0; j < 3; ++ j) {
stl_vertex v = facet.vertex[j];
double xold = v.x;
double yold = v.y;
double zold = v.z;
// Rotation around x axis.
v.z = float(sx * yold + cx * zold);
yold = v.y = float(cx * yold - sx * zold);
zold = v.z;
// Rotation around y axis.
v.x = float(cy * xold + sy * zold);
v.z = float(-sy * xold + cy * zold);
xold = v.x;
// Rotation around z axis.
v.x = float(c * xold - s * yold);
v.y = float(s * xold + c * yold);
v.x *= float(this->scaling_factor * this->scaling_vector.x);
v.y *= float(this->scaling_factor * this->scaling_vector.y);
v.z *= float(this->scaling_factor * this->scaling_vector.z);
// if (!dont_translate) {
v.x += this->offset.x;
v.y += this->offset.y;
if (this->y_rotation || this->x_rotation)
v.z += -(mesh->stl.stats.min.z);
// }
points.push_back(Pointf(v.x,v.y));
}
}
return points;
}
BoundingBoxf3 ModelInstance::transform_mesh_bounding_box(const TriangleMesh* mesh, bool dont_translate) const
{
// rotate around mesh origin
@@ -132,6 +132,8 @@ class Model
/// \return BoundingBoxf3 a bounding box object.
BoundingBoxf3 bounding_box() const;

BoundingHull bounding_hull() const;

/// Repair the ModelObjects of the current Model.
/// This function calls repair function on each TriangleMesh of each model object volume
void repair();
@@ -449,6 +451,8 @@ class ModelObject

/// Destructor
~ModelObject();

BoundingHull instance_bounding_hull(size_t instance_idx) const;
};

/// An object STL, or a modifier volume, over which a different set of parameters shall be applied.
@@ -551,6 +555,9 @@ class ModelInstance
/// \return BoundingBoxf3 the bounding box after transformation
BoundingBoxf3 transform_mesh_bounding_box(const TriangleMesh* mesh, bool dont_translate = false) const;


std::vector<Pointf> transform_mesh_bounding_hull(const TriangleMesh *mesh) const;

/// Transform an external bounding box.
/// \param bbox BoundingBoxf3 the bounding box to be transformed
/// \param dont_translate bool whether to translate the bounding box or not

0 comments on commit ce678e8

Please sign in to comment.
You can’t perform that action at this time.