pub struct HeightField {
pub heights: Vec<Real>,
pub rows: usize,
pub cols: usize,
pub scale_x: Real,
pub scale_z: Real,
}Expand description
A grid-based height field for terrain representation.
Heights are stored in row-major order. The field spans from (0,0) to (scale_x * (cols-1), scale_z * (rows-1)) in the XZ plane.
Fields§
§heights: Vec<Real>Height values in row-major order.
rows: usizeNumber of rows (Z direction).
cols: usizeNumber of columns (X direction).
scale_x: RealSpacing in the X direction.
scale_z: RealSpacing in the Z direction.
Implementations§
Source§impl HeightField
impl HeightField
Sourcepub fn new(
heights: Vec<Real>,
rows: usize,
cols: usize,
scale_x: Real,
scale_z: Real,
) -> Self
pub fn new( heights: Vec<Real>, rows: usize, cols: usize, scale_x: Real, scale_z: Real, ) -> Self
Create a new height field.
Sourcepub fn height_at(&self, row: usize, col: usize) -> Real
pub fn height_at(&self, row: usize, col: usize) -> Real
Get the height at grid position (row, col).
Sourcepub fn from_fn(
cols: usize,
rows: usize,
cell_size: Real,
f: impl Fn(usize, usize) -> Real,
) -> Self
pub fn from_fn( cols: usize, rows: usize, cell_size: Real, f: impl Fn(usize, usize) -> Real, ) -> Self
Create a height field from a function.
The function f(col, row) returns the height at each grid point.
Sourcepub fn height_at_uv(&self, u: Real, v: Real) -> Real
pub fn height_at_uv(&self, u: Real, v: Real) -> Real
Bilinear interpolation of height at normalized coordinates (u, v).
u and v are in [0, 1], mapping to the full extent of the grid.
Sourcepub fn normal_at_grid(&self, col: usize, row: usize) -> [Real; 3]
pub fn normal_at_grid(&self, col: usize, row: usize) -> [Real; 3]
Compute the surface normal at grid point (col, row) via finite differences.
Sourcepub fn to_triangle_mesh(&self) -> (Vec<[Real; 3]>, Vec<[usize; 3]>)
pub fn to_triangle_mesh(&self) -> (Vec<[Real; 3]>, Vec<[usize; 3]>)
Tessellate the height field into a triangle mesh.
Returns (vertices, triangles) where each vertex is [x, y, z]
and each triangle is 3 vertex indices.
Sourcepub fn min_height(&self) -> Real
pub fn min_height(&self) -> Real
Return the minimum height in the field.
Sourcepub fn max_height(&self) -> Real
pub fn max_height(&self) -> Real
Return the maximum height in the field.
Sourcepub fn surface_area(&self) -> Real
pub fn surface_area(&self) -> Real
Compute the total surface area by summing triangle areas from tessellation.
Source§impl HeightField
impl HeightField
Sourcepub fn lod_downsample(&self, factor: usize) -> HeightField
pub fn lod_downsample(&self, factor: usize) -> HeightField
Generate a downsampled (LOD) version of the height field.
factor must be ≥ 1. A factor of 2 halves the resolution by averaging
2×2 blocks of cells. If the grid is too small the original is returned.
Sourcepub fn lod_pyramid(&self, levels: usize) -> Vec<HeightField>
pub fn lod_pyramid(&self, levels: usize) -> Vec<HeightField>
Generate multiple LOD levels.
Returns a Vec where index 0 is the original, index 1 is 2× downsampled, index 2 is 4× downsampled, etc., until the grid is too small.
Sourcepub fn normal_at_world(&self, x: Real, z: Real) -> [Real; 3]
pub fn normal_at_world(&self, x: Real, z: Real) -> [Real; 3]
Compute the normal at an arbitrary world-space XZ position via bilinear interpolation of the four surrounding grid normals.
Sourcepub fn serialize(&self) -> Vec<Real> ⓘ
pub fn serialize(&self) -> Vec<Real> ⓘ
Serialize the height field to a flat Vecf64` prefixed by metadata.
Format: \[rows, cols, scale_x, scale_z, h0, h1, ...\]
Sourcepub fn deserialize(data: &[Real]) -> Option<HeightField>
pub fn deserialize(data: &[Real]) -> Option<HeightField>
Deserialize a height field from a flat Vecf64(inverse ofserialize`).
Sourcepub fn compute_all_normals(&self) -> Vec<[Real; 3]>
pub fn compute_all_normals(&self) -> Vec<[Real; 3]>
Compute per-vertex normals for all grid vertices.
Returns a flat Vec<[Real; 3]> in row-major order.
Sourcepub fn height_at_world(&self, x: Real, z: Real) -> Real
pub fn height_at_world(&self, x: Real, z: Real) -> Real
Height at arbitrary world-space XZ position (bilinear interpolation).
Clamps to grid extents.
Source§impl HeightField
impl HeightField
Sourcepub fn aabb(&self) -> ([f64; 3], [f64; 3])
pub fn aabb(&self) -> ([f64; 3], [f64; 3])
Axis-aligned bounding box of this height field in world space.
Returns (min, max) where each is [x, y, z].
Sourcepub fn height_at_xz(&self, x: f64, z: f64) -> f64
pub fn height_at_xz(&self, x: f64, z: f64) -> f64
Bilinear height interpolation at world-space position (x, z).
Unlike height_at(row, col) (grid indices) and height_at_world,
this method accepts (x, z) world coordinates and returns the
interpolated height, clamping to the grid extents.
Sourcepub fn normals(&self) -> Vec<[f64; 3]>
pub fn normals(&self) -> Vec<[f64; 3]>
Per-vertex normals for the entire grid in row-major order.
Each normal is computed via finite differences over the 4-connected
neighborhood and returned as a unit vector [nx, ny, nz].
Sourcepub fn tessellate(&self) -> (Vec<[f64; 3]>, Vec<[usize; 3]>)
pub fn tessellate(&self) -> (Vec<[f64; 3]>, Vec<[usize; 3]>)
Tessellate the height field into a triangle mesh.
Returns (vertices, indices) where:
verticesis a list of world-space[x, y, z]positions, one per grid point, in row-major order.indicesis a list of[i0, i1, i2]triangles (two per quad cell).
Sourcepub fn ray_intersect(
&self,
origin: [f64; 3],
dir: [f64; 3],
) -> Option<(f64, [f64; 3])>
pub fn ray_intersect( &self, origin: [f64; 3], dir: [f64; 3], ) -> Option<(f64, [f64; 3])>
DDA ray intersection returning (t, normal) for the first hit.
Uses grid-cell DDA traversal in the XZ plane, testing the two
triangles of each cell with Möller–Trumbore intersection.
Returns None if the ray misses the terrain or is going away from it.
The search is limited to a reasonable distance (diagonal of the AABB).
Source§impl HeightField
impl HeightField
Sourcepub fn slope_at(&self, col: usize, row: usize) -> f64
pub fn slope_at(&self, col: usize, row: usize) -> f64
Compute the slope magnitude at grid point (col, row).
Slope is defined as sqrt((dh/dx)² + (dh/dz)²) using the same
finite-difference stencil as normal_at_grid.
Sourcepub fn curvature_at(&self, col: usize, row: usize) -> f64
pub fn curvature_at(&self, col: usize, row: usize) -> f64
Compute the mean curvature at grid point (col, row).
Uses a second-order central difference approximation:
κ ≈ (d²h/dx² + d²h/dz²) / 2.
Sourcepub fn slope_map(&self) -> Vec<f64>
pub fn slope_map(&self) -> Vec<f64>
Return a flat Vecf64` of slope magnitudes in row-major order.
Sourcepub fn curvature_map(&self) -> Vec<f64>
pub fn curvature_map(&self) -> Vec<f64>
Return a flat Vecf64` of mean curvatures in row-major order.
Sourcepub fn closest_vertex(&self, q: [f64; 3]) -> ([f64; 3], usize, usize)
pub fn closest_vertex(&self, q: [f64; 3]) -> ([f64; 3], usize, usize)
Find the closest surface point to query point q by brute-force search
over all grid vertices.
Returns (closest_world_point, row, col).
Sourcepub fn resample(&self, new_cols: usize, new_rows: usize) -> HeightField
pub fn resample(&self, new_cols: usize, new_rows: usize) -> HeightField
Resample the height field to new grid dimensions by bilinear interpolation.
new_cols and new_rows must be ≥ 2. Returns a new HeightField with
the same world extents but a different resolution.
Sourcepub fn hydraulic_erode(&mut self, sediment_rate: f64, iterations: usize)
pub fn hydraulic_erode(&mut self, sediment_rate: f64, iterations: usize)
Simple hydraulic erosion step: for each cell, water flows to the
steepest-descent neighbor and carries a fraction sediment_rate of
height difference.
iterations controls how many passes are performed. For correctness
the caller should keep sediment_rate ≤ 0.5.
Sourcepub fn flow_accumulation(&self) -> Vec<f64>
pub fn flow_accumulation(&self) -> Vec<f64>
Compute a simple flow-accumulation map (watershed proxy).
Each cell accumulates 1 unit plus the total of all upstream cells that
drain into it following steepest descent. Returns a flat Vecf64`
in row-major order; larger values indicate valleys/channels.
Sourcepub fn clamp_heights(&mut self, min_h: f64, max_h: f64)
pub fn clamp_heights(&mut self, min_h: f64, max_h: f64)
Clamp all height values to the range \[min_h, max_h\].
Sourcepub fn scale_heights(&mut self, factor: f64)
pub fn scale_heights(&mut self, factor: f64)
Scale all heights by a uniform factor.
Sourcepub fn offset_heights(&mut self, offset: f64)
pub fn offset_heights(&mut self, offset: f64)
Offset all heights by a constant value.
Sourcepub fn normalize_heights(&mut self)
pub fn normalize_heights(&mut self)
Normalize heights to the range \[0, 1\]. If all heights are equal, all
become 0.
Sourcepub fn invert_heights(&mut self)
pub fn invert_heights(&mut self)
Invert heights: each height h becomes max_height - (h - min_height).
Sourcepub fn mean_height(&self) -> f64
pub fn mean_height(&self) -> f64
Compute the average height over the entire grid.
Sourcepub fn height_variance(&self) -> f64
pub fn height_variance(&self) -> f64
Compute the variance of heights.
Sourcepub fn count_peaks(&self) -> usize
pub fn count_peaks(&self) -> usize
Count the number of local maxima (peaks) in the grid.
A cell is a peak if it is strictly higher than all 4-connected neighbors.
Trait Implementations§
Source§impl Clone for HeightField
impl Clone for HeightField
Source§fn clone(&self) -> HeightField
fn clone(&self) -> HeightField
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for HeightField
impl Debug for HeightField
Source§impl Shape for HeightField
impl Shape for HeightField
Source§fn bounding_box(&self) -> Aabb
fn bounding_box(&self) -> Aabb
Source§fn support_point(&self, direction: &Vec3) -> Vec3
fn support_point(&self, direction: &Vec3) -> Vec3
Source§fn center_of_mass(&self) -> Vec3
fn center_of_mass(&self) -> Vec3
Source§fn inertia_tensor(&self, _mass: Real) -> Mat3
fn inertia_tensor(&self, _mass: Real) -> Mat3
Source§fn ray_cast(
&self,
ray_origin: &Vec3,
ray_direction: &Vec3,
max_toi: Real,
) -> Option<RayHit>
fn ray_cast( &self, ray_origin: &Vec3, ray_direction: &Vec3, max_toi: Real, ) -> Option<RayHit>
max_toi.Source§fn mass_properties(&self, density: Real) -> MassProperties
fn mass_properties(&self, density: Real) -> MassProperties
Auto Trait Implementations§
impl Freeze for HeightField
impl RefUnwindSafe for HeightField
impl Send for HeightField
impl Sync for HeightField
impl Unpin for HeightField
impl UnsafeUnpin for HeightField
impl UnwindSafe for HeightField
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.