trimesh.path.packing

Classes

RectangleBin([bounds, size])

2D BSP tree node.

Functions

images(images[, power_resize])

Pack a list of images and return result and offsets.

paths(paths, **kwargs)

Pack a list of Path2D objects into a rectangle.

polygons(polygons[, sheet_size, iterations, …])

Pack polygons into a rectangle by taking each Polygon’s OBB and then packing that as a rectangle.

rectangles(rectangles[, sheet_size, …])

Run multiple iterations of rectangle packing.

rectangles_single(rectangles[, sheet_size, …])

Execute a single insertion order of smaller rectangles onto a larger rectangle using a binary space partition tree.

class trimesh.path.packing.RectangleBin(bounds=None, size=None)

Bases: object

2D BSP tree node. http://www.blackpawn.com/texts/lightmaps/

Attributes

extents

Bounding box size.

Methods

insert(rectangle)

Insert a rectangle into the bin.

split(length[, vertical])

Returns two bounding boxes representing the current bounds split into two smaller boxes.

property extents

Bounding box size.

Returns

extents – Edge lengths of bounding box

Return type

(2,) float

insert(rectangle)

Insert a rectangle into the bin.

Parameters

rectangle ((2,) float) – Size of rectangle to insert

Returns

inserted – Position of insertion in the tree

Return type

None or (2,) float

split(length, vertical=True)

Returns two bounding boxes representing the current bounds split into two smaller boxes.

Parameters
  • length (float) – Length to split

  • vertical (bool) – Split the box vertically rather than horizontally

Returns

box

Two bounding boxes with min-max:

[minx, miny, maxx, maxy]

Return type

(2, 4) float

trimesh.path.packing.images(images, power_resize=False)

Pack a list of images and return result and offsets.

Parameters
  • images ((n,) PIL.Image) – Images to be packed

  • deduplicate (bool) – If True deduplicate images before packing

Returns

  • packed (PIL.Image) – Multiple images packed into result

  • offsets ((n, 2) int) – Offsets for original image to pack

trimesh.path.packing.paths(paths, **kwargs)

Pack a list of Path2D objects into a rectangle.

Parameters

paths ((n,) Path2D) – Geometry to be packed

Returns

  • packed (trimesh.path.Path2D) – Object containing input geometry

  • inserted ((m,) int) – Indexes of paths inserted into result

trimesh.path.packing.polygons(polygons, sheet_size=None, iterations=50, density_escape=0.95, spacing=0.094, quantity=None, **kwargs)

Pack polygons into a rectangle by taking each Polygon’s OBB and then packing that as a rectangle.

Parameters
  • polygons ((n,) shapely.geometry.Polygon) – Source geometry

  • sheet_size ((2,) float) – Size of rectangular sheet

  • iterations (int) – Number of times to run the loop

  • density_escape (float) – When to exit early (0.0 - 1.0)

  • spacing (float) – How big a gap to leave between polygons

  • quantity ((n,) int, or None) – Quantity of each Polygon

Returns

  • overall_inserted ((m,) int) – Indexes of inserted polygons

  • packed ((m, 3, 3) float) – Homogeonous transforms from original frame to packed frame

trimesh.path.packing.rectangles(rectangles, sheet_size=None, density_escape=0.9, spacing=0.0, iterations=50, quanta=None)

Run multiple iterations of rectangle packing.

Parameters
  • rectangles ((n, 2) float) – Size of rectangles to be packed

  • sheet_size (None or (2,) float) – Size of sheet to pack onto

  • density_escape (float) – Exit early if density is above this threshold

  • spacing (float) – Distance to allow between rectangles

  • iterations (int) – Number of iterations to run

  • quanta (None or float) –

Returns

  • density (float) – Area filled over total sheet area

  • offset ((m,2) float) – Offsets to move rectangles to their packed location

  • inserted ((n,) bool) – Which of the original rectangles were packed

  • consumed_box ((2,) float) – Bounding box size of packed result

trimesh.path.packing.rectangles_single(rectangles, sheet_size=None, shuffle=False)

Execute a single insertion order of smaller rectangles onto a larger rectangle using a binary space partition tree.

Parameters
  • rectangles ((n, 2) float) – An array of (width, height) pairs representing the rectangles to be packed.

  • sheet_size ((2,) float) – Width, height of rectangular sheet

  • shuffle (bool) – Whether or not to shuffle the insert order of the smaller rectangles, as the final packing density depends on insertion order.

Returns

  • density (float) – Area filled over total sheet area

  • offset ((m,2) float) – Offsets to move rectangles to their packed location

  • inserted ((n,) bool) – Which of the original rectangles were packed

  • consumed_box ((2,) float) – Bounding box size of packed result