Install rectpack
masterYou can install rectpack using pip or by installing from source via setup.py.
pip install rectpackOr from source
python setup.py installrepository·master·Indexed 20 days ago
https://github.com/secnot/rectpackA collection of heuristic algorithms for solving the 2D knapsack (bin packing) problem, focused on packing rectangles into the smallest number of bins. It supports MaxRects, Skyline, and Guillotine algorithm variants, and provides tools for configuring packing modes, bin selection heuristics, and rectangle sort orders.
You can install rectpack using pip or by installing from source via setup.py.
pip install rectpackpython setup.py installTo avoid collisions caused by floating point rounding, all dimensions (bins and rectangles) should be integers or decimals. If your data uses floats, use the float2dec helper to convert them to fixed-point decimals.
Note: When rounding, ensure you round in a way that the actual rectangle size remains smaller than or equal to the intended size to prevent overflow.
from rectpack import float2dec, newPacker
float_rects = [(10.5555, 20.1234), (5.0, 5.0)]
# Convert to decimals with 3 decimal places
dec_rects = [(float2dec(r[0], 3), float2dec(r[1], 3)) for r in float_rects]
packer = newPacker()
for r in dec_rects:
packer.add_rect(*r)To use rectpack, initialize a packer using newPacker(), add your rectangles and bins, and then call pack(). You can then iterate over the packer to access the bins and the rectangles within them, or use rect_list() for a flattened list of all packed rectangles.
from rectpack import newPacker
rectangles = [(100, 30), (40, 60), (30, 30),(70, 70), (100, 50), (30, 30)]
bins = [(300, 450), (80, 40), (200, 150)]
packer = newPacker()
# Add the rectangles to packing queue
for r in rectangles:
packer.add_rect(*r)
# Add the bins where the rectangles will be placed
for b in bins:
packer.add_bin(*b)
# Start packing
packer.pack()
# Access results
for abin in packer:
print(abin.bid) # Bin id
for rect in abin:
print(rect.x, rect.y, rect.width, rect.height)The rect_list() method returns a list of all packed rectangles. Each rectangle is represented as a tuple: (b, x, y, w, h, rid).
b: Index of the bin the rectangle was packed into.x: X coordinate of the rectangle's bottom-left corner.y: Y coordinate of the rectangle's bottom-left corner.w: Rectangle width.h: Rectangle height.rid: The user-provided ID or None.all_rects = packer.rect_list()
for b, x, y, w, h, rid in all_rects:
print(f"Bin {b}: Rect at ({x}, {y}) size {w}x{h}")Use add_bin(width, height[, count][, bid]) to add empty bins to the packer.
width: Bin width.height: Bin height.count: Number of bins to add (default is 1). Use float("inf") to add infinite bins.bid: Optional bin identifier.packer.add_bin(300, 450, count=5, bid="bin_1")Use add_rect(width, height[, rid]) to add a rectangle to the packing queue.
width: Rectangle width.height: Rectangle height.rid: User-assigned rectangle ID.packer.add_rect(100, 50, rid="rect_01")The newPacker() function returns a new packer object. It accepts several configuration parameters to control the packing behavior.
Parameters:
mode: PackingMode.Offline (default, packing starts only when pack() is called) or PackingMode.Online (rectangles are packed as soon as they are added).bin_algo: Bin selection heuristic (PackingBin.BNF, PackingBin.BFF, PackingBin.BBF, or PackingBin.Global).pack_algo: The specific packing algorithm to use (e.g., MaxRects, Skyline, or Guillotine variants).sort_algo: Rectangle sort order (only for Offline mode). Options include SORT_NONE, SORT_AREA, SORT_PERI, SORT_DIFF, SORT_SSIDE, SORT_LSIDE, and SORT_RATIO.rotation: Boolean to enable or disable rectangle rotation.packer = newPacker(mode=PackingMode.Offline, bin_algo=PackingBin.BFF, pack_algo='MaxRectsBl')Rectpack implements three main algorithm families with various variants. If packing is too slow, consider switching to a Guillotine variant like GuillotineBssfSas.
MaxRects Variants:
MaxRectsBl, MaxRectsBssf, MaxRectsBaf, MaxRectsBlsfSkyline Variants:
SkylineBl, SkylineBlWm, SkylineMwf, SkylineMwfl, SkylineMwfWm, SkylineMwflWmGuillotine Variants:
GuillotineBssfSas, GuillotineBssfLas, GuillotineBssfSlas, GuillotineBssfLlas, GuillotineBssfMaxas, GuillotineBssfMinas, GuillotineBlsfSas, GuillotineBlsfLas, GuillotineBlsfSlas, GuillotineBlsfLlas, GuillotineBlsfMaxas, GuillotineBlsfMinas, GuillotineBafSas, GuillotineBafLas, GuillotineBafSlas, GuillotineBafLlas, GuillotineBafMaxas, GuillotineBafMinas