#include "BoundingBox.hpp" #include #include #include namespace Slic3r { template BoundingBoxBase::BoundingBoxBase(const Points &points); template BoundingBoxBase::BoundingBoxBase(const std::vector &points); template BoundingBox3Base::BoundingBox3Base(const std::vector &points); void BoundingBox::polygon(Polygon* polygon) const { polygon->points = { this->min, { this->max.x(), this->min.y() }, this->max, { this->min.x(), this->max.y() } }; } Polygon BoundingBox::polygon() const { Polygon p; this->polygon(&p); return p; } BoundingBox BoundingBox::rotated(double angle) const { BoundingBox out; out.merge(this->min.rotated(angle)); out.merge(this->max.rotated(angle)); out.merge(Point(this->min.x(), this->max.y()).rotated(angle)); out.merge(Point(this->max.x(), this->min.y()).rotated(angle)); 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)); out.merge(Point(this->min.x(), this->max.y()).rotated(angle, center)); out.merge(Point(this->max.x(), this->min.y()).rotated(angle, center)); return out; } template void BoundingBoxBase::scale(double factor) { this->min *= factor; this->max *= factor; } template void BoundingBoxBase::scale(double factor); template void BoundingBoxBase::scale(double factor); template void BoundingBoxBase::scale(double factor); template void BoundingBoxBase::merge(const PointType &point) { if (this->defined) { this->min = this->min.cwiseMin(point); this->max = this->max.cwiseMax(point); } else { this->min = point; this->max = point; this->defined = true; } } template void BoundingBoxBase::merge(const Point &point); template void BoundingBoxBase::merge(const Vec2f &point); template void BoundingBoxBase::merge(const Vec2d &point); template void BoundingBoxBase::merge(const PointsType &points) { this->merge(BoundingBoxBase(points)); } template void BoundingBoxBase::merge(const Points &points); template void BoundingBoxBase::merge(const Pointfs &points); template void BoundingBoxBase::merge(const BoundingBoxBase &bb) { assert(bb.defined || bb.min.x() >= bb.max.x() || bb.min.y() >= bb.max.y()); if (bb.defined) { if (this->defined) { 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; } } } template void BoundingBoxBase::merge(const BoundingBoxBase &bb); template void BoundingBoxBase::merge(const BoundingBoxBase &bb); template void BoundingBoxBase::merge(const BoundingBoxBase &bb); template void BoundingBox3Base::merge(const PointType &point) { if (this->defined) { this->min = this->min.cwiseMin(point); this->max = this->max.cwiseMax(point); } else { this->min = point; this->max = point; this->defined = true; } } template void BoundingBox3Base::merge(const Vec3f &point); template void BoundingBox3Base::merge(const Vec3d &point); template void BoundingBox3Base::merge(const PointsType &points) { this->merge(BoundingBox3Base(points)); } template void BoundingBox3Base::merge(const Pointf3s &points); template void BoundingBox3Base::merge(const BoundingBox3Base &bb) { assert(bb.defined || bb.min.x() >= bb.max.x() || bb.min.y() >= bb.max.y() || bb.min.z() >= bb.max.z()); if (bb.defined) { if (this->defined) { 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; } } } template void BoundingBox3Base::merge(const BoundingBox3Base &bb); template PointType BoundingBoxBase::size() const { return this->max - this->min; } template Point BoundingBoxBase::size() const; template Vec2f BoundingBoxBase::size() const; template Vec2d BoundingBoxBase::size() const; template PointType BoundingBox3Base::size() const { return this->max - this->min; } template Vec3f BoundingBox3Base::size() const; template Vec3d BoundingBox3Base::size() const; template double BoundingBoxBase::radius() const { assert(this->defined); return 0.5 * (this->max - this->min).template cast().norm(); } template double BoundingBoxBase::radius() const; template double BoundingBoxBase::radius() const; template double BoundingBox3Base::radius() const { return 0.5 * (this->max - this->min).template cast().norm(); } template double BoundingBox3Base::radius() const; template void BoundingBoxBase::offset(coordf_t delta) { PointType v(delta, delta); this->min -= v; this->max += v; } template void BoundingBoxBase::offset(coordf_t delta); template void BoundingBoxBase::offset(coordf_t delta); template void BoundingBox3Base::offset(coordf_t delta) { PointType v(delta, delta, delta); this->min -= v; this->max += v; } template void BoundingBox3Base::offset(coordf_t delta); template PointType BoundingBoxBase::center() const { return (this->min + this->max) / 2; } template Point BoundingBoxBase::center() const; template Vec2f BoundingBoxBase::center() const; template Vec2d BoundingBoxBase::center() const; template PointType BoundingBox3Base::center() const { return (this->min + this->max) / 2; } template Vec3f BoundingBox3Base::center() const; template Vec3d BoundingBox3Base::center() const; template coordf_t BoundingBox3Base::max_size() const { PointType s = size(); return std::max(s.x(), std::max(s.y(), s.z())); } template coordf_t BoundingBox3Base::max_size() const; template coordf_t BoundingBox3Base::max_size() const; void BoundingBox::align_to_grid(const coord_t cell_size) { if (this->defined) { min.x() = Slic3r::align_to_grid(min.x(), cell_size); min.y() = Slic3r::align_to_grid(min.y(), cell_size); } } BoundingBoxf3 BoundingBoxf3::transformed(const Transform3d& matrix) const { typedef Eigen::Matrix Vertices; Vertices src_vertices; src_vertices(0, 0) = min.x(); src_vertices(1, 0) = min.y(); src_vertices(2, 0) = min.z(); src_vertices(0, 1) = max.x(); src_vertices(1, 1) = min.y(); src_vertices(2, 1) = min.z(); src_vertices(0, 2) = max.x(); src_vertices(1, 2) = max.y(); src_vertices(2, 2) = min.z(); src_vertices(0, 3) = min.x(); src_vertices(1, 3) = max.y(); src_vertices(2, 3) = min.z(); src_vertices(0, 4) = min.x(); src_vertices(1, 4) = min.y(); src_vertices(2, 4) = max.z(); src_vertices(0, 5) = max.x(); src_vertices(1, 5) = min.y(); src_vertices(2, 5) = max.z(); src_vertices(0, 6) = max.x(); src_vertices(1, 6) = max.y(); src_vertices(2, 6) = max.z(); src_vertices(0, 7) = min.x(); src_vertices(1, 7) = max.y(); src_vertices(2, 7) = max.z(); 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; for (int i = 1; i < 8; ++i) { 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)); } } return BoundingBoxf3(v_min, v_max); } }