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

Container and processor for spatial ScatterItem collections. More...

#include <scatter_field.hpp>

Inheritance diagram for hmap::ScatterField:
Collaboration diagram for hmap::ScatterField:

Public Member Functions

 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.
virtual std::string to_string () const
 Returns a multi-line formatted summary string of the scatter field.

Protected Attributes

std::vector< ScatterItem > items = {}
 List of scatter item instances.

Detailed Description

Container and processor for spatial ScatterItem collections.

Constructor & Destructor Documentation

◆ ScatterField() [1/4]

hmap::ScatterField::ScatterField ( )
default

Default constructor initializing an empty field.

◆ ScatterField() [2/4]

hmap::ScatterField::ScatterField ( const std::vector< ScatterItem > & items)

Constructs a ScatterField from a vector of items.

Parameters
itemsVector of ScatterItem instances.

◆ ScatterField() [3/4]

hmap::ScatterField::ScatterField ( std::vector< ScatterItem > && items)
noexcept

Move-constructs a ScatterField from a vector of items.

Parameters
itemsRvalue vector of ScatterItem instances.

◆ ScatterField() [4/4]

hmap::ScatterField::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.

If a point's value v is non-zero, it is used as the item's radius; otherwise default_radius is assigned.

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

◆ ~ScatterField()

virtual hmap::ScatterField::~ScatterField ( )
virtualdefault

Member Function Documentation

◆ at() [1/2]

ScatterItem & hmap::ScatterField::at ( size_t index)

◆ at() [2/2]

const ScatterItem & hmap::ScatterField::at ( size_t index) const

◆ back() [1/2]

ScatterItem & hmap::ScatterField::back ( )

◆ back() [2/2]

const ScatterItem & hmap::ScatterField::back ( ) const

◆ begin() [1/2]

auto hmap::ScatterField::begin ( )
inlinenoexcept

◆ begin() [2/2]

auto hmap::ScatterField::begin ( ) const
inlinenoexcept

◆ cbegin()

auto hmap::ScatterField::cbegin ( ) const
inlinenoexcept

◆ capacity()

size_t hmap::ScatterField::capacity ( ) const
inlinenoexcept

◆ clear()

void hmap::ScatterField::clear ( )
inlinenoexcept

◆ data() [1/2]

ScatterItem * hmap::ScatterField::data ( )
inlinenoexcept

◆ data() [2/2]

const ScatterItem * hmap::ScatterField::data ( ) const
inlinenoexcept

◆ emplace_back()

template<typename... Args>
ScatterItem & hmap::ScatterField::emplace_back ( Args &&... args)
inline

◆ empty()

bool hmap::ScatterField::empty ( ) const
inlinenoexcept

◆ end() [1/2]

auto hmap::ScatterField::end ( )
inlinenoexcept

◆ end() [2/2]

auto hmap::ScatterField::end ( ) const
inlinenoexcept

◆ cend()

auto hmap::ScatterField::cend ( ) const
inlinenoexcept

◆ front() [1/2]

ScatterItem & hmap::ScatterField::front ( )

◆ front() [2/2]

const ScatterItem & hmap::ScatterField::front ( ) const

◆ operator[]() [1/2]

ScatterItem & hmap::ScatterField::operator[] ( size_t index)
inline

◆ operator[]() [2/2]

const ScatterItem & hmap::ScatterField::operator[] ( size_t index) const
inline

◆ push_back() [1/2]

void hmap::ScatterField::push_back ( const ScatterItem & item)
inline

◆ push_back() [2/2]

void hmap::ScatterField::push_back ( ScatterItem && item)
inline

◆ reserve()

void hmap::ScatterField::reserve ( size_t new_cap)
inline

◆ size()

size_t hmap::ScatterField::size ( ) const
inlinenoexcept

◆ densify()

void hmap::ScatterField::densify ( float default_radius = HMAP_DEFAULT_SCATTER_RADIUS)

Densifies the scatter field by adding Voronoi vertices (circumcenters of Delaunay triangles).

