rust/landgen/src/wavefront_collapse/tile_image.rs
branchtransitional_engine
changeset 15948 9bd828451d77
parent 15947 60b5639cc3a5
child 15949 659c92124c26
equal deleted inserted replaced
15947:60b5639cc3a5 15948:9bd828451d77
     1 use super::transform::RotationTransform;
     1 use super::transform::Transform;
       
     2 use integral_geometry::Size;
     2 use std::rc::Rc;
     3 use std::rc::Rc;
     3 use vec2d::Vec2D;
     4 use vec2d::Vec2D;
     4 
     5 
     5 #[derive(PartialEq, Clone, Debug)]
     6 #[derive(PartialEq, Clone, Debug)]
     6 pub struct Edge<I: PartialEq + Clone> {
     7 pub struct Edge<I: PartialEq + Clone> {
    23             id: self.id.clone(),
    24             id: self.id.clone(),
    24             symmetrical: self.symmetrical,
    25             symmetrical: self.symmetrical,
    25             reverse: !self.symmetrical && !self.reverse,
    26             reverse: !self.symmetrical && !self.reverse,
    26         }
    27         }
    27     }
    28     }
       
    29 
       
    30     pub fn is_compatible(&self, other: &Self) -> bool {
       
    31         self.id == other.id && (self.reverse != other.reverse || self.symmetrical)
       
    32     }
    28 }
    33 }
    29 
    34 
    30 #[derive(Clone)]
    35 #[derive(Clone)]
    31 pub struct TileImage<T, I: PartialEq + Clone> {
    36 pub struct TileImage<T, I: PartialEq + Clone> {
    32     image: Rc<Vec2D<T>>,
    37     image: Rc<Vec2D<T>>,
    33     transform: RotationTransform,
    38     transform: Transform,
    34     top: Edge<I>,
    39     top: Edge<I>,
    35     right: Edge<I>,
    40     right: Edge<I>,
    36     bottom: Edge<I>,
    41     bottom: Edge<I>,
    37     left: Edge<I>,
    42     left: Edge<I>,
    38 }
    43 }
    45         bottom: Edge<I>,
    50         bottom: Edge<I>,
    46         left: Edge<I>,
    51         left: Edge<I>,
    47     ) -> Self {
    52     ) -> Self {
    48         Self {
    53         Self {
    49             image: Rc::new(image),
    54             image: Rc::new(image),
    50             transform: RotationTransform::default(),
    55             transform: Transform::default(),
    51             top,
    56             top,
    52             right,
    57             right,
    53             bottom,
    58             bottom,
    54             left,
    59             left,
    55         }
    60         }
    89     }
    94     }
    90 
    95 
    91     pub fn rotated180(&self) -> Self {
    96     pub fn rotated180(&self) -> Self {
    92         Self {
    97         Self {
    93             image: self.image.clone(),
    98             image: self.image.clone(),
    94             transform: self.transform.rotate90(),
    99             transform: self.transform.rotate180(),
    95             top: self.bottom.clone(),
   100             top: self.bottom.clone(),
    96             right: self.left.clone(),
   101             right: self.left.clone(),
    97             bottom: self.top.clone(),
   102             bottom: self.top.clone(),
    98             left: self.right.clone(),
   103             left: self.right.clone(),
    99         }
   104         }
   100     }
   105     }
   101 
   106 
   102     pub fn rotated270(&self) -> Self {
   107     pub fn rotated270(&self) -> Self {
   103         Self {
   108         Self {
   104             image: self.image.clone(),
   109             image: self.image.clone(),
   105             transform: self.transform.rotate90(),
   110             transform: self.transform.rotate270(),
   106             top: self.left.clone(),
   111             top: self.left.clone(),
   107             right: self.top.clone(),
   112             right: self.top.clone(),
   108             bottom: self.right.clone(),
   113             bottom: self.right.clone(),
   109             left: self.bottom.clone(),
   114             left: self.bottom.clone(),
   110         }
   115         }
   122         &self.left
   127         &self.left
   123     }
   128     }
   124 
   129 
   125     pub fn top_edge(&self) -> &Edge<I> {
   130     pub fn top_edge(&self) -> &Edge<I> {
   126         &self.top
   131         &self.top
       
   132     }
       
   133 
       
   134     pub fn size(&self) -> Size {
       
   135         match self.transform {
       
   136             Transform::Rotate0(_) => self.image.size(),
       
   137             Transform::Rotate90(_) => Size::new(self.image.size().height, self.image.size().width),
       
   138         }
       
   139     }
       
   140 
       
   141     pub fn get(&self, row: usize, column: usize) -> Option<&T> {
       
   142         match self.transform {
       
   143             Transform::Rotate0(_) => {
       
   144                 let image_row = if self.transform.is_flipped() {
       
   145                     self.image.height().wrapping_sub(1).wrapping_sub(row)
       
   146                 } else {
       
   147                     row
       
   148                 };
       
   149 
       
   150                 let image_column = if self.transform.is_mirrored() {
       
   151                     self.image.width().wrapping_sub(1).wrapping_sub(column)
       
   152                 } else {
       
   153                     column
       
   154                 };
       
   155 
       
   156                 self.image.get(image_row, image_column)
       
   157             },
       
   158             Transform::Rotate90(_) => {
       
   159                 let image_row = if self.transform.is_flipped() {
       
   160                     column
       
   161                 } else {
       
   162                     self.image.height().wrapping_sub(1).wrapping_sub(column)
       
   163                 };
       
   164 
       
   165                 let image_column = if self.transform.is_mirrored() {
       
   166                     self.image.width().wrapping_sub(1).wrapping_sub(row)
       
   167                 } else {
       
   168                     row
       
   169                 };
       
   170 
       
   171                 self.image.get(image_row, image_column)
       
   172             },
       
   173         }
   127     }
   174     }
   128 }
   175 }
   129 
   176 
   130 #[cfg(test)]
   177 #[cfg(test)]
   131 mod tests {
   178 mod tests {