2020-04-23 16:19:03 +00:00
|
|
|
#ifndef ARRANGE_HPP
|
|
|
|
#define ARRANGE_HPP
|
2018-07-30 14:41:35 +00:00
|
|
|
|
2019-07-12 19:03:49 +00:00
|
|
|
#include "ExPolygon.hpp"
|
2018-08-03 10:37:27 +00:00
|
|
|
|
2020-05-23 22:50:11 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
class BoundingBox;
|
|
|
|
|
|
|
|
namespace arrangement {
|
2018-07-30 14:41:35 +00:00
|
|
|
|
2019-06-28 16:27:15 +00:00
|
|
|
/// A geometry abstraction for a circular print bed. Similarly to BoundingBox.
|
2019-06-28 15:03:50 +00:00
|
|
|
class CircleBed {
|
2018-08-03 12:49:26 +00:00
|
|
|
Point center_;
|
|
|
|
double radius_;
|
|
|
|
public:
|
|
|
|
|
2019-06-28 15:03:50 +00:00
|
|
|
inline CircleBed(): center_(0, 0), radius_(std::nan("")) {}
|
2020-04-23 16:19:03 +00:00
|
|
|
explicit inline CircleBed(const Point& c, double r): center_(c), radius_(r) {}
|
2018-08-03 12:49:26 +00:00
|
|
|
|
|
|
|
inline double radius() const { return radius_; }
|
|
|
|
inline const Point& center() const { return center_; }
|
|
|
|
};
|
|
|
|
|
2019-07-15 15:30:44 +00:00
|
|
|
/// Representing an unbounded bed.
|
2020-04-23 16:19:03 +00:00
|
|
|
struct InfiniteBed {
|
|
|
|
Point center;
|
|
|
|
explicit InfiniteBed(const Point &p = {0, 0}): center{p} {}
|
2019-07-15 15:30:44 +00:00
|
|
|
};
|
2019-07-12 19:03:49 +00:00
|
|
|
|
2019-07-15 15:30:44 +00:00
|
|
|
/// A logical bed representing an object not being arranged. Either the arrange
|
2019-07-18 14:30:32 +00:00
|
|
|
/// has not yet successfully run on this ArrangePolygon or it could not fit the
|
2019-07-15 15:30:44 +00:00
|
|
|
/// object due to overly large size or invalid geometry.
|
|
|
|
static const constexpr int UNARRANGED = -1;
|
|
|
|
|
|
|
|
/// Input/Output structure for the arrange() function. The poly field will not
|
|
|
|
/// be modified during arrangement. Instead, the translation and rotation fields
|
|
|
|
/// will mark the needed transformation for the polygon to be in the arranged
|
|
|
|
/// position. These can also be set to an initial offset and rotation.
|
|
|
|
///
|
|
|
|
/// The bed_idx field will indicate the logical bed into which the
|
|
|
|
/// polygon belongs: UNARRANGED means no place for the polygon
|
|
|
|
/// (also the initial state before arrange), 0..N means the index of the bed.
|
|
|
|
/// Zero is the physical bed, larger than zero means a virtual bed.
|
2019-07-16 16:33:42 +00:00
|
|
|
struct ArrangePolygon {
|
|
|
|
ExPolygon poly; /// The 2D silhouette to be arranged
|
2019-07-15 15:30:44 +00:00
|
|
|
Vec2crd translation{0, 0}; /// The translation of the poly
|
|
|
|
double rotation{0.0}; /// The rotation of the poly in radians
|
2020-04-23 16:19:03 +00:00
|
|
|
coord_t inflation = 0; /// Arrange with inflated polygon
|
2019-07-15 15:30:44 +00:00
|
|
|
int bed_idx{UNARRANGED}; /// To which logical bed does poly belong...
|
2019-07-16 16:33:42 +00:00
|
|
|
int priority{0};
|
2019-06-26 15:09:26 +00:00
|
|
|
|
2020-04-23 16:19:03 +00:00
|
|
|
// If empty, any rotation is allowed (currently unsupported)
|
|
|
|
// If only a zero is there, no rotation is allowed
|
|
|
|
std::vector<double> allowed_rotations = {0.};
|
|
|
|
|
2019-07-16 16:33:42 +00:00
|
|
|
/// Optional setter function which can store arbitrary data in its closure
|
|
|
|
std::function<void(const ArrangePolygon&)> setter = nullptr;
|
|
|
|
|
|
|
|
/// Helper function to call the setter with the arrange data arguments
|
|
|
|
void apply() const { if (setter) setter(*this); }
|
2019-07-18 14:30:32 +00:00
|
|
|
|
|
|
|
/// Test if arrange() was called previously and gave a successful result.
|
|
|
|
bool is_arranged() const { return bed_idx != UNARRANGED; }
|
2020-12-02 16:52:40 +00:00
|
|
|
|
|
|
|
inline ExPolygon transformed_poly() const
|
|
|
|
{
|
|
|
|
ExPolygon ret = poly;
|
|
|
|
ret.rotate(rotation);
|
|
|
|
ret.translate(translation.x(), translation.y());
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2019-04-29 12:32:02 +00:00
|
|
|
};
|
|
|
|
|
2019-07-12 19:03:49 +00:00
|
|
|
using ArrangePolygons = std::vector<ArrangePolygon>;
|
2019-06-26 15:09:26 +00:00
|
|
|
|
2020-04-23 16:19:03 +00:00
|
|
|
struct ArrangeParams {
|
|
|
|
|
|
|
|
/// The minimum distance which is allowed for any
|
|
|
|
/// pair of items on the print bed in any direction.
|
2020-04-30 10:04:49 +00:00
|
|
|
coord_t min_obj_distance = 0;
|
2020-04-23 16:19:03 +00:00
|
|
|
|
|
|
|
/// The accuracy of optimization.
|
|
|
|
/// Goes from 0.0 to 1.0 and scales performance as well
|
2020-11-24 13:42:15 +00:00
|
|
|
float accuracy = 1.f;
|
2020-04-23 16:19:03 +00:00
|
|
|
|
|
|
|
/// Allow parallel execution.
|
|
|
|
bool parallel = true;
|
2020-11-17 23:06:34 +00:00
|
|
|
|
|
|
|
bool allow_rotations = false;
|
2020-04-23 16:19:03 +00:00
|
|
|
|
|
|
|
/// Progress indicator callback called when an object gets packed.
|
|
|
|
/// The unsigned argument is the number of items remaining to pack.
|
2020-11-26 12:58:21 +00:00
|
|
|
std::function<void(unsigned)> progressind;
|
|
|
|
|
|
|
|
std::function<void(const ArrangePolygon &)> on_packed;
|
2020-04-23 16:19:03 +00:00
|
|
|
|
|
|
|
/// A predicate returning true if abort is needed.
|
|
|
|
std::function<bool(void)> stopcondition;
|
|
|
|
|
|
|
|
ArrangeParams() = default;
|
|
|
|
explicit ArrangeParams(coord_t md) : min_obj_distance(md) {}
|
|
|
|
};
|
|
|
|
|
2018-07-30 14:41:35 +00:00
|
|
|
/**
|
2019-07-15 15:30:44 +00:00
|
|
|
* \brief Arranges the input polygons.
|
2018-07-30 14:41:35 +00:00
|
|
|
*
|
2019-07-15 15:30:44 +00:00
|
|
|
* WARNING: Currently, only convex polygons are supported by the libnest2d
|
|
|
|
* library which is used to do the arrangement. This might change in the future
|
|
|
|
* this is why the interface contains a general polygon capable to have holes.
|
2019-06-28 16:27:15 +00:00
|
|
|
*
|
2019-07-15 15:30:44 +00:00
|
|
|
* \param items Input vector of ArrangePolygons. The transformation, rotation
|
|
|
|
* and bin_idx fields will be changed after the call finished and can be used
|
|
|
|
* to apply the result on the input polygon.
|
2018-07-30 14:41:35 +00:00
|
|
|
*/
|
2020-04-23 16:19:03 +00:00
|
|
|
template<class TBed> void arrange(ArrangePolygons &items, const ArrangePolygons &excludes, const TBed &bed, const ArrangeParams ¶ms = {});
|
|
|
|
|
|
|
|
// A dispatch function that determines the bed shape from a set of points.
|
|
|
|
template<> void arrange(ArrangePolygons &items, const ArrangePolygons &excludes, const Points &bed, const ArrangeParams ¶ms);
|
|
|
|
|
|
|
|
extern template void arrange(ArrangePolygons &items, const ArrangePolygons &excludes, const BoundingBox &bed, const ArrangeParams ¶ms);
|
|
|
|
extern template void arrange(ArrangePolygons &items, const ArrangePolygons &excludes, const CircleBed &bed, const ArrangeParams ¶ms);
|
|
|
|
extern template void arrange(ArrangePolygons &items, const ArrangePolygons &excludes, const Polygon &bed, const ArrangeParams ¶ms);
|
|
|
|
extern template void arrange(ArrangePolygons &items, const ArrangePolygons &excludes, const InfiniteBed &bed, const ArrangeParams ¶ms);
|
|
|
|
|
|
|
|
inline void arrange(ArrangePolygons &items, const Points &bed, const ArrangeParams ¶ms = {}) { arrange(items, {}, bed, params); }
|
|
|
|
inline void arrange(ArrangePolygons &items, const BoundingBox &bed, const ArrangeParams ¶ms = {}) { arrange(items, {}, bed, params); }
|
|
|
|
inline void arrange(ArrangePolygons &items, const CircleBed &bed, const ArrangeParams ¶ms = {}) { arrange(items, {}, bed, params); }
|
|
|
|
inline void arrange(ArrangePolygons &items, const Polygon &bed, const ArrangeParams ¶ms = {}) { arrange(items, {}, bed, params); }
|
|
|
|
inline void arrange(ArrangePolygons &items, const InfiniteBed &bed, const ArrangeParams ¶ms = {}) { arrange(items, {}, bed, params); }
|
|
|
|
|
|
|
|
}} // namespace Slic3r::arrangement
|
|
|
|
|
2018-07-30 14:41:35 +00:00
|
|
|
#endif // MODELARRANGE_HPP
|