rectangle_pack/bin_section/
overlaps.rs1use crate::bin_section::BinSection;
2
3impl BinSection {
4 pub fn overlaps(&self, other: &Self) -> bool {
6 (self.x >= other.x && self.x <= other.right())
7 && (self.y >= other.y && self.y <= other.top())
8 && (self.z >= other.z && self.z <= other.back())
9 }
10
11 fn right(&self) -> u32 {
12 self.x + (self.whd.width - 1)
13 }
14
15 fn top(&self) -> u32 {
16 self.y + (self.whd.height - 1)
17 }
18
19 fn back(&self) -> u32 {
20 self.z + (self.whd.depth - 1)
21 }
22}
23
24#[cfg(test)]
25mod tests {
26 use super::*;
27 use crate::width_height_depth::WidthHeightDepth;
28
29 #[test]
31 fn overlaps() {
32 OverlapsTest {
33 label: "Overlaps X, Y and Z",
34 section1: BinSection::new(3, 4, 5, WidthHeightDepth::new(1, 1, 1)),
35 section2: section_2_3_4(),
36 expected_overlap: true,
37 }
38 .test();
39
40 OverlapsTest {
41 label: "Overlaps X only",
42 section1: BinSection::new(3, 40, 50, WidthHeightDepth::new(1, 1, 1)),
43 section2: section_2_3_4(),
44 expected_overlap: false,
45 }
46 .test();
47
48 OverlapsTest {
49 label: "Overlaps Y only",
50 section1: BinSection::new(30, 4, 50, WidthHeightDepth::new(1, 1, 1)),
51 section2: section_2_3_4(),
52 expected_overlap: false,
53 }
54 .test();
55
56 OverlapsTest {
57 label: "Overlaps Z only",
58 section1: BinSection::new(30, 40, 5, WidthHeightDepth::new(1, 1, 1)),
59 section2: section_2_3_4(),
60 expected_overlap: false,
61 }
62 .test();
63 }
64
65 fn section_2_3_4() -> BinSection {
66 BinSection::new(2, 3, 4, WidthHeightDepth::new(2, 3, 4))
67 }
68
69 struct OverlapsTest {
70 label: &'static str,
71 section1: BinSection,
72 section2: BinSection,
73 expected_overlap: bool,
74 }
75
76 impl OverlapsTest {
77 fn test(self) {
78 assert_eq!(
79 self.section1.overlaps(&self.section2),
80 self.expected_overlap,
81 "{}",
82 self.label
83 )
84 }
85 }
86}