boxpacker3

package module
v0.0.0-...-60fa478 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 2, 2024 License: MIT Imports: 3 Imported by: 0

README

boxpacker3

A 3D bin packing library in golang.

Usage
packer := boxpacker3.NewPacker()

boxes := []*boxpacker3.Box{
  boxpacker3.NewBox("box std", 530, 380, 265, 20000),
}
items := []*boxpacker3.Item{
  boxpacker3.NewItem("product 1", 100, 100, 5, 2690),
  boxpacker3.NewItem("product 2", 100, 5, 100, 2690),
  boxpacker3.NewItem("product 3", 5, 100, 100, 2690),
}

packResult := packer.Pack(boxes, items)
fmt.Println(packResult.Boxes) // boxes and items
fmt.Println(packResult.UnfitItems) // Items that didn't fit in boxes

Supported by

Supported by JetBrains

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyPtr

func CopyPtr[T any](original *T) *T

CopyPtr creates a copy of a pointer.

It takes a pointer to a generic type T as an argument and returns a new pointer to a copy of the original value. If the original pointer is nil, it returns nil.

func CopySlicePtr

func CopySlicePtr[T any](data []*T) []*T

CopySlicePtr creates a copy of a slice of pointers.

It takes a slice of pointers as an argument and returns a new slice with the same elements, but with each element being a copy of the original.

Types

type Axis

type Axis int

Axis represents the axis of a dimension.

const (
	WidthAxis Axis = iota
	// HeightAxis represents the height axis.
	HeightAxis
	// DepthAxis represents the depth axis.
	DepthAxis
)

WidthAxis represents the width axis.

type Box

type Box struct {
	// contains filtered or unexported fields
}

Box represents a box that can hold items.

It has fields for the box's dimensions and maximum weight. It also has fields for tracking the box's current items and their volume and weight.

func NewBox

func NewBox(id string, w, h, d, mw float64) *Box

NewBox creates a new Box with the given id, dimensions, and maximum weight.

Parameters: - id: a unique identifier for the box. - w: the width of the box. - h: the height of the box. - d: the depth of the box. - mw: the maximum weight the box can hold.

Returns: - A pointer to the newly created Box.

func (*Box) GetDepth

func (b *Box) GetDepth() float64

GetDepth returns the depth of the box.

func (*Box) GetHeight

func (b *Box) GetHeight() float64

GetHeight returns the height of the box.

func (*Box) GetID

func (b *Box) GetID() string

GetID returns the unique identifier of the box.

func (*Box) GetItems

func (b *Box) GetItems() []*Item

GetItems returns a slice of pointers to the items currently in the box.

The slice is a copy and not a reference to the original slice, so modifying the slice returned by this function will not affect the contents of the box.

func (*Box) GetMaxWeight

func (b *Box) GetMaxWeight() float64

GetMaxWeight returns the maximum weight the box can hold.

func (*Box) GetVolume

func (b *Box) GetVolume() float64

GetVolume returns the volume of the box.

func (*Box) GetWidth

func (b *Box) GetWidth() float64

GetWidth returns the width of the box.

func (*Box) PutItem

func (b *Box) PutItem(item *Item, p Pivot) bool

PutItem Attempts to place the given item at the specified anchor point within the box.

Attempts to place the given item at the specified anchor point within the box.

It tries to place the item at the given anchor point by iterating through each rotation type (Whd, Hwd, Hdw, Dhw, Dwh, Wdh) and checks if the item can be placed within the box without intersecting with any of the other items in the box. If the item can be placed, it inserts the item into the box and returns true. If the item cannot be placed, it returns false.

Parameters:

  • item: The item to be placed in the box.
  • p: The anchor point at which to attempt placing the item within the box.

Returns:

  • bool: True if the item was successfully placed within the box, false otherwise.

func (*Box) Reset

func (b *Box) Reset()

Reset clears the box and resets the volume and weight.

It removes all items from the box by slicing the items slice to an empty slice. It sets the total volume and weight of items in the box to 0.

type Dimension

type Dimension [3]float64

Dimension represents the dimensions of an item or a box.

type Item

type Item struct {
	// contains filtered or unexported fields
}

Item represents an item that can be packed into a box.

func NewItem

func NewItem(id string, w, h, d, wg float64) *Item

