LMPacker indents fix

This commit is contained in:
MihailRis
2024-03-08 22:31:32 +03:00
parent 07dc12143e
commit 93979b2e53
2 changed files with 124 additions and 124 deletions
+94 -94
View File
@@ -3,83 +3,83 @@
#include <algorithm> #include <algorithm>
inline int getPackerScore(rectangle& rect) { inline int getPackerScore(rectangle& rect) {
if (rect.width * rect.height > 100) if (rect.width * rect.height > 100)
return rect.height * rect.height * 1000; return rect.height * rect.height * 1000;
return (rect.width * rect.height * rect.height); return (rect.width * rect.height * rect.height);
} }
LMPacker::LMPacker(const uint32_t sizes[], size_t length) { LMPacker::LMPacker(const uint32_t sizes[], size_t length) {
for (unsigned int i = 0; i < length/2; i++) { for (unsigned int i = 0; i < length/2; i++) {
rectangle rect(i, 0, 0, (int)sizes[i * 2], (int)sizes[i * 2 + 1]); rectangle rect(i, 0, 0, (int)sizes[i * 2], (int)sizes[i * 2 + 1]);
rects.push_back(rect); rects.push_back(rect);
} }
sort(rects.begin(), rects.end(), [](rectangle a, rectangle b) { sort(rects.begin(), rects.end(), [](rectangle a, rectangle b) {
return -getPackerScore(a) < -getPackerScore(b); return -getPackerScore(a) < -getPackerScore(b);
}); });
} }
LMPacker::~LMPacker() { LMPacker::~LMPacker() {
if (matrix) { if (matrix) {
for (unsigned int y = 0; y < (height >> mbit); y++) { for (unsigned int y = 0; y < (height >> mbit); y++) {
delete[] matrix[y]; delete[] matrix[y];
} }
delete[] matrix; delete[] matrix;
} }
} }
void LMPacker::cleanup() { void LMPacker::cleanup() {
placed.clear(); placed.clear();
} }
bool LMPacker::build(uint32_t width, uint32_t height, bool LMPacker::build(uint32_t width, uint32_t height,
uint16_t extension, uint32_t mbit, uint32_t vstep) { uint16_t extension, uint32_t mbit, uint32_t vstep) {
cleanup(); cleanup();
this->mbit = mbit; this->mbit = mbit;
this->width = width; this->width = width;
this->height = height; this->height = height;
int mpix = 1 << mbit; int mpix = 1 << mbit;
const unsigned int mwidth = width >> mbit; const unsigned int mwidth = width >> mbit;
const unsigned int mheight = height >> mbit; const unsigned int mheight = height >> mbit;
matrix = new rectangle**[mheight]; matrix = new rectangle**[mheight];
for (unsigned int y = 0; y < mheight; y++) { for (unsigned int y = 0; y < mheight; y++) {
matrix[y] = new rectangle*[mwidth]; matrix[y] = new rectangle*[mwidth];
for (unsigned int x = 0; x < mwidth; x++) { for (unsigned int x = 0; x < mwidth; x++) {
matrix[y][x] = nullptr; matrix[y][x] = nullptr;
} }
} }
for (unsigned int i = 0; i < rects.size(); i++) { for (unsigned int i = 0; i < rects.size(); i++) {
rectangle& rect = rects[i]; rectangle& rect = rects[i];
rect = rectangle(rect.idx, 0, 0, rect.width, rect.height); rect = rectangle(rect.idx, 0, 0, rect.width, rect.height);
rect.width += extension * 2; rect.width += extension * 2;
rect.height += extension * 2; rect.height += extension * 2;
if (mpix > 1) { if (mpix > 1) {
if (rect.width % mpix > 0) { if (rect.width % mpix > 0) {
rect.extX = mpix - (rect.width % mpix); rect.extX = mpix - (rect.width % mpix);
} }
if (rect.height % mpix > 0) { if (rect.height % mpix > 0) {
rect.extY = mpix - (rect.height % mpix); rect.extY = mpix - (rect.height % mpix);
} }
} }
rect.width += rect.extX; rect.width += rect.extX;
rect.height += rect.extY; rect.height += rect.extY;
} }
bool built = true; bool built = true;
for (unsigned int i = 0; i < rects.size(); i++) { for (unsigned int i = 0; i < rects.size(); i++) {
rectangle* rect = &rects[i]; rectangle* rect = &rects[i];
if (!place(rect, vstep)) { if (!place(rect, vstep)) {
built = false; built = false;
break; break;
} }
} }
for (unsigned int i = 0; i < rects.size(); i++) { for (unsigned int i = 0; i < rects.size(); i++) {
rectangle& rect = rects[i]; rectangle& rect = rects[i];
rect.x += extension; rect.x += extension;
rect.y += extension; rect.y += extension;
rect.width -= extension * 2 + rect.extX; rect.width -= extension * 2 + rect.extX;
rect.height -= extension * 2 + rect.extY; rect.height -= extension * 2 + rect.extY;
} }
return built; return built;
} }
inline rectangle* findCollision(rectangle*** matrix, int x, int y, int w, int h) { inline rectangle* findCollision(rectangle*** matrix, int x, int y, int w, int h) {
@@ -103,47 +103,47 @@ inline void fill(rectangle*** matrix, rectangle* rect, int x, int y, int w, int
} }
bool LMPacker::place(rectangle* rectptr, uint32_t vstep) { bool LMPacker::place(rectangle* rectptr, uint32_t vstep) {
rectangle& rect = *rectptr; rectangle& rect = *rectptr;
const unsigned int rw = rect.width >> mbit; const unsigned int rw = rect.width >> mbit;
const unsigned int rh = rect.height >> mbit; const unsigned int rh = rect.height >> mbit;
if (vstep > 1) { if (vstep > 1) {
vstep = (vstep > rh ? vstep : rh); vstep = (vstep > rh ? vstep : rh);
} }
const unsigned int mwidth = width >> mbit; const unsigned int mwidth = width >> mbit;
const unsigned int mheight = height >> mbit; const unsigned int mheight = height >> mbit;
for (unsigned int y = 0; y + rh < mheight; y += vstep) { for (unsigned int y = 0; y + rh < mheight; y += vstep) {
rectangle** line = matrix[y]; rectangle** line = matrix[y];
bool skiplines = true; bool skiplines = true;
rectangle** lower = matrix[y + rh - 1]; rectangle** lower = matrix[y + rh - 1];
for (unsigned int x = 0; x + rw < mwidth; x++) { for (unsigned int x = 0; x + rw < mwidth; x++) {
rectangle* prect = line[x]; rectangle* prect = line[x];
if (prect) { if (prect) {
x = (prect->x >> mbit) + (prect->width >> mbit) - 1; x = (prect->x >> mbit) + (prect->width >> mbit) - 1;
} else { } else {
if (skiplines) { if (skiplines) {
unsigned int lfree = 0; unsigned int lfree = 0;
while (lfree + x < mwidth && !lower[x + lfree] && lfree < rw) { while (lfree + x < mwidth && !lower[x + lfree] && lfree < rw) {
lfree++; lfree++;
} }
if (lfree >= rw) if (lfree >= rw)
skiplines = false; skiplines = false;
} }
prect = findCollision(matrix, x, y, rw, rh); prect = findCollision(matrix, x, y, rw, rh);
if (prect) { if (prect) {
x = (prect->x >> mbit) + (prect->width >> mbit) - 1; x = (prect->x >> mbit) + (prect->width >> mbit) - 1;
continue; continue;
} }
fill(matrix, rectptr, x, y, rw, rh); fill(matrix, rectptr, x, y, rw, rh);
rectptr->x = x << mbit; rectptr->x = x << mbit;
rectptr->y = y << mbit; rectptr->y = y << mbit;
placed.push_back(rectptr); placed.push_back(rectptr);
return true; return true;
} }
} }
if (skiplines) { if (skiplines) {
y += rh - vstep; y += rh - vstep;
} }
} }
return false; return false;
} }
+30 -30
View File
@@ -10,44 +10,44 @@
#include <vector> #include <vector>
struct rectangle { struct rectangle {
unsigned int idx; unsigned int idx;
int x; int x;
int y; int y;
int width; int width;
int height; int height;
int extX = 0; int extX = 0;
int extY = 0; int extY = 0;
rectangle(unsigned int idx, int x, int y, int width, int height) rectangle(unsigned int idx, int x, int y, int width, int height)
: idx(idx), x(x), y(y), width(width), height(height){ : idx(idx), x(x), y(y), width(width), height(height){
} }
}; };
class LMPacker { class LMPacker {
std::vector<rectangle> rects; std::vector<rectangle> rects;
std::vector<rectangle*> placed; std::vector<rectangle*> placed;
uint32_t width = 0; uint32_t width = 0;
uint32_t height = 0; uint32_t height = 0;
rectangle*** matrix = nullptr; rectangle*** matrix = nullptr;
uint32_t mbit = 0; uint32_t mbit = 0;
void cleanup(); void cleanup();
bool place(rectangle* rect, uint32_t vstep); bool place(rectangle* rect, uint32_t vstep);
public: public:
LMPacker(const uint32_t sizes[], size_t length); LMPacker(const uint32_t sizes[], size_t length);
virtual ~LMPacker(); virtual ~LMPacker();
bool buildCompact(uint32_t width, uint32_t height, uint16_t extension) { bool buildCompact(uint32_t width, uint32_t height, uint16_t extension) {
return build(width, height, extension, 0, 1); return build(width, height, extension, 0, 1);
} }
bool buildFast(uint32_t width, uint32_t height, uint16_t extension) { bool buildFast(uint32_t width, uint32_t height, uint16_t extension) {
return build(width, height, extension, 1, 2); return build(width, height, extension, 1, 2);
} }
bool build(uint32_t width, uint32_t height, uint16_t extension, uint32_t mbit, uint32_t vstep); bool build(uint32_t width, uint32_t height, uint16_t extension, uint32_t mbit, uint32_t vstep);
std::vector<rectangle> getResult() { std::vector<rectangle> getResult() {
return rects; return rects;
} }
}; };
#endif /* LMPACKER_H_ */ #endif /* LMPACKER_H_ */