Documentation ¶
Index ¶
- Constants
- func SortArea(a, b Size) int
- func SortDiff(a, b Size) int
- func SortMaxSide(a, b Size) int
- func SortMinSide(a, b Size) int
- func SortPerimeter(a, b Size) int
- func SortRatio(a, b Size) int
- type Heuristic
- type Packer
- func (p *Packer) AllowFlip(enabled bool)
- func (p *Packer) Clear()
- func (p *Packer) Insert(sizes ...Size) []Size
- func (p *Packer) InsertSize(id, width, height int) bool
- func (p *Packer) Map() map[int]Rect
- func (p *Packer) Pack() bool
- func (p *Packer) Rects() []Rect
- func (p *Packer) RepackAll() bool
- func (p *Packer) Size() Size
- func (p *Packer) Sorter(compare SortFunc, reverse bool)
- func (p *Packer) Unpacked() []Size
- func (p *Packer) Used(current bool) float64
- type Point
- type Rect
- func (r *Rect) Bottom() int
- func (r *Rect) BottomLeft() Point
- func (r *Rect) BottomRight() Point
- func (r *Rect) Center() Point
- func (r *Rect) Contains(x, y int) bool
- func (r *Rect) ContainsRect(rect Rect) bool
- func (r *Rect) Eq(rect Rect) bool
- func (r *Rect) Inflate(width, height int)
- func (r *Rect) Intersect(rect Rect) (result Rect)
- func (r *Rect) Intersects(rect Rect) bool
- func (r *Rect) IsEmpty() bool
- func (r *Rect) Left() int
- func (r *Rect) Right() int
- func (r *Rect) String() string
- func (r *Rect) Top() int
- func (r *Rect) TopLeft() Point
- func (r *Rect) TopRight() Point
- func (r *Rect) Union(rect Rect) Rect
- type Size
- type SortFunc
Constants ¶
const ( // MaxRects selects the MaxRects algorithm for packing. This generally results in the // most efficiently packed results when packing to a static size. It can result in a lot of // waste if a sensible size and bin heustic is not chosen for the given inputs, but has the // most potential for efficiency. // // Type: Algorithm MaxRects Heuristic = 0x0 // Skyline selects the Skyline algorithm for packing. Skyline provides a good balance between // speed and efficiency, and is good for maintaining the least amount of waste at any given // time, making it a good choice for dynamic data and simply using whatever the final size may // be. // // Type: Algorithm Skyline = 0x1 // Guillotine selects the Guillotine algorithm for packing. This algorithm is typically // faster, but is much more sensitive to choosing the correct packing/splitting methods for // specific inputs. This makes it less "general-purpose", but can still be the best choice // in certain situations where the input sizes are predictable. // // Type: Algorithm Guillotine = 0x2 // BestShortSideFit (BSSF) positions the rectangle against the short side of a free rectangle // into which it fits the best. // // * Type: Bin-Selection // * Valid With: MaxRects, Guillotine BestShortSideFit = 0x00 // BestLongSideFit (BLSF) positions the rectangle against the long side of a free rectangle // into which it fits the best. // // * Type: Bin-Selection // * Valid With: MaxRects, Guillotine BestLongSideFit = 0x10 // BestAreaFit (BAF) positions the rectangle into the smallest free rect into which it fits. // // * Type: Bin-Selection // * Valid With: MaxRects, Guillotine BestAreaFit = 0x20 // BottomLeft (BL) does the Tetris placement. // // * Type: Bin-Selection // * Valid With: MaxRects, Skyline BottomLeft = 0x30 // ContactPoint (CP) choosest the placement where the rectangle touches other rects as much // as possible. // // * Type: Bin-Selection // * Valid With: MaxRects ContactPoint = 0x40 // WorstAreaFit (WAF) is the opposite of the BestAreaFit (BAF) heuristic. Contrary to its // name, this is not always "worse" with speciifc inputs. // // * Type: Bin-Selection // * Valid With: Guillotine WorstAreaFit = 0x50 // WorstShortSideFit (WSSF) is the opposite of the BestShortSideFit (BSSF) heuristic. Contrary // to its name, this is not always "worse" with speciifc inputs. // // * Type: Bin-Selection // * Valid With: Guillotine WorstShortSideFit = 0x60 // WorstLongSideFit (WLSF) is the opposite of the BestLongSideFit (BLSF) heuristic. Contrary // to its name, this is not always "worse" with speciifc inputs. // // * Type: Bin-Selection // * Valid With: Guillotine WorstLongSideFit = 0x70 // MinWaste (MW) uses a "waste map" to split empty spaces and determine which placement will // result in the least amount of wasted space. This is most effective when flip/rotate is // enabled by the packer. // // * Type: Bin-Selection // * Valid With: Skyline MinWaste = 0x80 // SplitShorterLeftoverAxis (SLAS) // // * Type: Split Method // * Valid With: Guillotine SplitShorterLeftoverAxis = 0x0000 // SplitLongerLeftoverAxis (LLAS) // // * Type: Split Method // * Valid With: Guillotine SplitLongerLeftoverAxis = 0x0100 // SplitMinimizeArea (MINAS) try to make a single big rectangle at the expense of making the // other small. // // * Type: Split Method // * Valid With: Guillotine SplitMinimizeArea = 0x0200 // SplitMaximizeArea (MAXAS) try to make both remaining rectangles as even-sized as possible. // // *Type: Split Method // * Valid With: Guillotine SplitMaximizeArea = 0x0300 // SplitShorterAxis (SAS) // // * Type: Split Method // * Valid With: Guillotine SplitShorterAxis = 0x0400 // SplitLongerAxis (LAS) // // * Type: Split Method // * Valid With: Guillotine SplitLongerAxis = 0x0500 // MaxRectsBSSF // // * Type: Preset MaxRectsBSSF = MaxRects | BestShortSideFit // MaxRectsBL // // * Type: Preset MaxRectsBL = MaxRects | BottomLeft // MaxRectsCP // // * Type: Preset MaxRectsCP = MaxRects | ContactPoint // MaxRectsBLSF // // * Type: Preset MaxRectsBLSF = MaxRects | BestLongSideFit // MaxRectsBAF // // * Type: Preset MaxRectsBAF = MaxRects | BestAreaFit // GuillotineBAF // // * Type: Preset GuillotineBAF = Guillotine | BestAreaFit // GuillotineBSSF // // * Type: Preset GuillotineBSSF = Guillotine | BestShortSideFit // GuillotineBLSF // // * Type: Preset GuillotineBLSF = Guillotine | BestLongSideFit // GuillotineWAF // // * Type: Preset GuillotineWAF = Guillotine | WorstAreaFit // GuillotineWSSF // // * Type: Preset GuillotineWSSF = Guillotine | WorstShortSideFit // GuillotineWLSF // // * Type: Preset GuillotineWLSF = Guillotine | WorstLongSideFit // SkylineBLF // // * Type: Preset SkylineBLF = Skyline | BottomLeft // SkylineMinWaste // // * Type: Preset SkylineMinWaste = Skyline | MinWaste )
const DefaultSize = 4096
DefaultSize is the default width/height used as the maximum extent for packing rectangles.
There is based off a maximum texture size for many modern GPUs. If this library is not being used for creating a texture atlas, then there is absolutely no significance about this number other than providing a sane starting point.
Variables ¶
This section is empty.
Functions ¶
func SortArea ¶
SortArea sorts two rectangle sizes in descending order (greatest to least) by comparing the total area of each.
func SortDiff ¶
SortDiff sorts two rectangle sizes in descending order (greatest to least) by comparing the difference between the width/height of each.
func SortMaxSide ¶
SortMaxSide sorts two rectangle sizes in descending order (greatest to least) by comparing the longest side of each.
func SortMinSide ¶
SortMinSide sorts two rectangle sizes in descending order (greatest to least) by comparing the shortest side of each.
func SortPerimeter ¶
SortPerimeter sorts two rectangle sizes in descending order (greatest to least) by comparing the perimeter of each.
Types ¶
type Heuristic ¶
type Heuristic uint16
Heuristic is a bitfield used for configuration of a rectangle packing algorithm, including the general type, bin selection method, and strategy for how to split empty areas. Specific combinations of values can be XOR'ed together to achieve the desired behavior.
Note that not not all combinations are valid, each constant of this type will indicate what it is valid with. If in doubt, simply use a preset.
To test if a value is valid, use the Validate function, which will return an error message describing the issue. When an invalid value is used, the algorithm default will be used, but otherwise no error will occur.
func (Heuristic) Validate ¶
Validate tests whether the combination of heuristics are in good form. A value of nil is returned upon success, otherwise an error with message explaining the error.
Note that invalid heuristics will silently fail and cause the packer to revert to its default for that setting.
type Packer ¶
type Packer struct { // Padding defines the amount of empty space to place around rectangles. Values of 0 or less // indicates that rectangles will be tightly packed. // // Default: 0 Padding int // Online indicates if rectangles should be packed as they are inserted (online), or simply // collected until Pack is called. // // There is a trade-off to online/offline packing. // // * Online packing is faster to pack due to a lack of sorting or comparing to // other rectangles, but results in significantly less optimized results. // * Offline packing can be significantly slower, but allows the algorithm to achieve its // maximum potential by having all sizes known ahead of time and sorting for efficiency. // // Unless you are packing and using the results in real-time, it is recommended to use // offline mode (default). For tasks such creating a texture atlas, spending the extra time // to prepare the atlas in the most efficient manner is well worth the extra milliseconds // of computation. // // Default: false Online bool // contains filtered or unexported fields }
Packer contains the state of a 2D rectangle packer.
func NewDefaultPacker ¶
func NewDefaultPacker() *Packer
NewDefaultPacker initializes a new Packer with sensible default settings suitable for general-purpose rectangle packing.
func NewPacker ¶
NewPacker initializes a new Packer using the specified maximum size and heustistics for packing rectangles.
A width/height less than 1 will cause a panic,
func (*Packer) AllowFlip ¶
AllowFlip indicates if rectangles can be flipped/rotated to provide better placement.
Default: false
func (*Packer) Clear ¶
func (p *Packer) Clear()
Clear resets the internal state of the packer without changing its current configuration. All currently packed and pending rectangles are removed.
func (*Packer) Insert ¶
Insert adds to rectangles to the packer.
When online mode is enabled, the rectangle(s) are immediately packed. The return value will contain any values that could not be packed due to size limitations, or an empty slice upon success.
When online mode is disabled, the rectangles(s) are simply staged to be packed with the next call to Pack. The return value will contain a slice of all rectangles that are currently staged.
func (*Packer) InsertSize ¶
Insert adds to rectangles to the packer.
When online mode is enabled, the rectangle(s) are immediately packed. The return value will contain any values that could not be packed due to size limitations, or an empty slice upon success.
When online mode is disabled, the rectangles(s) are simply staged to be packed with the next call to Pack. The return value will contain a slice of all rectangles that are currently staged.
func (*Packer) Map ¶
Map creates and returns a map where each key is an ID, and the value is the rectangle it pertains to.
func (*Packer) Pack ¶
Pack will sort and pack all rectangles that are currently staged.
The return value indicates if all staged rectangles were successfully packed. When false, Unpacked can be used to retrieve the sizes that failed.
func (*Packer) Rects ¶
Rects returns a slice of rectangles that are currently packed.
The backing memory is owned by the packer, and a copy should be made if modification or persistence is required.
func (*Packer) RepackAll ¶
RepackAll clears the internal packed rectangles, and repacks them all with one operation. This can be useful to optimize the packing when/if it was previously performed in multiple pack operations, or to reflect settings for the packer that have been modified.
func (*Packer) Size ¶
Size computes the size of the current packing. The returned value is the minimum size required to contain all packed rectangles.
func (*Packer) Sorter ¶
Sorter sets the comparer function used for pre-sorting sizes before packing. Depending on the algorithm and the input data, this can provide a significant improvement on efficiency.
Default: SortArea
func (*Packer) Unpacked ¶
Unpacked returns a slice of rectangles that are currently staged to be packed.
The backing memory is owned by the packer, and a copy should be made if modification or persistence is required.
func (*Packer) Used ¶
Used computes the ratio of used surface area to the available area, in the range of 0.0 and 1.0.
When current is set to true, the ratio will reflect the ratio of used surface area relative to the current size required by the packer, otherwise it is the ratio of the maximum possible area.
type Point ¶
type Point struct { // X is the location on the horizontal x-axis. X int `json:"x"` // Y is the location on the vertical y-axis. Y int `json:"y"` }
Point describes a location in 2D space.
func (*Point) Move ¶
Move will move the location of the receiver to the specified absolute coordinates.
type Rect ¶
type Rect struct { // Point is the location of the rectangle. Point // Size is the dimensions of the rectangle. Size // Flipped indicates if a rectangle has been flipped to achieve a better fit while // being packed. Only relevant when the packer has AllowFlip enabled. Flipped bool `json:"flipped,omitempty"` }
Rect describes a location (top-left corner) and size in 2D space.
func NewRectLTRB ¶
NewRectLTRB initializes a new rectangle using the specified left/top/right/bottom values.
func (*Rect) Bottom ¶
Bottom returns the coordinate of the bottom-edge of the rectangle on the y-axis.
func (*Rect) BottomLeft ¶
BottomLeft returns a point representing the bottom-left corner of the rectangle.
func (*Rect) BottomRight ¶
BottomRight returns a point representing the bottom-right corner of the rectangle.
func (*Rect) Center ¶
Centers returns a point representing the center of the rectangle. For rectangles that are a power of two, the coordinate is floored.
func (*Rect) Contains ¶
Contains tests whether the specified coordinates are within the bounds of the receiver.
func (*Rect) ContainsRect ¶
ContainsRect tests whether the specified rectangle is contained within the bounds of the current receiver.
func (*Rect) Inflate ¶
Inflate pushes each edge of the rectangle out from the center by the specified relative amount on each axis.
func (*Rect) Intersect ¶
Intersect returns a rectangle representing only the overlapping area of this rectangle and another, or an empty recatangle when no overlap is present.
func (*Rect) Intersects ¶
Intersects tests whether the receiver has any overlap with the specified rectangle.
type Size ¶
type Size struct { // Width is the dimension on the horizontal x-axis. Width int `json:"width"` // Height is the dimensions on the vertical y-axis. Height int `json:"height"` // ID is a user-defined identifier that can be used to differentiate this instance from others. ID int `json:"-"` }
Size describes dimensions of an entity in 2D space.
func (*Size) Eq ¶
Eq tests whether the receiver and another size have equal values. The ID field is ignored.