将大矩形划分为小矩形(2D 打包)
我需要将大的静态大小的矩形分割成小矩形的算法。对我来说,一个完美的实现如下所示:
struct RECT
{
int l,t,r,b;
};
class BigRect
{
public:
// width and height of big rect
BigRect( unsigned width, unsigned height );
// returns -1 if rect cannot be allocated, otherwise returns id of found rect
int GetRect( unsigned width, unsigned height, RECT &out );
// returns allocated rect to big rectangle
void FreeRect( int id );
};
void test()
{
BigRect r( 10, 10 );
RECT out;
r.GetRect( 4, 4, out ); // rect found ({0,0,4,4} for example), returns 1
r.GetRect( 5, 5, out ); // rect found ({4,0,9,5} for example), returns 2
r.GetRect( 6, 6, out ); // no place found for rect, returns -1
r.FreeRect( 2 ); // add {4,0,9,5} back to rect
r.GetRect( 6, 6, out ); // rect found (4,0,10,6)
}
所以我需要 GetRect
和 FreeRect
方法的算法。任何想法和链接将不胜感激。
I need algorithm which splits big static sized rectangle to small ones. A perfect implementation for me look like this:
struct RECT
{
int l,t,r,b;
};
class BigRect
{
public:
// width and height of big rect
BigRect( unsigned width, unsigned height );
// returns -1 if rect cannot be allocated, otherwise returns id of found rect
int GetRect( unsigned width, unsigned height, RECT &out );
// returns allocated rect to big rectangle
void FreeRect( int id );
};
void test()
{
BigRect r( 10, 10 );
RECT out;
r.GetRect( 4, 4, out ); // rect found ({0,0,4,4} for example), returns 1
r.GetRect( 5, 5, out ); // rect found ({4,0,9,5} for example), returns 2
r.GetRect( 6, 6, out ); // no place found for rect, returns -1
r.FreeRect( 2 ); // add {4,0,9,5} back to rect
r.GetRect( 6, 6, out ); // rect found (4,0,10,6)
}
So I need algorithm for GetRect
and FreeRect
methods. Any ideas and links would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要做的是在线二维装箱。它是在线的,因为在您尝试将它们打包成大图片之前,您手中并没有所有的小图片。此外,一些小图片将被“释放”,它们的空间将被释放。另一方面,离线算法允许您在打包之前对小图片进行从大到小的排序。
这里有一篇文章调查了 2D 封装的最新技术:二维包装综述。这是相当理论化的。
本文二维在线装箱的新上限引用了其他文章描述在线 2D 打包算法。
游戏界的人也有和你类似的问题;他们称之为纹理打包或纹理图集。然而,他们使用离线算法。
John Ratcliff 发布了一篇博客文章关于纹理包装。
另请参阅 gamedev 上的相关问题: https://gamedev.stackexchange.com/questions/2829/texture-打包算法
What you're trying to do is online 2D bin packing. It's online because you don't have all your small pictures in hand before you attempt to pack them into the big picture. Furthermore some small pictures will be "deallocated" and their space will be freed up. On the other hand, an offline algorithm allows you to do things like sort your small pictures from largest to smallest before packing them.
Here's an article that surveys the state of the art in 2D packing: Survey on two-dimensional packing. It's quite theoretical.
This article A New Upper Bound on 2D Online Bin Packing cites other articles that describe online 2D packing algorithms.
People in the gaming world have a similar problem as you do; they call it texture packing or texture atlas. However, they use offline algorithms.
John Ratcliff posted a blog article on texture packing.
See also this related question on gamedev: https://gamedev.stackexchange.com/questions/2829/texture-packing-algorithm