Files
dl_old/dl/math/dl_texture_pack.h
T
2023-08-06 03:06:14 +08:00

206 lines
4.5 KiB
C++

/**
* @file dl_texture_pack.h
* @brief 纹理打包(装箱算法)
*
*
* @version 1.0
* @author lveyou
* @date 21-09-10
*
* @note 动态装箱Dynamic(不预先知道所有box)
* @note 根据高度分为不同行
* @note 比较低的空间利用率,但速度较快
*
* @note 静态装箱Static(知道所有box,排序后合并)
*/
#pragma once
#include <list>
#include <map>
#include "dl_type.h"
namespace dl::TexturePack
{
//! 动态装箱(DH为2时,32、31、30会放到同一行)
template<unsigned DH>
class Dynamic
{
public:
//! 按高度存储 每一行
struct Line
{
unsigned _y;//! 所在y
unsigned _h;//! 最大高度
unsigned _x;//! 当前x
bool _free;//! 是否有空位标记
};
/**
* @brief 清空初始化 或 再次使用
* @param[in] size 箱子大小
*/
void Reset(const Size& size)
{
_size = size;
_regY = 0;
_bFull = false;
_vecLine.clear();
}
/**
* @brief 添加一个,失败返回false
* @param[in] size 装入大小
* @param[out] rect 成功则返回位置
*/
bool Add(const Size& size, RectU& rect)
{
if (_bFull)
return false;
unsigned w = size[0];
unsigned h = size[1];
//找到一个h比自己大的,但又不能超过DH(然后还能放得下x)
auto iter = find_if(_vecLine.begin(), _vecLine.end(), [&](Line& line)
{
if (line._free
&& line._h >= h // 30 >= 28
&& line._h <= h + DH) // 30 <= 28 + 2 {28,29,30}
{
if (line._x + w > _size[0])
{
line._free = false;
return false;
}
else
return true;
}
return false;
});
if (iter == _vecLine.end())
{//没有就创建一个
if (h + _regY > _size[1])
{
_bFull = true;
return false;
}
Line line;
line._h = h;
line._y = _regY;
line._x = 0;
line._free = true;
_regY += h;
_vecLine.push_back(line);
iter = _vecLine.end() - 1;
}
rect = { iter->_x, iter->_y, iter->_x + w , iter->_y + h };
iter->_x += w;
//越界检查
assert(rect[2] <= _size[0] && rect[3] <= _size[1]);
return true;
}
private:
Size _size;
unsigned _regY;//当前y
bool _bFull;//满了标记
std::vector<Line> _vecLine;
};
//! 静态装箱
class Static
{
public:
/**
* @brief 设置箱子大小
* @param[in] size 箱子大小
*/
void SetSize(const Size& size)
{
_size = size;
}
/**
* @brief 加入一堆箱子(会重建自身)
* @param[in] all_box 所有箱子
* @param[in] get_size 从T获得箱子大小
* @param[in] call_back 成功添加回调
* @note 成功添加的会从 all_box移除,并执行call_back
*/
template<typename T, typename FS, typename F>
void Calc(std::list<T>& all_box, FS get_size, F call_back)
{
//维护一个按h排序的剩余空间
std::multimap<unsigned, RectU> map_remain;
//初始空间
map_remain.insert( { _size[1], createRectU(_size) });
//按面积大小排序(list的sort本身就是稳定的)
all_box.sort([get_size](const T& box0, const T& box1)
{
return Accumulate::Mul(get_size(box0))
> Accumulate::Mul(get_size(box1));
});
for (auto iter = all_box.begin(); iter != all_box.end();)
{
//获取大小
Size size = get_size(*iter);
//寻找一个比自己h大的剩余空间
bool failed = true;
auto iter_remain = map_remain.begin();
for (; iter_remain != map_remain.end(); ++iter_remain)
{
if (iter_remain->first >= size[1]
&& Align::GetWidth(iter_remain->second) >= size[0])
{//并且w也要更大
//放入自己,并将加入到的剩余空间拆分为新的两个剩余空间,加入到map_remain
PointU lt = Align::GetLT(iter_remain->second);
call_back(*iter, lt);
//在世界坐标里的 rect
RectU rect_parent = createRectU(lt, size);
//right rect
if (iter_remain->second[1] != rect_parent[3])
{//高度过小的剩余空间丢弃
RectU rect_right = { rect_parent[2], iter_remain->second[1], iter_remain->second[2], rect_parent[3] };
map_remain.insert({ Align::GetHeight(rect_right), rect_right });
}
//buttom rect
if (rect_parent[3] != iter_remain->second[3])
{//高度过小的剩余空间丢弃
RectU rect_buttom = { iter_remain->second[0], rect_parent[3], iter_remain->second[2], iter_remain->second[3] };
map_remain.insert({ Align::GetHeight(rect_buttom), rect_buttom });
}
//还需删除加入到的剩余空间
map_remain.erase(iter_remain);
//标记加入成功
failed = false;
break;
}
}
//成功则移除
if (!failed)
all_box.erase(iter++);
else
++iter;
}
}
private:
Size _size;
};
}