Title / Description
Code /* JvPack2d is authored by Julien Vernay, in year 2024 (dev AT jvernay DOT fr). The software is released to the public domain, as explained below (UNLICENSE). If you find the software useful, you can share its accompanying blog post: https://jvernay.fr/en/blog/skyline-2d-packer/implementation/ This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. For more information, please refer to <https://unlicense.org> */ #include "jv_pack2d.h" #include <string.h> #pragma region Assertion logic #if JVPACK2D_ENABLE_TEST #include "../munit.h" #define _jvASSERT(a, op, b) munit_assert_int64((int64_t)(a), op, (int64_t)(b)) #else #define _jvASSERT(a, op, b) while (0) #endif #pragma endregion bool jvPack2dAdd(JvPack2d* pP2D, uint16_t const size[2], uint16_t pos[2]) { // Utility structure to make the code more readable. typedef struct Point { uint16_t x; uint16_t y; } Point; Point* pSkyline = (Point*)pP2D->pSkyline; uint16_t width = size[0]; uint16_t height = size[1]; if (width == 0 || height == 0) return false; // Initial state contains the bottom-left point. if (!pP2D->_bInitialized) { _jvASSERT(pP2D->maxWidth, >, 0); _jvASSERT(pP2D->maxHeight, >, 0); pP2D->_bInitialized = true; pP2D->_skylineCount = 1; // Bottom-left point, indicating available space. pSkyline[0].x = 0; pSkyline[0].y = 0; } // Aliases to make the code less verbose. uint16_t maxWidth = pP2D->maxWidth; uint16_t maxHeight = pP2D->maxHeight; uint16_t skylineCount = pP2D->_skylineCount; // Stores the best candidate so far. uint16_t idxBest = UINT16_MAX; uint16_t idxBest2 = UINT16_MAX; uint16_t bestX = UINT16_MAX; uint16_t bestY = UINT16_MAX; // Search loop for best location. for (uint16_t idx = 0; idx < skylineCount; ++idx) { uint16_t x = pSkyline[idx].x; uint16_t y = pSkyline[idx].y; if (width > maxWidth - x) break; // We have reached the atlas' right boundary. if (y >= bestY) continue; // We will not beat the current best. // Raise 'y' such that we are above all intersecting skylines. uint16_t xMax = x + width; uint16_t idx2; for (idx2 = idx + 1; idx2 < skylineCount; ++idx2) { if (xMax <= pSkyline[idx2].x) break; // We do not reach the next skylines. if (y < pSkyline[idx2].y) { y = pSkyline[idx2].y; // Raise 'y' as to not intersect. } } if (y >= bestY) continue; // We did not beat the current best. if (height > maxHeight - y) continue; // We have reached the atlas' top boundary. idxBest = idx; idxBest2 = idx2; bestX = x; bestY = y; } if (idxBest == UINT16_MAX) return false; // Did not find any space available. _jvASSERT(idxBest, <, idxBest2); _jvASSERT(idxBest2, >, 0); // We replace the points overshadowed by the current rect, by new points. uint16_t removedCount = idxBest2 - idxBest; Point newTL, newBR; // TopLeft, BottomRight newTL.x = bestX; newTL.y = bestY + height; newBR.x = bestX + width; newBR.y = pSkyline[idxBest2 - 1].y; bool bBottomRightPoint = (idxBest2 < skylineCount ? newBR.x < pSkyline[idxBest2].x : newBR.x < maxWidth); // TopLeft is always inserted uint16_t insertedCount = 1 + bBottomRightPoint; _jvASSERT(skylineCount + insertedCount - removedCount, <=, maxWidth); // Logic for inserting and erasing in an array. // It can be done in two lines if we use a standard library, // eg. in C++ it would be std::vector erase() + insert() // but I preferred the algorithm to be explicit and freestanding. if (insertedCount > removedCount) { // Expansion. Shift elements to the right. We need to iterate backwards. uint16_t idx = skylineCount - 1; uint16_t idx2 = idx + (insertedCount - removedCount); for (; idx >= idxBest2; --idx, --idx2) pSkyline[idx2] = pSkyline[idx]; pP2D->_skylineCount = skylineCount + (insertedCount - removedCount); } else if (insertedCount < removedCount) { // Shrinking. Shift elements to the left. We need to iterate forwards. uint16_t idx = idxBest2; uint16_t idx2 = idx - (removedCount - insertedCount); for (; idx < skylineCount; ++idx, ++idx2) pSkyline[idx2] = pSkyline[idx]; pP2D->_skylineCount = skylineCount - (removedCount - insertedCount); } pSkyline[idxBest] = newTL; if (bBottomRightPoint) pSkyline[idxBest + 1] = newBR; pos[0] = bestX; pos[1] = bestY; return true; }
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code