summaryrefslogtreecommitdiff
path: root/assetFactory/texturePacker.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-04-09 23:29:00 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2023-04-09 23:29:00 +0100
commitb6b3944e9507f337f77a72ea44b5c0ccba7a2c01 (patch)
treea487df54853522f973ed987006e787117804b9eb /assetFactory/texturePacker.cpp
parentSwap messy glmvec wrapper for OpenMesh Point/Normal with real glm::vec and a ... (diff)
parentMove remaining split/plane functions to use library (diff)
downloadilt-b6b3944e9507f337f77a72ea44b5c0ccba7a2c01.tar.bz2
ilt-b6b3944e9507f337f77a72ea44b5c0ccba7a2c01.tar.xz
ilt-b6b3944e9507f337f77a72ea44b5c0ccba7a2c01.zip
Merge branch 'model-factory-textures'
Diffstat (limited to 'assetFactory/texturePacker.cpp')
-rw-r--r--assetFactory/texturePacker.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/assetFactory/texturePacker.cpp b/assetFactory/texturePacker.cpp
new file mode 100644
index 0000000..68a6010
--- /dev/null
+++ b/assetFactory/texturePacker.cpp
@@ -0,0 +1,80 @@
+#include "texturePacker.h"
+#include "collections.hpp"
+#include <algorithm>
+#include <cstdio>
+#include <numeric>
+#include <ostream>
+#include <set>
+
+TexturePacker::TexturePacker(std::span<const Image> in) :
+ inputImages {std::move(in)}, sortedIndexes {vectorOfN(inputImages.size(), size_t {})}
+{
+ std::sort(sortedIndexes.rbegin(), sortedIndexes.rend(), [this](const auto a, const auto b) {
+ return area(inputImages[a]) < area(inputImages[b]);
+ });
+}
+
+TexturePacker::Result
+TexturePacker::pack() const
+{
+ return pack(minSize());
+}
+
+TexturePacker::Result
+TexturePacker::pack(Size size) const
+{
+ using Spaces = std::set<Space>;
+ Spaces spaces {{{}, size}};
+
+ 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[idx] = space.position;
+ spaces.erase(spaceItr);
+ if (space.size.x > image.x) {
+ spaces.emplace(Position {space.position.x + image.x, space.position.y},
+ Size {space.size.x - image.x, image.y});
+ }
+ if (space.size.y > image.y) {
+ spaces.emplace(Position {space.position.x, space.position.y + image.y},
+ Size {space.size.x, space.size.y - image.y});
+ }
+ }
+ else {
+ if (size.x < size.y) {
+ return pack({size.x * 2, size.y});
+ }
+ else {
+ return pack({size.x, size.y * 2});
+ }
+ }
+ }
+
+ return {result, size};
+}
+
+TexturePacker::Size
+TexturePacker::minSize() const
+{
+ return std::accumulate(inputImages.begin(), inputImages.end(), Size {1}, [](Size size, const Image & i) {
+ while (size.x < i.x) {
+ size.x *= 2;
+ }
+ while (size.y < i.y) {
+ size.y *= 2;
+ }
+ return size;
+ });
+}
+
+decltype(TexturePacker::Size::x)
+TexturePacker::area(const Size & size)
+{
+ return size.x * size.y;
+}