dp_rect_pack  1.1.3
Rectangle packing library for C++
Classes | Public Member Functions | List of all members
dp::rect_pack::RectPacker< GeomT > Class Template Reference

Rectangle packer. More...

#include <dp_rect_pack.h>

Classes

struct  InsertResult
 Result returned by RectPacker::insert(). More...
 
struct  Padding
 
struct  Position
 
struct  Spacing
 

Public Member Functions

 RectPacker (GeomT maxPageWidth, GeomT maxPageHeight, const Spacing &rectsSpacing=Spacing(0), const Padding &pagePadding=Padding(0))
 RectPacker constructor. More...
 
std::size_t getNumPages () const
 Return the current number of pages. More...
 
void getPageSize (std::size_t pageIndex, GeomT &width, GeomT &height) const
 Return the current size of the page. More...
 
InsertResult insert (GeomT width, GeomT height)
 Insert a rectangle. More...
 

Detailed Description

template<typename GeomT = int>
class dp::rect_pack::RectPacker< GeomT >

Rectangle packer.

GeomT is not required to hold negative numbers, and thus can be an unsigned integer. It's also possible to use a floating-point or a custom numeric type.

A custom type for GeomT should support:

Template Parameters
GeomTnumeric type to use for geometry

Constructor & Destructor Documentation

◆ RectPacker()

template<typename GeomT = int>
dp::rect_pack::RectPacker< GeomT >::RectPacker ( GeomT  maxPageWidth,
GeomT  maxPageHeight,
const Spacing rectsSpacing = Spacing(0),
const Padding pagePadding = Padding(0) 
)
inline

RectPacker constructor.

maxPageWidth and maxPageHeight define the maximum size of a single page, including the padding. Depending on this limit and the features of GeomT, a RectPacker can work in multipage or infinite single-page mode.

To enable infinite single-page mode, you have two choices, depending on the properties of GeomT:

  • If GeomT has a physical limit (like any standard integer), you can set the maximum size to the maximum positive value GeomT can hold.
  • Otherwise, if GeomT is a floating-point type or a custom unbounded type, you can set the maximum size to a huge value or, if supported by the type, a magic value that always bigger than any finite number (like a positive infinity for floating-point types).

If GeomT can hold negative values, the maximum page size, spacing, and padding will be clamped to 0. Keep in mind that if the maximum page size is 0, or if the total padding greater or equal to the maximum page size, pages will have no free space for rectangles, and all calls to insert() will result in InsertStatus::rectTooBig.

Parameters
maxPageWidthmaximum width of a page, including the horizontal padding
maxPageHeightmaximum height of a page, including the vertical padding
rectsSpacingspace between rectangles
pagePaddingspace between rectangles and edges of a page

Member Function Documentation

◆ getNumPages()

template<typename GeomT = int>
std::size_t dp::rect_pack::RectPacker< GeomT >::getNumPages ( ) const
inline

Return the current number of pages.

Returns
number of pages (always > 0)

◆ getPageSize()

template<typename GeomT = int>
void dp::rect_pack::RectPacker< GeomT >::getPageSize ( std::size_t  pageIndex,
GeomT &  width,
GeomT &  height 
) const
inline

Return the current size of the page.

Parameters
pageIndexindex of the page in range [0..getNumPages())
[out]widthwidth of the page
[out]heightheight of the page
See also
getNumPages(), InsertResult::pageIndex

◆ insert()

template<typename GeomT >
RectPacker< GeomT >::InsertResult dp::rect_pack::RectPacker< GeomT >::insert ( GeomT  width,
GeomT  height 
)

Insert a rectangle.

The rectangles you'll feed to insert() should be sorted in descending order by comparing first by height, then by width. A comparison function for std::sort may look like the following:

bool compare(const T& a, const T& b)
{
if (a.height != b.height)
return a.height > b.height;
else
return a.width > b.width;
}
Parameters
widthwidth of the rectangle
heightheight of the rectangle
Returns
InsertResult

The documentation for this class was generated from the following file: