]> luflow.net public git repositories - flow-web.git/blob - blog-posts/2026-06-10-11:31-flow-rbp-a-library-for-packing-rectangles-into-two-dimensional-finite-bins.md
Initial commit.
[flow-web.git] / blog-posts / 2026-06-10-11:31-flow-rbp-a-library-for-packing-rectangles-into-two-dimensional-finite-bins.md
1 author: Andreas
2 published: 2026-06-10 11:31:00
3 updated: 2026-06-10 11:31:00
4 topics: flow-rbp, Rect bin packing, Rust
5 title: flow-rbp: A library for packing rectangles into two-dimensional finite bins
6 snippet: flow-rbp is a library for packing rectangles into two-dimensional finite bins using different heuristic methods for placement.
7
8 ---
9
10 [flow-rbp](https://luflow.net/git-repos/flow-rbp.git) is a library for packing
11 rectangles into two-dimensional finite bins using different heuristic methods
12 for placement.
13
14 The two-dimensional rectangle bin packing is a classical problem in
15 combinatorial optimization. In this problem, one is given a sequence of
16 rectangles `(R1, R2, ... Rn), Ri = (wi, hi)` and the task is to find a packing
17 of these items into a minimum number of bins of size `(W, H)`. No two
18 rectangles may intersect or be contained inside one another. This library uses
19 an algorithm sometimes referred as `The Maximal Rectangles ALgorithm`. This
20 algorithm stores a list of free rectangles that represents the free area of the
21 bin.
22
23 [flow-texpack](https://luflow.net/git-repos/flow-texpack.git) is a program that
24 uses [flow-rbp](https://luflow.net/git-repos/flow-rbp.git) to generate texture
25 atlas.
26
27 ### Installing Rust
28
29 Install `Rust` from your package manager or by downloading from here:
30 [https://rust-lang.org/](https://rust-lang.org/).
31
32 ### Getting the code
33
34 Install `git` from your package manager or by downloading from here:
35 [https://git-scm.com/install](https://git-scm.com/install). The [git
36 repository](https://luflow.net/git-repos/flow-rbp.git) can also be browsed
37 online.
38
39 Clone the `git` repository:
40
41 ```sh
42 git clone https://luflow.net/git/flow-rbp.git
43 ```
44
45 ### Usage
46
47 Add this to your `Cargo.toml`:
48
49 ```
50 [dependencies]
51 flow-rbp = { git = "https://luflow.net/git/flow-rbp.git", tag = "v0.1.0" }
52 ```
53
54 Then:
55
56 ```rust
57 use flow_rbp::FreeRectHeuristic;
58 use flow_rbp::RectsBinPack;
59
60 // create a new bin of size 32x32 which allows rotation:
61 let mut rbp = RectsBinPack::new(32, 32, true).unwrap();
62
63 // make sure occupancy is zero:
64 assert_eq!(rbp.get_occupancy(), 0.0);
65
66 // add a few rects that should fit:
67 assert_eq!(rbp.insert(16, 16, FreeRectHeuristic::BottomLeft).is_some(), true);
68 assert_eq!(rbp.insert(16, 16, FreeRectHeuristic::BottomLeft).is_some(), true);
69 assert_eq!(rbp.get_occupancy(), 0.5);
70 assert_eq!(rbp.insert(16, 16, FreeRectHeuristic::BottomLeft).is_some(), true);
71 assert_eq!(rbp.insert(16, 16, FreeRectHeuristic::BottomLeft).is_some(), true);
72 assert_eq!(rbp.get_occupancy(), 1.0);
73
74 // this rect will not fit and therefore returns None:
75 assert_eq!(rbp.insert(1, 1, FreeRectHeuristic::BottomLeft).is_none(), true);
76
77 ```
78
79 ### License
80
81 [flow-rbp](https://luflow.net/git-repos/flow-rbp.git) is licensed under the
82 zlib license. This license allows you to use `flow-rbp` freely in any software.