1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

fn approx_eq(a: f32, b: f32) -> bool {
    (a-b).abs() < 1e-04
}
#[derive(Copy, Clone, Debug)]
pub enum Id {
    None,
    Some(u32)
}

impl From<u32> for Id {
    fn from(id: u32) -> Self {
        Self::Some(id)
    }
}

///
/// Items we are packing
///
#[derive(Copy, Clone, Debug)]
pub struct SizedItem {
    pub id: Id,
    pub w: f32,
    pub h: f32,
}

///
/// Total sizes
///
#[derive(Copy, Clone, Debug)]
pub struct Packing {
    pub w: f32,
    pub h: f32,
    pub fill: f32
}

///
/// Location of each item.
///
#[derive(Copy, Clone, Debug)]
pub struct Space {
    pub id: Id,
    pub x: f32,
    pub y: f32,
    pub w: f32,
    pub h: f32,
}

impl From<&SizedItem> for Space {
    fn from(v: &SizedItem) -> Self {
        Self {
            id: v.id,
            x: 0.0,
            y: 0.0,
            w: v.w,
            h: v.h,
        }
    }
}

impl Space {
    pub fn area(&self) -> f32 {
        self.w * self.h
    }
}

pub struct PotPack {
    pub packing: Packing,
    pub spaces: Vec<Space>,
}

impl PotPack {
    pub fn new(
        boxes: &[SizedItem]
    ) -> Self {
        let mut boxes: Vec<Space> = boxes.iter().map(|x|
            x.into()
        ).collect();

        // calculate total box area and maximum box width
        let mut area: f32 = 0.0;
        let mut max_width: f32 = 0.0;

        // sort the boxes for insertion by height, descending
        for box_ in boxes.iter() {
            area += box_.area();
            max_width = max_width.max(box_.w);
        }

        // sort the boxes for insertion by height, descending
        boxes.sort_by(|a,b| b.h.partial_cmp(&a.h).unwrap());

        let start_width = (area/0.95).sqrt().ceil().max(max_width);

        let mut spaces = vec![
            Space { id: Id::None, x: 0.0, y: 0.0, w: start_width, h: f32::MAX }
        ];

        let mut width: f32 = 0.0;
        let mut height: f32 = 0.0;

        for mut box_ in boxes.iter_mut() {
            // look through spaces backwards so that we check smaller spaces first
            for (i, space) in spaces.iter().enumerate().rev() {

                // // look for empty spaces that can accommodate the current box
                if box_.w > space.w || box_.h > space.h {
                    continue;
                }

                // found the space; add the box to its top-left corner
                // |-------|-------|
                // |  box  |       |
                // |_______|       |
                // |         space |
                // |_______________|
                box_.x = space.x;
                box_.y = space.y;

                height = height.max(box_.y + box_.h);
                width = width.max(box_.x + box_.w);

                if box_.w == space.w && box_.h == space.h {
                    // space matches the box exactly; remove it
                    let last = spaces.pop();
                    if i < spaces.len() {
                        spaces[i] = last.unwrap();
                    }

                } else if approx_eq(box_.h, space.h) {
                    // space matches the box height; update it accordingly
                    // |-------|---------------|
                    // |  box  | updated space |
                    // |_______|_______________|
                    spaces[i].x += box_.w;
                    spaces[i].w -= box_.w;

                } else if approx_eq(box_.w, space.w) {
                    // space matches the box width; update it accordingly
                    // |---------------|
                    // |      box      |
                    // |_______________|
                    // | updated space |
                    // |_______________|
                    spaces[i].y += box_.h;
                    spaces[i].h -= box_.h;

                } else {
                    // otherwise the box splits the space into two spaces
                    // |-------|-----------|
                    // |  box  | new space |
                    // |_______|___________|
                    // | updated space     |
                    // |___________________|
                    spaces.push(Space {
                        id: space.id,
                        x: space.x + box_.w,
                        y: space.y,
                        w: space.w - box_.w,
                        h: box_.h
                    });
                    spaces[i].y += box_.h;
                    spaces[i].h -= box_.h;
                }
                break;
            }
        }

        Self {
            packing: Packing {
                w: width,
                h: height,
                fill: area / (width * height)
            },
            spaces
        }
    }
}

pub mod prelude {
    pub use super::{
        Id,
        Packing,
        PotPack,
        SizedItem,
        Space
    };
}

mod tests {
    use super::{
        approx_eq,
        PotPack,
        SizedItem
    };

    #[test]
    fn test_approx_eq() {
        let a = 1.00005;
        let b = 1.00006;
        assert!(approx_eq(a,b));
    }

    #[test]
    fn test_packing() {
        let mut boxes = vec![];

        for i in 0..100 {
            boxes.push(SizedItem {
                id: i.into(),
                w: i as _,
                h: i as _
            });
        }

        let pack = PotPack::new(&boxes);
        assert!(pack.packing.w == 588.0);
        assert!(pack.packing.h == 595.0);
        assert!(pack.packing.fill > 0.93);
    }
}