diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-03-17 02:05:42 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-03-17 02:05:42 +0000 |
commit | c915f1bcd46144578fad464e52b5a6013dc98de8 (patch) | |
tree | 4ad01cd4d1e4340fc2d46586a5ffa5c3ab642ccd | |
parent | Allow overriding vectorOfN's returned type (diff) | |
download | ilt-c915f1bcd46144578fad464e52b5a6013dc98de8.tar.bz2 ilt-c915f1bcd46144578fad464e52b5a6013dc98de8.tar.xz ilt-c915f1bcd46144578fad464e52b5a6013dc98de8.zip |
Fix texture packer return value so positions match inputs
-rw-r--r-- | assetFactory/texturePacker.cpp | 15 | ||||
-rw-r--r-- | assetFactory/texturePacker.h | 5 | ||||
-rw-r--r-- | test/test-assetFactory.cpp | 5 |
3 files changed, 15 insertions, 10 deletions
diff --git a/assetFactory/texturePacker.cpp b/assetFactory/texturePacker.cpp index 31c9a0e..68a6010 100644 --- a/assetFactory/texturePacker.cpp +++ b/assetFactory/texturePacker.cpp @@ -1,14 +1,16 @@ #include "texturePacker.h" +#include "collections.hpp" #include <algorithm> #include <cstdio> #include <numeric> #include <ostream> #include <set> -TexturePacker::TexturePacker(std::vector<Image> in) : inputImages {std::move(in)} +TexturePacker::TexturePacker(std::span<const Image> in) : + inputImages {std::move(in)}, sortedIndexes {vectorOfN(inputImages.size(), size_t {})} { - std::sort(inputImages.rbegin(), inputImages.rend(), [](const auto & a, const auto & b) { - return area(a) < area(b); + std::sort(sortedIndexes.rbegin(), sortedIndexes.rend(), [this](const auto a, const auto b) { + return area(inputImages[a]) < area(inputImages[b]); }); } @@ -24,15 +26,16 @@ TexturePacker::pack(Size size) const using Spaces = std::set<Space>; Spaces spaces {{{}, size}}; - Positions result; - for (const auto & image : inputImages) { + Positions result(inputImages.size()); + for (const auto & idx : sortedIndexes) { + const auto & image = inputImages[idx]; if (const auto spaceItr = std::find_if(spaces.begin(), spaces.end(), [image](const Space & s) { return image.x <= s.size.x && image.y <= s.size.y; }); spaceItr != spaces.end()) { auto space = *spaceItr; - result.push_back(space.position); + result[idx] = space.position; spaces.erase(spaceItr); if (space.size.x > image.x) { spaces.emplace(Position {space.position.x + image.x, space.position.y}, diff --git a/assetFactory/texturePacker.h b/assetFactory/texturePacker.h index 8e2061b..7e00f1a 100644 --- a/assetFactory/texturePacker.h +++ b/assetFactory/texturePacker.h @@ -23,7 +23,7 @@ public: using Positions = std::vector<Position>; using Result = std::pair<Positions, Size>; - TexturePacker(std::vector<Image>); + TexturePacker(std::span<const Image>); Result pack(Size) const; Result pack() const; @@ -32,5 +32,6 @@ public: static decltype(Size::x) area(const Size & size); private: - std::vector<Image> inputImages; + std::span<const Image> inputImages; + std::vector<size_t> sortedIndexes; }; diff --git a/test/test-assetFactory.cpp b/test/test-assetFactory.cpp index ad62a93..d71fa9b 100644 --- a/test/test-assetFactory.cpp +++ b/test/test-assetFactory.cpp @@ -129,14 +129,15 @@ BOOST_AUTO_TEST_CASE(parseX11RGB) BOOST_AUTO_TEST_CASE(texturePacker) { - TexturePacker tp {{ + std::vector<TexturePacker::Image> input { {10, 10}, {10, 10}, {10, 10}, {100, 10}, {10, 200}, {5, 5}, - }}; + }; + TexturePacker tp {input}; BOOST_CHECK_EQUAL(TexturePacker::Size(128, 256), tp.minSize()); const auto result = tp.pack(); } |