JvPack2d

C code posted by jvernay.fr
created at 20 Jul 18:20

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

/*
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;
}
5.43 KB in 4 ms with coderay