rust/integral-geometry/src/lib.rs
changeset 14032 2869c2ccb1b8
parent 14031 c47283feafac
child 14052 9c817b2eedae
equal deleted inserted replaced
14031:c47283feafac 14032:2869c2ccb1b8
    40 
    40 
    41     #[inline]
    41     #[inline]
    42     pub fn transform(self, matrix: &[i32; 4]) -> Self {
    42     pub fn transform(self, matrix: &[i32; 4]) -> Self {
    43         Point::new(matrix[0] * self.x + matrix[1] * self.y,
    43         Point::new(matrix[0] * self.x + matrix[1] * self.y,
    44                    matrix[2] * self.x + matrix[3] * self.y)
    44                    matrix[2] * self.x + matrix[3] * self.y)
       
    45     }
       
    46 }
       
    47 
       
    48 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
       
    49 pub struct Size {
       
    50     pub width: usize,
       
    51     pub height: usize,
       
    52 }
       
    53 
       
    54 impl Size {
       
    55     #[inline]
       
    56     pub fn new(width: usize, height: usize) -> Self {
       
    57         Size { width, height }
       
    58     }
       
    59 
       
    60     #[inline]
       
    61     pub fn square(size: usize) -> Self {
       
    62         Size { width: size, height: size }
       
    63     }
       
    64 
       
    65     #[inline]
       
    66     pub fn area(&self) -> usize {
       
    67         self.width * self.height
       
    68     }
       
    69 
       
    70     #[inline]
       
    71     pub fn linear_index(&self, x: usize, y: usize) -> usize {
       
    72         y * self.width + x
       
    73     }
       
    74 
       
    75     #[inline]
       
    76     pub fn is_power_of_two(&self) -> bool {
       
    77         self.width.is_power_of_two() && self.height.is_power_of_two()
       
    78     }
       
    79 
       
    80     #[inline]
       
    81     pub fn to_mask(&self) -> SizeMask {
       
    82         SizeMask::new(*self)
       
    83     }
       
    84 }
       
    85 
       
    86 pub struct SizeMask{ size: Size }
       
    87 
       
    88 impl SizeMask {
       
    89     #[inline]
       
    90     pub fn new(size: Size) -> Self {
       
    91         assert!(size.is_power_of_two());
       
    92         let size = Size {
       
    93             width: !(size.width - 1),
       
    94             height: !(size.height - 1)
       
    95         };
       
    96         SizeMask { size }
       
    97     }
       
    98 
       
    99     #[inline]
       
   100     pub fn contains_x<T: Into<usize>>(&self, x: T) -> bool {
       
   101         (self.size.width & x.into()) == 0
       
   102     }
       
   103 
       
   104     #[inline]
       
   105     pub fn contains_y<T: Into<usize>>(&self, y: T) -> bool {
       
   106         (self.size.height & y.into()) == 0
       
   107     }
       
   108 
       
   109     #[inline]
       
   110     pub fn contains(&self, point: Point) -> bool {
       
   111         self.contains_x(point.x as usize) && self.contains_y(point.y as usize)
    45     }
   112     }
    46 }
   113 }
    47 
   114 
    48 macro_rules! bin_op_impl {
   115 macro_rules! bin_op_impl {
    49     ($op: ty, $name: tt) => {
   116     ($op: ty, $name: tt) => {