HighMap library (C++)
Loading...
Searching...
No Matches
hmap::Forest Class Reference

Container class representing a collection of Tree instances. More...

#include <forest.hpp>

Inheritance diagram for hmap::Forest:
Collaboration diagram for hmap::Forest:

Public Member Functions

 Forest ()=default
 Default constructor initializing an empty forest.
 Forest (const std::vector< Tree > &trees)
 Constructs a Forest from a vector of trees.
 Forest (std::vector< Tree > &&trees) noexcept
 Move-constructs a Forest from a vector of trees.
 Forest (const ScatterField &field)
 Constructs a Forest from a base ScatterField.
 Forest (ScatterField &&field) noexcept
 Move-constructs a Forest from a base ScatterField.
 Forest (const Cloud &cloud, uint32_t species_id=0, float default_radius=HMAP_DEFAULT_TREE_RADIUS)
 Constructs a Forest from a Cloud of 2D points.
void prune_unviable (const std::vector< Species > &species={}, bool prune_collisions=true)
 Prunes unviable trees whose distance to their nearest neighbor is insufficient to meet their species' minimum radius requirement or pairwise collision distance.
std::string to_string () const override
 Returns a multi-line formatted summary string of the forest.
Public Member Functions inherited from hmap::ScatterField
 ScatterField ()=default
 Default constructor initializing an empty field.
 ScatterField (const std::vector< ScatterItem > &items)
 Constructs a ScatterField from a vector of items.
 ScatterField (std::vector< ScatterItem > &&items) noexcept
 Move-constructs a ScatterField from a vector of items.
 ScatterField (const Cloud &cloud, uint32_t class_id=0, float default_radius=HMAP_DEFAULT_SCATTER_RADIUS)
 Constructs a ScatterField from a Cloud of 2D points.
virtual ~ScatterField ()=default
ScatterItem & at (size_t index)
const ScatterItem & at (size_t index) const
ScatterItem & back ()
const ScatterItem & back () const
auto begin () noexcept
auto begin () const noexcept
auto cbegin () const noexcept
size_t capacity () const noexcept
void clear () noexcept
ScatterItem * data () noexcept
const ScatterItem * data () const noexcept
template<typename... Args>
ScatterItem & emplace_back (Args &&...args)
bool empty () const noexcept
auto end () noexcept
auto end () const noexcept
auto cend () const noexcept
ScatterItem & front ()
const ScatterItem & front () const
ScatterItem & operator[] (size_t index)
const ScatterItem & operator[] (size_t index) const
void push_back (const ScatterItem &item)
void push_back (ScatterItem &&item)
void reserve (size_t new_cap)
size_t size () const noexcept
void densify (float default_radius=HMAP_DEFAULT_SCATTER_RADIUS)
 Densifies the scatter field by adding Voronoi vertices (circumcenters of Delaunay triangles).
ScatterField filter_by_class (uint32_t class_id) const
 Returns a new ScatterField containing only items matching the given class_id.
BBox2D get_bbox () const
 Computes the 2D bounding box enclosing all item positions.
std::vector< uint32_t > get_class_ids () const
 Returns a sorted list of unique class identifiers present in the field.
std::vector< float > get_radius () const
 Extracts the characteristic radius of all items.
std::vector< float > get_x () const
 Extracts the x-coordinate of all items.
std::vector< float > get_y () const
 Extracts the y-coordinate of all items.
std::vector< float > get_z () const
 Extracts the z-coordinate (elevation) of all items.
void perturb_positions (float dx, float dy=0.0f, uint32_t seed=0)
 Slightly perturbs the 2D (x, y) coordinates of scatter items.
void pack_interstitial (size_t count, const Array &density={}, float r_min=5e-4f, float r_max=2e-3f, uint32_t class_id=0, uint32_t seed=0, const UvRect &uv_rect={0.f, 1.f, 0.f, 1.f})
 Packs smaller interstitial items in the voids between existing larger items without displacing existing items.
void prune_collisions ()
 Resolves overlapping items by discarding smaller items in collision.
void prune_collisions (const ScatterField &other)
 Prunes items in this field that overlap with (fall within the influence radius of) items in another field.
void prune_density (const Array &density_mask, float target_ratio=0.8f, uint32_t seed=0, const UvRect &uv_rect={0.f, 1.f, 0.f, 1.f})
 Prunes items in the field using density-based acceptance sampling modulated to reach an approximate target retention ratio.
void regularize_positions (size_t k_neighbors=8, float step_size=0.1f, size_t iterations=10)
 Relaxes/regularizes item positions using k-nearest neighbor repulsion.
void reinforce_class_clusters (size_t iterations=2, size_t k_neighbors=4, bool include_self=true)
 Reinforces spatial clustering of classes by iteratively assigning each item the dominant class among its nearest neighbors.
