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
impl HeightMap
Sourcepub fn new(width: usize, height: usize, scale: f32) -> Self
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.
Sourcepub fn data_mut(&mut self) -> &mut [f32]
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.
Sourcepub fn get_mut(&mut self, x: usize, z: usize) -> &mut f32
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.
Sourcepub fn set(&mut self, x: usize, z: usize, val: f32)
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.
Sourcepub fn get_clamped(&self, x: i32, z: i32) -> f32
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.
Sourcepub fn get_height_at(&self, world_x: f32, world_z: f32) -> f32
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.
Sourcepub fn get_normal_at(&self, world_x: f32, world_z: f32) -> [f32; 3]
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.
Sourcepub fn normal_at_grid(&self, x: usize, z: usize) -> [f32; 3]
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.
Sourcepub fn normals_grid(&self) -> &[[f32; 3]]
pub fn normals_grid(&self) -> &[[f32; 3]]
Lazily populated per-grid-cell normal table; row-major, length
width * height.
Sourcepub fn lakes(&self) -> &[Lake]
pub fn lakes(&self) -> &[Lake]
Lakes detected by the most recent hydraulic erosion pass. Empty until
HydraulicErosion::erode populates
it.
Sourcepub fn world_width(&self) -> f32
pub fn world_width(&self) -> f32
World-space width of the heightmap.
Sourcepub fn world_depth(&self) -> f32
pub fn world_depth(&self) -> f32
World-space depth of the heightmap.