rust/lib-hedgewars-engine/src/render/atlas.rs
author alfadur
Sat, 23 Mar 2019 03:44:11 +0300
changeset 14717 16024046d458
child 14720 b110cbe52e51
permissions -rw-r--r--
rescue the atlas
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
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
     4
pub struct Atlas {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
     5
    size: Size,
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
     6
    free_rects: Vec<Rect>,
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
     7
    used_rects: Vec<Rect>,
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
#[derive(PartialEq, Eq, PartialOrd, Ord)]
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    11
struct Fit {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    12
    short_size: u32,
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    13
    long_size: u32,
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    14
}
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    15
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    16
impl Fit {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    17
    fn new() -> Self {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    18
        Self {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    19
            short_size: u32::max_value(),
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    20
            long_size: u32::max_value(),
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    21
        }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    22
    }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    23
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    24
    fn measure(container: Size, size: Size) -> Option<Self> {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    25
        if container.contains(size) {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    26
            let x_leftover = container.width - size.width;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    27
            let y_leftover = container.height - size.height;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    28
            Some(Self {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    29
                short_size: min(x_leftover, y_leftover) as u32,
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    30
                long_size: max(x_leftover, y_leftover) as u32,
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    31
            })
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    32
        } else {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    33
            None
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    34
        }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    35
    }
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
fn split_rect(free_rect: Rect, rect: Rect) -> Vec<Rect> {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    39
    let mut result = vec![];
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    40
    if free_rect.intersects(&rect) {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    41
        if rect.left() > free_rect.left() {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    42
            let trim = free_rect.right() - rect.left() + 1;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    43
            result.push(free_rect.with_margins(0, -trim, 0, 0))
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    44
        }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    45
        if rect.right() < free_rect.right() {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    46
            let trim = rect.right() - free_rect.left() + 1;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    47
            result.push(free_rect.with_margins(-trim, 0, 0, 0))
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    48
        }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    49
        if rect.top() > free_rect.top() {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    50
            let trim = free_rect.bottom() - rect.top() + 1;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    51
            result.push(free_rect.with_margins(0, 0, 0, -trim));
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    52
        }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    53
        if rect.bottom() < free_rect.bottom() {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    54
            let trim = rect.bottom() - free_rect.top() + 1;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    55
            result.push(free_rect.with_margins(0, 0, -trim, 0));
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    56
        }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    57
    }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    58
    result
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    59
}
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    60
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    61
impl Atlas {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    62
    pub fn new(size: Size) -> Self {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    63
        Self {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    64
            size,
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    65
            free_rects: vec![Rect::at_origin(size)],
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    66
            used_rects: vec![],
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
    fn find_position(&self, size: Size) -> Option<(Rect, Fit)> {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    71
        let mut best_rect = Rect::EMPTY;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    72
        let mut best_fit = Fit::new();
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    73
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    74
        for rect in &self.free_rects {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    75
            if let Some(fit) = Fit::measure(rect.size(), size) {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    76
                if fit < best_fit {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    77
                    best_fit = fit;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    78
                    best_rect = Rect::from_size(rect.top_left(), size);
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    79
                }
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
            if let Some(fit) = Fit::measure(rect.size(), size.transpose()) {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    83
                if fit < best_fit {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    84
                    best_fit = fit;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    85
                    best_rect = Rect::from_size(rect.top_left(), size.transpose());
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    86
                }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    87
            }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    88
        }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    89
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    90
        if best_rect == Rect::EMPTY {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    91
            None
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    92
        } else {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    93
            Some((best_rect, best_fit))
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    94
        }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    95
    }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    96
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    97
    fn prune(&mut self) {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    98
        self.free_rects = self
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
    99
            .free_rects
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   100
            .iter()
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   101
            .filter(|inner| {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   102
                self.free_rects
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   103
                    .iter()
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   104
                    .all(|outer| outer == *inner || !outer.contains_rect(inner))
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   105
            })
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   106
            .cloned()
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   107
            .collect();
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
    pub fn insert_adaptive(&mut self, size: Size) -> Option<Rect> {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   111
        let (rect, fit) = self.find_position(size)?;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   112
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   113
        let mut rects_to_process = self.free_rects.len();
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   114
        let mut i = 0;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   115
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   116
        while i < rects_to_process {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   117
            let rects = split_rect(self.free_rects[i], rect);
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   118
            if !rects.is_empty() {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   119
                self.free_rects.remove(i);
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   120
                self.free_rects.extend(rects);
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   121
                rects_to_process -= 1
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   122
            } else {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   123
                i += 1;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   124
            }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   125
        }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   126
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   127
        self.used_rects.push(rect);
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   128
        self.prune();
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   129
        Some(rect)
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   130
    }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   131
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   132
    pub fn insert_set<Iter>(&mut self, sizes: Iter) -> Vec<Rect>
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   133
    where
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   134
        Iter: Iterator<Item = Size>,
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   135
    {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   136
        unimplemented!()
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   137
    }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   138
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   139
    pub fn reset(&mut self) {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   140
        self.free_rects.clear();
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   141
        self.used_rects.clear();
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   142
        self.free_rects.push(Rect::at_origin(self.size));
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
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   146
#[cfg(test)]
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   147
mod tests {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   148
    use super::Atlas;
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   149
    use integral_geometry::{Rect, Size};
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   150
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   151
    #[test]
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   152
    fn insert() {
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   153
        let atlas_size = Size::square(16);
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   154
        let mut atlas = Atlas::new(atlas_size);
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   155
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   156
        assert_eq!(None, atlas.insert_adaptive(Size::square(20)));
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   157
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   158
        let rect_size = Size::new(11, 3);
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   159
        let rect = atlas.insert_adaptive(rect_size).unwrap();
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   160
        assert_eq!(rect, Rect::at_origin(rect_size));
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   161
        assert_eq!(2, atlas.free_rects.len());
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   162
    }
16024046d458 rescue the atlas
alfadur
parents:
diff changeset
   163
}