Skip to main content

HeightMap

Struct HeightMap 

Source
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

Source

pub fn new(width: usize, height: usize) -> Self

Create a new zero-filled heightmap.

Source

pub fn from_data(width: usize, height: usize, data: Vec<f32>) -> Self

Create from existing data. Panics if data.len() != width * height.

Source

pub fn get(&self, x: usize, y: usize) -> f32

Get height at integer coordinates. Returns 0.0 if out-of-bounds.

Source

pub fn slope_at(&self, x: usize, y: usize) -> f32

Compute slope magnitude at (x, y) using central differences.

Source

pub fn set(&mut self, x: usize, y: usize, v: f32)

Set height at integer coordinates. No-op if out-of-bounds.

Source

pub fn sample_bilinear(&self, x: f32, y: f32) -> f32

Sample with bilinear interpolation at floating-point coordinates. Coordinates are clamped to valid range.

Source

pub fn sample_cubic(&self, x: f32, y: f32) -> f32

Sample with cubic (Catmull-Rom) interpolation.

Source

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.

Source

pub fn min_value(&self) -> f32

Minimum value in the map.

Source

pub fn max_value(&self) -> f32

Maximum value in the map.

Source

pub fn normalize(&mut self)

Normalize values so they span [0, 1].

Source

pub fn clamp_range(&mut self, min: f32, max: f32)

Clamp all values to [min, max].

Source

pub fn blur(&mut self, radius: usize)

Box-blur with the given radius (integer cells).

Source

pub fn sharpen(&mut self, amount: f32)

Sharpen using unsharp mask: original + amount * (original - blurred).

Source

pub fn terrace(&mut self, levels: usize)

Terrace the heightmap into levels discrete steps.

Source

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).

Source

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.

Source

pub fn slope_map(&self) -> HeightMap

Compute gradient magnitude (slope) at each cell.

Source

pub fn curvature_map(&self) -> HeightMap

Compute curvature (Laplacian) at each cell. Positive = convex (hill top), negative = concave (valley).

Source

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).

Source

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.

Source

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.

Source

pub fn to_raw_bytes(&self) -> Vec<u8>

Serialize to raw f32 binary (little-endian). Format: [width: u32][height: u32][data: f32 * width * height]

Source

pub fn from_raw_bytes(bytes: &[u8]) -> Option<Self>

Deserialize from raw f32 binary.

Source

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.

Source

pub fn to_png_16bit(&self) -> Vec<u8>

Export as 16-bit grayscale PNG bytes.

Source

pub fn from_png_8bit(width: usize, height: usize, pixels: &[u8]) -> Self

Import from 8-bit grayscale raw pixel data.

Source

pub fn from_png_16bit(width: usize, height: usize, pixels: &[u8]) -> Self

Import from 16-bit big-endian grayscale pixel data.

Source§

impl HeightMap

Source

pub fn mean(&self) -> f32

Compute the mean (average) value across all cells.

Source

pub fn variance(&self) -> f32

Compute the variance of height values.

Source

pub fn std_dev(&self) -> f32

Standard deviation of height values.

Source

pub fn invert(&mut self)

Invert heights: new_value = 1.0 - old_value.

Source

pub fn offset(&mut self, delta: f32)

Add a constant offset to all values (clamped to [0,1]).

Source

pub fn scale(&mut self, factor: f32)

Scale all values by a multiplier (clamped to [0,1]).

Source

pub fn add(&mut self, other: &HeightMap, weight: f32)

Element-wise addition of another heightmap. Maps must be same size.

Source

pub fn multiply(&mut self, other: &HeightMap)

Element-wise multiplication (mask).

Source

pub fn percentile(&self, p: f32) -> f32

Compute percentile value (e.g., 0.5 = median).

Source

pub fn equalize(&mut self)

Apply a histogram equalization to redistribute height values.

Source

pub fn histogram(&self, bins: usize) -> Vec<usize>

Compute a height histogram with bins buckets. Returns (bin_edges, counts).

Source

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.

Source

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.

Source

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.

Source

pub fn ridge_mask(&self, threshold: f32) -> HeightMap

Detect ridgelines: cells that are local maxima in at least one axis direction.

Source

pub fn valley_mask(&self, threshold: f32) -> HeightMap

Detect valley lines: cells that are local minima in at least one axis.

Source

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].

Source

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.

Source

pub fn flatten_circle( &mut self, cx: f32, cy: f32, radius: f32, target: f32, strength: f32, )

Flatten a circular area to a target height.

Source

pub fn resample(&self, new_width: usize, new_height: usize) -> HeightMap

Resample the heightmap to a new resolution using bilinear interpolation.

Source

pub fn crop(&self, x0: usize, y0: usize, x1: usize, y1: usize) -> HeightMap

Crop a rectangular region from the heightmap.

Source

pub fn tile_horizontal(&self, other: &HeightMap) -> HeightMap

Tile two heightmaps side by side (horizontal).

Trait Implementations§

Source§

impl Clone for HeightMap

Source§

fn clone(&self) -> HeightMap

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HeightMap

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,