From 8cd3304c29b699a6da82d4ae54c33f7c8d373dbf Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 21 Apr 2013 12:02:13 -0700 Subject: [PATCH] Shave 23% of time off of atlastool. No need to check a bool, let's just use goto. It's worth being faster. --- tools/atlastool.cpp | 46 +++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/tools/atlastool.cpp b/tools/atlastool.cpp index 45e0095afa..484b458248 100644 --- a/tools/atlastool.cpp +++ b/tools/atlastool.cpp @@ -212,35 +212,36 @@ int NextPowerOf2(int x) { } struct Bucket { - vector, Data> > items; - void AddItem(const Image &img, const Data &dat) { - items.push_back(make_pair(img, dat)); - } - vector Resolve(int image_width, Image &dest) { - // Place all the little images - whatever they are. - // Uses greedy fill algorithm. Slow but works surprisingly well, CPUs are fast. - Image masq; - masq.resize(image_width, 1); - dest.resize(image_width, 1); - sort(items.begin(), items.end()); - for (int i = 0; i < (int)items.size(); i++) { + vector, Data> > items; + void AddItem(const Image &img, const Data &dat) { + items.push_back(make_pair(img, dat)); + } + vector Resolve(int image_width, Image &dest) { + // Place all the little images - whatever they are. + // Uses greedy fill algorithm. Slow but works surprisingly well, CPUs are fast. + Image masq; + masq.resize(image_width, 1); + dest.resize(image_width, 1); + sort(items.begin(), items.end()); + for (int i = 0; i < (int)items.size(); i++) { if ((i + 1) % 200 == 0) { printf("Resolving (%i / %i)\n", i, (int)items.size()); } - int idx = items[i].first.dat[0].size(); - int idy = items[i].first.dat.size(); + int idx = items[i].first.dat[0].size(); + int idy = items[i].first.dat.size(); if (idx > 1 && idy > 1) { CHECK(idx <= image_width); - bool found = false; - for (int ty = 0; ty < 2047 && !found; ty++) { + for (int ty = 0; ty < 2047; ty++) { if(ty + idy + 1 > (int)dest.dat.size()) { masq.resize(image_width, ty + idy + 16); dest.resize(image_width, ty + idy + 16); } // Brute force packing. int sz = (int)items[i].first.dat[0].size(); - for (int tx = 0; tx < image_width - sz && !found; tx++) { - bool valid = !(masq.dat[ty][tx] || masq.dat[ty + idy - 1][tx] || masq.dat[ty][tx + idx - 1] || masq.dat[ty + idy - 1][tx + idx - 1]); + auto &masq_ty = masq.dat[ty]; + auto &masq_idy = masq.dat[ty + idy - 1]; + for (int tx = 0; tx < image_width - sz; tx++) { + bool valid = !(masq_ty[tx] || masq_idy[tx] || masq_ty[tx + idx - 1] || masq_idy[tx + idx - 1]); if (valid) { for(int ity = 0; ity < idy && valid; ity++) { for(int itx = 0; itx < idx && valid; itx++) { @@ -258,16 +259,17 @@ struct Bucket { items[i].second.ex = tx + idx; items[i].second.ey = ty + idy; - found = true; - // printf("Placed %d at %dx%d-%dx%d\n", items[i].second.id, tx, ty, tx + idx, ty + idy); + goto found; } skip: ; } } - } - } +found: + ; + } + } if ((int)dest.dat.size() > image_width * 2) { printf("PACKING FAIL : height=%i", (int)dest.dat.size());