pub struct HeightMap {
pub width: usize,
pub height: usize,
pub data: Vec<f32>,
}Expand description
A 2D grid of f32 height values.
Heights are stored in row-major order: data[y * width + x].
Values are typically in [0, 1] but the API does not enforce this.
Fields§
§width: usize§height: usize§data: Vec<f32>Implementations§
Source§impl HeightMap
impl HeightMap
Sourcepub fn from_data(width: usize, height: usize, data: Vec<f32>) -> Self
pub fn from_data(width: usize, height: usize, data: Vec<f32>) -> Self
Create from existing data. Panics if data.len() != width * height.
Sourcepub fn get(&self, x: usize, y: usize) -> f32
pub fn get(&self, x: usize, y: usize) -> f32
Get height at integer coordinates. Returns 0.0 if out-of-bounds.
Sourcepub fn slope_at(&self, x: usize, y: usize) -> f32
pub fn slope_at(&self, x: usize, y: usize) -> f32
Compute slope magnitude at (x, y) using central differences.
Sourcepub fn set(&mut self, x: usize, y: usize, v: f32)
pub fn set(&mut self, x: usize, y: usize, v: f32)
Set height at integer coordinates. No-op if out-of-bounds.
Sourcepub fn sample_bilinear(&self, x: f32, y: f32) -> f32
pub fn sample_bilinear(&self, x: f32, y: f32) -> f32
Sample with bilinear interpolation at floating-point coordinates. Coordinates are clamped to valid range.
Sourcepub fn sample_cubic(&self, x: f32, y: f32) -> f32
pub fn sample_cubic(&self, x: f32, y: f32) -> f32
Sample with cubic (Catmull-Rom) interpolation.
Sourcepub fn normal_at(&self, x: usize, y: usize) -> Vec3
pub fn normal_at(&self, x: usize, y: usize) -> Vec3
Compute surface normal at (x, y) from height differences. Returns a normalized Vec3 pointing upward.
Sourcepub fn clamp_range(&mut self, min: f32, max: f32)
pub fn clamp_range(&mut self, min: f32, max: f32)
Clamp all values to [min, max].
Sourcepub fn sharpen(&mut self, amount: f32)
pub fn sharpen(&mut self, amount: f32)
Sharpen using unsharp mask: original + amount * (original - blurred).
Sourcepub fn island_mask(&mut self, falloff: f32)
pub fn island_mask(&mut self, falloff: f32)
Apply a radial island mask, fading edges to zero.
falloff controls how quickly edges fade (larger = sharper).
Sourcepub fn ridge_noise(&mut self, octaves: usize)
pub fn ridge_noise(&mut self, octaves: usize)
Compute ridge noise by folding the noise into ridges. Modifies in place using the existing data as input.
Sourcepub fn curvature_map(&self) -> HeightMap
pub fn curvature_map(&self) -> HeightMap
Compute curvature (Laplacian) at each cell. Positive = convex (hill top), negative = concave (valley).
Sourcepub fn flow_map(&self) -> HeightMap
pub fn flow_map(&self) -> HeightMap
Compute water flow direction map using D8 steepest descent. Returns a map where value encodes direction (0-7 for 8 neighbors, 8 = flat).
Sourcepub fn shadow_map(&self, sun_dir: Vec3) -> HeightMap
pub fn shadow_map(&self, sun_dir: Vec3) -> HeightMap
Compute a static shadow map given a sun direction (normalized Vec3). Returns 1.0 = lit, 0.0 = shadowed.
Sourcepub fn visibility_map(&self, observer_height: f32) -> HeightMap
pub fn visibility_map(&self, observer_height: f32) -> HeightMap
Compute viewshed: for each cell, how many other cells can see it from observer_height.
Returns a normalized visibility count map.
Sourcepub fn to_raw_bytes(&self) -> Vec<u8> ⓘ
pub fn to_raw_bytes(&self) -> Vec<u8> ⓘ
Serialize to raw f32 binary (little-endian). Format: [width: u32][height: u32][data: f32 * width * height]
Sourcepub fn from_raw_bytes(bytes: &[u8]) -> Option<Self>
pub fn from_raw_bytes(bytes: &[u8]) -> Option<Self>
Deserialize from raw f32 binary.
Sourcepub fn to_png_8bit(&self) -> Vec<u8> ⓘ
pub fn to_png_8bit(&self) -> Vec<u8> ⓘ
Export as 8-bit grayscale PNG bytes (raw PNG, no external dep). Uses a minimal PNG encoder.
Sourcepub fn to_png_16bit(&self) -> Vec<u8> ⓘ
pub fn to_png_16bit(&self) -> Vec<u8> ⓘ
Export as 16-bit grayscale PNG bytes.
Sourcepub fn from_png_8bit(width: usize, height: usize, pixels: &[u8]) -> Self
pub fn from_png_8bit(width: usize, height: usize, pixels: &[u8]) -> Self
Import from 8-bit grayscale raw pixel data.
Sourcepub fn from_png_16bit(width: usize, height: usize, pixels: &[u8]) -> Self
pub fn from_png_16bit(width: usize, height: usize, pixels: &[u8]) -> Self
Import from 16-bit big-endian grayscale pixel data.
Source§impl HeightMap
impl HeightMap
Sourcepub fn add(&mut self, other: &HeightMap, weight: f32)
pub fn add(&mut self, other: &HeightMap, weight: f32)
Element-wise addition of another heightmap. Maps must be same size.
Sourcepub fn percentile(&self, p: f32) -> f32
pub fn percentile(&self, p: f32) -> f32
Compute percentile value (e.g., 0.5 = median).
Sourcepub fn histogram(&self, bins: usize) -> Vec<usize>
pub fn histogram(&self, bins: usize) -> Vec<usize>
Compute a height histogram with bins buckets. Returns (bin_edges, counts).
Sourcepub fn simple_erode(&mut self, iterations: usize, threshold: f32, rate: f32)
pub fn simple_erode(&mut self, iterations: usize, threshold: f32, rate: f32)
Erode using a simple “drop” model: any cell higher than its lowest neighbor
by more than threshold transfers rate of material downhill.
Sourcepub fn to_color_bytes(&self) -> Vec<u8> ⓘ
pub fn to_color_bytes(&self) -> Vec<u8> ⓘ
Generate a color-mapped image of the heightmap as RGB bytes. Uses a standard terrain color ramp: deep water → water → sand → grass → rock → snow.
Sourcepub fn ambient_occlusion(&self, radius: usize, samples: usize) -> HeightMap
pub fn ambient_occlusion(&self, radius: usize, samples: usize) -> HeightMap
Compute an ambient occlusion approximation using height-based sampling. Returns a map where 0 = fully occluded, 1 = fully lit.
Sourcepub fn ridge_mask(&self, threshold: f32) -> HeightMap
pub fn ridge_mask(&self, threshold: f32) -> HeightMap
Detect ridgelines: cells that are local maxima in at least one axis direction.
Sourcepub fn valley_mask(&self, threshold: f32) -> HeightMap
pub fn valley_mask(&self, threshold: f32) -> HeightMap
Detect valley lines: cells that are local minima in at least one axis.
Sourcepub fn distance_field(&self, threshold: f32) -> HeightMap
pub fn distance_field(&self, threshold: f32) -> HeightMap
Compute a distance field: each cell stores distance to the nearest cell
with value >= threshold, normalized to [0,1].
Sourcepub fn paint_circle(
&mut self,
cx: f32,
cy: f32,
radius: f32,
strength: f32,
add: bool,
)
pub fn paint_circle( &mut self, cx: f32, cy: f32, radius: f32, strength: f32, add: bool, )
Overlay a circle (Gaussian bump) at world-space (x, y) with given radius and strength.
Sourcepub fn flatten_circle(
&mut self,
cx: f32,
cy: f32,
radius: f32,
target: f32,
strength: f32,
)
pub fn flatten_circle( &mut self, cx: f32, cy: f32, radius: f32, target: f32, strength: f32, )
Flatten a circular area to a target height.
Sourcepub fn resample(&self, new_width: usize, new_height: usize) -> HeightMap
pub fn resample(&self, new_width: usize, new_height: usize) -> HeightMap
Resample the heightmap to a new resolution using bilinear interpolation.
Sourcepub fn crop(&self, x0: usize, y0: usize, x1: usize, y1: usize) -> HeightMap
pub fn crop(&self, x0: usize, y0: usize, x1: usize, y1: usize) -> HeightMap
Crop a rectangular region from the heightmap.
Sourcepub fn tile_horizontal(&self, other: &HeightMap) -> HeightMap
pub fn tile_horizontal(&self, other: &HeightMap) -> HeightMap
Tile two heightmaps side by side (horizontal).