2019-10-20 21:46:36 +02:00
|
|
|
#include "pack.h"
|
|
|
|
#include <algorithm>
|
2019-10-21 00:06:09 +02:00
|
|
|
#include <cstring>
|
2019-10-20 21:46:36 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
bool area(rect_xywhf *a, rect_xywhf *b) {
|
2019-10-20 21:46:36 +02:00
|
|
|
return a->area() > b->area();
|
|
|
|
}
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
bool perimeter(rect_xywhf *a, rect_xywhf *b) {
|
2019-10-20 21:46:36 +02:00
|
|
|
return a->perimeter() > b->perimeter();
|
|
|
|
}
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
bool max_side(rect_xywhf *a, rect_xywhf *b) {
|
2019-10-20 21:46:36 +02:00
|
|
|
return std::max(a->w, a->h) > std::max(b->w, b->h);
|
|
|
|
}
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
bool max_width(rect_xywhf *a, rect_xywhf *b) {
|
2019-10-20 21:46:36 +02:00
|
|
|
return a->w > b->w;
|
|
|
|
}
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
bool max_height(rect_xywhf *a, rect_xywhf *b) {
|
2019-10-20 21:46:36 +02:00
|
|
|
return a->h > b->h;
|
|
|
|
}
|
|
|
|
|
|
|
|
// just add another comparing function name to cmpf to perform another packing attempt
|
|
|
|
// more functions == slower but probably more efficient cases covered and hence less area wasted
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
bool (*cmpf[])(rect_xywhf *, rect_xywhf *) = {
|
|
|
|
area,
|
|
|
|
perimeter,
|
|
|
|
max_side,
|
2019-10-20 21:46:36 +02:00
|
|
|
max_width,
|
|
|
|
max_height
|
|
|
|
};
|
|
|
|
|
|
|
|
// if you find the algorithm running too slow you may double this factor to increase speed but also decrease efficiency
|
|
|
|
// 1 == most efficient, slowest
|
|
|
|
// efficiency may be still satisfying at 64 or even 256 with nice speedup
|
|
|
|
|
|
|
|
int discard_step = 128;
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
For every sorting function, algorithm will perform packing attempts beginning with a bin with width and height equal to max_side,
|
|
|
|
and decreasing its dimensions if it finds out that rectangles did actually fit, increasing otherwise.
|
|
|
|
Although, it's doing that in sort of binary search manner, so for every comparing function it will perform at most log2(max_side) packing attempts looking for the smallest possible bin size.
|
|
|
|
discard_step = 128 means that the algorithm will break of the searching loop if the rectangles fit but "it may be possible to fit them in a bin smaller by 128"
|
|
|
|
the bigger the value, the sooner the algorithm will finish but the rectangles will be packed less tightly.
|
|
|
|
use discard_step = 1 for maximum tightness.
|
|
|
|
|
|
|
|
the algorithm was based on http://www.blackpawn.com/texts/lightmaps/default.html
|
|
|
|
the algorithm reuses the node tree so it doesn't reallocate them between searching attempts
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*************************************************************************** CHAOS BEGINS HERE */
|
|
|
|
|
|
|
|
struct node {
|
|
|
|
struct pnode {
|
2019-10-21 00:06:09 +02:00
|
|
|
node *pn = nullptr;
|
2019-10-20 21:46:36 +02:00
|
|
|
bool fill = false;
|
|
|
|
|
|
|
|
void set(int l, int t, int r, int b) {
|
2019-10-21 00:06:09 +02:00
|
|
|
if (!pn)
|
|
|
|
pn = new node(rect_ltrb(l, t, r, b));
|
2019-10-20 21:46:36 +02:00
|
|
|
else {
|
|
|
|
(*pn).rc = rect_ltrb(l, t, r, b);
|
|
|
|
(*pn).id = false;
|
|
|
|
}
|
|
|
|
fill = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pnode c[2];
|
|
|
|
rect_ltrb rc;
|
|
|
|
bool id = false;
|
2019-10-21 00:06:09 +02:00
|
|
|
node(rect_ltrb rc = rect_ltrb()) :
|
|
|
|
rc(rc) {}
|
2019-10-20 21:46:36 +02:00
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
void reset(const rect_wh &r) {
|
2019-10-20 21:46:36 +02:00
|
|
|
id = false;
|
|
|
|
rc = rect_ltrb(0, 0, r.w, r.h);
|
|
|
|
delcheck();
|
|
|
|
}
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
node *insert(rect_xywhf &img, bool allowFlip) {
|
|
|
|
if (c[0].pn && c[0].fill) {
|
|
|
|
if (auto newn = c[0].pn->insert(img, allowFlip)) return newn;
|
|
|
|
return c[1].pn->insert(img, allowFlip);
|
2019-10-20 21:46:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
if (id) return 0;
|
|
|
|
int f = img.fits(rect_xywh(rc), allowFlip);
|
|
|
|
|
|
|
|
switch (f) {
|
|
|
|
case 0: return 0;
|
|
|
|
case 1: img.flipped = false; break;
|
|
|
|
case 2: img.flipped = true; break;
|
|
|
|
case 3:
|
|
|
|
id = true;
|
|
|
|
img.flipped = false;
|
|
|
|
return this;
|
|
|
|
case 4:
|
|
|
|
id = true;
|
|
|
|
img.flipped = true;
|
|
|
|
return this;
|
2019-10-20 21:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int iw = (img.flipped ? img.h : img.w), ih = (img.flipped ? img.w : img.h);
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
if (rc.w() - iw > rc.h() - ih) {
|
|
|
|
c[0].set(rc.l, rc.t, rc.l + iw, rc.b);
|
|
|
|
c[1].set(rc.l + iw, rc.t, rc.r, rc.b);
|
|
|
|
} else {
|
2019-10-20 21:46:36 +02:00
|
|
|
c[0].set(rc.l, rc.t, rc.r, rc.t + ih);
|
|
|
|
c[1].set(rc.l, rc.t + ih, rc.r, rc.b);
|
|
|
|
}
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
return c[0].pn->insert(img, allowFlip);
|
2019-10-20 21:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void delcheck() {
|
2019-10-21 00:06:09 +02:00
|
|
|
if (c[0].pn) {
|
|
|
|
c[0].fill = false;
|
|
|
|
c[0].pn->delcheck();
|
|
|
|
}
|
|
|
|
if (c[1].pn) {
|
|
|
|
c[1].fill = false;
|
|
|
|
c[1].pn->delcheck();
|
|
|
|
}
|
2019-10-20 21:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~node() {
|
2019-10-21 00:06:09 +02:00
|
|
|
if (c[0].pn) delete c[0].pn;
|
|
|
|
if (c[1].pn) delete c[1].pn;
|
2019-10-20 21:46:36 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
rect_wh _rect2D(rect_xywhf *const *v, int n, int max_s, bool allowFlip, vector<rect_xywhf *> &succ, vector<rect_xywhf *> &unsucc) {
|
2019-10-20 21:46:36 +02:00
|
|
|
node root;
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
const int funcs = (sizeof(cmpf) / sizeof(bool (*)(rect_xywhf *, rect_xywhf *)));
|
2019-10-20 21:46:36 +02:00
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
rect_xywhf **order[funcs];
|
2019-10-20 21:46:36 +02:00
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
for (int f = 0; f < funcs; ++f) {
|
|
|
|
order[f] = new rect_xywhf *[n];
|
|
|
|
std::memcpy(order[f], v, sizeof(rect_xywhf *) * n);
|
|
|
|
sort(order[f], order[f] + n, cmpf[f]);
|
2019-10-20 21:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rect_wh min_bin = rect_wh(max_s, max_s);
|
|
|
|
int min_func = -1, best_func = 0, best_area = 0, _area = 0, step, fit, i;
|
|
|
|
|
|
|
|
bool fail = false;
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
for (int f = 0; f < funcs; ++f) {
|
2019-10-20 21:46:36 +02:00
|
|
|
v = order[f];
|
|
|
|
step = min_bin.w / 2;
|
|
|
|
root.reset(min_bin);
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
while (true) {
|
|
|
|
if (root.rc.w() > min_bin.w) {
|
|
|
|
if (min_func > -1) break;
|
2019-10-20 21:46:36 +02:00
|
|
|
_area = 0;
|
|
|
|
|
|
|
|
root.reset(min_bin);
|
2019-10-21 00:06:09 +02:00
|
|
|
for (i = 0; i < n; ++i)
|
|
|
|
if (root.insert(*v[i], allowFlip))
|
2019-10-20 21:46:36 +02:00
|
|
|
_area += v[i]->area();
|
|
|
|
|
|
|
|
fail = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fit = -1;
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
for (i = 0; i < n; ++i)
|
|
|
|
if (!root.insert(*v[i], allowFlip)) {
|
2019-10-20 21:46:36 +02:00
|
|
|
fit = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
if (fit == -1 && step <= discard_step)
|
|
|
|
break;
|
2019-10-20 21:46:36 +02:00
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
root.reset(rect_wh(root.rc.w() + fit * step, root.rc.h() + fit * step));
|
2019-10-20 21:46:36 +02:00
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
step /= 2;
|
|
|
|
if (!step)
|
|
|
|
step = 1;
|
2019-10-20 21:46:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
if (!fail && (min_bin.area() >= root.rc.area())) {
|
2019-10-20 21:46:36 +02:00
|
|
|
min_bin = rect_wh(root.rc);
|
|
|
|
min_func = f;
|
|
|
|
}
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
else if (fail && (_area > best_area)) {
|
2019-10-20 21:46:36 +02:00
|
|
|
best_area = _area;
|
|
|
|
best_func = f;
|
|
|
|
}
|
|
|
|
fail = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
v = order[min_func == -1 ? best_func : min_func];
|
|
|
|
|
|
|
|
int clip_x = 0, clip_y = 0;
|
|
|
|
|
|
|
|
root.reset(min_bin);
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
if (auto ret = root.insert(*v[i], allowFlip)) {
|
2019-10-20 21:46:36 +02:00
|
|
|
v[i]->x = ret->rc.l;
|
|
|
|
v[i]->y = ret->rc.t;
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
if (v[i]->flipped) {
|
2019-10-20 21:46:36 +02:00
|
|
|
v[i]->flipped = false;
|
|
|
|
v[i]->flip();
|
|
|
|
}
|
|
|
|
|
|
|
|
clip_x = std::max(clip_x, ret->rc.r);
|
2019-10-21 00:06:09 +02:00
|
|
|
clip_y = std::max(clip_y, ret->rc.b);
|
2019-10-20 21:46:36 +02:00
|
|
|
|
|
|
|
succ.push_back(v[i]);
|
2019-10-21 00:06:09 +02:00
|
|
|
} else {
|
2019-10-20 21:46:36 +02:00
|
|
|
unsucc.push_back(v[i]);
|
|
|
|
|
|
|
|
v[i]->flipped = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
for (int f = 0; f < funcs; ++f)
|
|
|
|
delete[] order[f];
|
2019-10-20 21:46:36 +02:00
|
|
|
|
|
|
|
return rect_wh(clip_x, clip_y);
|
|
|
|
}
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
bool pack(rect_xywhf *const *v, int n, int max_s, bool allowFlip, vector<bin> &bins) {
|
2019-10-20 21:46:36 +02:00
|
|
|
rect_wh _rect(max_s, max_s);
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
if (!v[i]->fits(_rect, allowFlip)) return false;
|
2019-10-20 21:46:36 +02:00
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
vector<rect_xywhf *> vec[2], *p[2] = { vec, vec + 1 };
|
2019-10-20 21:46:36 +02:00
|
|
|
vec[0].resize(n);
|
|
|
|
vec[1].clear();
|
2019-10-21 00:06:09 +02:00
|
|
|
std::memcpy(&vec[0][0], v, sizeof(rect_xywhf *) * n);
|
2019-10-20 21:46:36 +02:00
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
bin *b = 0;
|
2019-10-20 21:46:36 +02:00
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
while (true) {
|
2019-10-20 21:46:36 +02:00
|
|
|
bins.push_back(bin());
|
2019-10-21 00:06:09 +02:00
|
|
|
b = &bins[bins.size() - 1];
|
2019-10-20 21:46:36 +02:00
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
b->size = _rect2D(&((*p[0])[0]), static_cast<int>(p[0]->size()), max_s, allowFlip, b->rects, *p[1]);
|
2019-10-20 21:46:36 +02:00
|
|
|
p[0]->clear();
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
if (!p[1]->size()) break;
|
2019-10-20 21:46:36 +02:00
|
|
|
|
|
|
|
std::swap(p[0], p[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
rect_wh::rect_wh(const rect_ltrb &rr) :
|
|
|
|
w(rr.w()),
|
|
|
|
h(rr.h()) {}
|
|
|
|
rect_wh::rect_wh(const rect_xywh &rr) :
|
|
|
|
w(rr.w),
|
|
|
|
h(rr.h) {}
|
|
|
|
rect_wh::rect_wh(int w, int h) :
|
|
|
|
w(w),
|
|
|
|
h(h) {}
|
|
|
|
|
|
|
|
int rect_wh::fits(const rect_wh &r, bool allowFlip) const {
|
|
|
|
if (w == r.w && h == r.h) return 3;
|
|
|
|
if (allowFlip && h == r.w && w == r.h) return 4;
|
|
|
|
if (w <= r.w && h <= r.h) return 1;
|
|
|
|
if (allowFlip && h <= r.w && w <= r.h) return 2;
|
2019-10-20 21:46:36 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
rect_ltrb::rect_ltrb() :
|
|
|
|
l(0),
|
|
|
|
t(0),
|
|
|
|
r(0),
|
|
|
|
b(0) {}
|
|
|
|
rect_ltrb::rect_ltrb(int l, int t, int r, int b) :
|
|
|
|
l(l),
|
|
|
|
t(t),
|
|
|
|
r(r),
|
|
|
|
b(b) {}
|
2019-10-20 21:46:36 +02:00
|
|
|
|
|
|
|
int rect_ltrb::w() const {
|
2019-10-21 00:06:09 +02:00
|
|
|
return r - l;
|
2019-10-20 21:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int rect_ltrb::h() const {
|
2019-10-21 00:06:09 +02:00
|
|
|
return b - t;
|
2019-10-20 21:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int rect_ltrb::area() const {
|
2019-10-21 00:06:09 +02:00
|
|
|
return w() * h();
|
2019-10-20 21:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int rect_ltrb::perimeter() const {
|
2019-10-21 00:06:09 +02:00
|
|
|
return 2 * w() + 2 * h();
|
2019-10-20 21:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void rect_ltrb::w(int ww) {
|
2019-10-21 00:06:09 +02:00
|
|
|
r = l + ww;
|
2019-10-20 21:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void rect_ltrb::h(int hh) {
|
2019-10-21 00:06:09 +02:00
|
|
|
b = t + hh;
|
2019-10-20 21:46:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
rect_xywh::rect_xywh() :
|
|
|
|
x(0),
|
|
|
|
y(0) {}
|
|
|
|
rect_xywh::rect_xywh(const rect_ltrb &rc) :
|
|
|
|
x(rc.l),
|
|
|
|
y(rc.t) {
|
|
|
|
b(rc.b);
|
|
|
|
r(rc.r);
|
|
|
|
}
|
|
|
|
rect_xywh::rect_xywh(int x, int y, int w, int h) :
|
|
|
|
rect_wh(w, h),
|
|
|
|
x(x),
|
|
|
|
y(y) {}
|
2019-10-20 21:46:36 +02:00
|
|
|
|
|
|
|
rect_xywh::operator rect_ltrb() {
|
|
|
|
rect_ltrb rr(x, y, 0, 0);
|
2019-10-21 00:06:09 +02:00
|
|
|
rr.w(w);
|
|
|
|
rr.h(h);
|
2019-10-20 21:46:36 +02:00
|
|
|
return rr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rect_xywh::r() const {
|
2019-10-21 00:06:09 +02:00
|
|
|
return x + w;
|
2019-10-20 21:46:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int rect_xywh::b() const {
|
2019-10-21 00:06:09 +02:00
|
|
|
return y + h;
|
2019-10-20 21:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void rect_xywh::r(int right) {
|
2019-10-21 00:06:09 +02:00
|
|
|
w = right - x;
|
2019-10-20 21:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void rect_xywh::b(int bottom) {
|
2019-10-21 00:06:09 +02:00
|
|
|
h = bottom - y;
|
2019-10-20 21:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int rect_wh::area() {
|
2019-10-21 00:06:09 +02:00
|
|
|
return w * h;
|
2019-10-20 21:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int rect_wh::perimeter() {
|
2019-10-21 00:06:09 +02:00
|
|
|
return 2 * w + 2 * h;
|
2019-10-20 21:46:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
rect_xywhf::rect_xywhf(const rect_ltrb &rr) :
|
|
|
|
rect_xywh(rr),
|
|
|
|
flipped(false) {}
|
|
|
|
rect_xywhf::rect_xywhf(int x, int y, int width, int height) :
|
|
|
|
rect_xywh(x, y, width, height),
|
|
|
|
flipped(false) {}
|
|
|
|
rect_xywhf::rect_xywhf() :
|
|
|
|
flipped(false) {}
|
2019-10-20 21:46:36 +02:00
|
|
|
|
2019-10-21 00:06:09 +02:00
|
|
|
void rect_xywhf::flip() {
|
2019-10-20 21:46:36 +02:00
|
|
|
flipped = !flipped;
|
|
|
|
std::swap(w, h);
|
2019-10-21 00:06:09 +02:00
|
|
|
}
|