Easy3D 2.6.1
Loading...
Searching...
No Matches
graph.h
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#ifndef EASY3D_CORE_GRAPH_H
28#define EASY3D_CORE_GRAPH_H
29
30
31#include <easy3d/core/model.h>
32
33#include <vector>
34
35#include <easy3d/core/types.h>
36#include <easy3d/core/property.h>
37
38
39namespace easy3d {
40
41
49 class Graph : public Model
50 {
51
52 public: //------------------------------------------------------ topology types
53
59 {
60 public:
65 explicit BaseHandle(int _idx = -1) : idx_(_idx) {}
66
71 int idx() const { return idx_; }
72
76 void reset() { idx_ = -1; }
77
82 bool is_valid() const { return idx_ != -1; }
83
89 bool operator==(const BaseHandle& _rhs) const {
90 return idx_ == _rhs.idx_;
91 }
92
98 bool operator!=(const BaseHandle& _rhs) const {
99 return idx_ != _rhs.idx_;
100 }
101
107 bool operator<(const BaseHandle& _rhs) const {
108 return idx_ < _rhs.idx_;
109 }
110
112 struct Hash {
118 std::size_t operator()(const BaseHandle& h) const { return h.idx(); }
119 };
120
121 private:
122 friend class Graph;
123 int idx_;
124 };
125
126
132 {
137 explicit Vertex(int _idx = -1) : BaseHandle(_idx) {}
143 std::ostream& operator<<(std::ostream& os) const { return os << 'v' << idx(); }
144 };
145
151 {
156 explicit Edge(int _idx = -1) : BaseHandle(_idx) {}
157 };
158
159
160 public: //-------------------------------------------------- connectivity types
161
167 {
169 std::vector<Edge> edges_;
170 };
171
181
182
183 public: //------------------------------------------------------ property types
184
190 template <class T> class VertexProperty : public Property<T>
191 {
192 public:
194 explicit VertexProperty() = default;
199 explicit VertexProperty(Property<T> p) : Property<T>(p) {}
200
207 {
208 return Property<T>::operator[](v.idx());
209 }
210
217 {
218 return Property<T>::operator[](v.idx());
219 }
220 };
221
222
228 template <class T> class EdgeProperty : public Property<T>
229 {
230 public:
232 explicit EdgeProperty() = default;
237 explicit EdgeProperty(Property<T> p) : Property<T>(p) {}
238
245 {
246 return Property<T>::operator[](e.idx());
247 }
248
255 {
256 return Property<T>::operator[](e.idx());
257 }
258 };
259
265 template <class T> class ModelProperty : public Property<T>
266 {
267 public:
269 explicit ModelProperty() = default;
274 explicit ModelProperty(Property<T> p) : Property<T>(p) {}
275
281 typename Property<T>::reference operator[](size_t idx) override
282 {
283 return Property<T>::operator[](idx);
284 }
285
291 typename Property<T>::const_reference operator[](size_t idx) const override
292 {
293 return Property<T>::operator[](idx);
294 }
295 };
296
297
298
299 public: //------------------------------------------------------ iterator types
300
307 {
308 public:
314 explicit VertexIterator(Vertex v = Vertex(), const Graph* g = nullptr) : hnd_(v), graph_(g)
315 {
316 if (graph_ && graph_->has_garbage()) while (graph_->is_valid(hnd_) && graph_->is_deleted(hnd_)) ++hnd_.idx_;
317 }
318
323 Vertex operator*() const { return hnd_; }
324
330 bool operator==(const VertexIterator& rhs) const
331 {
332 return (hnd_ == rhs.hnd_);
333 }
334
340 bool operator!=(const VertexIterator& rhs) const
341 {
342 return !operator==(rhs);
343 }
344
350 {
351 ++hnd_.idx_;
352 assert(graph_);
353 while (graph_->has_garbage() && graph_->is_valid(hnd_) && graph_->is_deleted(hnd_)) ++hnd_.idx_;
354 return *this;
355 }
356
362 {
363 --hnd_.idx_;
364 assert(graph_);
365 while (graph_->has_garbage() && graph_->is_valid(hnd_) && graph_->is_deleted(hnd_)) --hnd_.idx_;
366 return *this;
367 }
368
369 private:
370 Vertex hnd_;
371 const Graph* graph_;
372 };
373
374
381 {
382 public:
383
385 explicit EdgeIterator(Edge e = Edge(), const Graph* g = nullptr) : hnd_(e), graph_(g)
386 {
387 if (graph_ && graph_->has_garbage()) while (graph_->is_valid(hnd_) && graph_->is_deleted(hnd_)) ++hnd_.idx_;
388 }
389
394 Edge operator*() const { return hnd_; }
395
401 bool operator==(const EdgeIterator& rhs) const
402 {
403 return (hnd_ == rhs.hnd_);
404 }
405
411 bool operator!=(const EdgeIterator& rhs) const
412 {
413 return !operator==(rhs);
414 }
415
421 {
422 ++hnd_.idx_;
423 assert(graph_);
424 while (graph_->has_garbage() && graph_->is_valid(hnd_) && graph_->is_deleted(hnd_)) ++hnd_.idx_;
425 return *this;
426 }
427
433 {
434 --hnd_.idx_;
435 assert(graph_);
436 while (graph_->has_garbage() && graph_->is_valid(hnd_) && graph_->is_deleted(hnd_)) --hnd_.idx_;
437 return *this;
438 }
439
440 private:
441 Edge hnd_;
442 const Graph* graph_;
443 };
444
445
446 public: //-------------------------- containers for C++11 range-based for loops
447
453 {
454 public:
460 VertexContainer(VertexIterator _begin, VertexIterator _end) : begin_(_begin), end_(_end) {}
462 VertexIterator begin() const { return begin_; }
464 VertexIterator end() const { return end_; }
465 private:
466 VertexIterator begin_, end_;
467 };
468
474 {
475 public:
481 EdgeContainer(EdgeIterator _begin, EdgeIterator _end) : begin_(_begin), end_(_end) {}
483 EdgeIterator begin() const { return begin_; }
485 EdgeIterator end() const { return end_; }
486 private:
487 EdgeIterator begin_, end_;
488 };
489
490
491 public: //---------------------------------------------------- circulator types
492
499 {
500 public:
507 : graph_(g), vertex_(v), finished_(false)
508 {
509 iterator_ = graph_->vconn_[vertex_].edges_.begin();
510 end_ = graph_->vconn_[vertex_].edges_.end();
511 }
512
518 bool operator==(const EdgeAroundVertexCirculator& rhs) const {
519 assert(graph_);
520 return ((graph_ == rhs.graph_) && (vertex_ == rhs.vertex_) && (iterator_ == rhs.iterator_))
521 || (finished_); // to behave like a circulator
522 }
523
529 bool operator!=(const EdgeAroundVertexCirculator& rhs) const {
530 return !operator==(rhs);
531 }
532
538 assert(graph_);
539 ++iterator_;
540 if (iterator_ == end_) { // to behave like a circulator
541 iterator_ = graph_->vconn_[vertex_].edges_.begin();
542 finished_ = true;
543 }
544 return *this;
545 }
546
552 {
553 assert(graph_);
554 --iterator_;
555 return *this;
556 }
557
562 Edge operator*() const {
563 assert(graph_);
564 if (iterator_ != end_) return *iterator_;
565 else return Edge();
566 }
567
572 operator bool() const { return !graph_->vconn_[vertex_].edges_.empty(); }
573
578 Vertex vertex() const { return vertex_; }
579
581 EdgeAroundVertexCirculator& begin() { iterator_ = graph_->vconn_[vertex_].edges_.begin(); return *this; }
583 EdgeAroundVertexCirculator& end() { iterator_ = end_; return *this; }
584
585 private:
586 const Graph* graph_;
587 const Vertex vertex_;
588 std::vector<Edge>::const_iterator iterator_;
589 // helper for C++11 range-based for-loops
590 std::vector<Edge>::const_iterator end_;
591 bool finished_; // for the circulator behavior
592 };
593
594
601 {
602 public:
603
606 : graph_(g), vertex_(v), finished_(false)
607 {
608 iterator_ = graph_->vconn_[vertex_].edges_.begin();
609 end_ = graph_->vconn_[vertex_].edges_.end();
610 }
611
618 assert(graph_);
619 return ((graph_ == rhs.graph_) && (vertex_ == rhs.vertex_) && (iterator_ == rhs.iterator_))
620 || (finished_); // to behave like a circulator
621 }
622
629 return !operator==(rhs);
630 }
631
637 assert(graph_);
638 ++iterator_;
639 if (iterator_ == end_) { // to behave like a circulator
640 iterator_ = graph_->vconn_[vertex_].edges_.begin();
641 finished_ = true;
642 }
643 return *this;
644 }
645
651 {
652 assert(graph_);
653 --iterator_;
654 return *this;
655 }
656
662 assert(graph_);
663 if (iterator_ != end_) {
664 Vertex v = graph_->vertex(*iterator_, 1);
665 if (v != vertex_) return v;
666 else return graph_->vertex(*iterator_, 0);
667 }
668 return Vertex();
669 }
670
675 operator bool() const { return !graph_->vconn_[vertex_].edges_.empty(); }
676
681 Vertex vertex() const { return vertex_; }
682
685 iterator_ = graph_->vconn_[vertex_].edges_.begin();
686 return *this;
687 }
688
690 iterator_ = end_;
691 return *this;
692 }
693
694 private:
695 const Graph* graph_;
696 const Vertex vertex_;
697 std::vector<Edge>::const_iterator iterator_;
698 // helper for C++11 range-based for-loops
699 std::vector<Edge>::const_iterator end_;
700 bool finished_; // for the circulator behavior
701 };
702
703
704
705 public: //-------------------------------------------- constructor / destructor
706
708
709
711 Graph();
712
714 ~Graph() override = default;
715
720 Graph(const Graph& rhs) { operator=(rhs); }
721
727 Graph& operator=(const Graph& rhs);
728
734 Graph& assign(const Graph& rhs);
735
737
738 public: //----------------------------------------------- add new vertex / face
739
741
742
748 Vertex add_vertex(const vec3& p);
749
756 Edge add_edge(const Vertex& v1, const Vertex& v2);
758
759 public: //--------------------------------------------------- memory management
760
762
763
768 unsigned int vertices_size() const { return static_cast<unsigned int>(vprops_.size()); }
773 unsigned int edges_size() const { return static_cast<unsigned int>(eprops_.size()); }
774
779 unsigned int n_vertices() const { return vertices_size() - deleted_vertices_; }
784 unsigned int n_edges() const { return edges_size() - deleted_edges_; }
785
790 void clear();
796 void reserve(unsigned int nvertices, unsigned int nedges);
802 void resize(unsigned int nv, unsigned int ne) {
803 vprops_.resize(nv);
804 eprops_.resize(ne);
805 }
806
811 bool has_garbage() const { return garbage_; }
815 void collect_garbage();
816
823 bool is_deleted(Vertex v) const { return vdeleted_[v]; }
830 bool is_deleted(Edge e) const { return edeleted_[e]; }
831
837 bool is_valid(Vertex v) const
838 {
839 return (0 <= v.idx()) && (v.idx() < static_cast<int>(vertices_size()));
840 }
841
846 bool is_valid(Edge e) const
847 {
848 return (0 <= e.idx()) && (e.idx() < static_cast<int>(edges_size()));
849 }
850
851
852
853 public: //---------------------------------------------- low-level connectivity
854
856
857
863 bool is_isolated(Vertex v) const
864 {
865 return vconn_[v].edges_.empty();
866 }
867
874 Vertex vertex(Edge e, unsigned int i) const
875 {
876 assert(i<=1);
877 if (i == 0)
878 return econn_[e].source_;
879 else
880 return econn_[e].target_;
881 }
882
887 Vertex source(Edge e) const { return econn_[e].source_; }
892 Vertex target(Edge e) const { return econn_[e].target_; }
894
895 public: //--------------------------------------------------- property handling
896
898
899
908 template <class T> VertexProperty<T> add_vertex_property(const std::string& name, const T t = T())
909 {
910 return VertexProperty<T>(vprops_.add<T>(name, t));
911 }
912
920 template <class T> EdgeProperty<T> add_edge_property(const std::string& name, const T t = T())
921 {
922 return EdgeProperty<T>(eprops_.add<T>(name, t));
923 }
924
932 template <class T> ModelProperty<T> add_model_property(const std::string& name, const T t = T())
933 {
934 return ModelProperty<T>(mprops_.add<T>(name, t));
935 }
936
944 template <class T> VertexProperty<T> get_vertex_property(const std::string& name) const
945 {
946 return VertexProperty<T>(vprops_.get<T>(name));
947 }
948
955 template <class T> EdgeProperty<T> get_edge_property(const std::string& name) const
956 {
957 return EdgeProperty<T>(eprops_.get<T>(name));
958 }
959
966 template <class T> ModelProperty<T> get_model_property(const std::string& name) const
967 {
968 return ModelProperty<T>(mprops_.get<T>(name));
969 }
970
978 template <class T> VertexProperty<T> vertex_property(const std::string& name, const T t = T())
979 {
980 return VertexProperty<T>(vprops_.get_or_add<T>(name, t));
981 }
982
989 template <class T> EdgeProperty<T> edge_property(const std::string& name, const T t = T())
990 {
991 return EdgeProperty<T>(eprops_.get_or_add<T>(name, t));
992 }
993
1001 template <class T> ModelProperty<T> model_property(const std::string& name, const T t = T())
1002 {
1003 return ModelProperty<T>(mprops_.get_or_add<T>(name, t));
1004 }
1005
1007 template<class T>
1008 bool remove_vertex_property(VertexProperty<T> &p) { return vprops_.remove(p); }
1009
1011 bool remove_vertex_property(const std::string &n) { return vprops_.remove(n); }
1012
1014 template<class T>
1015 bool remove_edge_property(EdgeProperty<T> &p) { return eprops_.remove(p); }
1016
1018 bool remove_edge_property(const std::string &n) { return eprops_.remove(n); }
1019
1021 template<class T>
1022 bool remove_model_property(ModelProperty<T> &p) { return mprops_.remove(p); }
1023
1025 bool remove_model_property(const std::string &n) { return mprops_.remove(n); }
1026
1028 bool rename_vertex_property(const std::string &old_name, const std::string &new_name) {
1029 return vprops_.rename(old_name, new_name);
1030 }
1031
1033 bool rename_edge_property(const std::string &old_name, const std::string &new_name) {
1034 return eprops_.rename(old_name, new_name);
1035 }
1036
1038 bool rename_model_property(const std::string &old_name, const std::string &new_name) {
1039 return mprops_.rename(old_name, new_name);
1040 }
1041
1048 const std::type_info& get_vertex_property_type(const std::string& name) const
1049 {
1050 return vprops_.get_type(name);
1051 }
1052
1058 const std::type_info& get_edge_property_type(const std::string& name) const
1059 {
1060 return eprops_.get_type(name);
1061 }
1062
1068 const std::type_info& get_model_property_type(const std::string& name) const
1069 {
1070 return mprops_.get_type(name);
1071 }
1072
1073
1075 std::vector<std::string> vertex_properties() const
1076 {
1077 return vprops_.properties();
1078 }
1079
1080 std::vector<std::string> edge_properties() const
1081 {
1082 return eprops_.properties();
1083 }
1084
1085 std::vector<std::string> model_properties() const
1086 {
1087 return mprops_.properties();
1088 }
1089
1094 void property_stats(std::ostream& output) const override;
1095
1097
1098
1099 public: //--------------------------------------------- iterators & circulators
1100
1102
1103
1109 {
1110 return VertexIterator(Vertex(0), this);
1111 }
1112
1118 {
1119 return VertexIterator(Vertex(static_cast<int>(vertices_size())), this);
1120 }
1121
1127 {
1129 }
1130
1136 {
1137 return EdgeIterator(Edge(0), this);
1138 }
1139
1145 {
1146 return EdgeIterator(Edge(static_cast<int>(edges_size())), this);
1147 }
1148
1154 {
1155 return EdgeContainer(edges_begin(), edges_end());
1156 }
1157
1164 {
1165 return VertexAroundVertexCirculator(this, v);
1166 }
1167
1174 {
1175 return EdgeAroundVertexCirculator(this, v);
1176 }
1177
1178
1179
1180 public: //--------------------------------------------- higher-level operations
1181
1183
1184
1190 unsigned int valence(Vertex v) const;
1191
1198 Edge find_edge(Vertex a, Vertex b) const;
1199
1204 void delete_vertex(Vertex v);
1205
1210 void delete_edge(Edge e);
1212
1213
1214 public: //------------------------------------------ geometry-related functions
1215
1217
1218
1224 const vec3& position(Vertex v) const { return vpoint_[v]; }
1225
1231 vec3& position(Vertex v) { return vpoint_[v]; }
1232
1237 const std::vector<vec3>& points() const override { return vpoint_.vector(); }
1238
1243 std::vector<vec3>& points() override { return vpoint_.vector(); }
1244
1250 float edge_length(Edge e) const;
1251
1253
1254
1255 private: //---------------------------------------------- allocate new elements
1256
1258 Vertex new_vertex()
1259 {
1260 vprops_.push_back();
1261 return Vertex(static_cast<int>(vertices_size() - 1));
1262 }
1263
1265 Edge new_edge()
1266 {
1267 eprops_.push_back();
1268 return Edge(static_cast<int>(edges_size() - 1));
1269 }
1270
1271
1272 private: //------------------------------------------------------- private data
1273
1274 PropertyContainer vprops_;
1275 PropertyContainer eprops_;
1276 PropertyContainer mprops_;
1277
1280
1281 VertexProperty<bool> vdeleted_;
1282 EdgeProperty<bool> edeleted_;
1283
1284 VertexProperty<vec3> vpoint_;
1285
1286 unsigned int deleted_vertices_;
1287 unsigned int deleted_edges_;
1288 bool garbage_;
1289 };
1290
1291
1292 //------------------------------------------------------------ output operators
1293
1294
1300 inline std::ostream& operator<<(std::ostream& os, Graph::Vertex v) {
1301 return (os << 'v' << v.idx());
1302 }
1303
1309 inline std::ostream& operator<<(std::ostream& os, Graph::Edge e) {
1310 return (os << 'e' << e.idx());
1311 }
1312
1313} // namespace easy3d
1314
1315#endif // EASY3D_CORE_GRAPH_H
1316
1317
bool operator!=(const BaseHandle &_rhs) const
Inequality operator.
Definition graph.h:98
bool is_valid() const
Return whether the handle is valid, i.e., the index is not equal to -1.
Definition graph.h:82
int idx() const
Get the underlying index of this handle.
Definition graph.h:71
bool operator<(const BaseHandle &_rhs) const
Less than operator useful for sorting handles.
Definition graph.h:107
bool operator==(const BaseHandle &_rhs) const
Equality operator.
Definition graph.h:89
void reset()
Reset handle to be invalid (index=-1).
Definition graph.h:76
BaseHandle(int _idx=-1)
Constructor with index.
Definition graph.h:65
This class circulates through all edges connected with a vertex. It also acts as a container-concept ...
Definition graph.h:499
EdgeAroundVertexCirculator & operator--()
Pre-decrement circulator.
Definition graph.h:551
EdgeAroundVertexCirculator & operator++()
Pre-increment circulator.
Definition graph.h:537
EdgeAroundVertexCirculator & end()
Helper for C++11 range-based for-loops.
Definition graph.h:583
bool operator==(const EdgeAroundVertexCirculator &rhs) const
Equality operator.
Definition graph.h:518
Edge operator*() const
Get the edge the circulator refers to.
Definition graph.h:562
Vertex vertex() const
Return current vertex.
Definition graph.h:578
bool operator!=(const EdgeAroundVertexCirculator &rhs) const
Inequality operator.
Definition graph.h:529
EdgeAroundVertexCirculator & begin()
Helper for C++11 range-based for-loops.
Definition graph.h:581
EdgeAroundVertexCirculator(const Graph *g, Vertex v=Vertex())
Default constructor.
Definition graph.h:506
This helper class is a container for iterating through all edges using C++11 range-based for-loops.
Definition graph.h:474
EdgeIterator begin() const
Returns the begin iterator.
Definition graph.h:483
EdgeIterator end() const
Returns the end iterator.
Definition graph.h:485
EdgeContainer(EdgeIterator _begin, EdgeIterator _end)
Constructor with begin and end iterators.
Definition graph.h:481
This class iterates linearly over all edges.
Definition graph.h:381
bool operator!=(const EdgeIterator &rhs) const
Inequality operator.
Definition graph.h:411
EdgeIterator & operator++()
Pre-increment iterator.
Definition graph.h:420
EdgeIterator(Edge e=Edge(), const Graph *g=nullptr)
Default constructor.
Definition graph.h:385
Edge operator*() const
Get the edge the iterator refers to.
Definition graph.h:394
bool operator==(const EdgeIterator &rhs) const
Equality operator.
Definition graph.h:401
EdgeIterator & operator--()
Pre-decrement iterator.
Definition graph.h:432
Edge property of type T.
Definition graph.h:229
EdgeProperty()=default
Default constructor.
Property< T >::reference operator[](Edge e)
Access the data stored for edge e.
Definition graph.h:244
Property< T >::const_reference operator[](Edge e) const
Access the data stored for edge e.
Definition graph.h:254
EdgeProperty(Property< T > p)
Constructor with a property.
Definition graph.h:237
Graph property of type T.
Definition graph.h:266
ModelProperty()=default
Default constructor.
Property< T >::reference operator[](size_t idx) override
Access the data stored for the graph.
Definition graph.h:281
ModelProperty(Property< T > p)
Constructor with a property.
Definition graph.h:274
Property< T >::const_reference operator[](size_t idx) const override
Access the data stored for the graph.
Definition graph.h:291
This class circulates through all one-ring neighbors of a vertex. It also acts as a container-concept...
Definition graph.h:601
bool operator!=(const VertexAroundVertexCirculator &rhs) const
Inequality operator.
Definition graph.h:628
VertexAroundVertexCirculator & operator++()
Pre-increment circulator.
Definition graph.h:636
VertexAroundVertexCirculator & begin()
Helper for C++11 range-based for-loops.
Definition graph.h:684
Vertex operator*() const
Get the vertex the circulator refers to.
Definition graph.h:661
Vertex vertex() const
Return current vertex.
Definition graph.h:681
bool operator==(const VertexAroundVertexCirculator &rhs) const
Equality operator.
Definition graph.h:617
VertexAroundVertexCirculator & operator--()
Pre-decrement circulator.
Definition graph.h:650
VertexAroundVertexCirculator(const Graph *g, Vertex v=Vertex())
default constructor
Definition graph.h:605
VertexAroundVertexCirculator & end()
Helper for C++11 range-based for-loops.
Definition graph.h:689
This helper class is a container for iterating through all vertices using C++11 range-based for-loops...
Definition graph.h:453
VertexContainer(VertexIterator _begin, VertexIterator _end)
Constructor with begin and end iterators.
Definition graph.h:460
VertexIterator begin() const
Returns the begin iterator.
Definition graph.h:462
VertexIterator end() const
Returns the end iterator.
Definition graph.h:464
This class iterates linearly over all vertices.
Definition graph.h:307
Vertex operator*() const
Get the vertex the iterator refers to.
Definition graph.h:323
VertexIterator(Vertex v=Vertex(), const Graph *g=nullptr)
Default constructor.
Definition graph.h:314
bool operator==(const VertexIterator &rhs) const
Equality operator.
Definition graph.h:330
VertexIterator & operator--()
Pre-decrement iterator.
Definition graph.h:361
bool operator!=(const VertexIterator &rhs) const
Inequality operator.
Definition graph.h:340
VertexIterator & operator++()
Pre-increment iterator.
Definition graph.h:349
Vertex property of type T.
Definition graph.h:191
VertexProperty(Property< T > p)
Constructor with a property.
Definition graph.h:199
Property< T >::const_reference operator[](Vertex v) const
Access the data stored for vertex v.
Definition graph.h:216
Property< T >::reference operator[](Vertex v)
Access the data stored for vertex v.
Definition graph.h:206
VertexProperty()=default
Default constructor.
A Graph data structure with easy property management.
Definition graph.h:50
const vec3 & position(Vertex v) const
Returns the position of a vertex (read-only).
Definition graph.h:1224
EdgeContainer edges() const
Returns the edge container for range-based for-loops.
Definition graph.h:1153
Graph(const Graph &rhs)
Copy constructor: copies rhs to *this. Performs a deep copy of all properties.
Definition graph.h:720
const std::type_info & get_vertex_property_type(const std::string &name) const
Gets the type information of a vertex property.
Definition graph.h:1048
bool remove_vertex_property(const std::string &n)
remove the vertex property named n
Definition graph.h:1011
EdgeIterator edges_end() const
Returns the end iterator for edges.
Definition graph.h:1144
EdgeAroundVertexCirculator edges(Vertex v) const
Returns the circulator for edges around a vertex.
Definition graph.h:1173
VertexAroundVertexCirculator vertices(Vertex v) const
Returns the circulator for vertices around a vertex.
Definition graph.h:1163
unsigned int n_edges() const
Returns number of edges in the graph.
Definition graph.h:784
bool is_valid(Edge e) const
Checks if an edge is valid, i.e. the index is within the array bounds.
Definition graph.h:846
bool rename_vertex_property(const std::string &old_name, const std::string &new_name)
rename a vertex property given its name
Definition graph.h:1028
unsigned int valence(Vertex v) const
Returns the valence (number of incident edges or neighboring vertices) of a vertex.
Definition graph.cpp:248
VertexContainer vertices() const
Returns the vertex container for range-based for-loops.
Definition graph.h:1126
~Graph() override=default
destructor
const std::type_info & get_edge_property_type(const std::string &name) const
Gets the type information of an edge property.
Definition graph.h:1058
Graph & assign(const Graph &rhs)
Assign rhs to *this. Does not copy custom properties.
Definition graph.cpp:82
bool has_garbage() const
Checks if there are deleted vertices or edges.
Definition graph.h:811
VertexProperty< T > vertex_property(const std::string &name, const T t=T())
Gets or adds a vertex property.
Definition graph.h:978
unsigned int edges_size() const
Returns number of (deleted and valid) edges in the graph.
Definition graph.h:773
void reserve(unsigned int nvertices, unsigned int nedges)
Reserves memory for vertices and edges (mainly used in file readers)
Definition graph.cpp:151
ModelProperty< T > get_model_property(const std::string &name) const
Gets the model property of the specified type and name.
Definition graph.h:966
std::vector< std::string > vertex_properties() const
Returns the names of all vertex properties.
Definition graph.h:1075
EdgeProperty< T > get_edge_property(const std::string &name) const
Gets the edge property of the specified type and name.
Definition graph.h:955
void resize(unsigned int nv, unsigned int ne)
Resizes the space for vertices, edges, and their currently associated properties.
Definition graph.h:802
EdgeIterator edges_begin() const
Returns the start iterator for edges.
Definition graph.h:1135
Edge find_edge(Vertex a, Vertex b) const
Finds the edge connecting two vertices.
Definition graph.cpp:232
VertexProperty< T > get_vertex_property(const std::string &name) const
Gets the vertex property of the specified type and name.
Definition graph.h:944
Vertex source(Edge e) const
Returns the starting vertex of an edge, which is equal to vertex(e, 0).
Definition graph.h:887
std::vector< std::string > edge_properties() const
Returns the names of all edge properties.
Definition graph.h:1080
bool is_deleted(Vertex v) const
Checks if a vertex is deleted.
Definition graph.h:823
std::vector< std::string > model_properties() const
Returns the names of all model properties.
Definition graph.h:1085
void collect_garbage()
Removes deleted vertices and edges.
Definition graph.cpp:319
bool rename_edge_property(const std::string &old_name, const std::string &new_name)
rename an edge property given its name
Definition graph.h:1033
bool is_deleted(Edge e) const
Checks if an edge is deleted.
Definition graph.h:830
const std::type_info & get_model_property_type(const std::string &name) const
Gets the type information of a model property.
Definition graph.h:1068
std::vector< vec3 > & points() override
Returns the vector of vertex positions.
Definition graph.h:1243
bool remove_edge_property(const std::string &n)
remove the edge property named n
Definition graph.h:1018
ModelProperty< T > model_property(const std::string &name, const T t=T())
Gets or adds a model property.
Definition graph.h:1001
VertexProperty< T > add_vertex_property(const std::string &name, const T t=T())
Adds a vertex property.
Definition graph.h:908
bool remove_model_property(ModelProperty< T > &p)
remove the model property p
Definition graph.h:1022
bool rename_model_property(const std::string &old_name, const std::string &new_name)
rename a model property given its name
Definition graph.h:1038
ModelProperty< T > add_model_property(const std::string &name, const T t=T())
Adds a model property.
Definition graph.h:932
EdgeProperty< T > edge_property(const std::string &name, const T t=T())
Gets or adds an edge property.
Definition graph.h:989
bool remove_edge_property(EdgeProperty< T > &p)
remove the edge property p
Definition graph.h:1015
void property_stats(std::ostream &output) const override
Prints the names of all properties to an output stream.
Definition graph.cpp:162
void delete_edge(Edge e)
Deletes an edge from the graph.
Definition graph.cpp:303
Vertex add_vertex(const vec3 &p)
Add a new vertex with position p.
Definition graph.cpp:204
void clear()
Removes all vertices, edges, and properties, and resets the garbage state.
Definition graph.cpp:123
bool is_isolated(Vertex v) const
Checks if a vertex is isolated (not incident to any edge).
Definition graph.h:863
vec3 & position(Vertex v)
Returns the position of a vertex.
Definition graph.h:1231
unsigned int vertices_size() const
Returns number of (deleted and valid) vertices in the graph.
Definition graph.h:768
bool remove_model_property(const std::string &n)
remove the model property named n
Definition graph.h:1025
float edge_length(Edge e) const
Computes the length of an edge.
Definition graph.cpp:256
EdgeProperty< T > add_edge_property(const std::string &name, const T t=T())
Adds an edge property.
Definition graph.h:920
Vertex target(Edge e) const
Returns the ending vertex of an edge, which is equal to vertex(e, 1).
Definition graph.h:892
void delete_vertex(Vertex v)
Deletes a vertex from the graph.
Definition graph.cpp:264
VertexIterator vertices_begin() const
Returns the start iterator for vertices.
Definition graph.h:1108
bool remove_vertex_property(VertexProperty< T > &p)
remove the vertex property p
Definition graph.h:1008
VertexIterator vertices_end() const
Returns the end iterator for vertices.
Definition graph.h:1117
Edge add_edge(const Vertex &v1, const Vertex &v2)
Add a new edge connecting vertices v1 and v2.
Definition graph.cpp:212
bool is_valid(Vertex v) const
Checks if a vertex is valid, i.e. the index is within the array bounds.
Definition graph.h:837
Vertex vertex(Edge e, unsigned int i) const
Returns the i-th vertex of an edge.
Definition graph.h:874
Graph & operator=(const Graph &rhs)
Assign rhs to *this. Performs a deep copy of all properties.
Definition graph.cpp:53
Graph()
default constructor
Definition graph.cpp:33
const std::vector< vec3 > & points() const override
Returns the vector of vertex positions (read-only).
Definition graph.h:1237
unsigned int n_vertices() const
Returns number of vertices in the graph.
Definition graph.h:779
const std::string & name() const
The name of a model.
Definition model.h:61
Model(const std::string &name="unknown")
Default constructor. The parameter name is optional, but it is useful for handling multiple models wi...
Definition model.cpp:33
void push_back()
Adds a new element to each vector.
Definition property.h:771
PropertyArray< T >::const_reference const_reference
The const reference type of the property.
Definition property.h:329
virtual reference operator[](size_t i)
Accesses the i-th element.
Definition property.h:365
Property(PropertyArray< T > *p=nullptr)
Constructor.
Definition property.h:338
PropertyArray< T >::reference reference
The reference type of the property.
Definition property.h:328
Definition collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
std::ostream & operator<<(std::ostream &os, Graph::Vertex v)
Output stream support for Graph::Vertex.
Definition graph.h:1300
Helper structure to be able to use std::unordered_map.
Definition graph.h:112
std::size_t operator()(const BaseHandle &h) const
Hash function for BaseHandle.
Definition graph.h:118
This type stores the edge connectivity.
Definition graph.h:177
Vertex target_
The target vertex of the edge.
Definition graph.h:179
Vertex source_
The source vertex of the edge.
Definition graph.h:178
This type represents an edge (internally it is basically an index).
Definition graph.h:151
Edge(int _idx=-1)
Default constructor (with invalid index).
Definition graph.h:156
This type stores the vertex connectivity.
Definition graph.h:167
std::vector< Edge > edges_
All edges connected with the vertex.
Definition graph.h:169
This type represents a vertex (internally it is basically an index).
Definition graph.h:132
std::ostream & operator<<(std::ostream &os) const
Output operator.
Definition graph.h:143
Vertex(int _idx=-1)
Default constructor (with invalid index).
Definition graph.h:137