rust/integral-geometry/src/lib.rs
changeset 14059 c6745a1c827a
parent 14054 3185fb34f3b5
child 14076 e5904ead4864
--- a/rust/integral-geometry/src/lib.rs	Thu Nov 01 12:09:29 2018 +0100
+++ b/rust/integral-geometry/src/lib.rs	Thu Nov 01 21:30:21 2018 +0300
@@ -89,6 +89,10 @@
     pub fn to_mask(&self) -> SizeMask {
         SizeMask::new(*self)
     }
+
+    pub fn to_grid_index(&self) -> GridIndex {
+        GridIndex::new(*self)
+    }
 }
 
 pub struct SizeMask{ size: Size }
@@ -101,7 +105,7 @@
             width: !(size.width - 1),
             height: !(size.height - 1)
         };
-        SizeMask { size }
+        Self { size }
     }
 
     #[inline]
@@ -120,6 +124,22 @@
     }
 }
 
+pub struct GridIndex{ shift: Point }
+
+impl GridIndex {
+    pub fn new(size: Size) -> Self {
+        assert!(size.is_power_of_two());
+        let shift = Point::new(size.width.trailing_zeros() as i32,
+                               size.height.trailing_zeros() as i32);
+        Self { shift }
+    }
+
+    pub fn map(&self, position: Point) -> Point {
+        Point::new(position.x >> self.shift.x,
+                   position.y >> self.shift.y)
+    }
+}
+
 macro_rules! bin_op_impl {
     ($op: ty, $name: tt) => {
         impl $op for Point {