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