Parameters
default_radiusDefault radius assigned to newly added items.

◆ filter_by_class()

ScatterField hmap::ScatterField::filter_by_class ( uint32_t class_id) const

Returns a new ScatterField containing only items matching the given class_id.

Parameters
class_idClass identifier to filter by.
Returns
ScatterField Filtered field.

◆ get_bbox()

BBox2D hmap::ScatterField::get_bbox ( ) const

Computes the 2D bounding box enclosing all item positions.

Returns
glm::vec4 Bounding box as {xmin, xmax, ymin, ymax}.

◆ get_class_ids()

std::vector< uint32_t > hmap::ScatterField::get_class_ids ( ) const

Returns a sorted list of unique class identifiers present in the field.

Returns
std::vector<uint32_t> Unique class IDs.

◆ get_radius()

std::vector< float > hmap::ScatterField::get_radius ( ) const

Extracts the characteristic radius of all items.

Returns
std::vector<float> Vector of item radii.

◆ get_x()

std::vector< float > hmap::ScatterField::get_x ( ) const

Extracts the x-coordinate of all items.

Returns
std::vector<float> Vector of x-coordinates.

◆ get_y()

std::vector< float > hmap::ScatterField::get_y ( ) const

Extracts the y-coordinate of all items.

Returns
std::vector<float> Vector of y-coordinates.

◆ get_z()

std::vector< float > hmap::ScatterField::get_z ( ) const

Extracts the z-coordinate (elevation) of all items.

Returns
std::vector<float> Vector of z-coordinates.

◆ perturb_positions()

void hmap::ScatterField::perturb_positions ( float dx,
float dy = 0.0f,
uint32_t seed = 0 )

Slightly perturbs the 2D (x, y) coordinates of scatter items.

Parameters
dxMaximum displacement along the x-axis.
dyMaximum displacement along the y-axis (defaults to 0.0).
seedRandom seed for reproducibility.

◆ pack_interstitial()

void hmap::ScatterField::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.

Parameters
countTarget number of items to pack.
densityOptional 2D spatial probability density array for candidate sampling.
r_minMinimum interstitial item radius.
r_maxMaximum interstitial item radius.
class_idClass identifier for the packed items.
seedRandom seed for candidate sampling.
bboxDomain bounding box {xmin, xmax, ymin, ymax}.

◆ prune_collisions() [1/2]

void hmap::ScatterField::prune_collisions ( )

Resolves overlapping items by discarding smaller items in collision.

◆ prune_collisions() [2/2]

void hmap::ScatterField::prune_collisions ( const ScatterField & other)

Prunes items in this field that overlap with (fall within the influence radius of) items in another field.

The other field remains constant and unmodified. For each item in this field, if its distance to any item in other is strictly less than the sum of their radii, it is pruned.

Parameters
otherConst reference to the reference ScatterField (e.g. rocks).

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ prune_density()

void hmap::ScatterField::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.

Parameters
density_mask2D array defining the spatial density field.
target_ratioApproximate target fraction of items to retain in [0, 1].
seedRandom seed for reproducible pruning.
bboxBounding box defining the domain of the density mask.

◆ regularize_positions()

void hmap::ScatterField::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.

Uses PointSampler's relaxation algorithm to reduce clustering and obtain a more uniform or blue-noise-like spatial distribution.

Parameters
k_neighborsNumber of nearest neighbors to consider for repulsion.
step_sizeStep size per relaxation iteration.
iterationsNumber of relaxation iterations.

◆ reinforce_class_clusters()

void hmap::ScatterField::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.

Parameters
iterationsNumber of smoothing/reinforcement iterations.
k_neighborsNumber of spatial nearest neighbors to query.
include_selfIf true, considers the item's own current class in the majority vote.

◆ resolve_collisions() [1/2]

void hmap::ScatterField::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.

Preserves item count and overall distribution while moving items until overlap is resolved or max iterations are reached.

Parameters
iterationsMaximum number of relaxation iterations.
toleranceCollision tolerance ratio in [0, 1) where 0 means no overlap allowed.
step_sizeRelaxation step size / movement rate.
triangulation_substepFrequency (in iterations) to recompute the Delaunay triangulation.

◆ resolve_collisions() [2/2]

void hmap::ScatterField::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.

The other field remains constant and unmodified.

Parameters
otherConst reference to the reference ScatterField.
iterationsMaximum number of relaxation iterations.
toleranceCollision tolerance ratio in [0, 1).
step_sizeRelaxation step size.
triangulation_substepFrequency (in iterations) to recompute the Delaunay triangulation.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ set_elevation_from_terrain()

void hmap::ScatterField::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.

Parameters
elevationTerrain heightmap array.
bboxBounding box defining the domain of the elevation array.

◆ shuffle_classes()

void hmap::ScatterField::shuffle_classes ( float ratio = 0.1f,
size_t k_neighbors = 4,
uint32_t seed = 0 )

Randomly shuffles class identifiers between neighboring items.

Parameters
ratioFraction of items to attempt class shuffling on in [0, 1].
k_neighborsNumber of nearest spatial neighbors to consider.
seedRandom seed for reproducibility.

◆ to_cloud()

Cloud hmap::ScatterField::to_cloud ( ) const

Converts the field into a Cloud of 2D points (x, y) with value set to radius.

Returns
Cloud Point cloud representation.

◆ to_density_map()

Array hmap::ScatterField::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).

Parameters
shapeDimensions of the output density map {nx, ny}.
sigmaGaussian kernel standard deviation (bandwidth) in world/domain coordinates. If <= 0, automatically derived.
class_idOptional class identifier to compute density for a specific class only.
weighted_by_areaIf true, weights density by footprint area ($\pi r^2$).
regionTile region defining the domain and overlap layout.
commCommunicator for tile boundary synchronization.
Returns
Array 2D continuous density map array.

◆ to_heightmap()

Array hmap::ScatterField::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.

Parameters
shapeDimensions of the output heightmap {nx, ny}.
shape_typeObject geometry shape (default SCATTER_SHAPE_DISK).
height_radius_ratioRatio of object maximum height relative to its radius (default 1.0f).
class_idOptional class identifier to filter items.
p_rock_mapOptional output array for the rock map with normalized amplitude in [0, 1].
seedRandom seed for procedural shape variation (e.g. polygon).
bboxDomain bounding box {xmin, xmax, ymin, ymax}.
Returns
Array 2D heightmap array containing accumulated elevations.

◆ to_csv()

void hmap::ScatterField::to_csv ( const std::string & fname) const

Exports the items to a CSV file (x, y, z, class_id, radius).

Parameters
fnameOutput file path.

◆ to_img_8bit()

std::vector< uint8_t > hmap::ScatterField::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.

Parameters
shapeImage dimensions {width, height}.
backgroundOptional background terrain array.
bboxBounding box {xmin, xmax, ymin, ymax} of the domain.
flip_yIf true, row 0 is top (screen/image space). If false, row 0 is bottom (Cartesian domain space).
Returns
std::vector<uint8_t> Interleaved RGB 8-bit image data (size = width * height * 3).

◆ to_ply()

void hmap::ScatterField::to_ply ( const std::string & fname) const

Exports the items to a PLY file with radius and class as custom parameters.

Parameters
fnameOutput file path.

◆ to_png()

void hmap::ScatterField::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.

Parameters
fnameOutput PNG file path.
shapeImage dimensions {width, height}.
backgroundOptional background terrain array.
bboxBounding box {xmin, xmax, ymin, ymax} of the domain.

◆ to_string()

std::string hmap::ScatterField::to_string ( ) const
virtual

Returns a multi-line formatted summary string of the scatter field.

Returns
std::string Pretty-printed summary.

Reimplemented in hmap::Forest, and hmap::RockField.

Member Data Documentation

◆ items

std::vector<ScatterItem> hmap::ScatterField::items = {}
protected

List of scatter item instances.


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