flow-web/blog-posts/2026-06-10-11:31-flow-rbp-a-library-for-packing-rectangles-into-two-dimensional-finite-bins.md
Andreas Widen a4a5c7daa5
Initial commit.
Signed-off-by: Andreas Widen <aw@luflow.net>
2026-07-27 17:57:03 +02:00

82 lines
2.8 KiB
Markdown

author: Andreas
published: 2026-06-10 11:31:00
updated: 2026-06-10 11:31:00
topics: flow-rbp, Rect bin packing, Rust
title: flow-rbp: A library for packing rectangles into two-dimensional finite bins
snippet: flow-rbp is a library for packing rectangles into two-dimensional finite bins using different heuristic methods for placement.
---
[flow-rbp](https://luflow.net/git/hfsoulz/flow-rbp.git) 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](https://luflow.net/git/hfsoulz/flow-texpack.git) is a program that
uses [flow-rbp](https://luflow.net/git/hfsoulz/flow-rbp.git) to generate texture
atlas.
### Installing Rust
Install `Rust` from your package manager or by downloading from here:
[https://rust-lang.org/](https://rust-lang.org/).
### Getting the code
Install `git` from your package manager or by downloading from here:
[https://git-scm.com/install](https://git-scm.com/install). The [git
repository](https://luflow.net/git/hfsoulz/flow-rbp.git) can also be browsed
online.
Clone the `git` repository:
```sh
git clone https://luflow.net/git/hfsoulz/flow-rbp.git
```
### Usage
Add this to your `Cargo.toml`:
```
[dependencies]
flow-rbp = { git = "https://luflow.net/git/hfsoulz/flow-rbp.git", tag = "v0.1.0" }
```
Then:
```rust
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](https://luflow.net/git/hfsoulz/flow-rbp.git) is licensed under the
zlib license. This license allows you to use `flow-rbp` freely in any software.