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,
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,
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
320Array flow_accumulation_dinf(const Array &z, float talus_ref);
321
339
357std::vector<Array> flow_direction_dinf(const Array &z, float talus_ref);
358
359std::vector<float> flow_direction_dinf_flat(const Array &z, float talus_ref);
360
365Array flow_direction_dinf_angle(const Array &z, float talus_ref);
366
367Array flow_fixing(const Array &z,
368 float riverbed_talus = 0.f,
369 int iterations = 5,
370 int prefilter_ir = 8,
371 bool carve_riverbed = true,
372 bool smooth_river_bottom = true,
373 float talus_riverbank = 0.01f, // 4 / shape.X
374 uint seed = 0,
375 float riverbank_noise_ratio = 0.f,
376 float merging_distance = 8.f, // pixels
377 const Array *p_noise_x = nullptr,
378 const Array *p_noise_y = nullptr);
379
413Path flow_stream(const Array &z,
414 const glm::ivec2 ij_start,
415 const float elevation_ratio = 0.5f,
416 const float distance_exponent = 2.f,
417 const float upward_penalization = 100.f);
418
465Array generate_riverbed(const Path &path,
466 glm::ivec2 shape,
467 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
468 bool bezier_smoothing = false,
469 float depth_start = 0.01f,
470 float depth_end = 1.f,
471 float slope_start = 64.f,
472 float slope_end = 32.f,
473 float shape_exponent_start = 1.f,
474 float shape_exponent_end = 10.f,
475 float k_smoothing = 0.5f,
476 int post_filter_ir = 0,
477 Array *p_noise_x = nullptr,
478 Array *p_noise_y = nullptr,
479 Array *p_noise_r = nullptr);
480
491Array merge_water_depths(const Array &depth1,
492 const Array &depth2,
493 float k_smooth = 0.f);
494
533Array water_depth_from_mask(const Array &z,
534 const Array &mask,
535 float mask_threshold = 0.f,
536 int iterations_max = 10000,
537 float tolerance = 1e-2f,
538 float omega = 1.8f);
539
564void water_depth_dry_out(Array &water_depth,
565 float dry_out_ratio = 0.5f,
566 const Array *p_mask = nullptr,
567 float depth_max = std::numeric_limits<float>::max());
568
590Array water_depth_increase(const Array &water_depth,
591 const Array &z,
592 float additional_depth);
593
610Array water_mask(const Array &water_depth);
611
634Array water_mask(const Array &water_depth,
635 const Array &z,
636 float additional_depth);
637
638} // namespace hmap
639
640namespace hmap::gpu
641{
642
643Array flow_accumulation_from_velocity_field(const Array &u,
644 const Array &v,
645 int iterations);
646
648Array flow_direction_d8(const Array &z);
649
651Array generate_riverbed(const Path &path,
652 glm::ivec2 shape,
653 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
654 bool bezier_smoothing = false,
655 float depth_start = 0.01f,
656 float depth_end = 1.f,
657 float slope_start = 64.f,
658 float slope_end = 32.f,
659 float shape_exponent_start = 1.f,
660 float shape_exponent_end = 10.f,
661 float k_smoothing = 0.5f,
662 int post_filter_ir = 0,
663 Array *p_noise_x = nullptr,
664 Array *p_noise_y = nullptr,
665 Array *p_noise_r = nullptr);
666
667} // 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 generate_traversal_priority_flood(const Array &z, const std::vector< glm::ivec2 > &outlets={})
Definition drainage_basins.cpp:99
std::vector< glm::ivec2 > get_ridges()
Definition drainage_basins.cpp:251
std::vector< std::vector< glm::ivec2 > > get_ridges_neighbors()
Definition drainage_basins.cpp:292
Mat< glm::ivec2 > next
Definition hydrology.hpp:43
std::vector< std::vector< glm::ivec2 > > upstream_traversal
Definition hydrology.hpp:42
glm::ivec2 null_cell
Definition hydrology.hpp:44
void generate_traversal(const Array &z, FlowDirectionMethod fd_method=FlowDirectionMethod::FDM_D8, bool remove_lakes=true, const std::vector< glm::ivec2 > &outlets={})
Definition drainage_basins.cpp:30
void traverse_upstream(std::function< void(int, int, int, int, int)> op)
std::vector< glm::ivec2 > get_outlets() const
Definition drainage_basins.cpp:237
void traverse_downstream(std::function< void(int, int, int, int, int)> op)
std::vector< std::vector< glm::ivec2 > > get_main_channels()
Definition drainage_basins.cpp:196
void accumulate(Array &acc) const
Definition drainage_basins.cpp:13
size_t get_basins_number() const
Definition drainage_basins.cpp:191
void traverse_upstream(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_basins.cpp:44
void traverse_downstream(std::function< void(int, int, int)> op)
Represents an ordered set of points in 2D, forming a polyline (open or closed).
Definition path.hpp:50
Definition blending.hpp:186
Definition algebra.hpp:22
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_PRIORITY_FLOOD
Definition hydrology.hpp:36
@ FDM_D8
Definition hydrology.hpp:35
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 flooding.cpp:233
Array water_depth_increase(const Array &water_depth, const Array &z, float additional_depth)
Simulates the increase in water depth over a terrain.
Definition flooding.cpp:257
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: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:29
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:165
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 flooding.cpp:205
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 flooding.cpp:189
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:110
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:152
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:201
Array water_mask(const Array &water_depth)
Generates a binary mask representing water presence.
Definition flooding.cpp:375
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:111
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:155