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 {
|
|
|
|
|
|
|
|
typedef Point Size;
|
|
|
|
typedef Point3 Size3;
|
|
|
|
typedef Pointf Sizef;
|
|
|
|
typedef Pointf3 Sizef3;
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-09-21 08:51:36 +00:00
|
|
|
BoundingBoxBase() : defined(false) {};
|
2016-11-04 15:49:08 +00:00
|
|
|
BoundingBoxBase(const PointClass &pmin, const PointClass &pmax) :
|
2018-08-14 16:33:26 +00:00
|
|
|
min(pmin), max(pmax), defined(pmin.x() < pmax.x() && pmin.y() < pmax.y()) {}
|
2018-01-08 12:44:10 +00:00
|
|
|
BoundingBoxBase(const std::vector<PointClass>& points)
|
|
|
|
{
|
|
|
|
if (points.empty())
|
|
|
|
CONFESS("Empty point set supplied to BoundingBoxBase constructor");
|
|
|
|
|
2018-01-12 10:09:53 +00:00
|
|
|
typename std::vector<PointClass>::const_iterator it = points.begin();
|
2018-08-14 16:33:26 +00:00
|
|
|
this->min.x() = this->max.x() = it->x();
|
|
|
|
this->min.y() = this->max.y() = it->y();
|
2018-01-08 12:44:10 +00:00
|
|
|
for (++it; it != points.end(); ++it)
|
|
|
|
{
|
2018-08-14 16:33:26 +00:00
|
|
|
this->min.x() = std::min(it->x(), this->min.x());
|
|
|
|
this->min.y() = std::min(it->y(), this->min.y());
|
|
|
|
this->max.x() = std::max(it->x(), this->max.x());
|
|
|
|
this->max.y() = std::max(it->y(), this->max.y());
|
2018-01-08 12:44:10 +00:00
|
|
|
}
|
2018-08-14 16:33:26 +00:00
|
|
|
this->defined = (this->min.x() < this->max.x()) && (this->min.y() < this->max.y());
|
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 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; }
|
|
|
|
void translate(const Pointf &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-14 16:33:26 +00:00
|
|
|
return point.x() >= this->min.x() && point.x() <= this->max.x()
|
|
|
|
&& point.y() >= this->min.y() && point.y() <= this->max.y();
|
2017-03-13 15:03:11 +00:00
|
|
|
}
|
|
|
|
bool overlap(const BoundingBoxBase<PointClass> &other) const {
|
2018-08-14 16:33:26 +00:00
|
|
|
return ! (this->max.x() < other.min.x() || this->min.x() > other.max.x() ||
|
|
|
|
this->max.y() < other.min.y() || this->min.y() > other.max.y());
|
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:
|
2014-09-21 08:51:36 +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-14 16:33:26 +00:00
|
|
|
{ if (pmin.z() >= pmax.z()) BoundingBoxBase<PointClass>::defined = false; }
|
2018-01-08 12:44:10 +00:00
|
|
|
BoundingBox3Base(const std::vector<PointClass>& points)
|
|
|
|
: BoundingBoxBase<PointClass>(points)
|
|
|
|
{
|
|
|
|
if (points.empty())
|
|
|
|
CONFESS("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-14 16:33:26 +00:00
|
|
|
this->min.z() = this->max.z() = it->z();
|
2018-01-08 12:44:10 +00:00
|
|
|
for (++it; it != points.end(); ++it)
|
|
|
|
{
|
2018-08-14 16:33:26 +00:00
|
|
|
this->min.z() = std::min(it->z(), this->min.z());
|
|
|
|
this->max.z() = std::max(it->z(), this->max.z());
|
2018-01-08 12:44:10 +00:00
|
|
|
}
|
2018-08-14 16:33:26 +00:00
|
|
|
this->defined &= (this->min.z() < this->max.z());
|
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; }
|
|
|
|
void translate(const Pointf3 &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-14 16:33:26 +00:00
|
|
|
return BoundingBoxBase<PointClass>::contains(point) && point.z() >= this->min.z() && point.z() <= this->max.z();
|
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-14 16:33:26 +00:00
|
|
|
return (this->min.x() < other.max.x()) && (this->max.x() > other.min.x()) && (this->min.y() < other.max.y()) && (this->max.y() > other.min.y()) && (this->min.z() < other.max.z()) && (this->max.z() > other.min.z());
|
2018-07-18 12:26:42 +00:00
|
|
|
}
|
2014-01-06 17:29:10 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2014-09-21 08:51:36 +00:00
|
|
|
BoundingBox() : BoundingBoxBase<Point>() {};
|
2016-04-10 17:02:00 +00:00
|
|
|
BoundingBox(const Point &pmin, const Point &pmax) : BoundingBoxBase<Point>(pmin, pmax) {};
|
2014-01-09 18:56:12 +00:00
|
|
|
BoundingBox(const Points &points) : BoundingBoxBase<Point>(points) {};
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2017-06-06 17:12:46 +00:00
|
|
|
class BoundingBox3 : public BoundingBox3Base<Point3>
|
|
|
|
{
|
2017-10-17 12:36:30 +00:00
|
|
|
public:
|
2017-06-06 17:12:46 +00:00
|
|
|
BoundingBox3() : BoundingBox3Base<Point3>() {};
|
|
|
|
BoundingBox3(const Point3 &pmin, const Point3 &pmax) : BoundingBox3Base<Point3>(pmin, pmax) {};
|
2018-01-08 12:44:10 +00:00
|
|
|
BoundingBox3(const Points3& points) : BoundingBox3Base<Point3>(points) {};
|
2017-06-06 17:12:46 +00:00
|
|
|
};
|
2014-01-06 17:29:10 +00:00
|
|
|
|
2017-06-06 17:12:46 +00:00
|
|
|
class BoundingBoxf : public BoundingBoxBase<Pointf>
|
|
|
|
{
|
|
|
|
public:
|
2014-09-21 08:51:36 +00:00
|
|
|
BoundingBoxf() : BoundingBoxBase<Pointf>() {};
|
2016-04-10 17:02:00 +00:00
|
|
|
BoundingBoxf(const Pointf &pmin, const Pointf &pmax) : BoundingBoxBase<Pointf>(pmin, pmax) {};
|
2014-06-16 13:18:39 +00:00
|
|
|
BoundingBoxf(const std::vector<Pointf> &points) : BoundingBoxBase<Pointf>(points) {};
|
|
|
|
};
|
|
|
|
|
2017-06-06 17:12:46 +00:00
|
|
|
class BoundingBoxf3 : public BoundingBox3Base<Pointf3>
|
|
|
|
{
|
|
|
|
public:
|
2014-09-21 08:51:36 +00:00
|
|
|
BoundingBoxf3() : BoundingBox3Base<Pointf3>() {};
|
2016-04-10 17:02:00 +00:00
|
|
|
BoundingBoxf3(const Pointf3 &pmin, const Pointf3 &pmax) : BoundingBox3Base<Pointf3>(pmin, pmax) {};
|
2014-01-09 18:56:12 +00:00
|
|
|
BoundingBoxf3(const std::vector<Pointf3> &points) : BoundingBox3Base<Pointf3>(points) {};
|
2018-06-21 06:37:04 +00:00
|
|
|
|
2018-08-14 19:33:41 +00:00
|
|
|
BoundingBoxf3 transformed(const Transform3f& 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-14 16:33:26 +00:00
|
|
|
return ! bb.defined || bb.min.x() >= bb.max.x() || bb.min.y() >= bb.max.y();
|
2016-04-10 17:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename VT>
|
|
|
|
inline bool empty(const BoundingBox3Base<VT> &bb)
|
|
|
|
{
|
2018-08-14 16:33:26 +00:00
|
|
|
return ! bb.defined || bb.min.x() >= bb.max.x() || bb.min.y() >= bb.max.y() || bb.min.z() >= bb.max.z();
|
2014-01-06 17:29:10 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 11:30:00 +00:00
|
|
|
} // namespace Slic3r
|
|
|
|
|
2014-01-06 17:29:10 +00:00
|
|
|
#endif
|