rust/land2d/src/lib.rs
changeset 14050 4b40bdd214df
parent 13951 03e41712eef8
child 14052 9c817b2eedae
equal deleted inserted replaced
14029:259175ab7e8c 14050:4b40bdd214df
     1 extern crate integral_geometry;
     1 extern crate integral_geometry;
     2 extern crate vec2d;
     2 extern crate vec2d;
     3 
     3 
     4 use std::cmp;
     4 use std::cmp;
     5 use std::ops;
       
     6 
     5 
     7 use integral_geometry::{ArcPoints, EquidistantPoints, LinePoints, Point};
     6 use integral_geometry::{ArcPoints, EquidistantPoints, LinePoints, Point};
     8 
     7 
     9 pub struct Land2D<T> {
     8 pub struct Land2D<T> {
    10     pixels: vec2d::Vec2D<T>,
     9     pixels: vec2d::Vec2D<T>,
       
    10     play_width: usize,
       
    11     play_height: usize,
    11     width_mask: usize,
    12     width_mask: usize,
    12     height_mask: usize,
    13     height_mask: usize,
    13 }
    14 }
    14 
    15 
    15 impl<T: Copy + PartialEq> Land2D<T> {
    16 impl<T: Copy + PartialEq> Land2D<T> {
    16     pub fn new(width: usize, height: usize, fill_value: T) -> Self {
    17     pub fn new(play_width: usize, play_height: usize, fill_value: T) -> Self {
    17         assert!(width.is_power_of_two());
    18         let real_width = play_width.next_power_of_two();
    18         assert!(height.is_power_of_two());
    19         let real_height = play_height.next_power_of_two();
       
    20 
       
    21         assert!(real_width > 0);
       
    22         assert!(real_height > 0);
    19 
    23 
    20         Self {
    24         Self {
    21             pixels: vec2d::Vec2D::new(width, height, fill_value),
    25             pixels: vec2d::Vec2D::new(real_width, real_height, fill_value),
    22             width_mask: !(width - 1),
    26             play_width,
    23             height_mask: !(height - 1),
    27             play_height,
       
    28             width_mask: !(real_width - 1),
       
    29             height_mask: !(real_height - 1),
    24         }
    30         }
    25     }
    31     }
    26 
    32 
    27     #[inline]
    33     #[inline]
    28     pub fn width(&self) -> usize {
    34     pub fn width(&self) -> usize {
    30     }
    36     }
    31 
    37 
    32     #[inline]
    38     #[inline]
    33     pub fn height(&self) -> usize {
    39     pub fn height(&self) -> usize {
    34         self.pixels.height()
    40         self.pixels.height()
       
    41     }
       
    42 
       
    43     #[inline]
       
    44     pub fn play_width(&self) -> usize {
       
    45         self.play_width
       
    46     }
       
    47 
       
    48     #[inline]
       
    49     pub fn play_height(&self) -> usize {
       
    50         self.play_height
    35     }
    51     }
    36 
    52 
    37     #[inline]
    53     #[inline]
    38     pub fn is_valid_x(&self, x: i32) -> bool {
    54     pub fn is_valid_x(&self, x: i32) -> bool {
    39         (x as usize & self.width_mask) == 0
    55         (x as usize & self.width_mask) == 0
   238 mod tests {
   254 mod tests {
   239     use super::*;
   255     use super::*;
   240 
   256 
   241     #[test]
   257     #[test]
   242     fn basics() {
   258     fn basics() {
   243         let l: Land2D<u8> = Land2D::new(32, 64, 0);
   259         let l: Land2D<u8> = Land2D::new(30, 50, 0);
       
   260 
       
   261         assert_eq!(l.play_width(), 30);
       
   262         assert_eq!(l.play_height(), 50);
       
   263         assert_eq!(l.width(), 32);
       
   264         assert_eq!(l.height(), 64);
   244 
   265 
   245         assert!(l.is_valid_coordinate(0, 0));
   266         assert!(l.is_valid_coordinate(0, 0));
   246         assert!(!l.is_valid_coordinate(-1, -1));
   267         assert!(!l.is_valid_coordinate(-1, -1));
   247 
   268 
   248         assert!(l.is_valid_coordinate(31, 63));
   269         assert!(l.is_valid_coordinate(31, 63));