Module zarray::z3d

source · []
Expand description

This module is used for storing 3-dimensional data arrays, and internally uses Z-index arrays to improve data localization and alignment to the CPU cache-line fetches. In other words, use this to improve performance for 2D data that is randomly accessed rather than raster scanned or if your data processing makes heavy use of neighbor look-up in both the X and Y directions.

How It Works

When you initialize a zarray::z3d::ZArray3D struct, it creates an array of 8x8x8 data patches (512 total elements per patch), using Z-curve indexing within that patch. When you call a getter or setter method, it finds the corresponding data patch and then looks up (or sets) the data from within the patch.

Example Usage

The following example could be used as part of an erosion simulation:

use zarray::z3d::ZArray3D;
let width = 100;
let length = 200;
let depth = 25;
let air = 0f32;
let soil_hardness = 1f32;
let rock_hardness = 8f32;
let drip_power = 1.5f32;
let iterations = 12;
let mut map = ZArray3D::new(width, length, depth, air);
map.fill(0,0,5, width,length,depth, soil_hardness).unwrap();
map.fill(0,0,15, width,length,depth, rock_hardness).unwrap();
for boulder in [(34,88,6), (66,122,9), (11,154,5), (35,93,8), (72,75,12)]{
  map.set(boulder.0, boulder.1, boulder.2, rock_hardness).unwrap();
}
for _ in 0..iterations{
  for x in 0..width{for y in 0..length{
    let mut drip = drip_power;
    let mut z = 0;
    while drip > 0f32 {
      let h = *map.bounded_get(x as isize, y as isize, z).unwrap_or(&100f32);
      if h > drip {
        map.bounded_set(x as isize, y as isize, z, h - drip);
        drip = 0.;
      } else {
        map.bounded_set(x as isize, y as isize, z, 0.);
        drip -= h;
      }
      z += 1;
    }
  }}
}

Structs

This is primary struct for z-indexed 3D arrays. Create new instances with ZArray3D::new(x_size, y_size, z_size, initial_value)

Functions

General purpose Z-index function to convert a three-dimensional coordinate into a localized one-dimensional coordinate

General purpose Z-index function to convert a three-dimensional coordinate into a localized one-dimensional coordinate