This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrectpack.hpp
163 lines (141 loc) · 5.35 KB
/
rectpack.hpp
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
#ifndef MUSH_RECTPACK
#define MUSH_RECTPACK
#include <cstdint>
#include <deque>
#include <algorithm>
#include "geometry.hpp"
namespace mush
{
class RectanglePack
{
public:
uint32_t flags;
uint32_t minsize;
std::deque<Rectangle> unused;
std::deque<Rectangle> mapped;
uint32_t width, height;
RectanglePack() : minsize(4), width(0), height(0) {}
~RectanglePack() {}
size_t size()
{
return width * height;
}
//! Split the rectpack to smaller parts
void split(const Rectangle& r)
{
Rectangle temp;
for (auto r2 : unused)
{
if (overlap(r, r2))
{
if (r.x < (int32_t)(r2.x + r2.w) && (int32_t)(r.x + r.w) > r2.x)
{
if (r.y > r2.y && r.y < (int32_t)(r2.y + r2.h))
{
temp = r2;
temp.h = r.y - r2.y;
if (temp.h > minsize)
unused.push_front(temp);
}
if (r.y + r.h < r2.y + r2.h)
{
temp = r2;
temp.y = r.y + r.h;
temp.h = r2.y + r2.h - (r.y + r.h);
if (temp.h > minsize)
unused.push_front(temp);
}
}
if (r.y < (int32_t)(r2.y + r2.h) && (int32_t)(r.y + r.h) > r2.y)
{
if (r.x > r2.x && r.x < (int32_t)(r2.x + r2.w))
{
temp = r2;
temp.w = r.x - r2.x;
if (temp.w > minsize)
unused.push_front(temp);
}
if (r.x + r.w < r2.x + r2.w)
{
temp = r2;
temp.x = r.x + r.w;
temp.w = r2.x + r2.w - (r.x + r.w);
if (temp.w > minsize)
unused.push_front(temp);
}
}
}
}
}
//! Fit an rectangle into the pack
Rectangle fit(uint32_t w, uint32_t h)
{
Rectangle rval = {0,0,0,0};
std::sort(unused.begin(), unused.end());
for (auto it : unused)
{
if ((w < it.w) && (h < it.h))
{
rval.x = it.x;
rval.y = it.y;
rval.w = w;
rval.h = h;
split(rval);
return rval;
}
}
return rval;
}
//! Clear the packing data
void reset()
{
unused.clear();
mapped.clear();
unused.emplace_back(Rectangle{0,0,width,height});
}
//! Removes overlapping and useless rectangles
void prune(const Rectangle& r)
{
for (auto it = unused.begin(); it != unused.end(); ++it)
{
if (overlap(r, *it))
{
it = unused.erase(it);
it--;
}
}
for (auto i = unused.begin(); i != unused.end(); ++i)
for (auto j = i; j != unused.end(); ++j)
{
if (i == j)
continue;
if (contains(*j, *i))
{
i = unused.erase(i);
i--;
break;
} else if (contains(*i, *j)) {
j = unused.erase(j);
j--;
}
}
}
};
}
#endif
/*
Copyright (c) 2017 Jari Ronkainen
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose, including
commercial applications, and to alter it and redistribute it freely, subject to
the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim
that you wrote the original software. If you use this software in a product,
an acknowledgment in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/