HighMap library (C++)
Loading...
Searching...
No Matches
graph.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
22#pragma once
23#include <cmath>
24
25#include "highmap/array.hpp"
28
29namespace hmap
30{
31
32class Cloud;
33
37struct Edge
38{
39 int u{0};
40 int v{0};
41 float weight{0.f};
42};
43
48{
49 int target{0};
50 float weight{0.f};
51 int edge_index{-1};
52};
53
69class Graph : public Cloud
70{
71public:
75 Graph() = default;
76
81 Graph(Cloud cloud);
82
87 Graph(std::vector<Point> points);
88
94 Graph(std::vector<float> x, std::vector<float> y);
95
102 void add_edge(int u, int v, float weight);
103
110 void add_edge(int u, int v);
111
117 void add_edge(glm::ivec2 edge, float weight);
118
123 void add_edge(glm::ivec2 edge);
124
128 void clear_edges() noexcept;
129
140 std::vector<float> compute_distance_to_outlet() const;
141
155 std::vector<float> compute_flow_accumulation() const;
156
169 std::vector<float> compute_relative_widths(float exponent = 1.0f) const;
170
180 std::vector<int> compute_strahler_order() const;
181
187 size_t degree(int u) const;
188
201 std::vector<int> dijkstra(int source_point_index,
202 int target_point_index) const;
203
208 bool empty_edges() const noexcept;
209
220 void enforce_downhill_elevation(float min_slope = 0.001f);
221
227 Edge get_edge(size_t k) const;
228
234 float get_edge_length(size_t k) const;
235
240 std::vector<float> get_edge_x_pairs() const;
241
246 std::vector<float> get_edge_y_pairs() const;
247
252 const std::vector<Edge> &get_edges() const noexcept;
253
258 std::vector<float> get_lengths() const;
259
264 size_t get_nedges() const noexcept;
265
274
280 const std::vector<Neighbor> &neighbors(int u) const;
281
286 size_t num_edges() const noexcept;
287
291 void print() const;
292
298
304 void set_edge_weight(size_t k, float weight);
305
312 void set_edge_weight(int u, int v, float weight);
313
320 void to_array(Array &array,
321 const BBox2D &bbox,
322 bool color_by_edge_weight = true) const;
323
334 void to_array_fractalize(Array &array,
335 const BBox2D &bbox,
336 int iterations,
337 std::uint32_t seed,
338 float sigma = 0.3f,
339 int orientation = 0,
340 float persistence = 1.f) const;
341
352 Array to_array_sdf(glm::ivec2 shape,
353 const BBox2D &bbox,
354 Array *p_noise_x = nullptr,
355 Array *p_noise_y = nullptr,
356 const BBox2D &bbox_array = BBox2D{}) const;
357
363 void to_csv(const std::string &fname_xy,
364 const std::string &fname_edges) const;
365
371 void to_png(const std::string &fname, glm::ivec2 shape = {512, 512}) const;
372
373private:
374 std::vector<Edge> edge_list;
375 std::vector<std::vector<Neighbor>> adj_list;
376
377 static const std::vector<Neighbor> empty_neighbors;
378
379 void ensure_adj_capacity(size_t node_count);
380};
381
382} // 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:31
Represents a collection of unordered points in 2D space.
Definition cloud.hpp:61
Cloud()
Default constructor for the Cloud class.
std::vector< Point > points
Points of the cloud.
Definition cloud.hpp:663
std::vector< float > get_edge_x_pairs() const
Return x coordinates of the edges (as pairs).
Definition graph.cpp:700
std::vector< float > get_lengths() const
Get the Euclidean lengths of all the edges.
Definition graph.cpp:737
void to_png(const std::string &fname, glm::ivec2 shape={512, 512}) const
Export the graph as a PNG image file.
Definition graph.cpp:997
std::vector< float > compute_flow_accumulation() const
Compute accumulated upstream flow length along directed stream edges.
Definition graph.cpp:211
void to_array(Array &array, const BBox2D &bbox, bool color_by_edge_weight=true) const
Project the graph to an array and optionally color by edge weight.
Definition graph.cpp:902
void enforce_downhill_elevation(float min_slope=0.001f)
Enforce monotonic downhill elevations on all graph nodes.
Definition graph.cpp:520
void print() const
Print the graph data to the standard output.
Definition graph.cpp:813
bool empty_edges() const noexcept
Check whether the graph has no edges.
Definition graph.cpp:678
std::vector< float > compute_distance_to_outlet() const
Compute the distance to the downstream outlet for each node in the graph.
Definition graph.cpp:104
std::vector< float > get_edge_y_pairs() const
Return y coordinates of the edges (as pairs).
Definition graph.cpp:716
Graph()=default
Construct a new Graph object.
void to_csv(const std::string &fname_xy, const std::string &fname_edges) const
Export graph data to CSV files.
Definition graph.cpp:983
const std::vector< Edge > & get_edges() const noexcept
Get the edge list.
Definition graph.cpp:732
const std::vector< Neighbor > & neighbors(int u) const
Get the neighbors of node u.
Definition graph.cpp:801
void add_edge(int u, int v, float weight)
Add an edge connecting two node indices with explicit weight.
Definition graph.cpp:63
Graph minimum_spanning_tree_prim() const
Generate a Minimum Spanning Tree (MST) using Prim's algorithm.
Definition graph.cpp:751
std::vector< float > compute_relative_widths(float exponent=1.0f) const
Compute relative channel width ratio in [0, 1] for each node in the graph.
Definition graph.cpp:300
Edge get_edge(size_t k) const
Get an edge by index.
Definition graph.cpp:683
Graph remove_orphan_points() const
Remove orphan points (points with no connected edges).
Definition graph.cpp:836
size_t num_edges() const noexcept
Get the number of edges in the graph.
Definition graph.cpp:808
void to_array_fractalize(Array &array, const BBox2D &bbox, int iterations, std::uint32_t seed, float sigma=0.3f, int orientation=0, float persistence=1.f) const
Apply fractalization to graph edges and project to an array.
Definition graph.cpp:926
size_t degree(int u) const
Get the degree (number of incident edges) of node u.
Definition graph.cpp:608
Array to_array_sdf(glm::ivec2 shape, const BBox2D &bbox, Array *p_noise_x=nullptr, Array *p_noise_y=nullptr, const BBox2D &bbox_array=BBox2D{}) const
Generate an array filled with the Signed Distance Function (SDF) to the graph.
Definition graph.cpp:968
std::vector< int > compute_strahler_order() const
Compute the Strahler stream order for each node in the graph.
Definition graph.cpp:396
void clear_edges() noexcept
Clear all edges from the graph.
Definition graph.cpp:98
float get_edge_length(size_t k) const
Get the Euclidean length of edge k.
Definition graph.cpp:689
size_t get_nedges() const noexcept
Get the number of edges in the graph.
Definition graph.cpp:746
std::vector< int > dijkstra(int source_point_index, int target_point_index) const
Return the shortest route between two points using Dijkstra's algorithm.
Definition graph.cpp:615
void set_edge_weight(size_t k, float weight)
Set the weight of edge k.
Definition graph.cpp:869
Definition of the Cloud class for manipulating sets of 2D points.
float f(int i, float gi)
Definition distance_transform.cpp:14
Definition opencv.hpp:20
Axis-aligned 2D bounding box in world coordinates.
Definition bbox2d.hpp:25
Graph edge connecting two node indices with an associated weight.
Definition graph.hpp:38
int u
First node index.
Definition graph.hpp:39
float weight
Edge weight (cost or length).
Definition graph.hpp:41
int v
Second node index.
Definition graph.hpp:40
Graph neighbor information for adjacency representation.
Definition graph.hpp:48
float weight
Edge weight.
Definition graph.hpp:50
int target
Neighbor node index.
Definition graph.hpp:49
int edge_index
Index into the edge list.
Definition graph.hpp:51