rust/land2d/src/lib.rs
author alfadur
Thu, 18 Oct 2018 23:59:23 +0300
changeset 13966 2354264ab0b0
parent 13965 4162ea9ae333
child 13967 54e2a3698425
permissions -rw-r--r--
apply some codegolfing for dubious reasons
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
13966
2354264ab0b0 apply some codegolfing for dubious reasons
alfadur
parents: 13965
diff changeset
     4
use std::{cmp, mem::replace};
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     5
13965
4162ea9ae333 Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents: 13964
diff changeset
     6
use integral_geometry::{ArcPoints, EquidistantPoints, LinePoints, Point};
13959
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
     7
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     8
pub struct Land2D<T> {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     9
    pixels: vec2d::Vec2D<T>,
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    10
    width_mask: usize,
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    11
    height_mask: usize,
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    12
}
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    13
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    14
impl<T: Copy + PartialEq> Land2D<T> {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    15
    pub fn new(width: usize, height: usize, fill_value: T) -> Self {
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    16
        assert!(width.is_power_of_two());
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    17
        assert!(height.is_power_of_two());
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    18
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    19
        Self {
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    20
            pixels: vec2d::Vec2D::new(width, height, fill_value),
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    21
            width_mask: !(width - 1),
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    22
            height_mask: !(height - 1),
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
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    26
    #[inline]
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    27
    pub fn width(&self) -> usize {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    28
        self.pixels.width()
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    29
    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    30
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    31
    #[inline]
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    32
    pub fn height(&self) -> usize {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    33
        self.pixels.height()
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    34
    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    35
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    36
    #[inline]
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    37
    pub fn is_valid_x(&self, x: i32) -> bool {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    38
        (x as usize & self.width_mask) == 0
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    39
    }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    40
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    41
    #[inline]
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    42
    pub fn is_valid_y(&self, y: i32) -> bool {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    43
        (y as usize & self.height_mask) == 0
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    44
    }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    45
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    46
    #[inline]
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    47
    pub fn is_valid_coordinate(&self, x: i32, y: i32) -> bool {
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    48
        self.is_valid_x(x) && self.is_valid_y(y)
13938
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
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    51
    #[inline]
13961
1c30793b1cea put back land2d.map accidentally replaced by testing code
alfadur
parents: 13959
diff changeset
    52
    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
    53
        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
    54
            unsafe {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13952
diff changeset
    55
                // hey, I just checked that coordinates are valid!
13961
1c30793b1cea put back land2d.map accidentally replaced by testing code
alfadur
parents: 13959
diff changeset
    56
                f(self.pixels.get_unchecked_mut(y as usize, x as usize))
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    57
            }
13955
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13952
diff changeset
    58
        } else {
13961
1c30793b1cea put back land2d.map accidentally replaced by testing code
alfadur
parents: 13959
diff changeset
    59
            U::default()
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    60
        }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    61
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    62
13965
4162ea9ae333 Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents: 13964
diff changeset
    63
    #[inline]
4162ea9ae333 Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents: 13964
diff changeset
    64
    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
    65
        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
    66
    }
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13952
diff changeset
    67
13959
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
    68
    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
    69
    where
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
    70
        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
    71
    {
13964
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
    72
        i.map(|p| {
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
    73
            self.map(p.y, p.x, |v| {
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
    74
                *v = value;
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
    75
                1
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
    76
            })
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
    77
        }).count()
13959
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
    78
    }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
    79
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
    80
    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
    81
        self.fill_from_iter(LinePoints::new(from, to), value)
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    82
    }
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    83
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    84
    pub fn fill(&mut self, start_x: i32, start_y: i32, border_value: T, fill_value: T) {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    85
        debug_assert!(self.is_valid_coordinate(start_x - 1, start_y));
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    86
        debug_assert!(self.is_valid_coordinate(start_x, start_y));
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    87
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    88
        let mut stack: Vec<(usize, usize, usize, isize)> = Vec::new();
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    89
        fn push<T: Copy + PartialEq>(
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    90
            land: &Land2D<T>,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    91
            stack: &mut Vec<(usize, usize, usize, isize)>,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    92
            xl: usize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    93
            xr: usize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    94
            y: usize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    95
            dir: isize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    96
        ) {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    97
            let yd = y as isize + dir;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    98
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    99
            if land.is_valid_coordinate(0, yd as i32) {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   100
                stack.push((xl, xr, yd as usize, dir));
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   101
            }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   102
        };
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   103
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   104
        let start_x_l = (start_x - 1) as usize;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   105
        let start_x_r = start_x as usize;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   106
        push(self, &mut stack, start_x_l, start_x_r, start_y as usize, -1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   107
        push(self, &mut stack, start_x_l, start_x_r, start_y as usize, 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   108
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   109
        loop {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   110
            let a = stack.pop();
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   111
            match a {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   112
                None => return,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   113
                Some(a) => {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   114
                    let (mut xl, mut xr, y, mut dir) = a;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   115
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   116
                    while xl > 0 && self
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   117
                        .pixels
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   118
                        .get(y, xl)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   119
                        .map_or(false, |v| *v != border_value && *v != fill_value)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   120
                    {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   121
                        xl -= 1;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   122
                    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   123
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   124
                    while xr < self.width() - 1 && self
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   125
                        .pixels
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   126
                        .get(y, xr)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   127
                        .map_or(false, |v| *v != border_value && *v != fill_value)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   128
                    {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   129
                        xr += 1;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   130
                    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   131
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   132
                    while xl < xr {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   133
                        while xl <= xr
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   134
                            && (self.pixels[y][xl] == border_value
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   135
                                || self.pixels[y][xl] == fill_value)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   136
                        {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   137
                            xl += 1;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   138
                        }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   139
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   140
                        let mut x = xl;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   141
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   142
                        while xl <= xr
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   143
                            && (self.pixels[y][xl] != border_value
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   144
                                && self.pixels[y][xl] != fill_value)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   145
                        {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   146
                            self.pixels[y][xl] = fill_value;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   147
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   148
                            xl += 1;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   149
                        }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   150
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   151
                        if x < xl {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   152
                            push(self, &mut stack, x, xl - 1, y, dir);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   153
                            push(self, &mut stack, x, xl - 1, y, -dir);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   154
                        }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   155
                    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   156
                }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   157
            }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   158
        }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   159
    }
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   160
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   161
    #[inline]
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   162
    fn fill_circle_line<F: Fn(&mut T) -> usize>(
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   163
        &mut self,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   164
        y: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   165
        x_from: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   166
        x_to: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   167
        f: &F,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   168
    ) -> usize {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   169
        let mut result = 0;
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   170
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   171
        if self.is_valid_y(y) {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   172
            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
   173
                unsafe {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13952
diff changeset
   174
                    // coordinates are valid at this point
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   175
                    result += f(self.pixels.get_unchecked_mut(y as usize, i));
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   176
                }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   177
            }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   178
        }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   179
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   180
        result
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   181
    }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   182
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   183
    #[inline]
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   184
    fn fill_circle_lines<F: Fn(&mut T) -> usize>(
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   185
        &mut self,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   186
        x: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   187
        y: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   188
        dx: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   189
        dy: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   190
        f: &F,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   191
    ) -> usize {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   192
        self.fill_circle_line(y + dy, x - dx, x + dx, f)
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   193
            + self.fill_circle_line(y - dy, x - dx, x + dx, f)
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   194
            + self.fill_circle_line(y + dx, x - dy, x + dy, f)
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   195
            + self.fill_circle_line(y - dx, x - dy, x + dy, f)
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   196
    }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   197
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   198
    pub fn change_round<F: Fn(&mut T) -> usize>(
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   199
        &mut self,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   200
        x: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   201
        y: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   202
        radius: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   203
        f: F,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   204
    ) -> usize {
13964
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
   205
        ArcPoints::new(radius)
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
   206
            .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
   207
            .sum()
13955
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13952
diff changeset
   208
    }
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   209
13965
4162ea9ae333 Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents: 13964
diff changeset
   210
    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
   211
        let mut result = 0;
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   212
13965
4162ea9ae333 Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents: 13964
diff changeset
   213
        for point in LinePoints::new(from, to) {
4162ea9ae333 Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents: 13964
diff changeset
   214
            for vector in ArcPoints::new(radius) {
4162ea9ae333 Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents: 13964
diff changeset
   215
                for delta in EquidistantPoints::new(vector) {
13966
2354264ab0b0 apply some codegolfing for dubious reasons
alfadur
parents: 13965
diff changeset
   216
                    self.map_point(point + delta, |p|
2354264ab0b0 apply some codegolfing for dubious reasons
alfadur
parents: 13965
diff changeset
   217
                        result += (replace(p, value) != value) as usize)
13965
4162ea9ae333 Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents: 13964
diff changeset
   218
                }
13964
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
   219
            }
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
   220
        }
a325ed57ebfe Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents: 13961
diff changeset
   221
13965
4162ea9ae333 Use integral-geometry iterators to implement Land2D::draw_thick_line, remove no longer unused functions from Land2D
unc0rr
parents: 13964
diff changeset
   222
        result
13955
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13952
diff changeset
   223
    }
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   224
}
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   225
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   226
#[cfg(test)]
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   227
mod tests {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   228
    use super::*;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   229
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   230
    #[test]
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   231
    fn basics() {
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   232
        let l: Land2D<u8> = Land2D::new(32, 64, 0);
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   233
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   234
        assert!(l.is_valid_coordinate(0, 0));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   235
        assert!(!l.is_valid_coordinate(-1, -1));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   236
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   237
        assert!(l.is_valid_coordinate(31, 63));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   238
        assert!(!l.is_valid_coordinate(32, 63));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   239
        assert!(!l.is_valid_coordinate(31, 64));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   240
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   241
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   242
    #[test]
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   243
    fn fill() {
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   244
        let mut l: Land2D<u8> = Land2D::new(128, 128, 0);
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   245
13959
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
   246
        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
   247
        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
   248
        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
   249
        l.draw_line(Point::new(96, 80), Point::new(128, 0), 1);
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   250
13959
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13957
diff changeset
   251
        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
   252
        l.draw_line(Point::new(128, 128), Point::new(64, 96), 1);
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   253
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   254
        l.fill(32, 32, 1, 2);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   255
        l.fill(16, 96, 1, 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   256
        l.fill(60, 100, 1, 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   257
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   258
        assert_eq!(l.pixels[0][0], 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   259
        assert_eq!(l.pixels[96][64], 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   260
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   261
        assert_eq!(l.pixels[40][32], 2);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   262
        assert_eq!(l.pixels[40][96], 2);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   263
        assert_eq!(l.pixels[5][0], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   264
        assert_eq!(l.pixels[120][0], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   265
        assert_eq!(l.pixels[5][127], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   266
        assert_eq!(l.pixels[120][127], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   267
        assert_eq!(l.pixels[35][64], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   268
        assert_eq!(l.pixels[120][20], 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   269
        assert_eq!(l.pixels[120][100], 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   270
        assert_eq!(l.pixels[100][64], 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   271
    }
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   272
}