Skip to main content

HeightMap

Struct HeightMap 

Source
pub struct HeightMap { /* private fields */ }
Expand description

A 2D heightmap stored as a flat row-major Vec<f32> buffer.

Covers world space [0, width * scale) × [0, height * scale). width and height are grid-cell counts; scale is world units per cell.

The invariant data.len() == width * height is enforced by all constructors and mutation methods; fields are private to prevent external corruption.

A grid-cell normal cache is computed lazily on first read and reused across repeated queries (e.g. by SplatMapper). It is invalidated whenever the heights change via HeightMap::set, HeightMap::get_mut, HeightMap::data_mut, or HeightMap::normalize.

Implementations§

Source§

impl HeightMap

Source

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

Create a heightmap of width × height cells, all initialised to 0.0.

scale is the world-unit size of each cell (must be > 0).

§Panics

Panics if width == 0, height == 0, or scale <= 0.0.

Source

pub fn width(&self) -> usize

Grid-cell width.

Source

pub fn height(&self) -> usize

Grid-cell depth.

Source

pub fn scale(&self) -> f32

World units per grid cell.

Source

pub fn data(&self) -> &[f32]

Read-only view of the flat row-major height buffer.

Source

pub fn data_mut(&mut self) -> &mut [f32]

Mutable view of the flat row-major height buffer.

The caller must not change the slice length; only element values may be modified. Dimensions (width, height) are unaffected. Invalidates the normal cache.

Source

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

Return the height at grid cell (x, z).

§Panics

Panics if x >= width or z >= height.

Source

pub fn get_mut(&mut self, x: usize, z: usize) -> &mut f32

Return a mutable reference to the height at grid cell (x, z). Invalidates the normal cache.

§Panics

Panics if x >= width or z >= height.

Source

pub fn set(&mut self, x: usize, z: usize, val: f32)

Set the height at grid cell (x, z) to val. Invalidates the normal cache.

§Panics

Panics if x >= width or z >= height.

Source

pub fn get_clamped(&self, x: i32, z: i32) -> f32

Return the height at grid cell (x, z), clamping coordinates to the valid range instead of panicking on out-of-bounds indices.

Source

pub fn get_height_at(&self, world_x: f32, world_z: f32) -> f32

Sample height at world position using bilinear interpolation. Clamps to heightmap boundaries.

Source

pub fn get_normal_at(&self, world_x: f32, world_z: f32) -> [f32; 3]

Compute surface normal at world position using central differences. Returns a normalized [x, y, z] vector where y is up.

scale is always > 0 (enforced by the constructor), so the 2*scale divisor and the len (≥ 1.0 since ny = 1) are both safe.

Source

pub fn normal_at_grid(&self, x: usize, z: usize) -> [f32; 3]

Cached central-difference normal at grid cell (x, z).

Equivalent to get_normal_at(x as f32 * scale, z as f32 * scale) for in-bounds cells, but reads from a lazily populated cache so callers that scan the whole grid (e.g. SplatMapper) avoid recomputing four central differences per pixel.

§Panics

Panics if x >= width or z >= height.

Source

pub fn normals_grid(&self) -> &[[f32; 3]]

Lazily populated per-grid-cell normal table; row-major, length width * height.

Source

pub fn lakes(&self) -> &[Lake]

Lakes detected by the most recent hydraulic erosion pass. Empty until HydraulicErosion::erode populates it.

Source

pub fn normalize(&mut self)

Normalize all height values to [0.0, 1.0].

Source

pub fn world_width(&self) -> f32

World-space width of the heightmap.

Source

pub fn world_depth(&self) -> f32

World-space depth of the heightmap.

Trait Implementations§

Source§

impl Clone for HeightMap

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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
Source§

impl<'de> Deserialize<'de> for HeightMap

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for HeightMap

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,