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