HighMap library (C++)
Loading...
Searching...
No Matches
hydrology.hpp
Go to the documentation of this file.
1/* Copyright (c) 2023 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4
23#pragma once
24#include <cstdint>
25#include <functional>
26#include <limits>
27
28#include "highmap/array.hpp"
32
33namespace hmap
34{
35
55Array basin_id(const Array &z,
57 bool remove_lakes = true);
58
84Array carve_riverbed(const Array &z,
85 const Array &z_river,
86 float talus_riverbank = 0.01f,
87 bool smooth_river_bottom = true,
88 float merging_distance = 8.f,
89 std::uint32_t seed = 0,
90 float riverbank_noise_ratio = 0.f,
91 const Array *p_noise_x = nullptr,
92 const Array *p_noise_y = nullptr);
93
109Array d8_compute_ndip(const Array &d8);
110
122void find_flow_apex(const Array &z, std::vector<int> &is, std::vector<int> &js);
123
138void find_flow_sinks(const Array &z,
139 std::vector<int> &is,
140 std::vector<int> &js);
141
151std::vector<glm::ivec2> find_flow_sinks(const Array &z);
152
162std::vector<glm::ivec2> find_flow_sinks_border(const Array &z);
163
180Array flooding_uniform_level(const Array &z, float zref);
181
202Array flooding_from_boundaries(const Array &z,
203 float zref,
204 bool from_east = true,
205 bool from_west = true,
206 bool from_north = true,
207 bool from_south = true);
208
227Array flooding_from_point(const Array &z,
228 int i,
229 int j,
230 float depth_min = std::numeric_limits<float>::max());
231
249Array flooding_from_point(const Array &z,
250 const std::vector<int> &i,
251 const std::vector<int> &j,
252 float depth_min = std::numeric_limits<float>::max());
253
281Array flooding_lake_system(const Array &z, float surface_threshold = 0);
282
305Array flow_accumulation_d8(const Array &z);
306
343Array flow_accumulation_stochastic(const Array &z,
344 int n_samples = 1 << 19,
345 std::uint32_t seed = 0,
346 const Array *p_source = nullptr,
347 const Array *p_decay = nullptr);
348
376Array flow_accumulation_dinf(const Array &z, float talus_ref);
377
410Array flow_accumulation_dinf_perturbed(const Array &z,
411 float talus_ref,
412 int nsamples,
413 glm::vec2 kw,
414 std::uint32_t seed,
415 float amp,
416 const Array *p_perturb_scaling = nullptr,
417 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
418
435Array flow_direction_d8(const Array &z);
436
454std::vector<Array> flow_direction_dinf(const Array &z, float talus_ref);
455
456std::vector<float> flow_direction_dinf_flat(const Array &z, float talus_ref);
457
462Array flow_direction_dinf_angle(const Array &z, float talus_ref);
463
464Array flow_fixing(const Array &z,
465 float riverbed_talus = 0.f,
466 int iterations = 5,
467 int prefilter_ir = 8,
468 bool carve_riverbed = true,
469 float merging_distance = 8.f, // pixels
470 const Array *p_noise_r = nullptr);
471
506 const Array &z,
508 float riverbed_talus = 1e-4f,
509 int iterations = 3,
510 bool carve_riverbed = false,
511 float talus_riverbank = 0.01f,
512 float merging_distance = 8.f,
513 std::uint32_t seed = 0,
514 float noise_strength = 0.f,
515 const Array *p_noise_x = nullptr,
516 const Array *p_noise_y = nullptr);
517
555Array flow_fixing_mst(
556 const Array &z,
557 float riverbed_talus = 0.f,
558 float elevation_ratio = 0.95f,
559 float distance_exponent = 2.f,
560 float upward_penalization = 50.f,
561 float valley_affinity = 0.5f,
562 int prefilter_ir = 8,
563 float minimum_depth = 1e-4f,
564 bool carve_riverbed = true,
565 float merging_distance = 8.f,
567 float radial_profile_parameter = 2.f,
568 const Array *p_noise_r = nullptr);
569
603Path flow_stream(const Array &z,
604 const glm::ivec2 ij_start,
605 const float elevation_ratio = 0.5f,
606 const float distance_exponent = 2.f,
607 const float upward_penalization = 100.f);
608
655Array generate_riverbed(const Path &path,
656 glm::ivec2 shape,
657 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
658 bool bezier_smoothing = false,
659 float depth_start = 0.01f,
660 float depth_end = 1.f,
661 float slope_start = 64.f,
662 float slope_end = 32.f,
663 float shape_exponent_start = 1.f,
664 float shape_exponent_end = 10.f,
665 float k_smoothing = 0.5f,
666 int post_filter_ir = 0,
667 Array *p_noise_x = nullptr,
668 Array *p_noise_y = nullptr,
669 Array *p_noise_r = nullptr);
670
681Array merge_water_depths(const Array &depth1,
682 const Array &depth2,
683 float k_smooth = 0.f);
684
710Array snow_melting_map(const Array &z,
711 float melt_start_elevation = 0.f,
712 float melt_end_elevation = 0.5f,
713 float elevation_strength = 1.f,
714 float elevation_exp = 1.f,
715 float sun_azimuth = 0.f,
716 float sun_zenith = 60.f,
717 float aspect_strength = 0.f,
718 float slope_exp = 1.f,
719 float slope_strength = 0.f);
720
757Array water_depth_from_mask(const Array &z,
758 const Array &mask,
759 float mask_threshold = 0.f,
760 int iterations_max = 10000,
761 float tolerance = 1e-2f);
762
787void water_depth_dry_out(Array &water_depth,
788 float dry_out_ratio = 0.5f,
789 const Array *p_mask = nullptr,
790 float depth_max = std::numeric_limits<float>::max());
791
827Array water_depth_increase(const Array &water_depth,
828 const Array &z,
829 float additional_depth);
830
865Array water_depth_increase_with_flooding(const Array &water_depth,
866 const Array &z,
867 float additional_depth);
890Array water_frontier_curvature(const Array &water_depth,
891 int prefilter_ir,
892 bool extend_values_from_interface = false);
893
910Array water_mask(const Array &water_depth);
911
934Array water_mask(const Array &water_depth,
935 const Array &z,
936 float additional_depth);
937
938} // namespace hmap
939
940namespace hmap::gpu
941{
942
966Array coastal_fetch(const Array &z,
967 int ndirections,
968 const Array *p_compute_mask = nullptr);
969
1000Array coastal_fetch_directional(const Array &z,
1001 float angle,
1002 float directional_exp,
1003 int ndirections,
1004 const Array *p_compute_mask = nullptr);
1005
1019Array flow_accumulation_from_velocity_field(const Array &u,
1020 const Array &v,
1021 int iterations);
1022
1027Array flow_accumulation_stochastic(const Array &z,
1028 int n_samples = 1 << 19,
1029 std::uint32_t seed = 0,
1030 const Array *p_source = nullptr,
1031 const Array *p_decay = nullptr);
1032
1034Array flow_direction_d8(const Array &z);
1035
1062Array flow_simulation(const Array &z,
1063 float water_depth,
1064 const Array &depth_map,
1065 int iterations,
1066 float dt = 0.5f,
1067 bool flux_diffusion = true,
1068 float flux_diffusion_strength = 0.01f,
1069 float dry_out_ratio = 0.f,
1070 const Array *p_rain_map = nullptr,
1071 float rain_rate = 0.f,
1072 float evap_rate = 0.f,
1073 bool outflow_boundaries = false,
1074 Array *p_vel_u = nullptr,
1075 Array *p_vel_v = nullptr);
1076
1095Array flow_simulation_viscous(const Array &z,
1096 float water_depth,
1097 const Array &depth_map,
1098 int iterations,
1099 float dt = 0.5f,
1100 float dry_out_ratio = 0.f,
1101 float viscosity = 1.f,
1102 float power = 2.5f,
1103 float evap_rate = 0.f,
1104 bool outflow_boundaries = false);
1105
1107Array generate_riverbed(const Path &path,
1108 glm::ivec2 shape,
1109 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
1110 bool bezier_smoothing = false,
1111 float depth_start = 0.01f,
1112 float depth_end = 1.f,
1113 float slope_start = 64.f,
1114 float slope_end = 32.f,
1115 float shape_exponent_start = 1.f,
1116 float shape_exponent_end = 10.f,
1117 float k_smoothing = 0.5f,
1118 int post_filter_ir = 0,
1119 Array *p_noise_x = nullptr,
1120 Array *p_noise_y = nullptr,
1121 Array *p_noise_r = nullptr);
1122
1155Array snow_simulation(const Array &z,
1156 float snow_depth,
1157 const Array &fall_map,
1158 const Array &melting_map,
1159 const Array &talus,
1160 int iterations,
1161 float dt = 0.5f,
1162 float fall_iterations_ratio = 1.f,
1163 float k_snow = 0.5f,
1164 float k_visc = 0.1f,
1165 float k_melt_factor = 0.8f,
1166 float k_depth_ratio = 1.f,
1167 float k_depth_slope_ratio = 1.f,
1168 bool post_filter = true,
1169 float thermal_talus_ratio = 0.2f);
1170
1184void water_depth_filter(Array &depth,
1185 const Array &z,
1186 int ir,
1187 const Array *p_water_mask = nullptr,
1188 bool smooth_contour = false,
1189 float transition_ratio = 0.1f);
1190
1192Array water_frontier_curvature(const Array &water_depth,
1193 int prefilter_ir,
1194 bool extend_values_from_interface = false);
1195
1197Array water_depth_from_mask(const Array &z,
1198 const Array &mask,
1199 float mask_threshold = 0.f,
1200 int iterations_max = 10000,
1201 float tolerance = 1e-2f);
1202
1203} // namespace hmap::gpu
Declaration of the Array class for 2D floating-point arrays with various mathematical operations and ...
Header file for DrainageBasinCellBased class.
Definition blending.hpp:225
Array generate_riverbed(const Path &path, glm::ivec2 shape, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f}, bool bezier_smoothing=false, float depth_start=0.01f, float depth_end=1.f, float slope_start=64.f, float slope_end=32.f, float shape_exponent_start=1.f, float shape_exponent_end=10.f, float k_smoothing=0.5f, int post_filter_ir=0, Array *p_noise_x=nullptr, Array *p_noise_y=nullptr, Array *p_noise_r=nullptr)
See hmap::generate_riverbed.
Definition generate_riverbed_gpu.cpp:20
Array flow_accumulation_from_velocity_field(const Array &u, const Array &v, int iterations)
Approximates flow accumulation from a 2D velocity field.
Definition flow_accumulation_from_velocity_field.cpp:15
Array water_frontier_curvature(const Array &water_depth, int prefilter_ir, bool extend_values_from_interface=false)
See hmap::water_frontier_curvature.
Definition water_depth.cpp:436
void water_depth_filter(Array &depth, const Array &z, int ir, const Array *p_water_mask=nullptr, bool smooth_contour=false, float transition_ratio=0.1f)
Filters water depth values using elevation data and a given radius.
Definition water_depth.cpp:367
Array water_depth_from_mask(const Array &z, const Array &mask, float mask_threshold=0.f, int iterations_max=10000, float tolerance=1e-2f)
See hmap::water_depth_from_mask.
Definition water_depth.cpp:411
Array coastal_fetch(const Array &z, int ndirections, const Array *p_compute_mask=nullptr)
Computes the omnidirectional coastal fetch for each cell.
Definition coastal_fetch.cpp:16
Array snow_simulation(const Array &z, float snow_depth, const Array &fall_map, const Array &melting_map, const Array &talus, int iterations, float dt=0.5f, float fall_iterations_ratio=1.f, float k_snow=0.5f, float k_visc=0.1f, float k_melt_factor=0.8f, float k_depth_ratio=1.f, float k_depth_slope_ratio=1.f, bool post_filter=true, float thermal_talus_ratio=0.2f)
Simulate snow accumulation and redistribution over a terrain.
Definition snow_simulation.cpp:85
Array coastal_fetch_directional(const Array &z, float angle, float directional_exp, int ndirections, const Array *p_compute_mask=nullptr)
Computes directional coastal fetch weighted by alignment with a prevailing wind or wave direction.
Definition coastal_fetch.cpp:45
Array flow_accumulation_stochastic(const Array &z, int n_samples=1<< 19, std::uint32_t seed=0, const Array *p_source=nullptr, const Array *p_decay=nullptr)
GPU (OpenCL) variant of hmap::flow_accumulation_stochastic — see the CPU declaration for the algorith...
Definition flow_accumulation_stochastic_gpu.cpp:18
Array flow_simulation(const Array &z, float water_depth, const Array &depth_map, int iterations, float dt=0.5f, bool flux_diffusion=true, float flux_diffusion_strength=0.01f, float dry_out_ratio=0.f, const Array *p_rain_map=nullptr, float rain_rate=0.f, float evap_rate=0.f, bool outflow_boundaries=false, Array *p_vel_u=nullptr, Array *p_vel_v=nullptr)
GPU hydraulic flow simulation using a virtual-pipes model; simulates shallow-water transport over a h...
Definition flow_simulation.cpp:27
Array flow_direction_d8(const Array &z)
See hmap::flow_direction_d8.
Definition flow_accumulation_d8_gpu.cpp:15
Array flow_simulation_viscous(const Array &z, float water_depth, const Array &depth_map, int iterations, float dt=0.5f, float dry_out_ratio=0.f, float viscosity=1.f, float power=2.5f, float evap_rate=0.f, bool outflow_boundaries=false)
GPU viscous shallow-water flow simulation using non-linear thin-film diffusion with upwind mobility.
Definition flow_simulation.cpp:168
Definition algebra.hpp:23
void find_flow_apex(const Array &z, std::vector< int > &is, std::vector< int > &js)
Identifies flow apex (source) cells using D8 flow routing.
Definition find_flow_sinks.cpp:15
FlowDirectionMethod
Enumeration of available algorithms for computing flow directions.
Definition drainage_basin_cell_based.hpp:26
@ FDM_D8
Standard D8 flow direction algorithm.
Definition drainage_basin_cell_based.hpp:27
Array water_depth_increase(const Array &water_depth, const Array &z, float additional_depth)
Simulates a local increase in water depth without global reflooding.
Definition water_depth.cpp:97
Array flooding_uniform_level(const Array &z, float zref)
Compute water depth for a uniform flooding level.
Definition flooding.cpp:19
Array flow_accumulation_d8(const Array &z)
Computes the flow accumulation for each cell using the D8 flow direction model.
Definition flow_accumulation_d8.cpp:54
RadialProfile
Radial profile type.
Definition profiles.hpp:51
@ RP_SMOOTHSTEP_UPPER
Definition profiles.hpp:56
Array flooding_from_boundaries(const Array &z, float zref, bool from_east=true, bool from_west=true, bool from_north=true, bool from_south=true)
Compute flooding starting from the lowest boundary points.
Definition flooding.cpp:31
Array d8_compute_ndip(const Array &d8)
Computes the number of drainage paths for each cell based on the D8 flow direction model.
Definition flow_accumulation_d8.cpp:30
Array generate_riverbed(const Path &path, glm::ivec2 shape, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f}, bool bezier_smoothing=false, float depth_start=0.01f, float depth_end=1.f, float slope_start=64.f, float slope_end=32.f, float shape_exponent_start=1.f, float shape_exponent_end=10.f, float k_smoothing=0.5f, int post_filter_ir=0, Array *p_noise_x=nullptr, Array *p_noise_y=nullptr, Array *p_noise_r=nullptr)
Generates a 2D array representing a riverbed based on a specified path.
Definition generate_riverbed.cpp:21
Array flooding_lake_system(const Array &z, float surface_threshold=0)
Estimate lake water depths on a terrain by filling depressions.
Definition flooding.cpp:180
Array water_depth_from_mask(const Array &z, const Array &mask, float mask_threshold=0.f, int iterations_max=10000, float tolerance=1e-2f)
Compute water depth over a masked terrain using harmonic interpolation.
Definition water_depth.cpp:75
void water_depth_dry_out(Array &water_depth, float dry_out_ratio=0.5f, const Array *p_mask=nullptr, float depth_max=std::numeric_limits< float >::max())
Apply a drying factor to a water depth field.
Definition water_depth.cpp:44
Array flow_fixing_mst(const Array &z, float riverbed_talus=0.f, float elevation_ratio=0.95f, float distance_exponent=2.f, float upward_penalization=50.f, float valley_affinity=0.5f, int prefilter_ir=8, float minimum_depth=1e-4f, bool carve_riverbed=true, float merging_distance=8.f, RadialProfile radial_profile=RadialProfile::RP_SMOOTHSTEP_UPPER, float radial_profile_parameter=2.f, const Array *p_noise_r=nullptr)
Resolves flow sinks and unwanted upslopes using Dijkstra pathfinding and a Minimum Spanning Tree (MST...
Definition flow_fixing.cpp:369
Array flow_accumulation_dinf(const Array &z, float talus_ref)
Computes the flow accumulation for each cell using the Multiple Flow Direction (MFD) model.
Definition flow_accumulation_dinf.cpp:42
Array flow_fixing_drainage_basin(const Array &z, FlowDirectionMethod fd_method=FlowDirectionMethod::FDM_D8, float riverbed_talus=1e-4f, int iterations=3, bool carve_riverbed=false, float talus_riverbank=0.01f, float merging_distance=8.f, std::uint32_t seed=0, float noise_strength=0.f, const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr)
Fixes flow paths and unwanted upslopes using a cell-based drainage basin tree network and elevation u...
Definition flow_fixing.cpp:291
Array merge_water_depths(const Array &depth1, const Array &depth2, float k_smooth=0.f)
Merge two water depth fields.
Definition water_depth.cpp:27
Array carve_riverbed(const Array &z, const Array &z_river, float talus_riverbank=0.01f, bool smooth_river_bottom=true, float merging_distance=8.f, std::uint32_t seed=0, float riverbank_noise_ratio=0.f, const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr)
Blends and carves smooth riverbanks around modified riverbed profiles.
Definition carve_riverbed.cpp:19
Array snow_melting_map(const Array &z, float melt_start_elevation=0.f, float melt_end_elevation=0.5f, float elevation_strength=1.f, float elevation_exp=1.f, float sun_azimuth=0.f, float sun_zenith=60.f, float aspect_strength=0.f, float slope_exp=1.f, float slope_strength=0.f)
Generate a spatial snow melting map from terrain features.
Definition snow_simulation.cpp:22
std::vector< Array > flow_direction_dinf(const Array &z, float talus_ref)
Computes the flow direction and weights for each direction using the Multiple Flow Direction (MFD) mo...
Definition flow_accumulation_dinf.cpp:150
std::vector< glm::ivec2 > find_flow_sinks_border(const Array &z)
Find flow sinks located on the domain border.
Definition find_flow_sinks.cpp:98
Array basin_id(const Array &z, FlowDirectionMethod fd_method=FlowDirectionMethod::FDM_D8, bool remove_lakes=true)
Label drainage basins using a priority-flood algorithm.
Definition basin_id.cpp:15
Path flow_stream(const Array &z, const glm::ivec2 ij_start, const float elevation_ratio=0.5f, const float distance_exponent=2.f, const float upward_penalization=100.f)
Computes the optimal flow path from a starting point to the boundary of a given elevation array.
Definition flow_stream.cpp:50
Array flow_accumulation_dinf_perturbed(const Array &z, float talus_ref, int nsamples, glm::vec2 kw, std::uint32_t seed, float amp, const Array *p_perturb_scaling=nullptr, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f})
Computes averaged D-infinity flow accumulation with terrain perturbations.
Definition flow_accumulation_dinf.cpp:114
Array flow_accumulation_stochastic(const Array &z, int n_samples=1<< 19, std::uint32_t seed=0, const Array *p_source=nullptr, const Array *p_decay=nullptr)
Computes flow accumulation using a stochastic (Monte-Carlo) transport estimator.
Definition flow_accumulation_stochastic.cpp:43
std::vector< float > flow_direction_dinf_flat(const Array &z, float talus_ref)
Definition flow_accumulation_dinf.cpp:195
float angle(const Point &p1, const Point &p2)
Computes the angle between two points relative to the x-axis.
Definition points.cpp:52
Array flow_fixing(const Array &z, float riverbed_talus=0.f, int iterations=5, int prefilter_ir=8, bool carve_riverbed=true, float merging_distance=8.f, const Array *p_noise_r=nullptr)
Definition flow_fixing.cpp:28
Array water_frontier_curvature(const Array &water_depth, int prefilter_ir, bool extend_values_from_interface=false)
Compute the curvature of the water interface from a signed distance field.
Definition water_depth.cpp:302
Array flow_direction_dinf_angle(const Array &z, float talus_ref)
Computes the flow direction using the Multiple Flow Direction (MFD) model.
Definition flow_accumulation_dinf.cpp:253
Array water_mask(const Array &water_depth)
Generates a binary mask representing water presence.
Definition water_depth.cpp:334
Array flooding_from_point(const Array &z, int i, int j, float depth_min=std::numeric_limits< float >::max())
Flood terrain starting from a single seed point.
Definition flooding.cpp:115
void find_flow_sinks(const Array &z, std::vector< int > &is, std::vector< int > &js)
Identifies the indices of flow sinks within the heightmap.
Definition find_flow_sinks.cpp:38
Array flow_direction_d8(const Array &z)
Computes the flow direction from each cell to its downslope neighbor using the D8 model.
Definition flow_accumulation_d8.cpp:107
Array water_depth_increase_with_flooding(const Array &water_depth, const Array &z, float additional_depth)
Simulates a physical rise in water level with full domain flooding.
Definition water_depth.cpp:241
Path class for manipulating and analyzing paths in 2D space.