rust/integral-geometry/src/lib.rs
changeset 14059 c6745a1c827a
parent 14054 3185fb34f3b5
child 14076 e5904ead4864
equal deleted inserted replaced
14058:fa2ed06c2f55 14059:c6745a1c827a
    86     }
    86     }
    87 
    87 
    88     #[inline]
    88     #[inline]
    89     pub fn to_mask(&self) -> SizeMask {
    89     pub fn to_mask(&self) -> SizeMask {
    90         SizeMask::new(*self)
    90         SizeMask::new(*self)
       
    91     }
       
    92 
       
    93     pub fn to_grid_index(&self) -> GridIndex {
       
    94         GridIndex::new(*self)
    91     }
    95     }
    92 }
    96 }
    93 
    97 
    94 pub struct SizeMask{ size: Size }
    98 pub struct SizeMask{ size: Size }
    95 
    99 
    99         assert!(size.is_power_of_two());
   103         assert!(size.is_power_of_two());
   100         let size = Size {
   104         let size = Size {
   101             width: !(size.width - 1),
   105             width: !(size.width - 1),
   102             height: !(size.height - 1)
   106             height: !(size.height - 1)
   103         };
   107         };
   104         SizeMask { size }
   108         Self { size }
   105     }
   109     }
   106 
   110 
   107     #[inline]
   111     #[inline]
   108     pub fn contains_x<T: Into<usize>>(&self, x: T) -> bool {
   112     pub fn contains_x<T: Into<usize>>(&self, x: T) -> bool {
   109         (self.size.width & x.into()) == 0
   113         (self.size.width & x.into()) == 0
   115     }
   119     }
   116 
   120 
   117     #[inline]
   121     #[inline]
   118     pub fn contains(&self, point: Point) -> bool {
   122     pub fn contains(&self, point: Point) -> bool {
   119         self.contains_x(point.x as usize) && self.contains_y(point.y as usize)
   123         self.contains_x(point.x as usize) && self.contains_y(point.y as usize)
       
   124     }
       
   125 }
       
   126 
       
   127 pub struct GridIndex{ shift: Point }
       
   128 
       
   129 impl GridIndex {
       
   130     pub fn new(size: Size) -> Self {
       
   131         assert!(size.is_power_of_two());
       
   132         let shift = Point::new(size.width.trailing_zeros() as i32,
       
   133                                size.height.trailing_zeros() as i32);
       
   134         Self { shift }
       
   135     }
       
   136 
       
   137     pub fn map(&self, position: Point) -> Point {
       
   138         Point::new(position.x >> self.shift.x,
       
   139                    position.y >> self.shift.y)
   120     }
   140     }
   121 }
   141 }
   122 
   142 
   123 macro_rules! bin_op_impl {
   143 macro_rules! bin_op_impl {
   124     ($op: ty, $name: tt) => {
   144     ($op: ty, $name: tt) => {