1 unstable release
Uses new Rust 2024
| 0.1.0 | Nov 28, 2025 |
|---|
#1203 in Data structures
156 downloads per month
19KB
406 lines
Shelf Best Height Fit 2D atlas allocator (Rust port of smol-atlas).
This crate implements a dynamic 2D atlas allocator using the Shelf Best Height Fit
heuristic. Items are added with add and removed with remove; shelves are never
merged or resized, and free spans within each shelf are tracked as sorted segments.
Example
use smol_atlas::Atlas;
// Create a 128x128 atlas
let mut atlas = Atlas::new(128, 128);
// Insert a few items
let a = atlas.add(32, 16).expect("fits");
let b = atlas.add(16, 16).expect("fits");
// Query positions
let (ax, ay) = (atlas.item_x(a).unwrap(), atlas.item_y(a).unwrap());
let (bx, by) = (atlas.item_x(b).unwrap(), atlas.item_y(b).unwrap());
assert!(ax <= bx || ay <= by);
// Remove one item and insert another to reuse space
assert!(atlas.remove(a));
let c = atlas.add(32, 16).expect("should reuse free span");
assert_eq!(atlas.item_y(c), Some(ay));
smol-atlas
Pack rectangles into a fixed-size 2D atlas with item removal. Uses the Shelf Best Height Fit heuristic; shelves never merge or move, and free spans inside a shelf are tracked to reuse space.
Usage
use smol_atlas::Atlas;
let mut atlas = Atlas::new(256, 256);
let a = atlas.add(64, 32).expect("fits");
let b = atlas.add(32, 32).expect("fits");
println!("a at ({}, {})", atlas.item_x(a).unwrap(), atlas.item_y(a).unwrap());
atlas.remove(b);
let c = atlas.add(32, 32).expect("reuses space");
assert_eq!(atlas.item_y(c), atlas.item_y(a)); // likely same shelf
Credits
- smol-atlas: https://github.com/aras-p/smol-atlas