rust/landgen/src/wavefront_collapse/tile_image.rs
author unC0Rr
Thu, 02 Feb 2023 08:41:31 +0100
branchtransitional_engine
changeset 15915 8f093b1b18bc
parent 15913 c5684cc62de8
child 15916 e82de0410da5
permissions -rw-r--r--
Add loading of tiles from png
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
     1
use vec2d::Vec2D;
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
     2
use std::rc::Rc;
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
     3
use integral_geometry::Size;
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
     4
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
     5
pub struct TileImage<T> {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
     6
    image: Rc<Vec2D<T>>,
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
     7
    flip: bool,
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
     8
    mirror: bool,
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
     9
}
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    10
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    11
impl<T: Copy> TileImage<T> {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    12
    pub fn new(image: Vec2D<T>) -> Self {
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    13
        Self {
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    14
            image: Rc::new(image),
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    15
            flip: false,
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    16
            mirror: false,
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    17
        }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    18
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    19
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    20
    pub fn mirrored(&self) -> Self {
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    21
        Self {
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    22
            image: self.image.clone(),
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    23
            flip: self.flip,
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    24
            mirror: !self.mirror,
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    25
        }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    26
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    27
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    28
    pub fn flipped(&self) -> Self {
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    29
        Self {
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    30
            image: self.image.clone(),
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    31
            flip: !self.flip,
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    32
            mirror: self.mirror,
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    33
        }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    34
    }
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    35
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    36
    pub fn split(&self, rows: usize, columns: usize) -> Vec<TileImage<T>> {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    37
        let mut result = Vec::new();
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    38
        let self_image = self.image.as_ref();
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    39
        let (result_width, result_height) = (self_image.width() / columns, self.image.height() / rows);
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    40
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    41
        for row in 0..rows {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    42
            for column in 0..columns {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    43
                let mut tile_pixels = Vec::new();
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    44
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    45
                for out_row in 0..result_height {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    46
                    tile_pixels.push(self_image[row * result_height + out_row][column*result_width..(column+1)*result_width].iter());
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    47
                }
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    48
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    49
                let tile_image = Vec2D::from_iter(tile_pixels.into_iter().flatten().map(|p| *p), &Size::new(result_width, result_height));
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    50
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    51
                result.push(TileImage::new(tile_image.expect("correct calculation of tile dimensions")));
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    52
            }
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    53
        }
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    54
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    55
        result
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    56
    }
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    57
}
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    58
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    59
#[cfg(test)]
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    60
mod tests {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    61
    use super::TileImage;
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    62
    use integral_geometry::Size;
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    63
    use vec2d::Vec2D;
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    64
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    65
    #[test]
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    66
    fn test_split() {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    67
        let size = Size::new(6, 4);
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    68
        let sample_data = Vec2D::from_iter((0..24).into_iter(), &size);
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    69
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    70
        assert!(sample_data.is_some());
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    71
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    72
        let sample_data = sample_data.unwrap();
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    73
        let big_tile = TileImage::new(sample_data);
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    74
        let subtiles = big_tile.split(2, 2);
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    75
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    76
        assert_eq!(subtiles.len(), 4);
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    77
    }
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    78
}