Small experiments, and ran clang-format on the lib.

This commit is contained in:
Relintai 2019-10-21 00:06:09 +02:00
parent 8629ce4c43
commit d36a770d22
4 changed files with 191 additions and 120 deletions

View File

@ -1,5 +1,22 @@
#include "merge_texture.h"
void MergeTexture::merge() {
const int RECTS = 200;
const bool ALLOW_FLIP = false;
Vector<rect_xywhf *> e;
//note: add a pointer pointer to the entry into rect_xywhf istead.
for (int i = 0; i < _entries.size(); ++i) {
e.push_back(&(_entries.get(i).rect));
}
if (pack(e.ptr(), RECTS, 400, ALLOW_FLIP, _bins)) {
print_error("ok");
}
}
String MergeTexture::test() {
String res = "";
@ -38,6 +55,7 @@ MergeTexture::MergeTexture() {
}
MergeTexture::~MergeTexture() {
_entries.clear();
}
void MergeTexture::_bind_methods() {

View File

@ -1,8 +1,11 @@
#ifndef MERGE_TEXTURE_H
#define MERGE_TEXTURE_H
#include <vector>
#include "scene/resources/texture.h"
#include "core/ustring.h"
#include "core/vector.h"
#include "rectpack2D/pack.h"
@ -10,13 +13,26 @@ class MergeTexture : public ImageTexture {
GDCLASS(MergeTexture, ImageTexture);
public:
void merge();
String test();
MergeTexture();
~MergeTexture();
public:
struct MergeTextureEntry {
Ref<Texture> original_texture;
Ref<AtlasTexture> texture;
rect_xywhf rect;
};
protected:
static void _bind_methods();
private:
std::vector<bin> _bins;
Vector<MergeTextureEntry> _entries;
};
#endif

View File

@ -1,6 +1,6 @@
#include "pack.h"
#include <cstring>
#include <algorithm>
#include <cstring>
using namespace std;
@ -24,7 +24,6 @@ bool max_height(rect_xywhf* a, rect_xywhf* b) {
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
@ -64,7 +63,8 @@ struct node {
bool fill = false;
void set(int l, int t, int r, int b) {
if(!pn) pn = new node(rect_ltrb(l, t, r, b));
if (!pn)
pn = new node(rect_ltrb(l, t, r, b));
else {
(*pn).rc = rect_ltrb(l, t, r, b);
(*pn).id = false;
@ -76,7 +76,8 @@ struct node {
pnode c[2];
rect_ltrb rc;
bool id = false;
node(rect_ltrb rc = rect_ltrb()) : rc(rc) {}
node(rect_ltrb rc = rect_ltrb()) :
rc(rc) {}
void reset(const rect_wh &r) {
id = false;
@ -97,8 +98,14 @@ struct node {
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;
case 3:
id = true;
img.flipped = false;
return this;
case 4:
id = true;
img.flipped = true;
return this;
}
int iw = (img.flipped ? img.h : img.w), ih = (img.flipped ? img.w : img.h);
@ -106,8 +113,7 @@ struct node {
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 {
} else {
c[0].set(rc.l, rc.t, rc.r, rc.t + ih);
c[1].set(rc.l, rc.t + ih, rc.r, rc.b);
}
@ -116,8 +122,14 @@ struct node {
}
void delcheck() {
if(c[0].pn) { c[0].fill = false; c[0].pn->delcheck(); }
if(c[1].pn) { c[1].fill = false; c[1].pn->delcheck(); }
if (c[0].pn) {
c[0].fill = false;
c[0].pn->delcheck();
}
if (c[1].pn) {
c[1].fill = false;
c[1].pn->delcheck();
}
}
~node() {
@ -213,8 +225,7 @@ rect_wh _rect2D(rect_xywhf* const * v, int n, int max_s, bool allowFlip, vector<
clip_y = std::max(clip_y, ret->rc.b);
succ.push_back(v[i]);
}
else {
} else {
unsucc.push_back(v[i]);
v[i]->flipped = false;
@ -227,7 +238,6 @@ rect_wh _rect2D(rect_xywhf* const * v, int n, int max_s, bool allowFlip, vector<
return rect_wh(clip_x, clip_y);
}
bool pack(rect_xywhf *const *v, int n, int max_s, bool allowFlip, vector<bin> &bins) {
rect_wh _rect(max_s, max_s);
@ -256,10 +266,15 @@ bool pack(rect_xywhf* const * v, int n, int max_s, bool allowFlip, vector<bin>&
return true;
}
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) {}
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;
@ -269,8 +284,16 @@ int rect_wh::fits(const rect_wh& r, bool allowFlip) const {
return 0;
}
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) {}
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) {}
int rect_ltrb::w() const {
return r - l;
@ -296,13 +319,24 @@ void rect_ltrb::h(int hh) {
b = t + hh;
}
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) {}
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) {}
rect_xywh::operator rect_ltrb() {
rect_ltrb rr(x, y, 0, 0);
rr.w(w); rr.h(h);
rr.w(w);
rr.h(h);
return rr;
}
@ -330,10 +364,14 @@ int rect_wh::perimeter() {
return 2 * w + 2 * h;
}
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) {}
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) {}
void rect_xywhf::flip() {
flipped = !flipped;

View File

@ -73,7 +73,6 @@ struct rect_xywhf : public rect_xywh {
bool flipped;
};
struct bin {
rect_wh size;
std::vector<rect_xywhf *> rects;