rectangle_pack/
target_bin.rs

1use crate::bin_section::BinSection;
2use crate::width_height_depth::WidthHeightDepth;
3use alloc::vec::Vec;
4
5mod coalesce;
6mod push_available_bin_section;
7
8/// A bin that we'd like to play our incoming rectangles into
9#[derive(Debug, Clone)]
10pub struct TargetBin {
11    pub(crate) max_width: u32,
12    pub(crate) max_height: u32,
13    pub(crate) max_depth: u32,
14    pub(crate) available_bin_sections: Vec<BinSection>,
15}
16
17impl TargetBin {
18    #[allow(missing_docs)]
19    pub fn new(max_width: u32, max_height: u32, max_depth: u32) -> Self {
20        let available_bin_sections = vec![BinSection::new(
21            0,
22            0,
23            0,
24            WidthHeightDepth {
25                width: max_width,
26                height: max_height,
27                depth: max_depth,
28            },
29        )];
30
31        TargetBin {
32            max_width,
33            max_height,
34            max_depth,
35            available_bin_sections,
36        }
37    }
38
39    /// The free [`BinSection`]s within the [`TargetBin`] that rectangles can still be placed into.
40    pub fn available_bin_sections(&self) -> &Vec<BinSection> {
41        &self.available_bin_sections
42    }
43
44    /// Remove the section that was just split by a placed rectangle.
45    pub fn remove_filled_section(&mut self, idx: usize) {
46        self.available_bin_sections.remove(idx);
47    }
48
49    /// When a section is filled it gets split into three new sections.
50    /// Here we add those.
51    ///
52    /// TODO: Ignore sections with a volume of 0
53    pub fn add_new_sections(&mut self, new_sections: [BinSection; 3]) {
54        for new_section in new_sections.iter() {
55            if new_section.whd.volume() > 0 {
56                self.available_bin_sections.push(*new_section);
57            }
58        }
59    }
60}