HeightMap

Struct HeightMap 

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

A struct representing a height map.

Implementations§

Source§

impl HeightMap

Source

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

Returns a new height map with the given width and height. Initially, all the values of the height map are 0.0.

§Panics

If the width or the height is 0.

Source

pub fn new_with_values(width: usize, height: usize, values: &[f32]) -> Self

Returns a new height map with the given width and height, and a set of values.

§Panics
  • If the width or the height is 0.
  • If the length of values is not width * height.
Source

pub fn width(&self) -> usize

Returns the width of the height map.

Source

pub fn height(&self) -> usize

Returns the height of the height map.

Source

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

Returns the values of the height map.

Source

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

Returns the values of the height map.

Source

pub fn value(&self, position: UPosition) -> f32

Returns the value of the height map at the given position.

§Panics

If the position is outside the range of the height map.

Source

pub fn set_value(&mut self, position: UPosition, value: f32)

Sets the value of the height map at the given position.

§Panics

If the position is outside the range of the height map.

Source

pub fn interpolated_value(&self, position: FPosition) -> f32

Interpolates the value of the height map at the given position.

§Panics

If the position is outside the range of the height map.

Source

pub fn slope(&self, position: UPosition) -> f32

Calculates the slope at the given position.

§Panics

If the position is outside the range of the height map.

Source

pub fn normal(&self, position: FPosition, water_level: f32) -> [f32; 3]

Calculates the normal at the given position.

§Panics

If the position is outside the range of the height map.

Source

pub fn count_cells(&self, min: f32, max: f32) -> usize

Returns the number of cells that have a height between min and max, inclusive.

Source

pub fn has_land_on_border(&self, water_level: f32) -> bool

Returns whether there is any land along the edge of the height map. A result of false implies that the map is an island.

Source

pub fn min_max(&self) -> MinMax

Returns the lowest and highest height value in the height map.

Source

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

Clamps the values in the height map to be between min and max, inclusive.

§Panics

If max > min.

Source

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

Normalizes the values in the height map by scaling them proportionally such that the map’s current smallest value will be set to min, and its largest value will be set to max, and all values in-between will be the same as they were, relative to these new end points.

§Panics

If max > min.

§Examples
let mut hm = HeightMap::new_with_values(2, 5,
    &[-25.0, -15.0, -10.0, -5.0, 0.0, 10.0, 20.0, 30.0, 40.0, 50.0]);
hm.normalize(-25.0, 20.0);
assert_eq!(hm.values(), [
    -25.0, -19.0, -16.0, -13.0, -10.0, -4.0, 2.0, 8.0, 14.0, 20.0,
]);
Source

pub fn clear(&mut self)

Resets all the values in the height map to 0.0.

Source

pub fn lerp(&self, other: &Self, coefficient: f32) -> Self

Linearly interpolate two height maps together.

Source

pub fn add_hill(&mut self, position: FPosition, radius: f32, height: f32)

Adds a hill (a half spheroid) at the given position, with a radius and a height. If height == radius or -radius, the hill will be a half-sphere.

Source

pub fn dig_hill(&mut self, position: FPosition, radius: f32, height: f32)

Takes the highest value (if height > 0) or the lowest (if height < 0) between the map and the hill. Its main goal is to carve things into maps (like rivers) by digging hills along a curve.

Source

pub fn dig_bezier( &mut self, positions: [UPosition; 4], start_radius: f32, start_depth: f32, end_radius: f32, end_depth: f32, )

Carves a path along a cubic Bezier curve using the dig_hill method. Could be used for generating roads, rivers, etc. Both radius and depth can vary linearly along the path. The four positions are the 4 Bezier control points.

Source

pub fn rain_erosion<A: RandomAlgorithm>( &mut self, drops: u32, erosion_coefficient: f32, aggregation_coefficient: f32, random: &mut Random<A>, )

Simulates the effect of rain drops on the terrain, resulting in erosion patterns.

§Parameters
  • drops - The number of rain drops to simulate. Should be at least width * height.
  • erosion_coefficient - The amount of ground eroded on the drop’s path.
  • aggregation_coefficient - The amount of ground deposited when the drops stops to flow.
  • random - The random number generator to use.
Source

