Advertisement
Guest User

Untitled

a guest
Jan 16th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.06 KB | None | 0 0
  1. use ::nalgebra::{ Vector3, Point3, Vector2, Point2, cast };
  2. use std::ops::{ Index, IndexMut };
  3. use ::rnd::OctavesSeed;
  4.  
  5. // Make sure to use GenUnit3 for 3d, GenUnit2 for 2d.
  6. // This would simplify making them separate structs in the
  7. // future if that is needed.
  8. pub type GenUnit3<N> = GenUnit<N>;
  9. pub type GenUnit2<N> = GenUnit<N>;
  10.  
  11. pub struct GenUnit<N> {
  12.     pub size: Vector3<u32>,
  13.     pub data: Vec<N>,
  14. }
  15.  
  16. impl<N: Sized> GenUnit<N> {
  17.     pub fn new3(size: Vector3<u32>, default: N) -> GenUnit<N> where N: Clone {
  18.         let capacity: usize = cast(size.x * size.y * size.z);
  19.         GenUnit {
  20.             size: size,
  21.             data: vec![default; capacity],
  22.         }
  23.     }
  24.     pub fn new2(size: Vector2<u32>, default: N) -> GenUnit<N> where N: Clone {
  25.         GenUnit::new3(Vector3::new(size.x, 1, size.y), default)
  26.     }
  27.  
  28.     pub fn new2_from_vec(size: Vector2<u32>, data: Vec<N>) -> GenUnit<N> {
  29.         let capacity: usize = cast(size.x * size.y);
  30.         assert!(capacity == data.len());
  31.         GenUnit {
  32.             size: Vector3::new(size.x, 1, size.y),
  33.             data: data,
  34.         }
  35.     }
  36.     pub fn new3_from_vec(size: Vector3<u32>, data: Vec<N>) -> GenUnit<N> {
  37.         let capacity: usize = cast(size.x * size.y * size.z);
  38.         assert!(capacity == data.len());
  39.         GenUnit {
  40.             size: size,
  41.             data: data,
  42.         }
  43.     }
  44.  
  45.     pub fn gen_simplex(seed: &OctavesSeed, pos: Point3<f64>, size: Vector3<u32>,
  46.                    scale: Vector3<f64>, mult: f64) -> GenUnit<f64> {
  47.         let scaled = pos.coords * scale;
  48.  
  49.         let capacity: usize = cast(size.x * size.y * size.z);
  50.         let mut data = Vec::with_capacity(capacity);
  51.  
  52.         for x_pos in 0..size.x {
  53.             for z_pos in 0..size.z {
  54.                 for y_pos in 0..size.y {
  55.                     let scaled_pos = (scaled + (pos.coords * scale)) / 8192.0;
  56.                     let val = seed.simplex3(scaled_pos.as_ref()) * mult;
  57.                     data.push(val);
  58.                 }
  59.             }
  60.         }
  61.  
  62.         GenUnit {
  63.             size: size,
  64.             data: data,
  65.         }
  66.     }
  67.  
  68.     pub fn to_idx_vec3(&self, pos: Point3<u32>) -> usize {
  69.         self.to_idx(pos.x, pos.y, pos.z)
  70.     }
  71.     pub fn to_idx_vec2(&self, pos: Point2<u32>) -> usize {
  72.         self.to_idx(pos.x, 0, pos.y)
  73.     }
  74.     pub fn to_idx(&self, x: u32, y: u32, z: u32) -> usize {
  75.         debug_assert!(x < self.size.x);
  76.         debug_assert!(y < self.size.y);
  77.         debug_assert!(z < self.size.z);
  78.  
  79.         (x + (z*self.size.x) + (y*self.size.x*self.size.z)) as usize
  80.     }
  81.  
  82.     pub fn from_idx(&self, mut idx: usize) -> Point3<u32> {
  83.         let x = idx % self.size.x as usize;
  84.         idx /= self.size.x as usize;
  85.         let z = idx % self.size.z as usize;
  86.         idx /= self.size.z as usize;
  87.         let y = idx;
  88.         Point3::new(x as u32, y as u32, z as u32)
  89.     }
  90.  
  91.     pub fn len(&self) -> usize {
  92.         self.data.len()
  93.     }
  94.     pub fn size2(&self) -> Vector2<u32> {
  95.         Vector2::new(self.size.x, self.size.z)
  96.     }
  97. }
  98.  
  99. impl<N> Index<usize> for GenUnit<N> {
  100.     type Output = N;
  101.     fn index(&self, index: usize) -> &N { &self.data[index] }
  102. }
  103. impl<N> IndexMut<usize> for GenUnit<N> {
  104.     fn index_mut(&mut self, index: usize) -> &mut N { &mut self.data[index] }
  105. }
  106.  
  107. impl<N> Index<Point3<u32>> for GenUnit<N> {
  108.     type Output = N;
  109.     fn index(&self, index: Point3<u32>) -> &N {
  110.         let idx = self.to_idx_vec3(index);
  111.         &self.data[idx]
  112.     }
  113. }
  114. impl<N> IndexMut<Point3<u32>> for GenUnit<N> {
  115.     fn index_mut(&mut self, index: Point3<u32>) -> &mut N {
  116.         let idx = self.to_idx_vec3(index);
  117.         &mut self.data[idx]
  118.     }
  119. }
  120. impl<N> Index<Point2<u32>> for GenUnit<N> {
  121.     type Output = N;
  122.     fn index(&self, index: Point2<u32>) -> &N {
  123.         let idx = self.to_idx_vec2(index);
  124.         &self.data[idx]
  125.     }
  126. }
  127. impl<N> IndexMut<Point2<u32>> for GenUnit<N> {
  128.     fn index_mut(&mut self, index: Point2<u32>) -> &mut N {
  129.         let idx = self.to_idx_vec2(index);
  130.         &mut self.data[idx]
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement