mirror of
https://git.mirrors.martin98.com/https://github.com/prusa3d/PrusaSlicer.git
synced 2025-08-16 16:05:54 +08:00
Search for suitable rotation when arranging items larger than the bed
This commit is contained in:
parent
d3233d66fb
commit
49c6ce76d0
@ -90,12 +90,29 @@ inline R rectarea(const Pt& w, const std::array<It, 4>& rect)
|
|||||||
return rectarea<Pt, Unit, R>(w, *rect[0], *rect[1], *rect[2], *rect[3]);
|
return rectarea<Pt, Unit, R>(w, *rect[0], *rect[1], *rect[2], *rect[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class Pt, class Unit = TCompute<Pt>, class R = TCompute<Pt>>
|
||||||
|
inline R rectarea(const Pt& w, // the axis
|
||||||
|
const Unit& a,
|
||||||
|
const Unit& b)
|
||||||
|
{
|
||||||
|
R m = R(a) / pl::magnsq<Pt, Unit>(w);
|
||||||
|
m = m * b;
|
||||||
|
return m;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class R, class Pt, class Unit>
|
||||||
|
inline R rectarea(const RotatedBox<Pt, Unit> &rb)
|
||||||
|
{
|
||||||
|
return rectarea<Pt, Unit, R>(rb.axis(), rb.bottom_extent(), rb.right_extent());
|
||||||
|
};
|
||||||
|
|
||||||
// This function is only applicable to counter-clockwise oriented convex
|
// This function is only applicable to counter-clockwise oriented convex
|
||||||
// polygons where only two points can be collinear witch each other.
|
// polygons where only two points can be collinear witch each other.
|
||||||
template <class RawShape,
|
template <class RawShape,
|
||||||
class Unit = TCompute<RawShape>,
|
class Unit = TCompute<RawShape>,
|
||||||
class Ratio = TCompute<RawShape>>
|
class Ratio = TCompute<RawShape>,
|
||||||
RotatedBox<TPoint<RawShape>, Unit> minAreaBoundingBox(const RawShape& sh)
|
class VisitFn>
|
||||||
|
void rotcalipers(const RawShape& sh, VisitFn &&visitfn)
|
||||||
{
|
{
|
||||||
using Point = TPoint<RawShape>;
|
using Point = TPoint<RawShape>;
|
||||||
using Iterator = typename TContour<RawShape>::const_iterator;
|
using Iterator = typename TContour<RawShape>::const_iterator;
|
||||||
@ -106,9 +123,9 @@ RotatedBox<TPoint<RawShape>, Unit> minAreaBoundingBox(const RawShape& sh)
|
|||||||
auto last = std::prev(sl::cend(sh));
|
auto last = std::prev(sl::cend(sh));
|
||||||
|
|
||||||
// Check conditions and return undefined box if input is not sane.
|
// Check conditions and return undefined box if input is not sane.
|
||||||
if(last == first) return {};
|
if(last == first) return;
|
||||||
if(getX(*first) == getX(*last) && getY(*first) == getY(*last)) --last;
|
if(getX(*first) == getX(*last) && getY(*first) == getY(*last)) --last;
|
||||||
if(last - first < 2) return {};
|
if(last - first < 2) return;
|
||||||
|
|
||||||
RawShape shcpy; // empty at this point
|
RawShape shcpy; // empty at this point
|
||||||
{
|
{
|
||||||
@ -129,7 +146,7 @@ RotatedBox<TPoint<RawShape>, Unit> minAreaBoundingBox(const RawShape& sh)
|
|||||||
|
|
||||||
// Cyclic iterator increment
|
// Cyclic iterator increment
|
||||||
auto inc = [&first, &last](Iterator& it) {
|
auto inc = [&first, &last](Iterator& it) {
|
||||||
if(it == last) it = first; else ++it;
|
if(it == last) it = first; else ++it;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Cyclic previous iterator
|
// Cyclic previous iterator
|
||||||
@ -168,7 +185,7 @@ RotatedBox<TPoint<RawShape>, Unit> minAreaBoundingBox(const RawShape& sh)
|
|||||||
// the smallest rotation is selected and the supporting vertices are
|
// the smallest rotation is selected and the supporting vertices are
|
||||||
// returned in the 'rect' argument.
|
// returned in the 'rect' argument.
|
||||||
auto update = [&next, &inc]
|
auto update = [&next, &inc]
|
||||||
(const Point& w, std::array<Iterator, 4>& rect)
|
(const Point& w, std::array<Iterator, 4>& rect)
|
||||||
{
|
{
|
||||||
Iterator B = rect[0], Bn = next(B);
|
Iterator B = rect[0], Bn = next(B);
|
||||||
Iterator R = rect[1], Rn = next(R);
|
Iterator R = rect[1], Rn = next(R);
|
||||||
@ -219,12 +236,14 @@ RotatedBox<TPoint<RawShape>, Unit> minAreaBoundingBox(const RawShape& sh)
|
|||||||
};
|
};
|
||||||
|
|
||||||
Point w(1, 0);
|
Point w(1, 0);
|
||||||
Point w_min = w;
|
|
||||||
Ratio minarea((Unit(getX(*maxX)) - getX(*minX)) *
|
|
||||||
(Unit(getY(*maxY)) - getY(*minY)));
|
|
||||||
|
|
||||||
std::array<Iterator, 4> rect = {minY, maxX, maxY, minX};
|
std::array<Iterator, 4> rect = {minY, maxX, maxY, minX};
|
||||||
std::array<Iterator, 4> minrect = rect;
|
|
||||||
|
{
|
||||||
|
Unit a = dot<Point, Unit>(w, *rect[1] - *rect[3]);
|
||||||
|
Unit b = dot<Point, Unit>(-perp(w), *rect[2] - *rect[0]);
|
||||||
|
if (!visitfn(RotatedBox<Point, Unit>{w, a, b}))
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// An edge might be examined twice in which case the algorithm terminates.
|
// An edge might be examined twice in which case the algorithm terminates.
|
||||||
size_t c = 0, count = last - first + 1;
|
size_t c = 0, count = last - first + 1;
|
||||||
@ -243,18 +262,35 @@ RotatedBox<TPoint<RawShape>, Unit> minAreaBoundingBox(const RawShape& sh)
|
|||||||
// get the unnormalized direction vector
|
// get the unnormalized direction vector
|
||||||
w = *rect[0] - *prev(rect[0]);
|
w = *rect[0] - *prev(rect[0]);
|
||||||
|
|
||||||
// get the area of the rotated rectangle
|
Unit a = dot<Point, Unit>(w, *rect[1] - *rect[3]);
|
||||||
Ratio rarea = rectarea<Point, Unit, Ratio>(w, rect);
|
Unit b = dot<Point, Unit>(-perp(w), *rect[2] - *rect[0]);
|
||||||
|
if (!visitfn(RotatedBox<Point, Unit>{w, a, b}))
|
||||||
// Update min area and the direction of the min bounding box;
|
break;
|
||||||
if(rarea <= minarea) { w_min = w; minarea = rarea; minrect = rect; }
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Unit a = dot<Point, Unit>(w_min, *minrect[1] - *minrect[3]);
|
// This function is only applicable to counter-clockwise oriented convex
|
||||||
Unit b = dot<Point, Unit>(-perp(w_min), *minrect[2] - *minrect[0]);
|
// polygons where only two points can be collinear witch each other.
|
||||||
RotatedBox<Point, Unit> bb(w_min, a, b);
|
template <class S,
|
||||||
|
class Unit = TCompute<S>,
|
||||||
|
class Ratio = TCompute<S>>
|
||||||
|
RotatedBox<TPoint<S>, Unit> minAreaBoundingBox(const S& sh)
|
||||||
|
{
|
||||||
|
RotatedBox<TPoint<S>, Unit> minbox;
|
||||||
|
Ratio minarea = std::numeric_limits<Unit>::max();
|
||||||
|
auto minfn = [&minarea, &minbox](const RotatedBox<TPoint<S>, Unit> &rbox){
|
||||||
|
Ratio area = rectarea<Ratio>(rbox);
|
||||||
|
if (area <= minarea) {
|
||||||
|
minarea = area;
|
||||||
|
minbox = rbox;
|
||||||
|
}
|
||||||
|
|
||||||
return bb;
|
return true; // continue search
|
||||||
|
};
|
||||||
|
|
||||||
|
rotcalipers<S, Unit, Ratio>(sh, minfn);
|
||||||
|
|
||||||
|
return minbox;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class RawShape> Radians minAreaBoundingBoxRotation(const RawShape& sh)
|
template <class RawShape> Radians minAreaBoundingBoxRotation(const RawShape& sh)
|
||||||
@ -262,7 +298,75 @@ template <class RawShape> Radians minAreaBoundingBoxRotation(const RawShape& sh)
|
|||||||
return minAreaBoundingBox(sh).angleToX();
|
return minAreaBoundingBox(sh).angleToX();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to find a rotation for a shape that makes it fit into a box.
|
||||||
|
//
|
||||||
|
// The method is based on finding a pair of rotations from the rotating calipers
|
||||||
|
// algorithm such that the aspect ratio is changing from being smaller than
|
||||||
|
// that of the target to being bigger or vice versa. So that the correct
|
||||||
|
// AR is somewhere between the obtained pair of angles. Then bisecting that
|
||||||
|
// interval is sufficient to find the correct angle.
|
||||||
|
//
|
||||||
|
// The argument eps is the absolute error limit for the searched angle interval.
|
||||||
|
template<class S, class Unit = TCompute<S>, class Ratio = TCompute<S>>
|
||||||
|
Radians fitIntoBoxRotation(const S &shape, const _Box<TPoint<S>> &box, Radians eps = 1e-4)
|
||||||
|
{
|
||||||
|
constexpr auto get_aspect_r = [](const auto &b) -> double {
|
||||||
|
return double(b.width()) / b.height();
|
||||||
|
};
|
||||||
|
|
||||||
|
auto aspect_r = get_aspect_r(box);
|
||||||
|
|
||||||
|
RotatedBox<TPoint<S>, Unit> prev_rbox;
|
||||||
|
Radians a_from = 0., a_to = 0.;
|
||||||
|
auto visitfn = [&](const RotatedBox<TPoint<S>, Unit> &rbox) {
|
||||||
|
bool lower_prev = get_aspect_r(prev_rbox) < aspect_r;
|
||||||
|
bool lower_current = get_aspect_r(rbox) < aspect_r;
|
||||||
|
|
||||||
|
if (lower_prev != lower_current) {
|
||||||
|
a_from = prev_rbox.angleToX();
|
||||||
|
a_to = rbox.angleToX();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
rotcalipers<S, Unit, Ratio>(shape, visitfn);
|
||||||
|
|
||||||
|
auto rot_shape_bb = [&shape](Radians r) {
|
||||||
|
auto s = shape;
|
||||||
|
sl::rotate(s, r);
|
||||||
|
return sl::boundingBox(s);
|
||||||
|
};
|
||||||
|
|
||||||
|
auto rot_aspect_r = [&rot_shape_bb, &get_aspect_r](Radians r) {
|
||||||
|
return get_aspect_r(rot_shape_bb(r));
|
||||||
|
};
|
||||||
|
|
||||||
|
// Lets bisect the retrieved interval where the correct aspect ratio is.
|
||||||
|
double ar_from = rot_aspect_r(a_from);
|
||||||
|
auto would_fit = [&box](const _Box<TPoint<S>> &b) {
|
||||||
|
return b.width() < box.width() && b.height() < box.height();
|
||||||
|
};
|
||||||
|
|
||||||
|
Radians middle = (a_from + a_to) / 2.;
|
||||||
|
_Box<TPoint<S>> box_middle = rot_shape_bb(middle);
|
||||||
|
while (!would_fit(box_middle) && std::abs(a_to - a_from) > eps)
|
||||||
|
{
|
||||||
|
double ar_middle = get_aspect_r(box_middle);
|
||||||
|
if ((ar_from < aspect_r) != (ar_middle < aspect_r))
|
||||||
|
a_to = middle;
|
||||||
|
else
|
||||||
|
a_from = middle;
|
||||||
|
|
||||||
|
ar_from = rot_aspect_r(a_from);
|
||||||
|
middle = (a_from + a_to) / 2.;
|
||||||
|
box_middle = rot_shape_bb(middle);
|
||||||
|
}
|
||||||
|
|
||||||
|
return middle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace libnest2d
|
||||||
|
|
||||||
#endif // ROTCALIPERS_HPP
|
#endif // ROTCALIPERS_HPP
|
||||||
|
@ -472,6 +472,12 @@ template<class S> Radians min_area_boundingbox_rotation(const S &sh)
|
|||||||
.angleToX();
|
.angleToX();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class S>
|
||||||
|
Radians fit_into_box_rotation(const S &sh, const _Box<TPoint<S>> &box)
|
||||||
|
{
|
||||||
|
return fitIntoBoxRotation<S, TCompute<S>, boost::rational<LargeInt>>(sh, box);
|
||||||
|
}
|
||||||
|
|
||||||
template<class BinT> // Arrange for arbitrary bin type
|
template<class BinT> // Arrange for arbitrary bin type
|
||||||
void _arrange(
|
void _arrange(
|
||||||
std::vector<Item> & shapes,
|
std::vector<Item> & shapes,
|
||||||
@ -509,10 +515,19 @@ void _arrange(
|
|||||||
// Use the minimum bounding box rotation as a starting point.
|
// Use the minimum bounding box rotation as a starting point.
|
||||||
// TODO: This only works for convex hull. If we ever switch to concave
|
// TODO: This only works for convex hull. If we ever switch to concave
|
||||||
// polygon nesting, a convex hull needs to be calculated.
|
// polygon nesting, a convex hull needs to be calculated.
|
||||||
if (params.allow_rotations)
|
if (params.allow_rotations) {
|
||||||
for (auto &itm : shapes)
|
for (auto &itm : shapes) {
|
||||||
itm.rotation(min_area_boundingbox_rotation(itm.rawShape()));
|
itm.rotation(min_area_boundingbox_rotation(itm.rawShape()));
|
||||||
|
|
||||||
|
// If the item is too big, try to find a rotation that makes it fit
|
||||||
|
if constexpr (std::is_same_v<BinT, Box>) {
|
||||||
|
auto bb = itm.boundingBox();
|
||||||
|
if (bb.width() >= bin.width() || bb.height() >= bin.height())
|
||||||
|
itm.rotate(fit_into_box_rotation(itm.transformedShape(), bin));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
arranger(inp.begin(), inp.end());
|
arranger(inp.begin(), inp.end());
|
||||||
for (Item &itm : inp) itm.inflate(-infl);
|
for (Item &itm : inp) itm.inflate(-infl);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user