rust/integral-geometry/src/lib.rs
changeset 14052 9c817b2eedae
parent 14051 8a0d69c16cad
parent 14032 2869c2ccb1b8
child 14054 3185fb34f3b5
equal deleted inserted replaced
14051:8a0d69c16cad 14052:9c817b2eedae
    34     }
    34     }
    35 
    35 
    36     #[inline]
    36     #[inline]
    37     pub fn max_norm(self) -> i32 {
    37     pub fn max_norm(self) -> i32 {
    38         std::cmp::max(self.x.abs(), self.y.abs())
    38         std::cmp::max(self.x.abs(), self.y.abs())
       
    39     }
       
    40 
       
    41     #[inline]
       
    42     pub fn transform(self, matrix: &[i32; 4]) -> Self {
       
    43         Point::new(matrix[0] * self.x + matrix[1] * 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 next_power_of_two(&self) -> Self {
       
    82         Self {
       
    83             width: self.width.next_power_of_two(),
       
    84             height: self.height.next_power_of_two()
       
    85         }
       
    86     }
       
    87 
       
    88     #[inline]
       
    89     pub fn to_mask(&self) -> SizeMask {
       
    90         SizeMask::new(*self)
       
    91     }
       
    92 }
       
    93 
       
    94 pub struct SizeMask{ size: Size }
       
    95 
       
    96 impl SizeMask {
       
    97     #[inline]
       
    98     pub fn new(size: Size) -> Self {
       
    99         assert!(size.is_power_of_two());
       
   100         let size = Size {
       
   101             width: !(size.width - 1),
       
   102             height: !(size.height - 1)
       
   103         };
       
   104         SizeMask { size }
       
   105     }
       
   106 
       
   107     #[inline]
       
   108     pub fn contains_x<T: Into<usize>>(&self, x: T) -> bool {
       
   109         (self.size.width & x.into()) == 0
       
   110     }
       
   111 
       
   112     #[inline]
       
   113     pub fn contains_y<T: Into<usize>>(&self, y: T) -> bool {
       
   114         (self.size.height & y.into()) == 0
       
   115     }
       
   116 
       
   117     #[inline]
       
   118     pub fn contains(&self, point: Point) -> bool {
       
   119         self.contains_x(point.x as usize) && self.contains_y(point.y as usize)
    39     }
   120     }
    40 }
   121 }
    41 
   122 
    42 macro_rules! bin_op_impl {
   123 macro_rules! bin_op_impl {
    43     ($op: ty, $name: tt) => {
   124     ($op: ty, $name: tt) => {