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"
29
30namespace hmap
31{
32
38
40{
41public:
42 std::vector<std::vector<glm::ivec2>> upstream_traversal;
44 glm::ivec2 null_cell = glm::ivec2(-1, -1);
45
46 size_t get_basins_number() const;
47 std::vector<std::vector<glm::ivec2>> get_main_channels();
48 std::vector<glm::ivec2> get_outlets() const;
49 std::vector<glm::ivec2> get_ridges();
50 std::vector<std::vector<glm::ivec2>> get_ridges_neighbors();
51
53 const Array &z,
54 FlowDirectionMethod fd_method = FlowDirectionMethod::FDM_D8,
55 bool remove_lakes = true,
56 const std::vector<glm::ivec2> &outlets = {});
57
58 void generate_traversal_d8(const Array &z,
59 bool remove_lakes = true,
60 const std::vector<glm::ivec2> &outlets = {});
62 const Array &z,
63 const std::vector<glm::ivec2> &outlets = {});
64
65 void accumulate(Array &acc) const;
66 void traverse_downstream(std::function<void(int, int, int, int, int)> op);
67 void traverse_downstream(std::function<void(int, int, int)> op);
68 void traverse_upstream(std::function<void(int, int, int, int, int)> op);
69 void traverse_upstream(std::function<void(int, int, int)> op);
70
71private:
72 void remove_lakes_d8(const Array &z,
73 float dz_weight = 1.f,
74 float dz_downstream_cost_ratio = 0.f);
75 void update_traversal();
76};
77
97Array basin_id(const Array &z,
98 FlowDirectionMethod fd_method = FlowDirectionMethod::FDM_D8,
99 bool remove_lakes = true);
100
116Array d8_compute_ndip(const Array &d8);
117
129void find_flow_apex(const Array &z, std::vector<int> &is, std::vector<int> &js);
130
145void find_flow_sinks(const Array &z,
146 std::vector<int> &is,
147 std::vector<int> &js);
148
149std::vector<glm::ivec2> find_flow_sinks(const Array &z);
150
167Array flooding_uniform_level(const Array &z, float zref);
168
190 float zref,
191 bool from_east = true,
192 bool from_west = true,
193 bool from_north = true,
194 bool from_south = true);
195
215 int i,
216 int j,
217 float depth_min = std::numeric_limits<float>::max());
218
237 const std::vector<int> &i,
238 const std::vector<int> &j,
239 float depth_min = std::numeric_limits<float>::max());
240
268Array flooding_lake_system(const Array &z, float surface_threshold = 0);
269
293
321Array flow_accumulation_dinf(const Array &z, float talus_ref);
322
356 float talus_ref,
357 int nsamples,
358 glm::vec2 kw,
359 uint seed,
360 float amp,
361 const Array *p_perturb_scaling = nullptr,
362 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
363
380Array flow_direction_d8(const Array &z);
381
399std::vector<Array> flow_direction_dinf(const Array &z, float talus_ref);
400
401std::vector<float> flow_direction_dinf_flat(const Array &z, float talus_ref);
402
407Array flow_direction_dinf_angle(const Array &z, float talus_ref);
408
409Array flow_fixing(const Array &z,
410 float riverbed_talus = 0.f,
411 int iterations = 5,
412 int prefilter_ir = 8,
413 bool carve_riverbed = true,
414 bool smooth_river_bottom = true,
415 float talus_riverbank = 0.01f, // 4 / shape.X
416 uint seed = 0,
417 float riverbank_noise_ratio = 0.f,
418 float merging_distance = 8.f, // pixels
419 const Array *p_noise_x = nullptr,
420 const Array *p_noise_y = nullptr);
421
455Path flow_stream(const Array &z,
456 const glm::ivec2 ij_start,
457 const float elevation_ratio = 0.5f,
458 const float distance_exponent = 2.f,
459 const float upward_penalization = 100.f);
460
507Array generate_riverbed(const Path &path,
508 glm::ivec2 shape,
509 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
510 bool bezier_smoothing = false,
511 float depth_start = 0.01f,
512 float depth_end = 1.f,
513 float slope_start = 64.f,
514 float slope_end = 32.f,
515 float shape_exponent_start = 1.f,
516 float shape_exponent_end = 10.f,
517 float k_smoothing = 0.5f,
518 int post_filter_ir = 0,
519 Array *p_noise_x = nullptr,
520 Array *p_noise_y = nullptr,
521 Array *p_noise_r = nullptr);
522
533Array merge_water_depths(const Array &depth1,
534 const Array &depth2,
535 float k_smooth = 0.f);
536
562Array snow_melting_map(const Array &z,
563 float melt_start_elevation = 0.f,
564 float melt_end_elevation = 0.5f,
565 float elevation_strength = 1.f,
566 float elevation_exp = 1.f,
567 float sun_azimuth = 0.f,
568 float sun_zenith = 60.f,
569 float aspect_strength = 0.f,
570 float slope_exp = 1.f,
571 float slope_strength = 0.f);
572
611Array water_depth_from_mask(const Array &z,
612 const Array &mask,
613 float mask_threshold = 0.f,
614 int iterations_max = 10000,
615 float tolerance = 1e-2f,
616 float omega = 1.8f);
617
642void water_depth_dry_out(Array &water_depth,
643 float dry_out_ratio = 0.5f,
644 const Array *p_mask = nullptr,
645 float depth_max = std::numeric_limits<float>::max());
646
668Array water_depth_increase(const Array &water_depth,
669 const Array &z,
670 float additional_depth);
671
688Array water_mask(const Array &water_depth);
689
712Array water_mask(const Array &water_depth,
713 const Array &z,
714 float additional_depth);
715
716} // namespace hmap
717
718namespace hmap::gpu
719{
720
734Array flow_accumulation_from_velocity_field(const Array &u,
735 const Array &v,
736 int iterations);
737
739Array flow_direction_d8(const Array &z);
740
759Array flow_simulation(const Array &z,
760 float water_depth,
761 const Array &depth_map,
762 int iterations,
763 float dt = 0.5f,
764 bool flux_diffusion = true,
765 float flux_diffusion_strength = 0.01f,
766 float dry_out_ratio = 0.f);
767
768Array flow_simulation_viscous(const Array &z,
769 float water_depth,
770 const Array &depth_map,
771 int iterations,
772 float dt = 0.5f,
773 float dry_out_ratio = 0.f,
774 float viscosity = 1.f,
775 float power = 2.5f);
776
778Array generate_riverbed(const Path &path,
779 glm::ivec2 shape,
780 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
781 bool bezier_smoothing = false,
782 float depth_start = 0.01f,
783 float depth_end = 1.f,
784 float slope_start = 64.f,
785 float slope_end = 32.f,
786 float shape_exponent_start = 1.f,
787 float shape_exponent_end = 10.f,
788 float k_smoothing = 0.5f,
789 int post_filter_ir = 0,
790 Array *p_noise_x = nullptr,
791 Array *p_noise_y = nullptr,
792 Array *p_noise_r = nullptr);
793
826Array snow_simulation(const Array &z,
827 float snow_depth,
828 const Array &fall_map,
829 const Array &melting_map,
830 const Array &talus,
831 int iterations,
832 float dt = 0.5f,
833 float fall_iterations_ratio = 1.f,
834 float k_snow = 0.5f,
835 float k_visc = 0.1f,
836 float k_melt_factor = 0.8f,
837 float k_depth_ratio = 1.f,
838 float k_depth_slope_ratio = 1.f,
839 bool post_filter = true,
840 float thermal_talus_ratio = 0.2f);
841
855void water_depth_filter(Array &depth, const Array &z, int ir);
856
857} // namespace hmap::gpu
Declaration of the Array class for 2D floating-point arrays with various mathematical operations and ...
unsigned int uint
Definition array.hpp:14
Array class, helper to manipulate 2D float array with "(i, j)" indexing.
Definition array.hpp:32
Definition hydrology.hpp:40
void accumulate(Array &acc) const
Definition drainage_basin_cell_based.cpp:13
std::vector< std::vector< glm::ivec2 > > get_ridges_neighbors()
Definition drainage_basin_cell_based.cpp:294
void traverse_upstream(std::function< void(int, int, int, int, int)> op)
std::vector< glm::ivec2 > get_outlets() const
Definition drainage_basin_cell_based.cpp:238
void traverse_downstream(std::function< void(int, int, int)> op)
void generate_traversal_d8(const Array &z, bool remove_lakes=true, const std::vector< glm::ivec2 > &outlets={})
Definition drainage_basin_cell_based.cpp:45
void generate_traversal(const Array &z, FlowDirectionMethod fd_method=FlowDirectionMethod::FDM_D8, bool remove_lakes=true, const std::vector< glm::ivec2 > &outlets={})
Definition drainage_basin_cell_based.cpp:30
void generate_traversal_priority_flood(const Array &z, const std::vector< glm::ivec2 > &outlets={})
Definition drainage_basin_cell_based.cpp:100
std::vector< std::vector< glm::ivec2 > > upstream_traversal
Definition hydrology.hpp:42
std::vector< std::vector< glm::ivec2 > > get_main_channels()
Definition drainage_basin_cell_based.cpp:197
glm::ivec2 null_cell
Definition hydrology.hpp:44
void traverse_upstream(std::function< void(int, int, int)> op)
void traverse_downstream(std::function< void(int, int, int, int, int)> op)
size_t get_basins_number() const
Definition drainage_basin_cell_based.cpp:192
std::vector< glm::ivec2 > get_ridges()
Definition drainage_basin_cell_based.cpp:252
Mat< glm::ivec2 > next
Definition hydrology.hpp:43
Definition blending.hpp:186
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:10
FlowDirectionMethod
Definition hydrology.hpp:34
@ FDM_D8
Definition hydrology.hpp:35
@ FDM_PRIORITY_FLOOD
Definition hydrology.hpp:36
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:65
Array water_depth_increase(const Array &water_depth, const Array &z, float additional_depth)
Simulates the increase in water depth over a terrain.
Definition water_depth.cpp:89
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:49
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:27
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, uint 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:17
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:16
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:37
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:35
Array merge_water_depths(const Array &depth1, const Array &depth2, float k_smooth=0.f)
Merge two water depth fields.
Definition water_depth.cpp:21
Array flow_accumulation_dinf_perturbed(const Array &z, float talus_ref, int nsamples, glm::vec2 kw, uint 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:110
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:15
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:143
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:46
std::vector< float > flow_direction_dinf_flat(const Array &z, float talus_ref)
Definition flow_accumulation_dinf.cpp:186
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:234
Array water_mask(const Array &water_depth)
Generates a binary mask representing water presence.
Definition water_depth.cpp:207
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:32
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:100
Path class for manipulating and analyzing paths in 2D space.
Mat class for basic manipulation of 2D matrices.
Definition algebra.hpp:158