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
49class Graph : public Cloud
50{
51public:
60 std::vector<std::vector<int>> edges = {};
61
70 std::vector<float> weights = {};
71
80 std::vector<std::vector<int>> connectivity = {};
81
90 std::map<std::pair<int, int>, float> adjacency_matrix;
91
100 Graph() : Cloud(){};
101
112 Graph(Cloud cloud) : Cloud(cloud){};
113
124 Graph(std::vector<Point> points) : Cloud(points){};
125
138 Graph(std::vector<float> x, std::vector<float> y) : Cloud(x, y){};
139
153 void add_edge(std::vector<int> edge, float weight);
154
166 void add_edge(std::vector<int> edge);
167
186 std::vector<int> dijkstra(int source_point_index, int target_point_index);
187
198 float get_edge_length(int k);
199
209 std::vector<float> get_edge_x_pairs();
210
220 std::vector<float> get_edge_y_pairs();
221
231 std::vector<float> get_lengths();
232
242 size_t get_nedges();
243
260
267 void print();
268
279
293 void to_array(Array &array, glm::vec4 bbox, bool color_by_edge_weight = true);
294
315 void to_array_fractalize(Array &array,
316 glm::vec4 bbox,
317 int iterations,
318 std::uint32_t seed,
319 float sigma = 0.3f,
320 int orientation = 0.f,
321 float persistence = 1.f);
322
347 Array to_array_sdf(glm::ivec2 shape,
348 glm::vec4 bbox,
349 Array *p_noise_x = nullptr,
350 Array *p_noise_y = nullptr,
351 glm::vec4 bbox_array = {0.f, 1.f, 0.f, 1.f});
352
366 void to_csv(std::string fname_xy, std::string fname_adjacency);
367
378 void to_png(std::string fname, glm::ivec2 shape = {512, 512});
379
388
395 void update_connectivity();
396};
397} // 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 collection of unordered points in 2D space.
Definition cloud.hpp:49
std::vector< Point > points
Points of the cloud.
Definition cloud.hpp:51
Graph class, to manipulate graphs in 2D.
Definition graph.hpp:50
std::vector< float > get_edge_y_pairs()
Return y coordinates of the edges (as pairs).
Definition graph.cpp:135
std::vector< int > dijkstra(int source_point_index, int target_point_index)
Return the shortest route between two points using Dijkstra's algorithm.
Definition graph.cpp:29
Graph(std::vector< Point > points)
Construct a new Graph object based on a list of points.
Definition graph.hpp:124
Graph()
Construct a new Graph object.
Definition graph.hpp:100
void add_edge(std::vector< int > edge, float weight)
Add an edge to the graph.
Definition graph.cpp:94
void to_png(std::string fname, glm::ivec2 shape={512, 512})
Export the graph as a PNG image file.
Definition graph.cpp:394
void to_csv(std::string fname_xy, std::string fname_adjacency)
Export graph data to CSV files.
Definition graph.cpp:367
void update_adjacency_matrix()
Update the adjacency matrix of the graph.
Definition graph.cpp:401
Graph(std::vector< float > x, std::vector< float > y)
Construct a new Graph object based on x and y coordinates.
Definition graph.hpp:138
std::vector< float > weights
Edge weights.
Definition graph.hpp:70
void print()
Print the graph data to the standard output.
Definition graph.cpp:202
std::map< std::pair< int, int >, float > adjacency_matrix
Adjacency matrix.
Definition graph.hpp:90
float get_edge_length(int k)
Get the length of edge k.
Definition graph.cpp:106
std::vector< float > get_edge_x_pairs()
Return x coordinates of the edges (as pairs).
Definition graph.cpp:123
std::vector< float > get_lengths()
Get the length of all the edges.
Definition graph.cpp:112
std::vector< std::vector< int > > connectivity
Store point connectivity.
Definition graph.hpp:80
void to_array_fractalize(Array &array, glm::vec4 bbox, int iterations, std::uint32_t seed, float sigma=0.3f, int orientation=0.f, float persistence=1.f)
Apply fractalization to graph edges and project to an array.
Definition graph.cpp:293
size_t get_nedges()
Get the number of edges in the graph.
Definition graph.cpp:147
Graph remove_orphan_points()
Remove orphan points from the graph.
Definition graph.cpp:225
void update_connectivity()
Update the point connectivity information.
Definition graph.cpp:415
Graph minimum_spanning_tree_prim()
Generate a Minimum Spanning Tree (MST) of the graph using Prim's algorithm.
Definition graph.cpp:152
Array to_array_sdf(glm::ivec2 shape, glm::vec4 bbox, Array *p_noise_x=nullptr, Array *p_noise_y=nullptr, glm::vec4 bbox_array={0.f, 1.f, 0.f, 1.f})
Generate an array filled with the Signed Distance Function (SDF) to the graph.
Definition graph.cpp:323
std::vector< std::vector< int > > edges
Edges of the graph.
Definition graph.hpp:60
void to_array(Array &array, glm::vec4 bbox, bool color_by_edge_weight=true)
Project the graph to an array and optionally color by edge weight.
Definition graph.cpp:271
Graph(Cloud cloud)
Construct a new Graph object based on a cloud of points.
Definition graph.hpp:112
Definition of the Cloud class for manipulating sets of 2D points.
Definition algebra.hpp:23