flow-rbp: A library for packing rectangles into two-dimensional finite bins
flow-rbp is a library for packing rectangles into two-dimensional finite bins using different heuristic methods for placement.
The two-dimensional rectangle bin packing is a classical problem in
combinatorial optimization. In this problem, one is given a sequence of
rectangles (R1, R2, ... Rn), Ri = (wi, hi) and the task is to find a packing
of these items into a minimum number of bins of size (W, H). No two
rectangles may intersect or be contained inside one another. This library uses
an algorithm sometimes referred as The Maximal Rectangles ALgorithm. This
algorithm stores a list of free rectangles that represents the free area of the
bin.
flow-texpack is a program that uses flow-rbp to generate texture atlas.
Installing Rust
Install Rust from your package manager or by downloading from here:
https://rust-lang.org/.
Getting the code
Install git from your package manager or by downloading from here:
https://git-scm.com/install. The git
repository can also be browsed
online.
Clone the git repository:
git clone https://luflow.net/git/flow-rbp.git
Usage
Add this to your Cargo.toml:
[dependencies]
flow-rbp = { git = "https://luflow.net/git/flow-rbp.git", tag = "v0.1.0" }
Then:
use flow_rbp::FreeRectHeuristic;
use flow_rbp::RectsBinPack;
// create a new bin of size 32x32 which allows rotation:
let mut rbp = RectsBinPack::new(32, 32, true).unwrap();
// make sure occupancy is zero:
assert_eq!(rbp.get_occupancy(), 0.0);
// add a few rects that should fit:
assert_eq!(rbp.insert(16, 16, FreeRectHeuristic::BottomLeft).is_some(), true);
assert_eq!(rbp.insert(16, 16, FreeRectHeuristic::BottomLeft).is_some(), true);
assert_eq!(rbp.get_occupancy(), 0.5);
assert_eq!(rbp.insert(16, 16, FreeRectHeuristic::BottomLeft).is_some(), true);
assert_eq!(rbp.insert(16, 16, FreeRectHeuristic::BottomLeft).is_some(), true);
assert_eq!(rbp.get_occupancy(), 1.0);
// this rect will not fit and therefore returns None:
assert_eq!(rbp.insert(1, 1, FreeRectHeuristic::BottomLeft).is_none(), true);
License
flow-rbp is licensed under the
zlib license. This license allows you to use flow-rbp freely in any software.
Unless otherwise stated, blog posts on this site are copyrighted by their respective authors and published under the terms of the CC-BY-SA 4.0 license.