void resolve_collisions (size_t iterations=20, float tolerance=0.f, float step_size=2., size_t triangulation_substep=5)
 Resolves overlapping items by iteratively moving colliding items apart using Delaunay triangulation neighborhood and repulsive relaxation.
void resolve_collisions (const ScatterField &other, size_t iterations=20, float tolerance=0.f, float step_size=2.f, size_t triangulation_substep=5)
 Resolves collisions with another static ScatterField by pushing items in this field away from colliding items in the other field, using joint Delaunay triangulation neighborhood and repulsive relaxation.
void set_elevation_from_terrain (const Array &elevation, const UvRect &uv_rect={0.f, 1.f, 0.f, 1.f})
 Sets the elevation (z-coordinate) of all items by sampling a terrain heightmap.
void shuffle_classes (float ratio=0.1f, size_t k_neighbors=4, uint32_t seed=0)
 Randomly shuffles class identifiers between neighboring items.
Cloud to_cloud () const
 Converts the field into a Cloud of 2D points (x, y) with value set to radius.
Array to_density_map (glm::ivec2 shape, float sigma=0.0f, std::optional< uint32_t > class_id=std::nullopt, bool weighted_by_area=false, const UvTile &region=UvTile::dummy(), ICommunicator &comm=SingleArrayComm::instance()) const
 Exports the local density map of the field as a 2D Array using 2D Kernel Density Estimation (Gaussian splatting / KDE).
Array to_heightmap (glm::ivec2 shape, ScatterShape shape_type=SCATTER_SHAPE_DISK, float height_radius_ratio=1.0f, std::optional< uint32_t > class_id=std::nullopt, Array *p_rock_map=nullptr, uint32_t seed=0, const UvRect &uv_rect=UvRect::unit()) const
 Converts the scatter field into a heightmap by representing each scattered object as a local elevation increase on GPU.
void to_csv (const std::string &fname) const
 Exports the items to a CSV file (x, y, z, class_id, radius).
std::vector< uint8_t > to_img_8bit (glm::ivec2 shape, const Array &background={}, const UvRect &uv_rect=UvRect::unit(), bool flip_y=false) const
 Renders the scatter field to an 8-bit RGB image buffer.
void to_ply (const std::string &fname) const
 Exports the items to a PLY file with radius and class as custom parameters.
void to_png (const std::string &fname, glm::ivec2 shape, const Array &background={}, const UvRect &uv_rect=UvRect::unit()) const
 Exports a visual representation of the scatter field as a PNG image.

Additional Inherited Members

Protected Attributes inherited from hmap::ScatterField
std::vector< ScatterItem > items = {}
 List of scatter item instances.

Detailed Description

Container class representing a collection of Tree instances.

Constructor & Destructor Documentation

◆ Forest() [1/6]

hmap::Forest::Forest ( )
default

Default constructor initializing an empty forest.

◆ Forest() [2/6]

hmap::Forest::Forest ( const std::vector< Tree > & trees)

Constructs a Forest from a vector of trees.

Parameters
treesVector of Tree instances.

◆ Forest() [3/6]

hmap::Forest::Forest ( std::vector< Tree > && trees)
noexcept

Move-constructs a Forest from a vector of trees.

Parameters
treesRvalue vector of Tree instances.

◆ Forest() [4/6]

hmap::Forest::Forest ( const ScatterField & field)

Constructs a Forest from a base ScatterField.

Parameters
fieldBase scatter field.

◆ Forest() [5/6]

hmap::Forest::Forest ( ScatterField && field)
noexcept

Move-constructs a Forest from a base ScatterField.

Parameters
fieldRvalue base scatter field.

◆ Forest() [6/6]

hmap::Forest::Forest ( const Cloud & cloud,
uint32_t species_id = 0,
float default_radius = HMAP_DEFAULT_TREE_RADIUS )

Constructs a Forest from a Cloud of 2D points.

Parameters
cloudInput point cloud.
species_idSpecies identifier assigned to all imported trees.
default_radiusDefault radius assigned if point value is 0.

Member Function Documentation

◆ prune_unviable()

void hmap::Forest::prune_unviable ( const std::vector< Species > & species = {},
bool prune_collisions = true )

Prunes unviable trees whose distance to their nearest neighbor is insufficient to meet their species' minimum radius requirement or pairwise collision distance.

Parameters
speciesVector of species definitions (with radius_min, radius_max, competition_factor).
prune_collisionsIf true, also resolves overlapping tree crowns by discarding the smaller tree.

◆ to_string()

std::string hmap::Forest::to_string ( ) const
overridevirtual

Returns a multi-line formatted summary string of the forest.

Returns
std::string Pretty-printed summary.

Reimplemented from hmap::ScatterField.


The documentation for this class was generated from the following files: