2014-01-06 17:29:10 +00:00
|
|
|
#include "BoundingBox.hpp"
|
|
|
|
#include <algorithm>
|
2017-09-12 14:48:44 +00:00
|
|
|
#include <assert.h>
|
2014-01-06 17:29:10 +00:00
|
|
|
|
2018-06-21 06:37:04 +00:00
|
|
|
#include <Eigen/Dense>
|
|
|
|
|
2014-01-06 17:29:10 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
|
2014-01-09 18:56:12 +00:00
|
|
|
template BoundingBoxBase<Point>::BoundingBoxBase(const std::vector<Point> &points);
|
2018-08-21 19:05:24 +00:00
|
|
|
template BoundingBoxBase<Vec2d>::BoundingBoxBase(const std::vector<Vec2d> &points);
|
2014-01-06 17:29:10 +00:00
|
|
|
|
2018-08-21 15:43:05 +00:00
|
|
|
template BoundingBox3Base<Vec3d>::BoundingBox3Base(const std::vector<Vec3d> &points);
|
2014-01-09 18:56:12 +00:00
|
|
|
|
2018-08-21 15:43:05 +00:00
|
|
|
void BoundingBox::polygon(Polygon* polygon) const
|
2014-01-06 17:29:10 +00:00
|
|
|
{
|
|
|
|
polygon->points.clear();
|
|
|
|
polygon->points.resize(4);
|
2018-08-17 13:53:43 +00:00
|
|
|
polygon->points[0](0) = this->min(0);
|
|
|
|
polygon->points[0](1) = this->min(1);
|
|
|
|
polygon->points[1](0) = this->max(0);
|
|
|
|
polygon->points[1](1) = this->min(1);
|
|
|
|
polygon->points[2](0) = this->max(0);
|
|
|
|
polygon->points[2](1) = this->max(1);
|
|
|
|
polygon->points[3](0) = this->min(0);
|
|
|
|
polygon->points[3](1) = this->max(1);
|
2014-01-06 17:29:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 15:43:05 +00:00
|
|
|
Polygon BoundingBox::polygon() const
|
2014-05-13 18:06:01 +00:00
|
|
|
{
|
|
|
|
Polygon p;
|
|
|
|
this->polygon(&p);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-09-13 11:30:00 +00:00
|
|
|
BoundingBox BoundingBox::rotated(double angle) const
|
|
|
|
{
|
|
|
|
BoundingBox out;
|
|
|
|
out.merge(this->min.rotated(angle));
|
|
|
|
out.merge(this->max.rotated(angle));
|
2018-08-17 13:53:43 +00:00
|
|
|
out.merge(Point(this->min(0), this->max(1)).rotated(angle));
|
|
|
|
out.merge(Point(this->max(0), this->min(1)).rotated(angle));
|
2016-09-13 11:30:00 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
BoundingBox BoundingBox::rotated(double angle, const Point ¢er) const
|
|
|
|
{
|
|
|
|
BoundingBox out;
|
|
|
|
out.merge(this->min.rotated(angle, center));
|
|
|
|
out.merge(this->max.rotated(angle, center));
|
2018-08-17 13:53:43 +00:00
|
|
|
out.merge(Point(this->min(0), this->max(1)).rotated(angle, center));
|
|
|
|
out.merge(Point(this->max(0), this->min(1)).rotated(angle, center));
|
2016-09-13 11:30:00 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2014-01-06 17:29:10 +00:00
|
|
|
template <class PointClass> void
|
|
|
|
BoundingBoxBase<PointClass>::scale(double factor)
|
|
|
|
{
|
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
|
|
|
this->min *= factor;
|
|
|
|
this->max *= factor;
|
2014-01-06 17:29:10 +00:00
|
|
|
}
|
2014-01-07 11:48:09 +00:00
|
|
|
template void BoundingBoxBase<Point>::scale(double factor);
|
2018-08-21 19:05:24 +00:00
|
|
|
template void BoundingBoxBase<Vec2d>::scale(double factor);
|
2018-08-21 15:43:05 +00:00
|
|
|
template void BoundingBoxBase<Vec3d>::scale(double factor);
|
2014-01-07 11:48:09 +00:00
|
|
|
|
|
|
|
template <class PointClass> void
|
|
|
|
BoundingBoxBase<PointClass>::merge(const PointClass &point)
|
|
|
|
{
|
2014-09-21 08:51:36 +00:00
|
|
|
if (this->defined) {
|
2018-08-21 15:43:05 +00:00
|
|
|
this->min = this->min.cwiseMin(point);
|
|
|
|
this->max = this->max.cwiseMax(point);
|
2014-09-21 08:51:36 +00:00
|
|
|
} else {
|
2018-08-21 15:43:05 +00:00
|
|
|
this->min = point;
|
|
|
|
this->max = point;
|
2014-09-21 08:51:36 +00:00
|
|
|
this->defined = true;
|
|
|
|
}
|
2014-01-07 11:48:09 +00:00
|
|
|
}
|
|
|
|
template void BoundingBoxBase<Point>::merge(const Point &point);
|
2018-08-21 19:05:24 +00:00
|
|
|
template void BoundingBoxBase<Vec2d>::merge(const Vec2d &point);
|
2014-01-06 17:29:10 +00:00
|
|
|
|
2014-11-15 21:41:22 +00:00
|
|
|
template <class PointClass> void
|
|
|
|
BoundingBoxBase<PointClass>::merge(const std::vector<PointClass> &points)
|
|
|
|
{
|
|
|
|
this->merge(BoundingBoxBase(points));
|
|
|
|
}
|
|
|
|
template void BoundingBoxBase<Point>::merge(const Points &points);
|
2018-08-21 19:05:24 +00:00
|
|
|
template void BoundingBoxBase<Vec2d>::merge(const Pointfs &points);
|
2014-11-15 21:41:22 +00:00
|
|
|
|
2014-01-06 17:29:10 +00:00
|
|
|
template <class PointClass> void
|
|
|
|
BoundingBoxBase<PointClass>::merge(const BoundingBoxBase<PointClass> &bb)
|
|
|
|
{
|
2018-08-17 13:53:43 +00:00
|
|
|
assert(bb.defined || bb.min(0) >= bb.max(0) || bb.min(1) >= bb.max(1));
|
2017-09-12 13:55:38 +00:00
|
|
|
if (bb.defined) {
|
|
|
|
if (this->defined) {
|
2018-08-21 15:43:05 +00:00
|
|
|
this->min = this->min.cwiseMin(bb.min);
|
|
|
|
this->max = this->max.cwiseMax(bb.max);
|
2017-09-12 13:55:38 +00:00
|
|
|
} else {
|
|
|
|
this->min = bb.min;
|
|
|
|
this->max = bb.max;
|
|
|
|
this->defined = true;
|
|
|
|
}
|
2014-09-21 08:51:36 +00:00
|
|
|
}
|
2014-01-06 17:29:10 +00:00
|
|
|
}
|
2014-01-07 11:48:09 +00:00
|
|
|
template void BoundingBoxBase<Point>::merge(const BoundingBoxBase<Point> &bb);
|
2018-08-21 19:05:24 +00:00
|
|
|
template void BoundingBoxBase<Vec2d>::merge(const BoundingBoxBase<Vec2d> &bb);
|
2014-01-07 11:48:09 +00:00
|
|
|
|
|
|
|
template <class PointClass> void
|
|
|
|
BoundingBox3Base<PointClass>::merge(const PointClass &point)
|
|
|
|
{
|
2014-09-21 08:51:36 +00:00
|
|
|
if (this->defined) {
|
2018-08-21 15:43:05 +00:00
|
|
|
this->min = this->min.cwiseMin(point);
|
|
|
|
this->max = this->max.cwiseMax(point);
|
|
|
|
} else {
|
|
|
|
this->min = point;
|
|
|
|
this->max = point;
|
|
|
|
this->defined = true;
|
2014-09-21 08:51:36 +00:00
|
|
|
}
|
2014-01-07 11:48:09 +00:00
|
|
|
}
|
2018-08-21 15:43:05 +00:00
|
|
|
template void BoundingBox3Base<Vec3d>::merge(const Vec3d &point);
|
2014-01-06 17:29:10 +00:00
|
|
|
|
2014-11-15 21:41:22 +00:00
|
|
|
template <class PointClass> void
|
|
|
|
BoundingBox3Base<PointClass>::merge(const std::vector<PointClass> &points)
|
|
|
|
{
|
|
|
|
this->merge(BoundingBox3Base(points));
|
|
|
|
}
|
2018-08-21 15:43:05 +00:00
|
|
|
template void BoundingBox3Base<Vec3d>::merge(const Pointf3s &points);
|
2014-11-15 21:41:22 +00:00
|
|
|
|
2014-01-06 17:29:10 +00:00
|
|
|
template <class PointClass> void
|
|
|
|
BoundingBox3Base<PointClass>::merge(const BoundingBox3Base<PointClass> &bb)
|
|
|
|
{
|
2018-08-17 13:53:43 +00:00
|
|
|
assert(bb.defined || bb.min(0) >= bb.max(0) || bb.min(1) >= bb.max(1) || bb.min(2) >= bb.max(2));
|
2017-09-12 13:55:38 +00:00
|
|
|
if (bb.defined) {
|
|
|
|
if (this->defined) {
|
2018-08-21 15:43:05 +00:00
|
|
|
this->min = this->min.cwiseMin(bb.min);
|
|
|
|
this->max = this->max.cwiseMax(bb.max);
|
|
|
|
} else {
|
|
|
|
this->min = bb.min;
|
|
|
|
this->max = bb.max;
|
|
|
|
this->defined = true;
|
2017-09-12 13:55:38 +00:00
|
|
|
}
|
2014-09-21 08:51:36 +00:00
|
|
|
}
|
2014-01-06 17:29:10 +00:00
|
|
|
}
|
2018-08-21 15:43:05 +00:00
|
|
|
template void BoundingBox3Base<Vec3d>::merge(const BoundingBox3Base<Vec3d> &bb);
|
2014-01-06 17:29:10 +00:00
|
|
|
|
|
|
|
template <class PointClass> PointClass
|
2014-01-07 11:48:09 +00:00
|
|
|
BoundingBoxBase<PointClass>::size() const
|
2014-01-06 17:29:10 +00:00
|
|
|
{
|
2018-08-17 13:53:43 +00:00
|
|
|
return PointClass(this->max(0) - this->min(0), this->max(1) - this->min(1));
|
2014-01-06 17:29:10 +00:00
|
|
|
}
|
2014-01-07 11:48:09 +00:00
|
|
|
template Point BoundingBoxBase<Point>::size() const;
|
2018-08-21 19:05:24 +00:00
|
|
|
template Vec2d BoundingBoxBase<Vec2d>::size() const;
|
2014-01-06 17:29:10 +00:00
|
|
|
|
|
|
|
template <class PointClass> PointClass
|
|
|
|
BoundingBox3Base<PointClass>::size() const
|
|
|
|
{
|
2018-08-17 13:53:43 +00:00
|
|
|
return PointClass(this->max(0) - this->min(0), this->max(1) - this->min(1), this->max(2) - this->min(2));
|
2014-01-06 17:29:10 +00:00
|
|
|
}
|
2018-08-21 15:43:05 +00:00
|
|
|
template Vec3d BoundingBox3Base<Vec3d>::size() const;
|
2014-01-06 17:29:10 +00:00
|
|
|
|
2017-10-25 10:53:31 +00:00
|
|
|
template <class PointClass> double BoundingBoxBase<PointClass>::radius() const
|
2016-09-13 11:30:00 +00:00
|
|
|
{
|
2017-10-25 10:53:31 +00:00
|
|
|
assert(this->defined);
|
2018-08-17 13:53:43 +00:00
|
|
|
double x = this->max(0) - this->min(0);
|
|
|
|
double y = this->max(1) - this->min(1);
|
2016-09-13 11:30:00 +00:00
|
|
|
return 0.5 * sqrt(x*x+y*y);
|
|
|
|
}
|
|
|
|
template double BoundingBoxBase<Point>::radius() const;
|
2018-08-21 19:05:24 +00:00
|
|
|
template double BoundingBoxBase<Vec2d>::radius() const;
|
2016-09-13 11:30:00 +00:00
|
|
|
|
2017-10-25 10:53:31 +00:00
|
|
|
template <class PointClass> double BoundingBox3Base<PointClass>::radius() const
|
2016-09-13 11:30:00 +00:00
|
|
|
{
|
2018-08-17 13:53:43 +00:00
|
|
|
double x = this->max(0) - this->min(0);
|
|
|
|
double y = this->max(1) - this->min(1);
|
|
|
|
double z = this->max(2) - this->min(2);
|
2016-09-13 11:30:00 +00:00
|
|
|
return 0.5 * sqrt(x*x+y*y+z*z);
|
|
|
|
}
|
2018-08-21 15:43:05 +00:00
|
|
|
template double BoundingBox3Base<Vec3d>::radius() const;
|
2016-09-13 11:30:00 +00:00
|
|
|
|
2014-07-24 16:32:07 +00:00
|
|
|
template <class PointClass> void
|
|
|
|
BoundingBoxBase<PointClass>::offset(coordf_t delta)
|
|
|
|
{
|
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
|
|
|
PointClass v(delta, delta);
|
|
|
|
this->min -= v;
|
|
|
|
this->max += v;
|
2014-07-24 16:32:07 +00:00
|
|
|
}
|
|
|
|
template void BoundingBoxBase<Point>::offset(coordf_t delta);
|
2018-08-21 19:05:24 +00:00
|
|
|
template void BoundingBoxBase<Vec2d>::offset(coordf_t delta);
|
2014-07-24 16:32:07 +00:00
|
|
|
|
|
|
|
template <class PointClass> void
|
|
|
|
BoundingBox3Base<PointClass>::offset(coordf_t delta)
|
|
|
|
{
|
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
|
|
|
PointClass v(delta, delta, delta);
|
|
|
|
this->min -= v;
|
|
|
|
this->max += v;
|
2014-07-24 16:32:07 +00:00
|
|
|
}
|
2018-08-21 15:43:05 +00:00
|
|
|
template void BoundingBox3Base<Vec3d>::offset(coordf_t delta);
|
2014-07-24 16:32:07 +00:00
|
|
|
|
2014-01-06 17:29:10 +00:00
|
|
|
template <class PointClass> PointClass
|
2014-01-07 11:48:09 +00:00
|
|
|
BoundingBoxBase<PointClass>::center() const
|
2014-01-06 17:29:10 +00:00
|
|
|
{
|
2018-08-21 15:43:05 +00:00
|
|
|
return (this->min + this->max) / 2;
|
2014-01-06 17:29:10 +00:00
|
|
|
}
|
2014-01-07 11:48:09 +00:00
|
|
|
template Point BoundingBoxBase<Point>::center() const;
|
2018-08-21 19:05:24 +00:00
|
|
|
template Vec2d BoundingBoxBase<Vec2d>::center() const;
|
2014-01-06 17:29:10 +00:00
|
|
|
|
|
|
|
template <class PointClass> PointClass
|
|
|
|
BoundingBox3Base<PointClass>::center() const
|
|
|
|
{
|
2018-08-21 15:43:05 +00:00
|
|
|
return (this->min + this->max) / 2;
|
2014-01-06 17:29:10 +00:00
|
|
|
}
|
2018-08-21 15:43:05 +00:00
|
|
|
template Vec3d BoundingBox3Base<Vec3d>::center() const;
|
2014-01-06 17:29:10 +00:00
|
|
|
|
2018-05-15 07:50:01 +00:00
|
|
|
template <class PointClass> coordf_t
|
|
|
|
BoundingBox3Base<PointClass>::max_size() const
|
|
|
|
{
|
|
|
|
PointClass s = size();
|
2018-08-17 13:53:43 +00:00
|
|
|
return std::max(s(0), std::max(s(1), s(2)));
|
2018-05-15 07:50:01 +00:00
|
|
|
}
|
2018-08-21 15:43:05 +00:00
|
|
|
template coordf_t BoundingBox3Base<Vec3d>::max_size() const;
|
2018-05-15 07:50:01 +00:00
|
|
|
|
2016-11-29 18:25:10 +00:00
|
|
|
// Align a coordinate to a grid. The coordinate may be negative,
|
|
|
|
// the aligned value will never be bigger than the original one.
|
|
|
|
static inline coord_t _align_to_grid(const coord_t coord, const coord_t spacing) {
|
|
|
|
// Current C++ standard defines the result of integer division to be rounded to zero,
|
|
|
|
// for both positive and negative numbers. Here we want to round down for negative
|
|
|
|
// numbers as well.
|
|
|
|
coord_t aligned = (coord < 0) ?
|
|
|
|
((coord - spacing + 1) / spacing) * spacing :
|
|
|
|
(coord / spacing) * spacing;
|
|
|
|
assert(aligned <= coord);
|
|
|
|
return aligned;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoundingBox::align_to_grid(const coord_t cell_size)
|
|
|
|
{
|
|
|
|
if (this->defined) {
|
2018-08-17 13:53:43 +00:00
|
|
|
min(0) = _align_to_grid(min(0), cell_size);
|
|
|
|
min(1) = _align_to_grid(min(1), cell_size);
|
2016-11-29 18:25:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 13:37:38 +00:00
|
|
|
BoundingBoxf3 BoundingBoxf3::transformed(const Transform3d& matrix) const
|
2018-06-21 06:37:04 +00:00
|
|
|
{
|
2018-08-23 13:37:38 +00:00
|
|
|
typedef Eigen::Matrix<double, 3, 8, Eigen::DontAlign> Vertices;
|
|
|
|
|
|
|
|
Vertices src_vertices;
|
|
|
|
src_vertices(0, 0) = min(0); src_vertices(1, 0) = min(1); src_vertices(2, 0) = min(2);
|
|
|
|
src_vertices(0, 1) = max(0); src_vertices(1, 1) = min(1); src_vertices(2, 1) = min(2);
|
|
|
|
src_vertices(0, 2) = max(0); src_vertices(1, 2) = max(1); src_vertices(2, 2) = min(2);
|
|
|
|
src_vertices(0, 3) = min(0); src_vertices(1, 3) = max(1); src_vertices(2, 3) = min(2);
|
|
|
|
src_vertices(0, 4) = min(0); src_vertices(1, 4) = min(1); src_vertices(2, 4) = max(2);
|
|
|
|
src_vertices(0, 5) = max(0); src_vertices(1, 5) = min(1); src_vertices(2, 5) = max(2);
|
|
|
|
src_vertices(0, 6) = max(0); src_vertices(1, 6) = max(1); src_vertices(2, 6) = max(2);
|
|
|
|
src_vertices(0, 7) = min(0); src_vertices(1, 7) = max(1); src_vertices(2, 7) = max(2);
|
|
|
|
|
|
|
|
Vertices dst_vertices = matrix * src_vertices.colwise().homogeneous();
|
|
|
|
|
|
|
|
Vec3d v_min(dst_vertices(0, 0), dst_vertices(1, 0), dst_vertices(2, 0));
|
|
|
|
Vec3d v_max = v_min;
|
2018-06-21 06:37:04 +00:00
|
|
|
|
|
|
|
for (int i = 1; i < 8; ++i)
|
|
|
|
{
|
2018-08-23 13:37:38 +00:00
|
|
|
for (int j = 0; j < 3; ++j)
|
|
|
|
{
|
|
|
|
v_min(j) = std::min(v_min(j), dst_vertices(j, i));
|
|
|
|
v_max(j) = std::max(v_max(j), dst_vertices(j, i));
|
|
|
|
}
|
2018-06-21 06:37:04 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 13:37:38 +00:00
|
|
|
return BoundingBoxf3(v_min, v_max);
|
2018-06-21 06:37:04 +00:00
|
|
|
}
|
|
|
|
|
2014-01-06 17:29:10 +00:00
|
|
|
}
|