2019-06-06 12:27:07 +00:00
|
|
|
#include "MinAreaBoundingBox.hpp"
|
|
|
|
|
|
|
|
#include <libslic3r/ExPolygon.hpp>
|
2019-08-01 14:03:52 +00:00
|
|
|
|
|
|
|
#if defined(_MSC_VER) && defined(__clang__)
|
|
|
|
#define BOOST_NO_CXX17_HDR_STRING_VIEW
|
|
|
|
#endif
|
|
|
|
|
2019-06-06 12:27:07 +00:00
|
|
|
#include <boost/rational.hpp>
|
|
|
|
|
|
|
|
#include <libslic3r/Int128.hpp>
|
|
|
|
|
|
|
|
#if !defined(HAS_INTRINSIC_128_TYPE) || defined(__APPLE__)
|
|
|
|
#include <boost/multiprecision/integer.hpp>
|
|
|
|
#endif
|
|
|
|
|
2021-04-21 15:20:20 +00:00
|
|
|
#include <libnest2d/backends/libslic3r/geometries.hpp>
|
2019-06-06 12:27:07 +00:00
|
|
|
#include <libnest2d/utils/rotcalipers.hpp>
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
// Used as compute type.
|
|
|
|
using Unit = int64_t;
|
|
|
|
|
|
|
|
#if !defined(HAS_INTRINSIC_128_TYPE) || defined(__APPLE__)
|
|
|
|
using Rational = boost::rational<boost::multiprecision::int128_t>;
|
|
|
|
#else
|
|
|
|
using Rational = boost::rational<__int128>;
|
|
|
|
#endif
|
|
|
|
|
2021-04-21 15:20:20 +00:00
|
|
|
template<class P>
|
|
|
|
libnest2d::RotatedBox<Point, Unit> minAreaBoundigBox_(
|
|
|
|
const P &p, MinAreaBoundigBox::PolygonLevel lvl)
|
2019-06-06 12:27:07 +00:00
|
|
|
{
|
2021-04-21 15:20:20 +00:00
|
|
|
P chull = lvl == MinAreaBoundigBox::pcConvex ?
|
|
|
|
p :
|
|
|
|
libnest2d::sl::convexHull(p);
|
|
|
|
|
|
|
|
libnest2d::removeCollinearPoints(chull);
|
2019-06-26 08:33:42 +00:00
|
|
|
|
2021-04-21 15:20:20 +00:00
|
|
|
return libnest2d::minAreaBoundingBox<P, Unit, Rational>(chull);
|
|
|
|
}
|
|
|
|
|
|
|
|
MinAreaBoundigBox::MinAreaBoundigBox(const Polygon &p, PolygonLevel pc)
|
|
|
|
{
|
|
|
|
libnest2d::RotatedBox<Point, Unit> box = minAreaBoundigBox_(p, pc);
|
2019-06-26 08:33:42 +00:00
|
|
|
|
|
|
|
m_right = libnest2d::cast<long double>(box.right_extent());
|
|
|
|
m_bottom = libnest2d::cast<long double>(box.bottom_extent());
|
|
|
|
m_axis = box.axis();
|
2019-06-06 12:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MinAreaBoundigBox::MinAreaBoundigBox(const ExPolygon &p, PolygonLevel pc)
|
|
|
|
{
|
2021-04-21 15:20:20 +00:00
|
|
|
libnest2d::RotatedBox<Point, Unit> box = minAreaBoundigBox_(p, pc);
|
2019-06-26 08:33:42 +00:00
|
|
|
|
|
|
|
m_right = libnest2d::cast<long double>(box.right_extent());
|
|
|
|
m_bottom = libnest2d::cast<long double>(box.bottom_extent());
|
|
|
|
m_axis = box.axis();
|
2019-06-06 12:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MinAreaBoundigBox::MinAreaBoundigBox(const Points &pts, PolygonLevel pc)
|
|
|
|
{
|
2021-04-21 15:20:20 +00:00
|
|
|
libnest2d::RotatedBox<Point, Unit> box = minAreaBoundigBox_(pts, pc);
|
2019-06-26 08:33:42 +00:00
|
|
|
|
|
|
|
m_right = libnest2d::cast<long double>(box.right_extent());
|
|
|
|
m_bottom = libnest2d::cast<long double>(box.bottom_extent());
|
|
|
|
m_axis = box.axis();
|
2019-06-06 12:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double MinAreaBoundigBox::angle_to_X() const
|
|
|
|
{
|
|
|
|
double ret = std::atan2(m_axis.y(), m_axis.x());
|
2019-06-26 08:33:42 +00:00
|
|
|
auto s = std::signbit(ret);
|
|
|
|
if (s) ret += 2 * PI;
|
2019-06-06 12:27:07 +00:00
|
|
|
return -ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
long double MinAreaBoundigBox::width() const
|
|
|
|
{
|
2019-06-26 08:33:42 +00:00
|
|
|
return std::abs(m_bottom) /
|
|
|
|
std::sqrt(libnest2d::pl::magnsq<Point, long double>(m_axis));
|
2019-06-06 12:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
long double MinAreaBoundigBox::height() const
|
|
|
|
{
|
2019-06-26 08:33:42 +00:00
|
|
|
return std::abs(m_right) /
|
|
|
|
std::sqrt(libnest2d::pl::magnsq<Point, long double>(m_axis));
|
2019-06-06 12:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
long double MinAreaBoundigBox::area() const
|
|
|
|
{
|
|
|
|
long double asq = libnest2d::pl::magnsq<Point, long double>(m_axis);
|
2019-06-26 08:33:42 +00:00
|
|
|
return m_bottom * m_right / asq;
|
2019-06-06 12:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void remove_collinear_points(Polygon &p)
|
|
|
|
{
|
|
|
|
p = libnest2d::removeCollinearPoints<Polygon>(p, Unit(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_collinear_points(ExPolygon &p)
|
|
|
|
{
|
|
|
|
p = libnest2d::removeCollinearPoints<ExPolygon>(p, Unit(0));
|
|
|
|
}
|
2019-06-26 08:33:42 +00:00
|
|
|
} // namespace Slic3r
|