binpack2d 1.0.1

A two-dimensional rectangle bin-packing algorithm.
Documentation
use super::*;

#[test]
fn bin_shrink() {
    let mut bin = MaxRectsBin::new(16, 16);
    bin.insert(&Dimension::new(6, 7), Heuristic::BestShortSideFit);
    bin.shrink(true);
    assert_eq!(8, bin.width());
    assert_eq!(8, bin.height());

    bin.shrink(false);
    assert_eq!(6, bin.width());
    assert_eq!(7, bin.height());
}

#[test]
fn bin_insert() {
    let nodes = vec![
        Dimension::with_padding(2, 4, 0),
        Dimension::with_padding(6, 4, 1),
        Dimension::with_padding(10, 3, 1),
        Dimension::with_padding(6, 6, 0),
        Dimension::with_padding(4, 4, 2),
        Dimension::with_padding(3, 8, 0),
        Dimension::with_padding(8, 3, 1),
    ];

    let rule = Heuristic::ContactPointRule;

    let mut bin = MaxRectsBin::new(16, 16);
    assert_eq!(16, bin.width());
    assert_eq!(16, bin.height());

    for node in nodes.iter() {
        bin.insert(node, rule);
    }

    for rect1 in bin.iter() {
        for rect2 in bin.iter() {
            if rect1 != rect2 {
                assert!(!rect1.intersects(rect2));
            }
        }
    }
}

#[test]
fn bin_insert_list() {
    let nodes = vec![
        Dimension::with_padding(2, 4, 0),
        Dimension::with_padding(6, 4, 1),
        Dimension::with_padding(10, 3, 1),
        Dimension::with_padding(6, 6, 0),
        Dimension::with_padding(4, 4, 2),
        Dimension::with_padding(3, 8, 0),
        Dimension::with_padding(8, 3, 1),
    ];

    let rule = Heuristic::ContactPointRule;

    let mut bin = MaxRectsBin::new(16, 16);
    assert_eq!(16, bin.width());
    assert_eq!(16, bin.height());

    let (inserted, rejected) = bin.insert_list(&nodes, rule);
    assert_eq!(nodes.len(), inserted.len() + rejected.len());

    for rect1 in bin.iter() {
        for rect2 in bin.iter() {
            if rect1 != rect2 {
                assert!(!rect1.intersects(rect2));
            }
        }
    }
}