2014-01-06 17:29:10 +00:00
|
|
|
#ifndef slic3r_BoundingBox_hpp_
|
|
|
|
#define slic3r_BoundingBox_hpp_
|
|
|
|
|
2015-12-07 23:39:54 +00:00
|
|
|
#include "libslic3r.h"
|
2014-01-06 17:29:10 +00:00
|
|
|
#include "Point.hpp"
|
|
|
|
#include "Polygon.hpp"
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
template <class PointClass>
|
|
|
|
class BoundingBoxBase
|
|
|
|
{
|
2017-10-17 12:36:30 +00:00
|
|
|
public:
|
2014-01-06 17:29:10 +00:00
|
|
|
PointClass min;
|
|
|
|
PointClass max;
|
2014-09-21 08:51:36 +00:00
|
|
|
bool defined;
|
2014-01-06 17:29:10 +00:00
|
|
|
|
2019-09-24 12:54:33 +00:00
|
|
|
BoundingBoxBase() : min(PointClass::Zero()), max(PointClass::Zero()), defined(false) {}
|
2016-11-04 15:49:08 +00:00
|
|
|
BoundingBoxBase(const PointClass &pmin, const PointClass &pmax) :
|
2018-08-17 13:53:43 +00:00
|
|
|
min(pmin), max(pmax), defined(pmin(0) < pmax(0) && pmin(1) < pmax(1)) {}
|
2018-08-21 18:34:45 +00:00
|
|
|
BoundingBoxBase(const std::vector<PointClass>& points) : min(PointClass::Zero()), max(PointClass::Zero())
|
2018-01-08 12:44:10 +00:00
|
|
|
{
|
|
|
|
if (points.empty())
|
2018-09-18 08:09:58 +00:00
|
|
|
throw std::invalid_argument("Empty point set supplied to BoundingBoxBase constructor");
|
2018-01-08 12:44:10 +00:00
|
|
|
|
2018-01-12 10:09:53 +00:00
|
|
|
typename std::vector<PointClass>::const_iterator it = points.begin();
|
2018-08-21 15:43:05 +00:00
|
|
|
this->min = *it;
|
|
|
|
this->max = *it;
|
|
|
|
for (++ it; it != points.end(); ++ it) {
|
|
|
|
this->min = this->min.cwiseMin(*it);
|
|
|
|
this->max = this->max.cwiseMax(*it);
|
2018-01-08 12:44:10 +00:00
|
|
|
}
|
2018-08-17 13:53:43 +00:00
|
|
|
this->defined = (this->min(0) < this->max(0)) && (this->min(1) < this->max(1));
|
2018-01-08 12:44:10 +00:00
|
|
|
}
|
2019-01-26 17:51:34 +00:00
|
|
|
void reset() { this->defined = false; this->min = PointClass::Zero(); this->max = PointClass::Zero(); }
|
2014-01-07 11:48:09 +00:00
|
|
|
void merge(const PointClass &point);
|
2014-11-15 21:41:22 +00:00
|
|
|
void merge(const std::vector<PointClass> &points);
|
2014-01-06 18:42:31 +00:00
|
|
|
void merge(const BoundingBoxBase<PointClass> &bb);
|
2014-01-06 17:29:10 +00:00
|
|
|
void scale(double factor);
|
|
|
|
PointClass size() const;
|
2016-09-13 11:30:00 +00:00
|
|
|
double radius() const;
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
void translate(coordf_t x, coordf_t y) { assert(this->defined); PointClass v(x, y); this->min += v; this->max += v; }
|
2018-08-21 19:05:24 +00:00
|
|
|
void translate(const Vec2d &v) { this->min += v; this->max += v; }
|
2014-07-24 16:32:07 +00:00
|
|
|
void offset(coordf_t delta);
|
2014-01-06 17:29:10 +00:00
|
|
|
PointClass center() const;
|
2017-03-13 15:03:11 +00:00
|
|
|
bool contains(const PointClass &point) const {
|
2018-08-17 13:53:43 +00:00
|
|
|
return point(0) >= this->min(0) && point(0) <= this->max(0)
|
|
|
|
&& point(1) >= this->min(1) && point(1) <= this->max(1);
|
2017-03-13 15:03:11 +00:00
|
|
|
}
|
|
|
|
bool overlap(const BoundingBoxBase<PointClass> &other) const {
|
2018-08-17 13:53:43 +00:00
|
|
|
return ! (this->max(0) < other.min(0) || this->min(0) > other.max(0) ||
|
|
|
|
this->max(1) < other.min(1) || this->min(1) > other.max(1));
|
2017-03-13 15:03:11 +00:00
|
|
|
}
|
2017-10-17 12:36:30 +00:00
|
|
|
bool operator==(const BoundingBoxBase<PointClass> &rhs) { return this->min == rhs.min && this->max == rhs.max; }
|
|
|
|
bool operator!=(const BoundingBoxBase<PointClass> &rhs) { return ! (*this == rhs); }
|
2014-01-06 17:29:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class PointClass>
|
|
|
|
class BoundingBox3Base : public BoundingBoxBase<PointClass>
|
|
|
|
{
|
2017-10-17 12:36:30 +00:00
|
|
|
public:
|
2019-09-24 12:54:33 +00:00
|
|
|
BoundingBox3Base() : BoundingBoxBase<PointClass>() {}
|
2016-11-04 15:49:08 +00:00
|
|
|
BoundingBox3Base(const PointClass &pmin, const PointClass &pmax) :
|
|
|
|
BoundingBoxBase<PointClass>(pmin, pmax)
|
2018-08-17 13:53:43 +00:00
|
|
|
{ if (pmin(2) >= pmax(2)) BoundingBoxBase<PointClass>::defined = false; }
|
2018-01-08 12:44:10 +00:00
|
|
|
BoundingBox3Base(const std::vector<PointClass>& points)
|
|
|
|
{
|
|
|
|
if (points.empty())
|
2018-09-18 08:09:58 +00:00
|
|
|
throw std::invalid_argument("Empty point set supplied to BoundingBox3Base constructor");
|
2018-01-12 10:09:53 +00:00
|
|
|
typename std::vector<PointClass>::const_iterator it = points.begin();
|
2018-08-21 15:43:05 +00:00
|
|
|
this->min = *it;
|
|
|
|
this->max = *it;
|
|
|
|
for (++ it; it != points.end(); ++ it) {
|
|
|
|
this->min = this->min.cwiseMin(*it);
|
|
|
|
this->max = this->max.cwiseMax(*it);
|
2018-01-08 12:44:10 +00:00
|
|
|
}
|
2018-08-21 15:43:05 +00:00
|
|
|
this->defined = (this->min(0) < this->max(0)) && (this->min(1) < this->max(1)) && (this->min(2) < this->max(2));
|
2018-01-08 12:44:10 +00:00
|
|
|
}
|
2014-01-07 11:48:09 +00:00
|
|
|
void merge(const PointClass &point);
|
2014-11-15 21:41:22 +00:00
|
|
|
void merge(const std::vector<PointClass> &points);
|
2014-01-06 18:42:31 +00:00
|
|
|
void merge(const BoundingBox3Base<PointClass> &bb);
|
2014-01-06 17:29:10 +00:00
|
|
|
PointClass size() const;
|
2016-09-13 11:30:00 +00:00
|
|
|
double radius() const;
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
void translate(coordf_t x, coordf_t y, coordf_t z) { assert(this->defined); PointClass v(x, y, z); this->min += v; this->max += v; }
|
2018-08-21 15:43:05 +00:00
|
|
|
void translate(const Vec3d &v) { this->min += v; this->max += v; }
|
2014-07-24 16:32:07 +00:00
|
|
|
void offset(coordf_t delta);
|
2014-01-06 17:29:10 +00:00
|
|
|
PointClass center() const;
|
2018-05-15 07:50:01 +00:00
|
|
|
coordf_t max_size() const;
|
2018-03-09 09:40:42 +00:00
|
|
|
|
|
|
|
bool contains(const PointClass &point) const {
|
2018-08-17 13:53:43 +00:00
|
|
|
return BoundingBoxBase<PointClass>::contains(point) && point(2) >= this->min(2) && point(2) <= this->max(2);
|
2018-03-09 09:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool contains(const BoundingBox3Base<PointClass>& other) const {
|
2018-03-14 15:11:57 +00:00
|
|
|
return contains(other.min) && contains(other.max);
|
2018-03-09 09:40:42 +00:00
|
|
|
}
|
2018-07-18 12:26:42 +00:00
|
|
|
|
|
|
|
bool intersects(const BoundingBox3Base<PointClass>& other) const {
|
2018-08-17 13:53:43 +00:00
|
|
|
return (this->min(0) < other.max(0)) && (this->max(0) > other.min(0)) && (this->min(1) < other.max(1)) && (this->max(1) > other.min(1)) && (this->min(2) < other.max(2)) && (this->max(2) > other.min(2));
|
2018-07-18 12:26:42 +00:00
|
|
|
}
|
2014-01-06 17:29:10 +00:00
|
|
|
};
|
|
|
|
|
2019-09-24 12:54:33 +00:00
|
|
|
// Will prevent warnings caused by non existing definition of template in hpp
|
|
|
|
extern template void BoundingBoxBase<Point>::scale(double factor);
|
|
|
|
extern template void BoundingBoxBase<Vec2d>::scale(double factor);
|
|
|
|
extern template void BoundingBoxBase<Vec3d>::scale(double factor);
|
|
|
|
extern template void BoundingBoxBase<Point>::offset(coordf_t delta);
|
|
|
|
extern template void BoundingBoxBase<Vec2d>::offset(coordf_t delta);
|
|
|
|
extern template void BoundingBoxBase<Point>::merge(const Point &point);
|
|
|
|
extern template void BoundingBoxBase<Vec2d>::merge(const Vec2d &point);
|
|
|
|
extern template void BoundingBoxBase<Point>::merge(const Points &points);
|
|
|
|
extern template void BoundingBoxBase<Vec2d>::merge(const Pointfs &points);
|
|
|
|
extern template void BoundingBoxBase<Point>::merge(const BoundingBoxBase<Point> &bb);
|
|
|
|
extern template void BoundingBoxBase<Vec2d>::merge(const BoundingBoxBase<Vec2d> &bb);
|
|
|
|
extern template Point BoundingBoxBase<Point>::size() const;
|
|
|
|
extern template Vec2d BoundingBoxBase<Vec2d>::size() const;
|
|
|
|
extern template double BoundingBoxBase<Point>::radius() const;
|
|
|
|
extern template double BoundingBoxBase<Vec2d>::radius() const;
|
|
|
|
extern template Point BoundingBoxBase<Point>::center() const;
|
|
|
|
extern template Vec2d BoundingBoxBase<Vec2d>::center() const;
|
|
|
|
extern template void BoundingBox3Base<Vec3d>::merge(const Vec3d &point);
|
|
|
|
extern template void BoundingBox3Base<Vec3d>::merge(const Pointf3s &points);
|
|
|
|
extern template void BoundingBox3Base<Vec3d>::merge(const BoundingBox3Base<Vec3d> &bb);
|
|
|
|
extern template Vec3d BoundingBox3Base<Vec3d>::size() const;
|
|
|
|
extern template double BoundingBox3Base<Vec3d>::radius() const;
|
|
|
|
extern template void BoundingBox3Base<Vec3d>::offset(coordf_t delta);
|
|
|
|
extern template Vec3d BoundingBox3Base<Vec3d>::center() const;
|
|
|
|
extern template coordf_t BoundingBox3Base<Vec3d>::max_size() const;
|
|
|
|
|
2014-01-07 11:48:09 +00:00
|
|
|
class BoundingBox : public BoundingBoxBase<Point>
|
2014-01-06 17:29:10 +00:00
|
|
|
{
|
2017-10-17 12:36:30 +00:00
|
|
|
public:
|
2014-01-06 17:29:10 +00:00
|
|
|
void polygon(Polygon* polygon) const;
|
2014-05-13 18:06:01 +00:00
|
|
|
Polygon polygon() const;
|
2016-09-13 11:30:00 +00:00
|
|
|
BoundingBox rotated(double angle) const;
|
|
|
|
BoundingBox rotated(double angle, const Point ¢er) const;
|
|
|
|
void rotate(double angle) { (*this) = this->rotated(angle); }
|
|
|
|
void rotate(double angle, const Point ¢er) { (*this) = this->rotated(angle, center); }
|
2016-11-29 18:25:10 +00:00
|
|
|
// Align the min corner to a grid of cell_size x cell_size cells,
|
|
|
|
// to encompass the original bounding box.
|
|
|
|
void align_to_grid(const coord_t cell_size);
|
2014-01-06 17:29:10 +00:00
|
|
|
|
2019-09-24 12:54:33 +00:00
|
|
|
BoundingBox() : BoundingBoxBase<Point>() {}
|
|
|
|
BoundingBox(const Point &pmin, const Point &pmax) : BoundingBoxBase<Point>(pmin, pmax) {}
|
|
|
|
BoundingBox(const Points &points) : BoundingBoxBase<Point>(points) {}
|
2014-01-09 18:56:12 +00:00
|
|
|
BoundingBox(const Lines &lines);
|
2016-11-07 21:49:11 +00:00
|
|
|
|
|
|
|
friend BoundingBox get_extents_rotated(const Points &points, double angle);
|
2014-01-06 17:29:10 +00:00
|
|
|
};
|
|
|
|
|
2018-08-21 20:14:47 +00:00
|
|
|
class BoundingBox3 : public BoundingBox3Base<Vec3crd>
|
2017-06-06 17:12:46 +00:00
|
|
|
{
|
2017-10-17 12:36:30 +00:00
|
|
|
public:
|
2019-09-24 12:54:33 +00:00
|
|
|
BoundingBox3() : BoundingBox3Base<Vec3crd>() {}
|
|
|
|
BoundingBox3(const Vec3crd &pmin, const Vec3crd &pmax) : BoundingBox3Base<Vec3crd>(pmin, pmax) {}
|
|
|
|
BoundingBox3(const Points3& points) : BoundingBox3Base<Vec3crd>(points) {}
|
2017-06-06 17:12:46 +00:00
|
|
|
};
|
2014-01-06 17:29:10 +00:00
|
|
|
|
2018-08-21 19:05:24 +00:00
|
|
|
class BoundingBoxf : public BoundingBoxBase<Vec2d>
|
2017-06-06 17:12:46 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-09-24 12:54:33 +00:00
|
|
|
BoundingBoxf() : BoundingBoxBase<Vec2d>() {}
|
|
|
|
BoundingBoxf(const Vec2d &pmin, const Vec2d &pmax) : BoundingBoxBase<Vec2d>(pmin, pmax) {}
|
|
|
|
BoundingBoxf(const std::vector<Vec2d> &points) : BoundingBoxBase<Vec2d>(points) {}
|
2014-06-16 13:18:39 +00:00
|
|
|
};
|
|
|
|
|
2018-08-21 15:43:05 +00:00
|
|
|
class BoundingBoxf3 : public BoundingBox3Base<Vec3d>
|
2017-06-06 17:12:46 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-09-24 12:54:33 +00:00
|
|
|
BoundingBoxf3() : BoundingBox3Base<Vec3d>() {}
|
|
|
|
BoundingBoxf3(const Vec3d &pmin, const Vec3d &pmax) : BoundingBox3Base<Vec3d>(pmin, pmax) {}
|
|
|
|
BoundingBoxf3(const std::vector<Vec3d> &points) : BoundingBox3Base<Vec3d>(points) {}
|
2018-06-21 06:37:04 +00:00
|
|
|
|
2018-08-23 13:37:38 +00:00
|
|
|
BoundingBoxf3 transformed(const Transform3d& matrix) const;
|
2014-01-06 17:29:10 +00:00
|
|
|
};
|
|
|
|
|
2016-04-10 17:02:00 +00:00
|
|
|
template<typename VT>
|
|
|
|
inline bool empty(const BoundingBoxBase<VT> &bb)
|
|
|
|
{
|
2018-08-17 13:53:43 +00:00
|
|
|
return ! bb.defined || bb.min(0) >= bb.max(0) || bb.min(1) >= bb.max(1);
|
2016-04-10 17:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename VT>
|
|
|
|
inline bool empty(const BoundingBox3Base<VT> &bb)
|
|
|
|
{
|
2018-08-17 13:53:43 +00:00
|
|
|
return ! bb.defined || bb.min(0) >= bb.max(0) || bb.min(1) >= bb.max(1) || bb.min(2) >= bb.max(2);
|
2014-01-06 17:29:10 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 11:30:00 +00:00
|
|
|
} // namespace Slic3r
|
|
|
|
|
2019-06-26 14:29:12 +00:00
|
|
|
// Serialization through the Cereal library
|
|
|
|
namespace cereal {
|
|
|
|
template<class Archive> void serialize(Archive& archive, Slic3r::BoundingBox &bb) { archive(bb.min, bb.max, bb.defined); }
|
|
|
|
template<class Archive> void serialize(Archive& archive, Slic3r::BoundingBox3 &bb) { archive(bb.min, bb.max, bb.defined); }
|
|
|
|
template<class Archive> void serialize(Archive& archive, Slic3r::BoundingBoxf &bb) { archive(bb.min, bb.max, bb.defined); }
|
|
|
|
template<class Archive> void serialize(Archive& archive, Slic3r::BoundingBoxf3 &bb) { archive(bb.min, bb.max, bb.defined); }
|
|
|
|
}
|
|
|
|
|
2014-01-06 17:29:10 +00:00
|
|
|
#endif
|