rust/lib-hedgewars-engine/src/render/atlas.rs
author unc0rr
Tue, 04 Jun 2019 22:34:42 +0200
changeset 15120 febccab419b1
parent 14743 731c8406bff0
child 15186 9cf0c2f44f0e
permissions -rw-r--r--
Apply dos2unix to rust sources
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
     1
use integral_geometry::{Rect, Size};
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
     2
use std::cmp::{max, min, Ordering};
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
     3
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
     4
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
     5
struct Fit {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
     6
    short_side: u32,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
     7
    long_side: u32,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
     8
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
     9
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    10
impl Fit {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    11
    fn new() -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    12
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    13
            short_side: u32::max_value(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    14
            long_side: u32::max_value(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    15
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    16
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    17
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    18
    fn measure(container: Size, size: Size) -> Option<Self> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    19
        if container.contains(size) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    20
            let x_leftover = container.width - size.width;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    21
            let y_leftover = container.height - size.height;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    22
            Some(Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    23
                short_side: min(x_leftover, y_leftover) as u32,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    24
                long_side: max(x_leftover, y_leftover) as u32,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    25
            })
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    26
        } else {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    27
            None
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    28
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    29
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    30
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    31
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    32
#[derive(PartialEq, Eq)]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    33
pub struct UsedSpace {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    34
    used_area: usize,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    35
    total_area: usize,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    36
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    37
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    38
impl UsedSpace {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    39
    const fn new(used_area: usize, total_area: usize) -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    40
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    41
            used_area,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    42
            total_area,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    43
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    44
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    45
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    46
    const fn used(&self) -> usize {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    47
        self.used_area
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    48
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    49
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    50
    const fn total(&self) -> usize {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    51
        self.total_area
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    52
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    53
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    54
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    55
impl std::fmt::Debug for UsedSpace {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    56
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    57
        write!(
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    58
            f,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    59
            "{:.2}%",
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    60
            self.used() as f32 / self.total() as f32 / 100.0
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    61
        )?;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    62
        Ok(())
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    63
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    64
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    65
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    66
pub struct Atlas {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    67
    size: Size,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    68
    free_rects: Vec<Rect>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    69
    used_rects: Vec<Rect>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    70
    splits: Vec<Rect>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    71
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    72
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    73
impl Atlas {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    74
    pub fn new(size: Size) -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    75
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    76
            size,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    77
            free_rects: vec![Rect::at_origin(size)],
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    78
            used_rects: vec![],
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    79
            splits: vec![],
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    80
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    81
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    82
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    83
    pub fn size(&self) -> Size {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    84
        self.size
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    85
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    86
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    87
    pub fn used_space(&self) -> UsedSpace {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    88
        let used = self.used_rects.iter().map(|r| r.size().area()).sum();
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    89
        UsedSpace::new(used, self.size.area())
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    90
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    91
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    92
    fn find_position(&self, size: Size) -> Option<(Rect, Fit)> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    93
        let mut best_rect = Rect::EMPTY;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    94
        let mut best_fit = Fit::new();
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    95
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    96
        for rect in &self.free_rects {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    97
            if let Some(fit) = Fit::measure(rect.size(), size) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    98
                if fit < best_fit {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
    99
                    best_fit = fit;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   100
                    best_rect = Rect::from_size(rect.top_left(), size);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   101
                }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   102
            }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   103
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   104
            if let Some(fit) = Fit::measure(rect.size(), size.transpose()) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   105
                if fit < best_fit {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   106
                    best_fit = fit;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   107
                    best_rect = Rect::from_size(rect.top_left(), size.transpose());
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   108
                }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   109
            }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   110
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   111
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   112
        if best_rect == Rect::EMPTY {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   113
            None
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   114
        } else {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   115
            Some((best_rect, best_fit))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   116
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   117
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   118
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   119
    fn split_insert(&mut self, rect: Rect) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   120
        let mut splits = std::mem::replace(&mut self.splits, vec![]);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   121
        let mut buffer = [Rect::EMPTY; 4];
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   122
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   123
        for i in (0..self.free_rects.len()).rev() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   124
            if let Some(count) = split_rect(self.free_rects[i], rect, &mut splits, &mut buffer) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   125
                self.free_rects.swap_remove(i as usize);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   126
                splits.extend_from_slice(&buffer[0..count]);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   127
            }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   128
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   129
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   130
        filter_swap_remove(&mut splits, |s| {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   131
            self.free_rects.iter().any(|r| r.contains_rect(s))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   132
        });
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   133
        self.free_rects.extend(splits.drain(..));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   134
        std::mem::replace(&mut self.splits, splits);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   135
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   136
        self.used_rects.push(rect);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   137
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   138
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   139
    pub fn insert(&mut self, size: Size) -> Option<Rect> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   140
        let (rect, _) = self.find_position(size)?;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   141
        self.split_insert(rect);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   142
        Some(rect)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   143
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   144
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   145
    pub fn insert_set<Iter>(&mut self, sizes: Iter) -> Vec<Rect>
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   146
    where
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   147
        Iter: Iterator<Item = Size>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   148
    {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   149
        let mut sizes: Vec<_> = sizes.collect();
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   150
        let mut result = vec![];
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   151
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   152
        while let Some((index, (rect, _))) = sizes
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   153
            .iter()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   154
            .enumerate()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   155
            .filter_map(|(i, s)| self.find_position(*s).map(|res| (i, res)))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   156
            .min_by_key(|(_, (_, fit))| fit.clone())
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   157
        {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   158
            self.split_insert(rect);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   159
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   160
            result.push(rect);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   161
            sizes.swap_remove(index);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   162
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   163
        result
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   164
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   165
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   166
    pub fn reset(&mut self) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   167
        self.free_rects.clear();
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   168
        self.used_rects.clear();
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   169
        self.free_rects.push(Rect::at_origin(self.size));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   170
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   171
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   172
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   173
pub struct AtlasCollection {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   174
    texture_size: Size,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   175
    atlases: Vec<Atlas>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   176
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   177
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   178
impl AtlasCollection {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   179
    pub fn new(texture_size: Size) -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   180
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   181
            texture_size,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   182
            atlases: vec![],
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   183
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   184
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   185
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   186
    fn repack(&mut self, size: Size) -> bool {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   187
        for atlas in &mut self.atlases {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   188
            let mut temp_atlas = Atlas::new(atlas.size());
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   189
            let sizes = atlas
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   190
                .used_rects
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   191
                .iter()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   192
                .map(|r| r.size())
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   193
                .chain(std::iter::once(size));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   194
            if !temp_atlas.insert_set(sizes).is_empty() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   195
                std::mem::swap(atlas, &mut temp_atlas);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   196
                return true;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   197
            }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   198
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   199
        false
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   200
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   201
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   202
    pub fn insert_sprite(&mut self, size: Size) -> bool {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   203
        if !self.texture_size.contains(size) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   204
            false
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   205
        } else {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   206
            if let Some(rect) = self.atlases.iter_mut().find_map(|a| a.insert(size)) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   207
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   208
            } else if !self.repack(size) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   209
                let mut atlas = Atlas::new(self.texture_size);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   210
                atlas.insert(size);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   211
                self.atlases.push(atlas);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   212
            }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   213
            true
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   214
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   215
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   216
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   217
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   218
#[inline]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   219
fn filter_swap_remove<T, F>(vec: &mut Vec<T>, predicate: F)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   220
where
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   221
    F: Fn(&T) -> bool,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   222
{
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   223
    let mut i = 0;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   224
    while i < vec.len() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   225
        if predicate(&vec[i]) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   226
            vec.swap_remove(i);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   227
        } else {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   228
            i += 1;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   229
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   230
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   231
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   232
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   233
#[inline]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   234
fn prune_push(
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   235
    previous_splits: &mut Vec<Rect>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   236
    buffer: &mut [Rect; 4],
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   237
    buffer_size: &mut usize,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   238
    rect: Rect,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   239
) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   240
    if !previous_splits.iter().any(|r| r.contains_rect(&rect)) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   241
        filter_swap_remove(previous_splits, |s| rect.contains_rect(s));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   242
        buffer[*buffer_size] = rect;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   243
        *buffer_size += 1;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   244
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   245
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   246
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   247
fn split_rect(
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   248
    free_rect: Rect,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   249
    rect: Rect,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   250
    previous_splits: &mut Vec<Rect>,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   251
    buffer: &mut [Rect; 4],
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   252
) -> Option<usize> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   253
    let mut buffer_size = 0usize;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   254
    let split = free_rect.intersects(&rect);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   255
    if split {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   256
        if rect.left() > free_rect.left() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   257
            let trim = free_rect.right() - rect.left() + 1;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   258
            prune_push(
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   259
                previous_splits,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   260
                buffer,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   261
                &mut buffer_size,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   262
                free_rect.with_margins(0, -trim, 0, 0),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   263
            );
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   264
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   265
        if rect.right() < free_rect.right() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   266
            let trim = rect.right() - free_rect.left() + 1;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   267
            prune_push(
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   268
                previous_splits,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   269
                buffer,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   270
                &mut buffer_size,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   271
                free_rect.with_margins(-trim, 0, 0, 0),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   272
            );
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   273
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   274
        if rect.top() > free_rect.top() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   275
            let trim = free_rect.bottom() - rect.top() + 1;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   276
            prune_push(
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   277
                previous_splits,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   278
                buffer,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   279
                &mut buffer_size,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   280
                free_rect.with_margins(0, 0, 0, -trim),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   281
            );;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   282
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   283
        if rect.bottom() < free_rect.bottom() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   284
            let trim = rect.bottom() - free_rect.top() + 1;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   285
            prune_push(
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   286
                previous_splits,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   287
                buffer,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   288
                &mut buffer_size,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   289
                free_rect.with_margins(0, 0, -trim, 0),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   290
            );;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   291
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   292
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   293
    if split {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   294
        Some(buffer_size)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   295
    } else {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   296
        None
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   297
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   298
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   299
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   300
#[cfg(test)]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   301
mod tests {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   302
    use super::Atlas;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   303
    use integral_geometry::{Rect, Size};
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   304
    use itertools::Itertools as _;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   305
    use proptest::prelude::*;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   306
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   307
    #[test]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   308
    fn insert() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   309
        let atlas_size = Size::square(16);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   310
        let mut atlas = Atlas::new(atlas_size);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   311
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   312
        assert_eq!(None, atlas.insert(Size::square(20)));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   313
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   314
        let rect_size = Size::new(11, 3);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   315
        let rect = atlas.insert(rect_size).unwrap();
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   316
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   317
        assert_eq!(rect, Rect::at_origin(rect_size));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   318
        assert_eq!(2, atlas.free_rects.len());
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   319
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   320
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   321
    #[derive(Debug, Clone)]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   322
    struct TestRect(Size);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   323
    struct TestRectParameters(Size);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   324
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   325
    impl Default for TestRectParameters {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   326
        fn default() -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   327
            Self(Size::square(64))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   328
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   329
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   330
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   331
    impl Arbitrary for TestRect {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   332
        type Parameters = TestRectParameters;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   333
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   334
        fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   335
            (1..=args.0.width, 1..=args.0.height)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   336
                .prop_map(|(w, h)| TestRect(Size::new(w, h)))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   337
                .boxed()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   338
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   339
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   340
        type Strategy = BoxedStrategy<TestRect>;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   341
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   342
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   343
    trait HasSize {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   344
        fn size(&self) -> Size;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   345
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   346
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   347
    impl HasSize for TestRect {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   348
        fn size(&self) -> Size {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   349
            self.0
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   350
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   351
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   352
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   353
    impl HasSize for Rect {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   354
        fn size(&self) -> Size {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   355
            self.size()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   356
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   357
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   358
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   359
    fn sum_area<S: HasSize>(items: &[S]) -> usize {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   360
        items.iter().map(|s| s.size().area()).sum()
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   361
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   362
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   363
    proptest! {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   364
        #[test]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   365
        fn prop_insert(rects in Vec::<TestRect>::arbitrary()) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   366
            let container = Rect::at_origin(Size::square(2048));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   367
            let mut atlas = Atlas::new(container.size());
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   368
            let inserted: Vec<_> = rects.iter().filter_map(|TestRect(size)| atlas.insert(*size)).collect();
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   369
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   370
            let mut inserted_pairs = inserted.iter().cartesian_product(inserted.iter());
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   371
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   372
            assert!(inserted.iter().all(|r| container.contains_rect(r)));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   373
            assert!(inserted_pairs.all(|(r1, r2)| r1 == r2 || r1 != r2 && !r1.intersects(r2)));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   374
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   375
            assert_eq!(inserted.len(), rects.len());
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   376
            assert_eq!(sum_area(&inserted), sum_area(&rects));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   377
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   378
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   379
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   380
    proptest! {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   381
        #[test]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   382
        fn prop_insert_set(rects in Vec::<TestRect>::arbitrary()) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   383
            let container = Rect::at_origin(Size::square(2048));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   384
            let mut atlas = Atlas::new(container.size());
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   385
            let mut set_atlas = Atlas::new(container.size());
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   386
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   387
            let inserted: Vec<_> = rects.iter().filter_map(|TestRect(size)| atlas.insert(*size)).collect();
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   388
            let set_inserted: Vec<_> = set_atlas.insert_set(rects.iter().map(|TestRect(size)| *size));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   389
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   390
            let mut set_inserted_pairs = set_inserted.iter().cartesian_product(set_inserted.iter());
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   391
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   392
            assert!(set_inserted_pairs.all(|(r1, r2)| r1 == r2 || r1 != r2 && !r1.intersects(r2)));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   393
            assert!(set_atlas.used_space().used() <= atlas.used_space().used());
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   394
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   395
            assert_eq!(sum_area(&set_inserted), sum_area(&inserted));
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   396
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   397
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14743
diff changeset
   398
}