pub fn kernel_transform( &mut self, cells: &[NeighborCell], min_level: f32, max_level: f32, )

Apply a generic transformation on the height map, so that each resulting cell value is the weighted sum of several neighbour cells. This can be used to, e.g. smooth/sharpen the map.

§Examples

Do simple horizontal smoothing with direct neighbor cells.

let mut hm =
    HeightMap::new_with_values(3, 3, &[3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0, 24.0, 27.0]);
let cells = [
    NeighborCell { relative_position: Position::new(-1, 0), weight: 0.33 },
    NeighborCell { relative_position: Position::new(0, 0), weight: 0.33 },
    NeighborCell { relative_position: Position::new(1, 0), weight: 0.33 },
];
hm.kernel_transform(&cells, 0.0, 100.0);
assert_eq!(hm.values(), &[4.5, 6.5, 7.75, 13.5, 15.5, 16.75, 22.5, 24.5, 25.75])
Source

pub fn add_voronoi<A: RandomAlgorithm>( &mut self, sites: usize, coefficients: &[f32], random: &mut Random<A>, )

Adds values from a Voronoi diagram to the height map.

Source

pub fn mid_point_displacement<A: RandomAlgorithm>( &mut self, random: &mut Random<A>, roughness: f32, )

Generates a height map with mid-point displacement.

The mid-point displacement algorithm generates a realistic fractal height map using the diamond-square (aka random midpoint displacement) algorithm.

The roughness range should be comprised between 0.4 and 0.6.

§Panics

If the width or the height is 0.

Source

pub fn add_fbm<A: NoiseAlgorithm>( &mut self, noise: &mut Noise<A>, octaves: f32, coordinates: FbmCoordinateParameters, delta: f32, scale: f32, )

Add an FBM to the height map.

The noise value for map cell (x, y) is (x + add_x) * mul_x / width and (y + add_y) * mul_y / height, respectively. Those values allow you to scale and translate the noise function over the height map.

§Panics

If the noise provided isn’t 2D.

Source

pub fn scale_fbm<A: NoiseAlgorithm>( &mut self, noise: &mut Noise<A>, coordinates: FbmCoordinateParameters, octaves: f32, delta: f32, scale: f32, )

Scale the map by an FBM.

The noise coordinate for map cell (x, y) is (x + add_x) * mul_x / width and (y + add_y) * mul_y / height, respectively. Those values allow you to scale and translate the noise function over the height map.

The value multiplied with the height map is delta + noise * scale.

§Panics

If the noise generator provided isn’t 2D.

Trait Implementations§

Source§

impl<'a, 'b> Add<&'a HeightMap> for &'b HeightMap

Source§

type Output = HeightMap

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &HeightMap) -> Self::Output

Performs the + operation. Read more
Source§

impl<'a> Add<&'a HeightMap> for HeightMap

Source§

type Output = HeightMap

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &HeightMap) -> Self::Output

Performs the + operation. Read more
Source§

impl<'a> Add<HeightMap> for &'a HeightMap

Source§

type Output = HeightMap

The resulting type after applying the + operator.
Source§

fn add(self, rhs: HeightMap) -> Self::Output

Performs the + operation. Read more
Source§

impl Add for HeightMap

Source§

type Output = HeightMap

The resulting type after applying the + operator.
Source§

fn add(self, rhs: HeightMap) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign<f32> for HeightMap

Source§

fn add_assign(&mut self, rhs: f32)

Performs the += operation. Read more
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
Source§

impl<'a, 'b> Mul<&'a HeightMap> for &'b HeightMap

Source§

type Output = HeightMap

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &HeightMap) -> Self::Output

Performs the * operation. Read more
Source§

impl<'a> Mul<&'a HeightMap> for HeightMap

Source§

type Output = HeightMap

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &HeightMap) -> Self::Output

Performs the * operation. Read more
Source§

impl<'a> Mul<HeightMap> for &'a HeightMap

Source§

type Output = HeightMap

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: HeightMap) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul for HeightMap

Source§

type Output = HeightMap

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: HeightMap) -> Self::Output

Performs the * operation. Read more
Source§

impl MulAssign<f32> for HeightMap

Source§

fn mul_assign(&mut self, rhs: f32)

Performs the *= operation. 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.