Skip to main content

Heightfield

Struct Heightfield 

Source
pub struct Heightfield {
    pub origin: Vec3,
    pub cell: f64,
    pub nx: usize,
    pub ny: usize,
    pub heights: Vec<f32>,
}
Expand description

A regular grid of terrain heights, sampled bilinearly.

Node (ix, iy) sits at world (origin.x + ix·cell, origin.y + iy·cell) with surface height origin.z + heights[iy·nx + ix]. Between nodes the surface is the bilinear patch through the four surrounding nodes; outside the grid the query is clamped to the border, so the terrain extends flatly (per-edge) to infinity rather than falling away to zero. That clamp is also what makes a 1×1 field an exact infinite plane.

Heights are stored as f32 deliberately: the GPU contact shader consumes this buffer verbatim, and keeping one representation means the CPU and GPU paths sample identical terrain instead of terrain that agrees to rounding. Construction is a couple of allocations, cheap enough to draw a fresh field per training iteration.

Fields§

§origin: Vec3

World position of node (0, 0); origin.z is added to every height.

§cell: f64

Grid spacing in metres, same in x and y. Must be positive.

§nx: usize

Number of nodes along x. At least 1.

§ny: usize

Number of nodes along y. At least 1.

§heights: Vec<f32>

Row-major heights, ny rows of nx: heights[iy * nx + ix].

Implementations§

Source§

impl Heightfield

Source

pub fn new(origin: Vec3, cell: f64, nx: usize, ny: usize) -> Self

A heightfield of nx × ny nodes spaced cell apart, all at height zero above origin.

§Panics

If nx or ny is zero or cell is not a positive finite number.

Source

pub fn flat(height: f64) -> Self

The infinite flat plane at z = height: a single node whose border clamp covers the whole world.

Source

pub fn height(&self, x: f64, y: f64) -> f64

Surface height at world (x, y), bilinear between nodes, clamped to the border outside the grid.

Source

pub fn normal(&self, x: f64, y: f64) -> Vec3

Unit surface normal at world (x, y).

The analytic normal of the bilinear patch: (-∂h/∂x, -∂h/∂y, 1) normalized. Outside the grid the clamp makes the surface flat, so the normal degrades to +ẑ there, and a 1×1 field reports +ẑ everywhere — exactly the flat-plane contact normal.

Trait Implementations§

Source§

impl Clone for Heightfield

Source§

fn clone(&self) -> Heightfield

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 Heightfield

Source§

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

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Heightfield

Source§

fn eq(&self, other: &Heightfield) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Heightfield

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.