dl
dl_texture_pack.h
浏览该文件的文档.
1
16#pragma once
17
18#include <list>
19#include <map>
20
21#include "dl_type.h"
22
24{
25
27template<unsigned DH>
29{
30public:
32 struct Line
33 {
34 unsigned _y;
35 unsigned _h;
36 unsigned _x;
37 bool _free;
38 };
39
44 void Reset(const Size& size)
45 {
46 _size = size;
47 _regY = 0;
48 _bFull = false;
49
50 _vecLine.clear();
51 }
52
58 bool Add(const Size& size, RectU& rect)
59 {
60 if (_bFull)
61 return false;
62
63 unsigned w = size[0];
64 unsigned h = size[1];
65 //找到一个h比自己大的,但又不能超过DH(然后还能放得下x)
66 auto iter = find_if(_vecLine.begin(), _vecLine.end(), [&](Line& line)
67 {
68 if (line._free
69 && line._h >= h // 30 >= 28
70 && line._h <= h + DH) // 30 <= 28 + 2 {28,29,30}
71 {
72 if (line._x + w > _size[0])
73 {
74 line._free = false;
75 return false;
76 }
77 else
78 return true;
79 }
80 return false;
81 });
82
83 if (iter == _vecLine.end())
84 {//没有就创建一个
85 if (h + _regY > _size[1])
86 {
87 _bFull = true;
88 return false;
89 }
90
91 Line line;
92 line._h = h;
93 line._y = _regY;
94 line._x = 0;
95 line._free = true;
96
97 _regY += h;
98 _vecLine.push_back(line);
99
100 iter = _vecLine.end() - 1;
101 }
102
103 rect = { iter->_x, iter->_y, iter->_x + w , iter->_y + h };
104
105 iter->_x += w;
106
107 //越界检查
108 assert(rect[2] <= _size[0] && rect[3] <= _size[1]);
109
110 return true;
111 }
112
113private:
114 Size _size;
115 unsigned _regY;//当前y
116 bool _bFull;//满了标记
117 std::vector<Line> _vecLine;
118};
119
122{
123public:
128 void SetSize(const Size& size)
129 {
130 _size = size;
131 }
132
139 template<typename T, typename FS, typename F>
140 void Calc(std::list<T>& all_box, FS get_size, F call_back)
141 {
142 //维护一个按h排序的剩余空间
143 std::multimap<unsigned, RectU> map_remain;
144 //初始空间
145 map_remain.insert( { _size[1], createRectU(_size) });
146
147 //按面积大小排序(list的sort本身就是稳定的)
148 all_box.sort([get_size](const T& box0, const T& box1)
149 {
150 return Accumulate::Mul(get_size(box0))
151 > Accumulate::Mul(get_size(box1));
152 });
153
154 for (auto iter = all_box.begin(); iter != all_box.end();)
155 {
156 //获取大小
157 Size size = get_size(*iter);
158 //寻找一个比自己h大的剩余空间
159 bool failed = true;
160 auto iter_remain = map_remain.begin();
161 for (; iter_remain != map_remain.end(); ++iter_remain)
162 {
163 if (iter_remain->first >= size[1]
164 && Align::GetWidth(iter_remain->second) >= size[0])
165 {//并且w也要更大
166 //放入自己,并将加入到的剩余空间拆分为新的两个剩余空间,加入到map_remain
167 PointU lt = Align::GetLT(iter_remain->second);
168 call_back(*iter, lt);
169
170 //在世界坐标里的 rect
171 RectU rect_parent = createRectU(lt, size);
172
173 //right rect
174 if (iter_remain->second[1] != rect_parent[3])
175 {//高度过小的剩余空间丢弃
176 RectU rect_right = { rect_parent[2], iter_remain->second[1], iter_remain->second[2], rect_parent[3] };
177 map_remain.insert({ Align::GetHeight(rect_right), rect_right });
178 }
179
180 //buttom rect
181 if (rect_parent[3] != iter_remain->second[3])
182 {//高度过小的剩余空间丢弃
183 RectU rect_buttom = { iter_remain->second[0], rect_parent[3], iter_remain->second[2], iter_remain->second[3] };
184 map_remain.insert({ Align::GetHeight(rect_buttom), rect_buttom });
185 }
186
187 //还需删除加入到的剩余空间
188 map_remain.erase(iter_remain);
189 //标记加入成功
190 failed = false;
191 break;
192 }
193 }
194 //成功则移除
195 if (!failed)
196 all_box.erase(iter++);
197 else
198 ++iter;
199 }
200 }
201private:
202 Size _size;
203};
204
205}
动态装箱(DH为2时,32、31、30会放到同一行)
void Reset(const Size &size)
清空初始化 或 再次使用
bool Add(const Size &size, RectU &rect)
添加一个,失败返回false
void SetSize(const Size &size)
设置箱子大小
void Calc(std::list< T > &all_box, FS get_size, F call_back)
加入一堆箱子(会重建自身)
通用类型
constexpr auto Mul(const C &container)
求积product
constexpr std::array< T, 2 > GetLT(const std::array< T, 4 > &rect)
constexpr T GetWidth(const std::array< T, 4 > &rect)
计算返回 矩形 右下角坐标x 减去 左上角坐标x
constexpr T GetHeight(const std::array< T, 4 > &rect)
计算返回 矩形 右下角坐标y 减去 左上角坐标y
Point2U PointU
constexpr RectU createRectU(const Size &size)
创建一个RectU,依据大小
Point4U RectU
PointU Size