#atlas #2d #allocator #shelf

smol-atlas

2D rectangular bin packing with optional item removal

1 unstable release

Uses new Rust 2024

0.1.0 Nov 28, 2025

#1203 in Data structures

Download history 28/week @ 2025-12-04 27/week @ 2025-12-11 51/week @ 2025-12-18 25/week @ 2025-12-25 31/week @ 2026-01-01 30/week @ 2026-01-08 19/week @ 2026-01-15 28/week @ 2026-01-22 32/week @ 2026-01-29 30/week @ 2026-02-05 34/week @ 2026-02-12 40/week @ 2026-02-19 39/week @ 2026-02-26 47/week @ 2026-03-05 28/week @ 2026-03-12 37/week @ 2026-03-19

156 downloads per month

MIT license

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

No runtime deps