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
30#pragma once
31#include <cmath>
32
33#include "highmap/array.hpp"
36
37namespace hmap
38{
39
40class Cloud;
41
57class Graph : public Cloud
58{
59public:
68 std::vector<std::vector<int>> edges = {};
69
78 std::vector<float> weights = {};
79
88 std::vector<std::vector<int>> connectivity = {};
89
98 std::map<std::pair<int, int>, float> adjacency_matrix;
99
108 Graph() : Cloud(){};
109
120 Graph(Cloud cloud) : Cloud(cloud){};
121
132 Graph(std::vector<Point> points) : Cloud(points){};
133
146 Graph(std::vector<float> x, std::vector<float> y) : Cloud(x, y){};
147
161 void add_edge(std::vector<int> edge, float weight);
162
174 void add_edge(std::vector<int> edge);
175
194 std::vector<int> dijkstra(int source_point_index, int target_point_index);
195
206 float get_edge_length(int k);
207
217 std::vector<float> get_edge_x_pairs();
218
228 std::vector<float> get_edge_y_pairs();
229
239 std::vector<float> get_lengths();
240
250 size_t get_nedges();
251
268
275 void print();
276
287
301 void to_array(Array &array,
302 Vec4<float> bbox,
303 bool color_by_edge_weight = true);
304
325 void to_array_fractalize(Array &array,
326 Vec4<float> bbox,
327 int iterations,
328 uint seed,
329 float sigma = 0.3f,
330 int orientation = 0.f,
331 float persistence = 1.f);
332
358 Vec4<float> bbox,
359 Array *p_noise_x = nullptr,
360 Array *p_noise_y = nullptr,
361 Vec4<float> bbox_array = {0.f, 1.f, 0.f, 1.f});
362
376 void to_csv(std::string fname_xy, std::string fname_adjacency);
377
388 void to_png(std::string fname, Vec2<int> shape = {512, 512});
389
398
405 void update_connectivity();
406};
407} // namespace hmap
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
Represents a collection of unordered points in 2D space.
Definition cloud.hpp:55
std::vector< Point > points
Points of the cloud.
Definition cloud.hpp:57
Graph class, to manipulate graphs in 2D.
Definition graph.hpp:58
std::vector< float > get_edge_y_pairs()
Return y coordinates of the edges (as pairs).
Definition graph.cpp:127
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:21
Graph(std::vector< Point > points)
Construct a new Graph object based on a list of points.
Definition graph.hpp:132
Graph()
Construct a new Graph object.
Definition graph.hpp:108
void add_edge(std::vector< int > edge, float weight)
Add an edge to the graph.
Definition graph.cpp:86
void to_array_fractalize(Array &array, Vec4< float > bbox, int iterations, uint 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:285
void to_csv(std::string fname_xy, std::string fname_adjacency)
Export graph data to CSV files.
Definition graph.cpp:359
void update_adjacency_matrix()
Update the adjacency matrix of the graph.
Definition graph.cpp:393
Graph(std::vector< float > x, std::vector< float > y)
Construct a new Graph object based on x and y coordinates.
Definition graph.hpp:146
std::vector< float > weights
Edge weights.
Definition graph.hpp:78
void print()
Print the graph data to the standard output.
Definition graph.cpp:194
std::map< std::pair< int, int >, float > adjacency_matrix
Adjacency matrix.
Definition graph.hpp:98
void to_png(std::string fname, Vec2< int > shape={512, 512})
Export the graph as a PNG image file.
Definition graph.cpp:386
float get_edge_length(int k)
Get the length of edge k.
Definition graph.cpp:98
std::vector< float > get_edge_x_pairs()
Return x coordinates of the edges (as pairs).
Definition graph.cpp:115
std::vector< float > get_lengths()
Get the length of all the edges.
Definition graph.cpp:104
std::vector< std::vector< int > > connectivity
Store point connectivity.
Definition graph.hpp:88
size_t get_nedges()
Get the number of edges in the graph.
Definition graph.cpp:139
void to_array(Array &array, Vec4< float > bbox, bool color_by_edge_weight=true)
Project the graph to an array and optionally color by edge weight.
Definition graph.cpp:263
Graph remove_orphan_points()
Remove orphan points from the graph.
Definition graph.cpp:217
void update_connectivity()
Update the point connectivity information.
Definition graph.cpp:407
Graph minimum_spanning_tree_prim()
Generate a Minimum Spanning Tree (MST) of the graph using Prim's algorithm.
Definition graph.cpp:144
std::vector< std::vector< int > > edges
Edges of the graph.
Definition graph.hpp:68
Array to_array_sdf(Vec2< int > shape, Vec4< float > bbox, Array *p_noise_x=nullptr, Array *p_noise_y=nullptr, Vec4< float > 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:315
Graph(Cloud cloud)
Construct a new Graph object based on a cloud of points.
Definition graph.hpp:120
Definition of the Cloud class for manipulating sets of 2D points.
Definition algebra.hpp:28
Defines a class for representing and manipulating 3D points.
Vec2 class for basic manipulation of 2D vectors.
Definition algebra.hpp:40
Vec4 class for basic manipulation of 4D vectors.
Definition algebra.hpp:564