rust/land2d/src/lib.rs
author alfadur
Tue, 30 Oct 2018 19:05:52 +0300
changeset 14053 2869c2ccb1b8
parent 14052 c47283feafac
child 14073 9c817b2eedae
permissions -rw-r--r--
extract size struct for common usage
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13957
78c798d655ad Make use of LinePoints in Land2D::draw_line
unc0rr
parents: 13955
diff changeset
     1
extern crate integral_geometry;
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     2
extern crate vec2d;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     3
13967
54e2a3698425 revert 2354264ab0b0
alfadur
parents: 13966
diff changeset
     4
use std::cmp;
54e2a3698425 revert 2354264ab0b0
alfadur
parents: 13966
diff changeset
     5
use std::ops;
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     6
14053
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14052
diff changeset
     7
use integral_geometry::{
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14052
diff changeset
     8
    ArcPoints, EquidistantPoints, LinePoints,
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14052
diff changeset
     9
    Point, Size, SizeMask
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14052
diff changeset
    10
};
13959
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
    11
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    12
pub struct Land2D<T> {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    13
    pixels: vec2d::Vec2D<T>,
14053
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14052
diff changeset
    14
    mask: SizeMask
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    15
}
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    16
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    17
impl<T: Copy + PartialEq> Land2D<T> {
14053
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14052
diff changeset
    18
    pub fn new(size: Size, fill_value: T) -> Self {
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    19
        Self {
14053
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14052
diff changeset
    20
            pixels: vec2d::Vec2D::new(size, fill_value),
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14052
diff changeset
    21
            mask: size.to_mask()
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    22
        }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    23
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    24
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    25
    #[inline]
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    26
    pub fn width(&self) -> usize {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    27
        self.pixels.width()
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    28
    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    29
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    30
    #[inline]
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    31
    pub fn height(&self) -> usize {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    32
        self.pixels.height()
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    33
    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    34
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    35
    #[inline]
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    36
    pub fn is_valid_x(&self, x: i32) -> bool {
14053
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14052
diff changeset
    37
        self.mask.contains_x(x as usize)
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    38
    }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    39
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    40
    #[inline]
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    41
    pub fn is_valid_y(&self, y: i32) -> bool {
14053
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14052
diff changeset
    42
        self.mask.contains_y(y as usize)
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    43
    }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    44
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    45
    #[inline]
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    46
    pub fn is_valid_coordinate(&self, x: i32, y: i32) -> bool {
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    47
        self.is_valid_x(x) && self.is_valid_y(y)
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    48
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    49
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    50
    #[inline]
14051
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 13972
diff changeset
    51
    pub fn rows(&self) -> impl Iterator<Item = &[T]> {
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 13972
diff changeset
    52
        self.pixels.rows()
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 13972
diff changeset
    53
    }
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 13972
diff changeset
    54
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 13972
diff changeset
    55
    #[inline]
13961
1c30793b1cea put back land2d.map accidentally replaced by testing code
alfadur
parents: 13959
diff changeset
    56
    pub fn map<U: Default, F: FnOnce(&mut T) -> U>(&mut self, y: i32, x: i32, f: F) -> U {
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    57
        if self.is_valid_coordinate(x, y) {
13955
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13952
diff changeset
    58
            unsafe {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13952
diff changeset
    59
                // hey, I just checked that coordinates are valid!
13961
1c30793b1cea put back land2d.map accidentally replaced by testing code
alfadur
parents: 13959
diff changeset
    60
                f(self.pixels.get_unchecked_mut(y as usize, x as usize))
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    61
            }
13955
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13952
diff changeset
    62
        } else {
13961
1c30793b1cea put back land2d.map accidentally replaced by testing code
alfadur
parents: 13959
diff changeset
    63
            U::default()
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    64
        }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    65
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    66
13965
4162ea9ae333 Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents: 13964
diff changeset
    67
    #[inline]
4162ea9ae333 Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents: 13964
diff changeset
    68
    pub fn map_point<U: Default, F: FnOnce(&mut T) -> U>(&mut self, point: Point, f: F) -> U {
4162ea9ae333 Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents: 13964
diff changeset
    69
        self.map(point.y, point.x, f)
13955
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13952
diff changeset
    70
    }
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13952
diff changeset
    71
13959
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
    72
    pub fn fill_from_iter<I>(&mut self, i: I, value: T) -> usize
13964
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
    73
    where
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
    74
        I: std::iter::Iterator<Item = Point>,
13959
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
    75
    {
13964
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
    76
        i.map(|p| {
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
    77
            self.map(p.y, p.x, |v| {
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
    78
                *v = value;
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
    79
                1
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
    80
            })
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
    81
        }).count()
13959
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
    82
    }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
    83
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
    84
    pub fn draw_line(&mut self, from: Point, to: Point, value: T) -> usize {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
    85
        self.fill_from_iter(LinePoints::new(from, to), value)
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    86
    }
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    87
13969
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
    88
    pub fn fill(&mut self, start_point: Point, border_value: T, fill_value: T) {
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
    89
        debug_assert!(self.is_valid_coordinate(start_point.x - 1, start_point.y));
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
    90
        debug_assert!(self.is_valid_coordinate(start_point.x, start_point.y));
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    91
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    92
        let mut stack: Vec<(usize, usize, usize, isize)> = Vec::new();
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    93
        fn push<T: Copy + PartialEq>(
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    94
            land: &Land2D<T>,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    95
            stack: &mut Vec<(usize, usize, usize, isize)>,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    96
            xl: usize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    97
            xr: usize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    98
            y: usize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    99
            dir: isize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   100
        ) {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   101
            let yd = y as isize + dir;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   102
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   103
            if land.is_valid_coordinate(0, yd as i32) {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   104
                stack.push((xl, xr, yd as usize, dir));
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   105
            }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   106
        };
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   107
13969
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   108
        let start_x_l = (start_point.x - 1) as usize;
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   109
        let start_x_r = start_point.x as usize;
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   110
        push(
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   111
            self,
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   112
            &mut stack,
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   113
            start_x_l,
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   114
            start_x_r,
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   115
            start_point.y as usize,
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   116
            -1,
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   117
        );
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   118
        push(
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   119
            self,
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   120
            &mut stack,
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   121
            start_x_l,
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   122
            start_x_r,
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   123
            start_point.y as usize,
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   124
            1,
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   125
        );
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   126
13972
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   127
        while let Some(a) = stack.pop() {
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   128
            let (mut xl, mut xr, y, mut dir) = a;
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   129
13972
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   130
            while xl > 0 && self
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   131
                .pixels
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   132
                .get(y, xl)
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   133
                .map_or(false, |v| *v != border_value && *v != fill_value)
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   134
            {
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   135
                xl -= 1;
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   136
            }
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   137
13972
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   138
            while xr < self.width() - 1 && self
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   139
                .pixels
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   140
                .get(y, xr)
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   141
                .map_or(false, |v| *v != border_value && *v != fill_value)
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   142
            {
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   143
                xr += 1;
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   144
            }
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   145
13972
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   146
            while xl < xr {
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   147
                while xl <= xr
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   148
                    && (self.pixels[y][xl] == border_value || self.pixels[y][xl] == fill_value)
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   149
                {
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   150
                    xl += 1;
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   151
                }
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   152
13972
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   153
                let mut x = xl;
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   154
13972
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   155
                while xl <= xr
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   156
                    && (self.pixels[y][xl] != border_value && self.pixels[y][xl] != fill_value)
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   157
                {
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   158
                    self.pixels[y][xl] = fill_value;
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   159
13972
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   160
                    xl += 1;
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   161
                }
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   162
13972
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   163
                if x < xl {
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   164
                    push(self, &mut stack, x, xl - 1, y, dir);
03e41712eef8 Fix silly loop
unc0rr
parents: 13970
diff changeset
   165
                    push(self, &mut stack, x, xl - 1, y, -dir);
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   166
                }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   167
            }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   168
        }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   169
    }
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   170
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   171
    #[inline]
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   172
    fn fill_circle_line<F: Fn(&mut T) -> usize>(
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   173
        &mut self,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   174
        y: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   175
        x_from: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   176
        x_to: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   177
        f: &F,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   178
    ) -> usize {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   179
        let mut result = 0;
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   180
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   181
        if self.is_valid_y(y) {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   182
            for i in cmp::min(x_from, 0) as usize..cmp::max(x_to as usize, self.width() - 1) {
13955
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13952
diff changeset
   183
                unsafe {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13952
diff changeset
   184
                    // coordinates are valid at this point
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   185
                    result += f(self.pixels.get_unchecked_mut(y as usize, i));
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   186
                }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   187
            }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   188
        }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   189
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   190
        result
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   191
    }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   192
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   193
    #[inline]
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   194
    fn fill_circle_lines<F: Fn(&mut T) -> usize>(
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   195
        &mut self,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   196
        x: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   197
        y: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   198
        dx: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   199
        dy: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   200
        f: &F,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   201
    ) -> usize {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   202
        self.fill_circle_line(y + dy, x - dx, x + dx, f)
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   203
            + self.fill_circle_line(y - dy, x - dx, x + dx, f)
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   204
            + self.fill_circle_line(y + dx, x - dy, x + dy, f)
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   205
            + self.fill_circle_line(y - dx, x - dy, x + dy, f)
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   206
    }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   207
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   208
    pub fn change_round<F: Fn(&mut T) -> usize>(
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   209
        &mut self,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   210
        x: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   211
        y: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   212
        radius: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   213
        f: F,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   214
    ) -> usize {
13964
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
   215
        ArcPoints::new(radius)
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
   216
            .map(&mut |p: Point| self.fill_circle_lines(x, y, p.x, p.y, &f))
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
   217
            .sum()
13955
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13952
diff changeset
   218
    }
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   219
14052
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   220
    fn fill_row(&mut self, center: Point, offset: Point, value: T) -> usize {
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   221
        let row_index = center.y + offset.y;
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   222
        if self.is_valid_y(row_index) {
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   223
            let from_x = cmp::max(0, center.x - offset.x) as usize;
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   224
            let to_x = cmp::min(self.width() - 1, (center.x + offset.x) as usize);
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   225
            self.pixels[row_index as usize][from_x..=to_x]
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   226
                .iter_mut().for_each(|v| *v = value);
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   227
            to_x - from_x + 1
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   228
        } else {
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   229
            0
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   230
        }
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   231
    }
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   232
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   233
    pub fn fill_circle(&mut self, center: Point, radius: i32, value: T) -> usize {
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   234
        let transforms =
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   235
            [[0, 1, 1, 0], [0, 1, -1, 0],
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   236
             [1, 0, 0, 1], [1, 0, 0, -1]];
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   237
        ArcPoints::new(radius).map(|vector| {
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   238
            transforms.iter().map(|m|
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   239
                self.fill_row(center, vector.transform(m), value)
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   240
            ).sum::<usize>()
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   241
        }).sum()
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   242
    }
c47283feafac add circle filling to land2d
alfadur
parents: 14051
diff changeset
   243
13965
4162ea9ae333 Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents: 13964
diff changeset
   244
    pub fn draw_thick_line(&mut self, from: Point, to: Point, radius: i32, value: T) -> usize {
4162ea9ae333 Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents: 13964
diff changeset
   245
        let mut result = 0;
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   246
13970
a1895019bb94 change draw_thick_line iteration order to benchmark winner
alfadur
parents: 13969
diff changeset
   247
        for vector in ArcPoints::new(radius) {
a1895019bb94 change draw_thick_line iteration order to benchmark winner
alfadur
parents: 13969
diff changeset
   248
            for delta in EquidistantPoints::new(vector) {
a1895019bb94 change draw_thick_line iteration order to benchmark winner
alfadur
parents: 13969
diff changeset
   249
                for point in LinePoints::new(from, to) {
13967
54e2a3698425 revert 2354264ab0b0
alfadur
parents: 13966
diff changeset
   250
                    self.map_point(point + delta, |p| {
54e2a3698425 revert 2354264ab0b0
alfadur
parents: 13966
diff changeset
   251
                        if *p != value {
54e2a3698425 revert 2354264ab0b0
alfadur
parents: 13966
diff changeset
   252
                            *p = value;
54e2a3698425 revert 2354264ab0b0
alfadur
parents: 13966
diff changeset
   253
                            result += 1;
54e2a3698425 revert 2354264ab0b0
alfadur
parents: 13966
diff changeset
   254
                        }
54e2a3698425 revert 2354264ab0b0
alfadur
parents: 13966
diff changeset
   255
                    })
13965
4162ea9ae333 Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents: 13964
diff changeset
   256
                }
13964
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
   257
            }
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
   258
        }
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
   259
13965
4162ea9ae333 Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents: 13964
diff changeset
   260
        result
13955
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13952
diff changeset
   261
    }
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   262
}
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   263
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   264
#[cfg(test)]
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   265
mod tests {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   266
    use super::*;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   267
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   268
    #[test]
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   269
    fn basics() {
14053
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14052
diff changeset
   270
        let l: Land2D<u8> = Land2D::new(Size::new(32, 64), 0);
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   271
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   272
        assert!(l.is_valid_coordinate(0, 0));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   273
        assert!(!l.is_valid_coordinate(-1, -1));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   274
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   275
        assert!(l.is_valid_coordinate(31, 63));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   276
        assert!(!l.is_valid_coordinate(32, 63));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   277
        assert!(!l.is_valid_coordinate(31, 64));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   278
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   279
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   280
    #[test]
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   281
    fn fill() {
14053
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14052
diff changeset
   282
        let mut l: Land2D<u8> = Land2D::new(Size::square(128), 0);
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   283
13959
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
   284
        l.draw_line(Point::new(0, 0), Point::new(32, 96), 1);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
   285
        l.draw_line(Point::new(32, 96), Point::new(64, 32), 1);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
   286
        l.draw_line(Point::new(64, 32), Point::new(96, 80), 1);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
   287
        l.draw_line(Point::new(96, 80), Point::new(128, 0), 1);
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   288
13959
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
   289
        l.draw_line(Point::new(0, 128), Point::new(64, 96), 1);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
   290
        l.draw_line(Point::new(128, 128), Point::new(64, 96), 1);
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   291
13969
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   292
        l.fill(Point::new(32, 32), 1, 2);
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   293
        l.fill(Point::new(16, 96), 1, 3);
c6e1769ac9aa Change Land2D::fill() arguments a bit
unc0rr
parents: 13967
diff changeset
   294
        l.fill(Point::new(60, 100), 1, 4);
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   295
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   296
        assert_eq!(l.pixels[0][0], 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   297
        assert_eq!(l.pixels[96][64], 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   298
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   299
        assert_eq!(l.pixels[40][32], 2);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   300
        assert_eq!(l.pixels[40][96], 2);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   301
        assert_eq!(l.pixels[5][0], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   302
        assert_eq!(l.pixels[120][0], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   303
        assert_eq!(l.pixels[5][127], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   304
        assert_eq!(l.pixels[120][127], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   305
        assert_eq!(l.pixels[35][64], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   306
        assert_eq!(l.pixels[120][20], 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   307
        assert_eq!(l.pixels[120][100], 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   308
        assert_eq!(l.pixels[100][64], 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   309
    }
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   310
}