rust/lib-hedgewars-engine/src/render/atlas.rs
author alfadur
Tue, 26 Mar 2019 22:29:48 +0300
changeset 14722 c97faf0aef78
parent 14720 b110cbe52e51
child 14723 766ce87dfdfc
permissions -rw-r--r--
proptest atlas, find 🐛🐜🦋
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14717
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
     1
use integral_geometry::{Rect, Size};
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
     2
use std::cmp::{max, min, Ordering};
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
     3
14720
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
     4
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)]
14717
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
     5
struct Fit {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
     6
    short_size: u32,
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
     7
    long_size: u32,
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
     8
}
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
     9
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    10
impl Fit {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    11
    fn new() -> Self {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    12
        Self {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    13
            short_size: u32::max_value(),
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    14
            long_size: u32::max_value(),
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    15
        }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    16
    }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    17
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    18
    fn measure(container: Size, size: Size) -> Option<Self> {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    19
        if container.contains(size) {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    20
            let x_leftover = container.width - size.width;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    21
            let y_leftover = container.height - size.height;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    22
            Some(Self {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    23
                short_size: min(x_leftover, y_leftover) as u32,
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    24
                long_size: max(x_leftover, y_leftover) as u32,
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    25
            })
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    26
        } else {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    27
            None
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    28
        }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    29
    }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    30
}
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    31
14720
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
    32