NewItem creates a new item with the given parameters.

func (*Item) GetDepth

func (i *Item) GetDepth() float64

GetDepth returns the depth of the item.

func (*Item) GetDimension

func (i *Item) GetDimension() Dimension

GetDimension returns the dimensions of the item based on its current rotation type.

The dimensions are returned as a Dimension struct.

Returns:

  • Dimension: The dimensions of the item.

func (*Item) GetHeight

func (i *Item) GetHeight() float64

GetHeight returns the height of the item.

func (*Item) GetID

func (i *Item) GetID() string

GetID returns the id of the item.

func (*Item) GetPosition

func (i *Item) GetPosition() Pivot

GetPosition returns the position of the item.

func (*Item) GetVolume

func (i *Item) GetVolume() float64

GetVolume returns the volume of the item.

func (*Item) GetWeight

func (i *Item) GetWeight() float64

GetWeight returns the weight of the item.

func (*Item) GetWidth

func (i *Item) GetWidth() float64

GetWidth returns the width of the item.

func (*Item) Intersect

func (i *Item) Intersect(it *Item) bool

Intersect tests for intersections between two items.

It checks for intersections between the current item and the given item. It does this by getting the dimensions of the current item and the given item and then calling the intersect method with the appropriate parameters.

Parameters:

  • it: The item to check for intersections with.

Returns:

  • bool: True if the items intersect, false otherwise.

type Packer

type Packer struct{}

Packer is a struct that packs items into boxes.

It sorts input boxes and items by volume and weight. It then selects the box with the largest volume and weight that can accommodate the items. If there are still items left after packing the boxes, it sets them as unfit items.

func NewPacker

func NewPacker() *Packer

NewPacker creates a new instance of Packer.

Returns: - a pointer to a Packer struct.

func (*Packer) Pack

func (p *Packer) Pack(inputBoxes []*Box, inputItems []*Item) *Result

Pack packs items into boxes.

This function sorts input boxes and items by volume and weight. It selects the box with the largest volume and weight that can accommodate the items. If there are still items left after packing the boxes, they will be set as unfit items.

Parameters: - inputBoxes: a list of boxes. - inputItems: a list of items.

Returns: - a Result struct that contains two slices:

  • Boxes: a list of boxes with items.
  • UnfitItems: a list of items that didn't fit into boxes.

func (*Packer) PackCtx

func (p *Packer) PackCtx(ctx context.Context, inputBoxes []*Box, inputItems []*Item) (*Result, error)

PackCtx packs items into boxes asynchronously and handles the context.

This function sorts input boxes and items by volume and weight. It selects the box with the largest volume and weight that can accommodate the items. If there are still items left after packing the boxes, they will be set as unfit items.

Parameters: - ctx: the context.Context to use. - inputBoxes: a list of boxes. - inputItems: a list of items.

Returns:

  • *Result: the result of packing items into boxes.
  • error: If the context is done before the packing process is complete, an error will be returned. nil will be returned otherwise.

This function is useful when you want to pack items into boxes asynchronously and handle the context at the same time.

type Pivot

type Pivot [3]float64

Pivot represents the position of an item within a box.

type Result

type Result struct {
	// UnfitItems is a list of items that didn't fit into boxes.
	UnfitItems itemSlice

	// Boxes is a list of boxes with items.
	Boxes boxSlice
}

Result represents the result of packing items into boxes.

It is a struct that contains two slices: - UnfitItems: a list of items that didn't fit into boxes. - Boxes: a list of boxes with items.

type RotationType

type RotationType int

RotationType represents the type of rotation for an item.

const (
	RotationTypeWhd RotationType = iota
	// RotationTypeHwd represents the rotation type where the height is the longest dimension.
	RotationTypeHwd
	// RotationTypeHdw represents the rotation type where the depth is the longest dimension.
	RotationTypeHdw
	// RotationTypeDhw represents the rotation type where the depth is the longest dimension.
	RotationTypeDhw
	// RotationTypeDwh represents the rotation type where the width is the longest dimension.
	RotationTypeDwh
	// RotationTypeWdh represents the rotation type where the height is the longest dimension.
	RotationTypeWdh
)

RotationTypeWhd represents the rotation type where the width is the longest dimension.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL