### Installation Source: https://github.com/secnot/rectpack/blob/master/README.md Instructions for installing the rectpack library using either the setup script or pip. ```bash python setup.py install ``` ```bash pip install rectpack ``` -------------------------------- ### Float to Decimal Conversion Source: https://github.com/secnot/rectpack/blob/master/README.md Example of using the `float2dec` helper function from the `rectpack` library to convert floating-point rectangle dimensions to fixed-point Decimals. This is useful for precise packing calculations. ```python from rectpack import float2dec, newPacker float_rects = [...] # Assume float_rects is a list of tuples like [(width, height), ...] dec_rects = [(float2dec(r[0], 3), float2dec(r[1], 3)) for r in float_rects] p = newPacker() # ... further packing operations using dec_rects ``` -------------------------------- ### Iterating Through Packed Rectangles Source: https://github.com/secnot/rectpack/blob/master/README.md Shows methods for iterating through all packed rectangles, either by looping through each bin and its rectangles or by using the `rect_list()` method to get a flat list of all packed items. ```python for abin in packer: print(abin.bid) # Bin id if it has one for rect in abin: print(rect) # Full rectangle list all_rects = packer.rect_list() for rect in all_rects: b, x, y, w, h, rid = rect # b - Bin index # x - Rectangle bottom-left corner x coordinate # y - Rectangle bottom-left corner y coordinate # w - Rectangle width # h - Rectangle height # rid - User asigned rectangle id or None ``` -------------------------------- ### rectpack API Documentation Source: https://github.com/secnot/rectpack/blob/master/README.md Comprehensive documentation for the rectpack library's API, detailing the `newPacker` class and its methods for configuring and executing the 2D bin packing algorithm. Includes explanations of modes, algorithms, sorting, rotation, and how to add bins and rectangles. ```APIDOC class newPacker([, mode][, bin_algo][, pack_algo][, sort_algo][, rotation]) Return a new packer object * mode: Mode of operations * PackingMode.Offline: The set of rectangles is known beforehand, packing won't start until *pack()* is called. * PackingMode.Online: The rectangles are unknown at the beginning of the job, and will be packed as soon as they are added. * bin_algo: Bin selection heuristic * PackingBin.BNF: (Bin Next Fit) If a rectangle doesn't fit into the current bin, close it and try next one. * PackingBin.BFF: (Bin First Fit) Pack rectangle into the first bin it fits (without closing) * PackingBin.BBF: (Bin Best Fit) Pack rectangle into the bin that gives best fitness. * PackingBin.Global: For each bin pack the rectangle with the best fitness until it is full, then continue with next bin. * pack_algo: One of the supported packing algorithms (see list below) * sort_algo: Rectangle sort order before packing (only for offline mode) * SORT_NONE: Rectangles left unsorted. * SORT_AREA: Sort by descending area. * SORT_PERI: Sort by descending perimeter. * SORT_DIFF: Sort by difference of rectangle sides. * SORT_SSIDE: Sort by shortest side. * SORT_LSIDE: Sort by longest side. * SORT_RATIO: Sort by ration between sides. * rotation: Enable or disable rectangle rotation. packer.add_bin(width, height[, count][, bid]) Add empty bin or bins to a packer * width: Bin width * height: Bin height * count: Number of bins to add, 1 by default. It's possible to add infinie bins with *count=float("inf")* * bid: Optional bin identifier packer.add_rect(width, height[, rid]) Add rectangle to packing queue * width: Rectangle width * height: Rectangle height * rid: User assigned rectangle id packer.pack(): Starts packing process (only for offline mode). packer.rect_list(): Returns the list of packed rectangles, each one represented by the tuple (b, x, y, w, h, rid) where: * b: Index for the bin the rectangle was packed into * x: X coordinate for the rectangle bottom-left corner * y: Y coordinate for the rectangle bottom-left corner * w: Rectangle width * h: Rectangle height * rid: User provided id or None ``` -------------------------------- ### Run Tests Source: https://github.com/secnot/rectpack/blob/master/README.md Commands to execute the test suite for the rectpack library. These commands utilize standard Python testing tools. ```bash python setup.py test ``` ```bash python -m unittest discover ``` -------------------------------- ### Basic Usage: Packing Rectangles Source: https://github.com/secnot/rectpack/blob/master/README.md Demonstrates the fundamental steps to pack a list of rectangles into a set of bins using the rectpack library. It covers adding rectangles and bins, initiating the packing, and accessing basic results like the number of bins used and dimensions of packed rectangles. ```python 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() ``` -------------------------------- ### Accessing Packed Rectangle Data Source: https://github.com/secnot/rectpack/blob/master/README.md Illustrates how to retrieve and inspect the details of packed rectangles after the packing process is complete. This includes accessing bin information, individual rectangle positions, dimensions, and user-assigned IDs. ```python # Obtain number of bins used for packing nbins = len(packer) # Index first bin abin = packer[0] # Bin dimmensions (bins can be reordered during packing) width, height = abin.width, abin.height # Number of rectangles packed into first bin nrect = len(packer[0]) # Second bin first rectangle rect = packer[1][0] # rect is a Rectangle object x = rect.x # rectangle bottom-left x coordinate y = rect.y # rectangle bottom-left y coordinate w = rect.width h = rect.height ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.