Deal with infinite box.

This commit is contained in:
tamasmeszaros 2019-07-03 15:06:10 +02:00
parent 320f2ecefd
commit bc315f4c2c
6 changed files with 175 additions and 128 deletions

View file

@ -41,25 +41,25 @@ template<> struct HolesContainer<PolygonImpl> { using Type = ClipperLib::Paths;
namespace pointlike {
// Tell libnest2d how to extract the X coord from a ClipperPoint object
template<> inline TCoord<PointImpl> x(const PointImpl& p)
template<> inline ClipperLib::cInt x(const PointImpl& p)
{
return p.X;
}
// Tell libnest2d how to extract the Y coord from a ClipperPoint object
template<> inline TCoord<PointImpl> y(const PointImpl& p)
template<> inline ClipperLib::cInt y(const PointImpl& p)
{
return p.Y;
}
// Tell libnest2d how to extract the X coord from a ClipperPoint object
template<> inline TCoord<PointImpl>& x(PointImpl& p)
template<> inline ClipperLib::cInt& x(PointImpl& p)
{
return p.X;
}
// Tell libnest2d how to extract the Y coord from a ClipperPoint object
template<> inline TCoord<PointImpl>& y(PointImpl& p)
template<> inline ClipperLib::cInt& y(PointImpl& p)
{
return p.Y;
}

View file

@ -166,7 +166,9 @@ public:
using Tag = BoxTag;
using PointType = P;
inline _Box(const P& p = {TCoord<P>(0), TCoord<P>(0)});
inline _Box(const P& center = {TCoord<P>(0), TCoord<P>(0)}):
_Box(TCoord<P>(0), TCoord<P>(0), center) {}
inline _Box(const P& p, const P& pp):
PointPair<P>({p, pp}) {}
@ -189,6 +191,8 @@ public:
inline Unit area() const BP2D_NOEXCEPT {
return Unit(width())*height();
}
static inline _Box infinite(const P &center);
};
template<class S> struct PointType<_Box<S>> {
@ -463,12 +467,19 @@ inline _Box<P>::_Box(TCoord<P> width, TCoord<P> height, const P & center) :
modulo(height, TCoord<P>(2))}}) {}
template<class P>
inline _Box<P>::_Box(const P& center) {
inline _Box<P> _Box<P>::infinite(const P& center) {
using C = TCoord<P>;
TCoord<P> M = std::max(getX(center), getY(center)) -
std::numeric_limits<C>::lowest();
maxCorner() = center + P{M, M};
minCorner() = center - P{M, M};
_Box<P> ret;
// It is important for Mx and My to be strictly less than half of the
// range of type C. width(), height() and area() will not overflow this way.
C Mx = C((std::numeric_limits<C>::lowest() + 2 * getX(center)) / 2.01);
C My = C((std::numeric_limits<C>::lowest() + 2 * getY(center)) / 2.01);
ret.maxCorner() = center - P{Mx, My};
ret.minCorner() = center + P{Mx, My};
return ret;
}
template<class P>
@ -478,7 +489,7 @@ inline P _Box<P>::center() const BP2D_NOEXCEPT {
using Coord = TCoord<P>;
P ret = { // No rounding here, we dont know if these are int coords
P ret = { // No rounding here, we dont know if these are int coords
Coord( (getX(minc) + getX(maxc)) / Coord(2) ),
Coord( (getY(minc) + getY(maxc)) / Coord(2) )
};

View file

@ -581,8 +581,12 @@ public:
static inline double overfit(const Box& bb, const Box& bin)
{
auto wdiff = double(bb.width() - bin.width());
auto hdiff = double(bb.height() - bin.height());
auto Bw = bin.width();
auto Bh = bin.height();
auto mBw = -Bw;
auto mBh = -Bh;
auto wdiff = double(bb.width()) + mBw;
auto hdiff = double(bb.height()) + mBh;
double diff = 0;
if(wdiff > 0) diff += wdiff;
if(hdiff > 0) diff += hdiff;

View file

@ -379,6 +379,7 @@ TEST(GeometryAlgorithms, ArrangeRectanglesTight)
for(Item& r2 : result) {
if(&r1 != &r2 ) {
valid = !Item::intersects(r1, r2) || Item::touches(r1, r2);
ASSERT_TRUE(valid);
valid = (valid && !r1.isInside(r2) && !r2.isInside(r1));
ASSERT_TRUE(valid);
}