rect_packer/lib.rs
1//! Pack small rectangles into a larger one. This is useful for creating texture atlases for the efficient GPU rendering.
2//!
3//! Usage example:
4//!
5//! ```
6//! use rect_packer::Packer;
7//!
8//! let config = rect_packer::Config {
9//! width: 1024,
10//! height: 1024,
11//!
12//! border_padding: 5,
13//! rectangle_padding: 10,
14//! };
15//!
16//! let rectangles = [(50, 70), (350, 210), (255, 410)];
17//!
18//! let mut packer = Packer::new(config);
19//! for &(width, height) in &rectangles {
20//! if let Some(rect) = packer.pack(width, height, false) {
21//! println!("Rectangle is at position ({}, {}) within the encompassing rectangle",
22//! rect.x,
23//! rect.y);
24//! }
25//! }
26//!
27#[cfg(test)]
28extern crate rand;
29
30#[cfg(test)]
31extern crate image;
32
33pub use rect::Rect;
34pub use packer::Packer;
35pub use packer::DensePacker;
36
37mod rect;
38mod packer;
39
40#[cfg(test)]
41mod test;
42
43/// Describes size and padding requirements of rectangle packing.
44#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
45pub struct Config {
46 /// Width of the encompassing rectangle.
47 pub width: i32,
48 /// Height of the encompassing rectangle.
49 pub height: i32,
50
51 /// Minimum spacing between border and rectangles.
52 pub border_padding: i32,
53 /// Minimum spacing between rectangles.
54 pub rectangle_padding: i32,
55}