Easy3D 2.5.3
Tutorial_109_Graph_Connectivity
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#include <easy3d/core/graph.h>
28#include <easy3d/util/initializer.h>
29
30
31using namespace easy3d;
32
33// This example shows how to
34// - access the incident vertices of each vertex
35// - access the incident edges of each vertex
36// - access the two end points of each edge
37
38// There are two ways to traverse the incident entities of an element.
39// - use a "for" loop (cleaner code);
40// - use a circulator.
41#define USE_FOR_LOOP
42
43// the graph created in the previous tutorial (so you can skip it)
44Graph* old_graph_from_previous_example() {
45 // Create a graph
46 auto graph = new Graph;
47
48 // Add 4 vertices
49 Graph::Vertex v0 = graph->add_vertex(vec3(0, 0, 0));
50 Graph::Vertex v1 = graph->add_vertex(vec3(1, 0, 0));
51 Graph::Vertex v2 = graph->add_vertex(vec3(0, 1, 0));
52 Graph::Vertex v3 = graph->add_vertex(vec3(0, 0, 1));
53
54 // Add some edges
55 graph->add_edge(v0, v1); // e0
56 graph->add_edge(v1, v2); // e1
57 graph->add_edge(v2, v3); // e2
58 graph->add_edge(v3, v0); // e3
59 graph->add_edge(v1, v3); // e4
60
61 return graph;
62}
63
64
65int main(int argc, char** argv) {
66 // initialize Easy3D.
67 initialize();
68
69 Graph* graph = old_graph_from_previous_example();
70
71 std::cout << "----------------------------------------\n";
72 std::cout << "The incident vertices of each vertex" << std::endl;
73 std::cout << "----------------------------------------\n";
74
75 // loop over all vertices
76 for (auto v : graph->vertices()) {
77 std::cout << "incident vertices of vertex " << v << ": ";
78#ifdef USE_FOR_LOOP
79 // loop over all incident vertices
80 for (auto vv : graph->vertices(v))
81 std::cout << vv << " ";
82#else // use circulator
85 do {
86 Graph::Vertex vv = *cir;
87 std::cout << vv << " ";
88 ++cir;
89 } while (cir != end);
90#endif
91 std::cout << std::endl;
92 }
93
94 std::cout << "\n--------------------------------------\n";
95 std::cout << "The incident edges of each vertex" << std::endl;
96 std::cout << "----------------------------------------\n";
97
98 // loop over all vertices
99 for (auto v : graph->vertices()) {
100 std::cout << "incident edges of vertex " << v << ": ";
101#ifdef USE_FOR_LOOP
102 // loop over all incident outgoing/ingoing edges
103 for (auto h : graph->edges(v))
104 std::cout << h << " ";
105#else // use circulator
108 do {
109 Graph::Edge h = *cir;
110 std::cout << h << " ";
111 ++cir;
112 } while (cir != end);
113#endif
114 std::cout << std::endl;
115 }
116
117 std::cout << "\n--------------------------------------\n";
118 std::cout << "The two end points of each edge" << std::endl;
119 std::cout << "----------------------------------------\n";
120
121 // loop over all edges
122 for (auto e : graph->edges()) {
123 std::cout << "the two end points of edge " << e << ": ";
124 Graph::Vertex vs = graph->vertex(e, 0);
125 std::cout << vs << " ";
126 Graph::Vertex vt = graph->vertex(e, 1);
127 std::cout << vt << " " << std::endl;
128 }
129
130 // Delete the graph (i.e., release memory)
131 delete graph;
132
133 return EXIT_SUCCESS;
134}
A Graph data structure with easy property management.
Definition: graph.h:51
EdgeContainer edges() const
returns edge container for C++11 range-based for-loops
Definition: graph.h:816
VertexContainer vertices() const
returns vertex container for C++11 range-based for-loops
Definition: graph.h:798
Vertex vertex(Edge e, unsigned int i) const
returns the i'th vertex of edge e. i has to be 0 or 1.
Definition: graph.h:618
Definition: collider.cpp:182
void initialize(bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition: initializer.cpp:35
Definition: graph.h:112
Definition: graph.h:103