HighMap library (C++)
Loading...
Searching...
No Matches
drainage_basin_cell_based.hpp
Go to the documentation of this file.
1/* Copyright (c) 2026 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
12#pragma once
13#include <functional>
14#include <limits>
15
16#include "highmap/array.hpp"
17
18namespace hmap
19{
20
30
41{
42public:
43 // --- Construction ---
44
49
55
56 // --- Geometry / Mesh ---
57
62 const Array &get_z() const;
63
64 // --- Flow graph construction ---
65
72 void compute_receivers(unsigned int seed = 0, float noise_strength = 0.f);
73
78
84 void update_stream_tree(unsigned int seed, float noise_strength);
85
89 void update_stream_tree();
90
94 void update_traversals();
95
100 std::vector<glm::ivec2> get_outlets() const;
101
106 void set_outlets(const std::vector<glm::ivec2> &outlet_indices);
107
112 std::vector<std::vector<glm::ivec2>> compute_upstream_traversals();
113
114 // --- Basin topology utilities ---
115
120 std::pair<Mat<glm::ivec2>, bool> find_subroots();
121
126 void remove_lakes(const Mat<glm::ivec2> &subroot);
127
133 std::vector<std::vector<glm::ivec2>> get_main_channels() const;
134
135 // --- Hydrology computations ---
136
144 Array compute_response_times(const Array &area_acc,
145 const Array &erodibility,
146 float m_exp) const;
147
155 float update_elevations(const Array &response_times,
156 float uplift_rate,
157 const Array &max_slope);
158
163 void accumulate_area_by_outlet(Array &acc) const;
164
168 void flow_breach();
169
170 // --- Members ---
171
173
178 // coordinates.
179
180 std::unordered_map<glm::ivec2, std::vector<glm::ivec2>, IVec2Hash>
182 // Cached
183 // traversal
184 // paths.
185
186 const glm::ivec2 null_cell = glm::ivec2(-1, -1);
187 // invalid/null cell.
188
189private:
190 // constants
191 const int di[8] = {1, 1, 0, -1, -1, -1, 0, 1};
192 const int dj[8] = {0, -1, -1, -1, 0, 1, 1, 1};
193 const float cd[8] =
194 {1.f, M_SQRT1_2, 1.f, M_SQRT1_2, 1.f, M_SQRT1_2, 1.f, M_SQRT1_2};
195};
196
204Mat<std::vector<glm::ivec2>> invert_receiver_map(
205 const Mat<glm::ivec2> &receivers);
206
207} // namespace hmap
Declaration of the Array class for 2D floating-point arrays with various mathematical operations and ...
Array class, helper to manipulate 2D float array with "(i, j)" indexing.
Definition array.hpp:32
Represents a cell-based hydrology drainage basin network on a 2D heightmap grid.
Definition drainage_basin_cell_based.hpp:41
void compute_receivers(unsigned int seed=0, float noise_strength=0.f)
Compute flow receivers using standard D8, with optional noise.
Definition drainage_basin_cell_based.cpp:59
std::vector< std::vector< glm::ivec2 > > get_main_channels() const
Get the main channel paths of the flow network.
Definition drainage_basin_cell_based.cpp:326
std::unordered_map< glm::ivec2, std::vector< glm::ivec2 >, IVec2Hash > traversals
Definition drainage_basin_cell_based.hpp:181
std::vector< glm::ivec2 > get_outlets() const
Get the outlets of the basin.
Definition drainage_basin_cell_based.cpp:364
Mat< glm::ivec2 > receivers
Grid of receiver coordinates.
Definition drainage_basin_cell_based.hpp:175
void update_stream_tree()
Update the stream tree deterministically.
Definition drainage_basin_cell_based.cpp:552
std::vector< std::vector< glm::ivec2 > > compute_upstream_traversals()
Compute upstream traversal orders for the entire grid.
Definition drainage_basin_cell_based.cpp:215
void compute_receivers_priority_flood()
Compute flow receivers using the priority flood routing algorithm.
Definition drainage_basin_cell_based.cpp:110
Array z
The heightmap array.
Definition drainage_basin_cell_based.hpp:172
Array compute_response_times(const Array &area_acc, const Array &erodibility, float m_exp) const
Compute response times of the basin cells.
Definition drainage_basin_cell_based.cpp:178
void accumulate_area_by_outlet(Array &acc) const
Accumulate contributing area down the network by outlet.
Definition drainage_basin_cell_based.cpp:38
Mat< std::vector< glm::ivec2 > > children
Grid of children coordinates.
Definition drainage_basin_cell_based.hpp:176
void remove_lakes(const Mat< glm::ivec2 > &subroot)
Remove lakes by draining local depressions.
Definition drainage_basin_cell_based.cpp:383
Mat< int > outlets_mask
Mask indicating outlet cells.
Definition drainage_basin_cell_based.hpp:174
void flow_breach()
Perform flow breaching to resolve depressions.
Definition drainage_basin_cell_based.cpp:299
void set_outlets(const std::vector< glm::ivec2 > &outlet_indices)
Set the outlets of the basin.
Definition drainage_basin_cell_based.cpp:481
const Array & get_z() const
Get the underlying heightmap array.
Definition drainage_basin_cell_based.cpp:378
float update_elevations(const Array &response_times, float uplift_rate, const Array &max_slope)
Update elevations based on response times and uplift.
Definition drainage_basin_cell_based.cpp:491
void update_traversals()
Update cached traversal orders for upstream/downstream computations.
Definition drainage_basin_cell_based.cpp:557
const glm::ivec2 null_cell
Constant representing an.
Definition drainage_basin_cell_based.hpp:186
DrainageBasinCellBased()=default
Default constructor.
Mat< glm::ivec2 > roots
Grid of basin root.
Definition drainage_basin_cell_based.hpp:177
std::pair< Mat< glm::ivec2 >, bool > find_subroots()
Find subroots of the flow network.
Definition drainage_basin_cell_based.cpp:248
Definition algebra.hpp:23
FlowDirectionMethod
Enumeration of available algorithms for computing flow directions.
Definition drainage_basin_cell_based.hpp:26
@ FDM_PRIORITY_FLOOD
Priority flood flow routing algorithm.
Definition drainage_basin_cell_based.hpp:28
@ FDM_D8
Standard D8 flow direction algorithm.
Definition drainage_basin_cell_based.hpp:27
Mat< std::vector< glm::ivec2 > > invert_receiver_map(const Mat< glm::ivec2 > &receivers)
Invert the receiver map to build a map of children/downstream receivers.
Definition drainage_basin_cell_based.cpp:587
Definition algebra.hpp:31
Mat class for basic manipulation of 2D matrices.
Definition algebra.hpp:92