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 <functional>
25#include <limits>
26
27#include "highmap/array.hpp"
30
31namespace hmap
32{
33
53Array basin_id(const Array &z,
55 bool remove_lakes = true);
56
72Array d8_compute_ndip(const Array &d8);
73
85void find_flow_apex(const Array &z, std::vector<int> &is, std::vector<int> &js);
86
101void find_flow_sinks(const Array &z,
102 std::vector<int> &is,
103 std::vector<int> &js);
104
114std::vector<glm::ivec2> find_flow_sinks(const Array &z);
115
125std::vector<glm::ivec2> find_flow_sinks_border(const Array &z);
126
143Array flooding_uniform_level(const Array &z, float zref);
144
165Array flooding_from_boundaries(const Array &z,
166 float zref,
167 bool from_east = true,
168 bool from_west = true,
169 bool from_north = true,
170 bool from_south = true);
171
190Array flooding_from_point(const Array &z,
191 int i,
192 int j,
193 float depth_min = std::numeric_limits<float>::max());
194
212Array flooding_from_point(const Array &z,
213 const std::vector<int> &i,
214 const std::vector<int> &j,
215 float depth_min = std::numeric_limits<float>::max());
216
244Array flooding_lake_system(const Array &z, float surface_threshold = 0);
245
268Array flow_accumulation_d8(const Array &z);
269
297Array flow_accumulation_dinf(const Array &z, float talus_ref);
298
331Array flow_accumulation_dinf_perturbed(const Array &z,
332 float talus_ref,
333 int nsamples,
334 glm::vec2 kw,
335 std::uint32_t seed,
336 float amp,
337 const Array *p_perturb_scaling = nullptr,
338 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
339
356Array flow_direction_d8(const Array &z);
357
375std::vector<Array> flow_direction_dinf(const Array &z, float talus_ref);
376
377std::vector<float> flow_direction_dinf_flat(const Array &z, float talus_ref);
378
383Array flow_direction_dinf_angle(const Array &z, float talus_ref);
384
385Array flow_fixing(const Array &z,
386 float riverbed_talus = 0.f,
387 int iterations = 5,
388 int prefilter_ir = 8,
389 bool carve_riverbed = true,
390 bool smooth_river_bottom = true,
391 float talus_riverbank = 0.01f, // 4 / shape.X
392 std::uint32_t seed = 0,
393 float riverbank_noise_ratio = 0.f,
394 float merging_distance = 8.f, // pixels
395 const Array *p_noise_x = nullptr,
396 const Array *p_noise_y = nullptr);
397
431Path flow_stream(const Array &z,
432 const glm::ivec2 ij_start,
433 const float elevation_ratio = 0.5f,
434 const float distance_exponent = 2.f,
435 const float upward_penalization = 100.f);
436
483Array generate_riverbed(const Path &path,
484 glm::ivec2 shape,
485 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
486 bool bezier_smoothing = false,
487 float depth_start = 0.01f,
488 float depth_end = 1.f,
489 float slope_start = 64.f,
490 float slope_end = 32.f,
491 float shape_exponent_start = 1.f,
492 float shape_exponent_end = 10.f,
493 float k_smoothing = 0.5f,
494 int post_filter_ir = 0,
495 Array *p_noise_x = nullptr,
496 Array *p_noise_y = nullptr,
497 Array *p_noise_r = nullptr);
498
509Array merge_water_depths(const Array &depth1,
510 const Array &depth2,
511 float k_smooth = 0.f);
512
538Array snow_melting_map(const Array &z,
539 float melt_start_elevation = 0.f,
540 float melt_end_elevation = 0.5f,
541 float elevation_strength = 1.f,
542 float elevation_exp = 1.f,
543 float sun_azimuth = 0.f,
544 float sun_zenith = 60.f,
545 float aspect_strength = 0.f,
546 float slope_exp = 1.f,
547 float slope_strength = 0.f);
548
587Array water_depth_from_mask(const Array &z,
588 const Array &mask,
589 float mask_threshold = 0.f,
590 int iterations_max = 10000,
591 float tolerance = 1e-2f,
592 float omega = 1.8f);
593
618void water_depth_dry_out(Array &water_depth,
619 float dry_out_ratio = 0.5f,
620 const Array *p_mask = nullptr,
621 float depth_max = std::numeric_limits<float>::max());
622
658Array water_depth_increase(const Array &water_depth,
659 const Array &z,
660 float additional_depth);
661
696Array water_depth_increase_with_flooding(const Array &water_depth,
697 const Array &z,
698 float additional_depth);
721Array water_frontier_curvature(const Array &water_depth,
722 int prefilter_ir,
723 bool extend_values_from_interface = false);
724
741Array water_mask(const Array &water_depth);
742
765Array water_mask(const Array &water_depth,
766 const Array &z,
767 float additional_depth);
768
769} // namespace hmap
770
771namespace hmap::gpu
772{
773
797Array coastal_fetch(const Array &z,
798 int ndirections,
799 const Array *p_compute_mask = nullptr);
800
831Array coastal_fetch_directional(const Array &z,
832 float angle,
833 float directional_exp,
834 int ndirections,
835 const Array *p_compute_mask = nullptr);
836
850Array flow_accumulation_from_velocity_field(const Array &u,
851 const Array &v,
852 int iterations);
853
855Array flow_direction_d8(const Array &z);
856
875Array flow_simulation(const Array &z,
876 float water_depth,
877 const Array &depth_map,
878 int iterations,
879 float dt = 0.5f,
880 bool flux_diffusion = true,
881 float flux_diffusion_strength = 0.01f,
882 float dry_out_ratio = 0.f);
883
884Array flow_simulation_viscous(const Array &z,
885 float water_depth,
886 const Array &depth_map,
887 int iterations,
888 float dt = 0.5f,
889 float dry_out_ratio = 0.f,
890 float viscosity = 1.f,
891 float power = 2.5f);
892
894Array generate_riverbed(const Path &path,
895 glm::ivec2 shape,
896 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
897 bool bezier_smoothing = false,
898 float depth_start = 0.01f,
899 float depth_end = 1.f,
900 float slope_start = 64.f,
901 float slope_end = 32.f,
902 float shape_exponent_start = 1.f,
903 float shape_exponent_end = 10.f,
904 float k_smoothing = 0.5f,
905 int post_filter_ir = 0,
906 Array *p_noise_x = nullptr,
907 Array *p_noise_y = nullptr,
908 Array *p_noise_r = nullptr);
909
942Array snow_simulation(const Array &z,
943 float snow_depth,
944 const Array &fall_map,
945 const Array &melting_map,
946 const Array &talus,
947 int iterations,
948 float dt = 0.5f,
949 float fall_iterations_ratio = 1.f,
950 float k_snow = 0.5f,
951 float k_visc = 0.1f,
952 float k_melt_factor = 0.8f,
953 float k_depth_ratio = 1.f,
954 float k_depth_slope_ratio = 1.f,
955 bool post_filter = true,
956 float thermal_talus_ratio = 0.2f);
957
971void water_depth_filter(Array &depth,
972 const Array &z,
973 int ir,
974 const Array *p_water_mask = nullptr,
975 bool smooth_contour = false,
976 float transition_ratio = 0.1f);
977
979Array water_frontier_curvature(const Array &water_depth,
980 int prefilter_ir,
981 bool extend_values_from_interface = false);
982
983} // namespace hmap::gpu
Declaration of the Array class for 2D floating-point arrays with various mathematical operations and ...
Definition blending.hpp:186
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:19
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:14
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:389
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:349
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:15
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:82
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)
GPU hydraulic flow simulation using a virtual-pipes model; simulates shallow-water transport over a h...
Definition flow_simulation.cpp:26
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:40
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)
Definition flow_simulation.cpp:112
Array flow_direction_d8(const Array &z)
See hmap::flow_direction_d8.
Definition flow_accumulation_d8_gpu.cpp:14
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:14
FlowDirectionMethod
Definition drainage_basin_cell_based.hpp:14
@ FDM_D8
Definition drainage_basin_cell_based.hpp:15
Array water_depth_from_mask(const Array &z, const Array &mask, float mask_threshold=0.f, int iterations_max=10000, float tolerance=1e-2f, float omega=1.8f)
Compute water depth over a masked terrain using harmonic interpolation.
Definition water_depth.cpp:68
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:92
Array flow_fixing(const Array &z, float riverbed_talus=0.f, int iterations=5, int prefilter_ir=8, bool carve_riverbed=true, bool smooth_river_bottom=true, float talus_riverbank=0.01f, std::uint32_t seed=0, float riverbank_noise_ratio=0.f, float merging_distance=8.f, const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr)
Definition flow_fixing.cpp:24
Array flooding_uniform_level(const Array &z, float zref)
Compute water depth for a uniform flooding level.
Definition flooding.cpp:18
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:51
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:28
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:29
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:164
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:40
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:41
Array merge_water_depths(const Array &depth1, const Array &depth2, float k_smooth=0.f)
Merge two water depth fields.
Definition water_depth.cpp:26
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:21
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:144
std::vector< glm::ivec2 > find_flow_sinks_border(const Array &z)
Find flow sinks located on the domain border.
Definition find_flow_sinks.cpp:93
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:14
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:49
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:111
std::vector< float > flow_direction_dinf_flat(const Array &z, float talus_ref)
Definition flow_accumulation_dinf.cpp:187
float angle(const Point &p1, const Point &p2)
Computes the angle between two points relative to the x-axis.
Definition points.cpp:46
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:291
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:243
Array water_mask(const Array &water_depth)
Generates a binary mask representing water presence.
Definition water_depth.cpp:321
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:110
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:36
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:102
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:233
Path class for manipulating and analyzing paths in 2D space.