pub struct Atlas {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
    33
    size: Size,
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
    34
    free_rects: Vec<Rect>,
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
    35
    used_rects: Vec<Rect>,
14717
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    36
}
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    37
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    38
impl Atlas {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    39
    pub fn new(size: Size) -> Self {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    40
        Self {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    41
            size,
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    42
            free_rects: vec![Rect::at_origin(size)],
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    43
            used_rects: vec![],
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    44
        }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    45
    }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    46
14720
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
    47
    pub fn size(&self) -> Size {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
    48
        self.size
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
    49
    }
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
    50
14717
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    51
    fn find_position(&self, size: Size) -> Option<(Rect, Fit)> {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    52
        let mut best_rect = Rect::EMPTY;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    53
        let mut best_fit = Fit::new();
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    54
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    55
        for rect in &self.free_rects {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    56
            if let Some(fit) = Fit::measure(rect.size(), size) {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    57
                if fit < best_fit {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    58
                    best_fit = fit;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    59
                    best_rect = Rect::from_size(rect.top_left(), size);
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    60
                }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    61
            }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    62
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    63
            if let Some(fit) = Fit::measure(rect.size(), size.transpose()) {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    64
                if fit < best_fit {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    65
                    best_fit = fit;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    66
                    best_rect = Rect::from_size(rect.top_left(), size.transpose());
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    67
                }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    68
            }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    69
        }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    70
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    71
        if best_rect == Rect::EMPTY {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    72
            None
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    73
        } else {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    74
            Some((best_rect, best_fit))
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    75
        }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    76
    }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    77
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    78
    fn prune(&mut self) {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    79
        self.free_rects = self
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    80
            .free_rects
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    81
            .iter()
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    82
            .filter(|inner| {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    83
                self.free_rects
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    84
                    .iter()
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    85
                    .all(|outer| outer == *inner || !outer.contains_rect(inner))
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    86
            })
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    87
            .cloned()
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    88
            .collect();
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    89
    }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    90
14720
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
    91
    pub fn insert(&mut self, size: Size) -> Option<Rect> {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
    92
        let (rect, _) = self.find_position(size)?;
14717
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    93
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    94
        let mut rects_to_process = self.free_rects.len();
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    95
        let mut i = 0;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    96
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    97
        while i < rects_to_process {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    98
            let rects = split_rect(self.free_rects[i], rect);
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    99
            if !rects.is_empty() {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   100
                self.free_rects.remove(i);
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   101
                self.free_rects.extend(rects);
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   102
                rects_to_process -= 1
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   103
            } else {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   104
                i += 1;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   105
            }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   106
        }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   107
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   108
        self.used_rects.push(rect);
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   109
        self.prune();
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   110
        Some(rect)
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   111
    }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   112
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   113
    pub fn insert_set<Iter>(&mut self, sizes: Iter) -> Vec<Rect>
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   114
    where
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   115
        Iter: Iterator<Item = Size>,
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   116
    {
14720
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   117
        let mut sizes: Vec<_> = sizes.collect();
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   118
        let mut result = vec![];
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   119
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   120
        while let Some((index, (rect, _))) = sizes
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   121
            .iter()
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   122
            .enumerate()
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   123
            .filter_map(|(i, s)| self.find_position(*s).map(|res| (i, res)))
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   124
            .min_by_key(|(_, (_, fit))| fit.clone())
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   125
        {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   126
            result.push(rect);
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   127
            sizes.swap_remove(index);
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   128
        }
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   129
        if sizes.is_empty() {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   130
            self.used_rects.extend_from_slice(&result);
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   131
            result
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   132
        } else {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   133
            vec![]
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   134
        }
14717
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   135
    }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   136
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   137
    pub fn reset(&mut self) {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   138
        self.free_rects.clear();
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   139
        self.used_rects.clear();
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   140
        self.free_rects.push(Rect::at_origin(self.size));
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   141
    }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   142
}
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   143
14720
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   144
pub struct AtlasCollection {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   145
    texture_size: Size,
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   146
    atlases: Vec<Atlas>,
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   147
}
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   148
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   149
impl AtlasCollection {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   150
    pub fn new(texture_size: Size) -> Self {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   151
        Self {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   152
            texture_size,
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   153
            atlases: vec![],
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   154
        }
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   155
    }
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   156
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   157
    fn repack(&mut self, size: Size) -> bool {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   158
        for atlas in &mut self.atlases {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   159
            let mut temp_atlas = Atlas::new(atlas.size());
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   160
            let sizes = atlas
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   161
                .used_rects
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   162
                .iter()
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   163
                .map(|r| r.size())
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   164
                .chain(std::iter::once(size));
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   165
            if !temp_atlas.insert_set(sizes).is_empty() {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   166
                std::mem::swap(atlas, &mut temp_atlas);
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   167
                return true;
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   168
            }
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   169
        }
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   170
        false
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   171
    }
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   172
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   173
    pub fn insert_sprite(&mut self, size: Size) -> bool {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   174
        if !self.texture_size.contains(size) {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   175
            false
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   176
        } else {
14722
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   177
            if let Some(rect) = self.atlases.iter_mut().find_map(|a| a.insert(size)) {
14720
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   178
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   179
            } else if !self.repack(size) {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   180
                let mut atlas = Atlas::new(self.texture_size);
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   181
                atlas.insert(size);
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   182
                self.atlases.push(atlas);
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   183
            }
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   184
            true
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   185
        }
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   186
    }
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   187
}
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   188
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   189
fn split_rect(free_rect: Rect, rect: Rect) -> Vec<Rect> {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   190
    let mut result = vec![];
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   191
    if free_rect.intersects(&rect) {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   192
        if rect.left() > free_rect.left() {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   193
            let trim = free_rect.right() - rect.left() + 1;
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   194
            result.push(free_rect.with_margins(0, -trim, 0, 0))
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   195
        }
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   196
        if rect.right() < free_rect.right() {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   197
            let trim = rect.right() - free_rect.left() + 1;
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   198
            result.push(free_rect.with_margins(-trim, 0, 0, 0))
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   199
        }
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   200
        if rect.top() > free_rect.top() {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   201
            let trim = free_rect.bottom() - rect.top() + 1;
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   202
            result.push(free_rect.with_margins(0, 0, 0, -trim));
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   203
        }
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   204
        if rect.bottom() < free_rect.bottom() {
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   205
            let trim = rect.bottom() - free_rect.top() + 1;
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   206
            result.push(free_rect.with_margins(0, 0, -trim, 0));
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   207
        }
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   208
    }
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   209
    result
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   210
}
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   211
14717
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   212
#[cfg(test)]
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   213
mod tests {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   214
    use super::Atlas;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   215
    use integral_geometry::{Rect, Size};
14722
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   216
    use proptest::prelude::*;
14717
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   217
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   218
    #[test]
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   219
    fn insert() {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   220
        let atlas_size = Size::square(16);
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   221
        let mut atlas = Atlas::new(atlas_size);
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   222
14720
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   223
        assert_eq!(None, atlas.insert(Size::square(20)));
14717
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   224
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   225
        let rect_size = Size::new(11, 3);
14720
b110cbe52e51 save more of the atlas
alfadur
parents: 14717
diff changeset
   226
        let rect = atlas.insert(rect_size).unwrap();
14722
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   227
        
14717
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   228
        assert_eq!(rect, Rect::at_origin(rect_size));
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   229
        assert_eq!(2, atlas.free_rects.len());
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   230
    }
14722
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   231
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   232
    #[derive(Debug, Clone)]
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   233
    struct TestRect(Size);
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   234
    struct TestRectParameters(Size);
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   235
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   236
    impl Default for TestRectParameters {
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   237
        fn default() -> Self {
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   238
            Self(Size::square(64))
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   239
        }
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   240
    }
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   241
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   242
    impl Arbitrary for TestRect {
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   243
        type Parameters = TestRectParameters;
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   244
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   245
        fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   246
            (1..=args.0.width, 1..=args.0.height)
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   247
                .prop_map(|(w, h)| TestRect(Size::new(w, h)))
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   248
                .boxed()
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   249
        }
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   250
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   251
        type Strategy = BoxedStrategy<TestRect>;
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   252
    }
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   253
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   254
    trait HasSize {
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   255
        fn size(&self) -> Size;
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   256
    }
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   257
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   258
    impl HasSize for TestRect {
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   259
        fn size(&self) -> Size {
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   260
            self.0
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   261
        }
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   262
    }
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   263
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   264
    impl HasSize for Rect {
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   265
        fn size(&self) -> Size {
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   266
            self.size()
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   267
        }
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   268
    }
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   269
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   270
    fn sum_area<S: HasSize>(items: &[S]) -> usize {
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   271
        items.iter().map(|s| s.size().area()).sum()
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   272
    }
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   273
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   274
    proptest! {
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   275
        #[test]
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   276
        fn prop_insert(rects in Vec::<TestRect>::arbitrary()) {
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   277
            let mut atlas = Atlas::new(Size::square(2048));
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   278
            let inserted: Vec<_> = rects.iter().take_while(|TestRect(size)| atlas.insert(*size).is_some()).cloned().collect();
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   279
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   280
            assert_eq!(inserted.len(), atlas.used_rects.len());
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   281
            assert_eq!(sum_area(&inserted), sum_area(&atlas.used_rects));
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   282
        }
c97faf0aef78 proptest atlas, find 🐛🐜🦋
alfadur
parents: 14720
diff changeset
   283
    }
14717
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   284
}