mirror of https://github.com/mosra/magnum.git
11 changed files with 261 additions and 0 deletions
@ -0,0 +1,53 @@
|
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
This file is part of Magnum. |
||||
|
||||
Magnum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License version 3 |
||||
only, as published by the Free Software Foundation. |
||||
|
||||
Magnum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License version 3 for more details. |
||||
*/ |
||||
|
||||
#include "Atlas.h" |
||||
|
||||
#include "Math/Geometry/Rectangle.h" |
||||
|
||||
namespace Magnum { namespace TextureTools { |
||||
|
||||
std::vector<Rectanglei> atlas(const Vector2i& atlasSize, const std::vector<Vector2i>& sizes) { |
||||
if(sizes.empty()) return {}; |
||||
|
||||
/* Size of largest texture */ |
||||
Vector2i maxSize; |
||||
for(const Vector2i& size: sizes) { |
||||
/** @todo max() for vector types */ |
||||
if(size.x() > maxSize.x()) maxSize.x() = size.x(); |
||||
if(size.y() > maxSize.y()) maxSize.y() = size.y(); |
||||
} |
||||
|
||||
std::vector<Rectanglei> atlas; |
||||
|
||||
/* Columns and rows */ |
||||
Vector2i gridSize = atlasSize/maxSize; |
||||
if(std::size_t(gridSize.product()) < sizes.size()) { |
||||
Error() << "TextureTools::Atlas::create(): requested atlas size" |
||||
<< atlasSize << "is too small to fit" << sizes.size() |
||||
<< maxSize << "textures. Generated atlas will be empty."; |
||||
return atlas; |
||||
} |
||||
|
||||
/** @todo actual magic implementation, not this joke */ |
||||
|
||||
atlas.reserve(sizes.size()); |
||||
for(std::size_t i = 0; i != sizes.size(); ++i) |
||||
atlas.push_back(Rectanglei::fromSize(Vector2i(i%gridSize.x(), i/gridSize.x())*maxSize, sizes[i])); |
||||
|
||||
return atlas; |
||||
} |
||||
|
||||
}} |
||||
@ -0,0 +1,42 @@
|
||||
#ifndef Magnum_TextureTools_Atlas_h |
||||
#define Magnum_TextureTools_Atlas_h |
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
This file is part of Magnum. |
||||
|
||||
Magnum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License version 3 |
||||
only, as published by the Free Software Foundation. |
||||
|
||||
Magnum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License version 3 for more details. |
||||
*/ |
||||
|
||||
/** @file
|
||||
* @brief Function Magnum::TextureTools::atlas() |
||||
*/ |
||||
|
||||
#include <vector> |
||||
|
||||
#include "Magnum.h" |
||||
|
||||
#include "magnumTextureToolsVisibility.h" |
||||
|
||||
namespace Magnum { namespace TextureTools { |
||||
|
||||
/**
|
||||
@brief Pack textures into texture atlas |
||||
@param atlasSize Size of resulting atlas |
||||
@param sizes Sizes of all textures in the atlas |
||||
|
||||
Packs many small textures into one larger. If the textures cannot be packed |
||||
into required size, empty vector is returned. |
||||
*/ |
||||
std::vector<Rectanglei> MAGNUM_TEXTURETOOLS_EXPORT atlas(const Vector2i& atlasSize, const std::vector<Vector2i>& sizes); |
||||
|
||||
}} |
||||
|
||||
#endif |
||||
@ -0,0 +1,19 @@
|
||||
set(MagnumTextureTools_SRCS |
||||
Atlas.cpp) |
||||
|
||||
set(MagnumTextureTools_HEADERS |
||||
Atlas.h |
||||
|
||||
magnumTextureToolsVisibility.h) |
||||
|
||||
add_library(MagnumTextureTools SHARED ${MagnumTextureTools_SRCS}) |
||||
|
||||
target_link_libraries(MagnumTextureTools Magnum) |
||||
|
||||
install(TARGETS MagnumTextureTools DESTINATION ${MAGNUM_LIBRARY_INSTALL_DIR}) |
||||
install(FILES ${MagnumTextureTools_HEADERS} DESTINATION ${MAGNUM_INCLUDE_INSTALL_DIR}/TextureTools) |
||||
|
||||
if(BUILD_TESTS) |
||||
enable_testing() |
||||
add_subdirectory(Test) |
||||
endif() |
||||
@ -0,0 +1,65 @@
|
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
This file is part of Magnum. |
||||
|
||||
Magnum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License version 3 |
||||
only, as published by the Free Software Foundation. |
||||
|
||||
Magnum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License version 3 for more details. |
||||
*/ |
||||
|
||||
#include "AtlasTest.h" |
||||
|
||||
#include <sstream> |
||||
|
||||
#include "Math/Geometry/Rectangle.h" |
||||
#include "TextureTools/Atlas.h" |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::TextureTools::Test::AtlasTest) |
||||
|
||||
namespace Magnum { namespace TextureTools { namespace Test { |
||||
|
||||
AtlasTest::AtlasTest() { |
||||
addTests(&AtlasTest::create, |
||||
&AtlasTest::createEmpty, |
||||
&AtlasTest::createTooSmall); |
||||
} |
||||
|
||||
void AtlasTest::create() { |
||||
std::vector<Rectanglei> atlas = TextureTools::atlas({64, 64}, { |
||||
{12, 18}, |
||||
{32, 15}, |
||||
{23, 25} |
||||
}); |
||||
|
||||
CORRADE_COMPARE(atlas.size(), 3); |
||||
CORRADE_COMPARE(atlas, (std::vector<Rectanglei>{ |
||||
Rectanglei::fromSize({0, 0}, {12, 18}), |
||||
Rectanglei::fromSize({32, 0}, {32, 15}), |
||||
Rectanglei::fromSize({0, 25}, {23, 25})})); |
||||
} |
||||
|
||||
void AtlasTest::createEmpty() { |
||||
std::vector<Rectanglei> atlas = TextureTools::atlas({}, {}); |
||||
CORRADE_VERIFY(atlas.empty()); |
||||
} |
||||
|
||||
void AtlasTest::createTooSmall() { |
||||
std::ostringstream o; |
||||
Error::setOutput(&o); |
||||
|
||||
std::vector<Rectanglei> atlas = TextureTools::atlas({64, 32}, { |
||||
{12, 18}, |
||||
{25, 15}, |
||||
{23, 31} |
||||
}); |
||||
CORRADE_VERIFY(atlas.empty()); |
||||
CORRADE_COMPARE(o.str(), "TextureTools::Atlas::create(): requested atlas size Vector(64, 32) is too small to fit 3 Vector(25, 31) textures. Generated atlas will be empty.\n"); |
||||
} |
||||
|
||||
}}} |
||||
@ -0,0 +1,33 @@
|
||||
#ifndef Magnum_TextureTools_Test_AtlasTest_h |
||||
#define Magnum_TextureTools_Test_AtlasTest_h |
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
This file is part of Magnum. |
||||
|
||||
Magnum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License version 3 |
||||
only, as published by the Free Software Foundation. |
||||
|
||||
Magnum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License version 3 for more details. |
||||
*/ |
||||
|
||||
#include <TestSuite/Tester.h> |
||||
|
||||
namespace Magnum { namespace TextureTools { namespace Test { |
||||
|
||||
class AtlasTest: public Corrade::TestSuite::Tester { |
||||
public: |
||||
explicit AtlasTest(); |
||||
|
||||
void create(); |
||||
void createEmpty(); |
||||
void createTooSmall(); |
||||
}; |
||||
|
||||
}}} |
||||
|
||||
#endif |
||||
@ -0,0 +1 @@
|
||||
corrade_add_test(TextureToolsAtlasTest AtlasTest.cpp LIBRARIES MagnumTextureTools) |
||||
@ -0,0 +1,28 @@
|
||||
#ifndef Magnum_TextureTools_magnumTextureToolsVisibility_h |
||||
#define Magnum_TextureTools_magnumTextureToolsVisibility_h |
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
This file is part of Magnum. |
||||
|
||||
Magnum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License version 3 |
||||
only, as published by the Free Software Foundation. |
||||
|
||||
Magnum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License version 3 for more details. |
||||
*/ |
||||
|
||||
#ifdef _WIN32 |
||||
#ifdef MagnumTextureTools_EXPORTS |
||||
#define MAGNUM_TEXTURETOOLS_EXPORT __declspec(dllexport) |
||||
#else |
||||
#define MAGNUM_TEXTURETOOLS_EXPORT __declspec(dllimport) |
||||
#endif |
||||
#else |
||||
#define MAGNUM_TEXTURETOOLS_EXPORT __attribute__ ((visibility ("default"))) |
||||
#endif |
||||
|
||||
#endif |
||||
Loading…
Reference in new issue