27#ifndef EASY3D_CORE_GRAPH_H
28#define EASY3D_CORE_GRAPH_H
31#include <easy3d/core/model.h>
35#include <easy3d/core/types.h>
36#include <easy3d/core/property.h>
66 int idx()
const {
return idx_; }
76 return idx_ == _rhs.idx_;
81 return idx_ != _rhs.idx_;
86 return idx_ < _rhs.idx_;
91 std::size_t operator()(
const BaseHandle& h)
const {
return h.
idx(); }
106 std::ostream& operator<<(std::ostream& os)
const {
return os <<
'v' <<
idx(); }
204 typename Property<T>::const_reference
operator[](
size_t idx)
const
233 return (hnd_ == rhs.hnd_);
285 return (hnd_ == rhs.hnd_);
359 : graph_(g), vertex_(v), finished_(false)
361 iterator_ = graph_->vconn_[vertex_].edges_.begin();
362 end_ = graph_->vconn_[vertex_].edges_.end();
368 return ((graph_ == rhs.graph_) && (vertex_ == rhs.vertex_) && (iterator_ == rhs.iterator_))
381 if (iterator_ == end_) {
382 iterator_ = graph_->vconn_[vertex_].edges_.begin();
399 if (iterator_ != end_)
return *iterator_;
404 operator bool()
const {
return !graph_->vconn_[vertex_].edges_.empty(); }
416 const Vertex vertex_;
417 std::vector<Edge>::const_iterator iterator_;
419 std::vector<Edge>::const_iterator end_;
434 : graph_(g), vertex_(v), finished_(false)
436 iterator_ = graph_->vconn_[vertex_].edges_.begin();
437 end_ = graph_->vconn_[vertex_].edges_.end();
443 return ((graph_ == rhs.graph_) && (vertex_ == rhs.vertex_) && (iterator_ == rhs.iterator_))
456 if (iterator_ == end_) {
457 iterator_ = graph_->vconn_[vertex_].edges_.begin();
474 if (iterator_ != end_) {
476 if (v != vertex_)
return v;
477 else return graph_->
vertex(*iterator_, 0);
483 operator bool()
const {
return !graph_->vconn_[vertex_].edges_.empty(); }
490 iterator_ = graph_->vconn_[vertex_].edges_.begin();
501 const Vertex vertex_;
502 std::vector<Edge>::const_iterator iterator_;
504 std::vector<Edge>::const_iterator end_;
541 Edge
add_edge(
const Vertex& v1,
const Vertex& v2);
552 unsigned int edges_size()
const {
return (
unsigned int)eprops_.size(); }
564 void reserve(
unsigned int nvertices,
unsigned int nedges);
568 void resize(
unsigned int nv,
unsigned int ne) {
614 return vconn_[v].edges_.empty();
622 return econn_[e].source_;
624 return econn_[e].target_;
725 return vprops_.rename(old_name, new_name);
730 return eprops_.rename(old_name, new_name);
735 return mprops_.rename(old_name, new_name);
742 return vprops_.get_type(
name);
748 return eprops_.get_type(
name);
754 return mprops_.get_type(
name);
761 return vprops_.properties();
766 return eprops_.properties();
771 return mprops_.properties();
843 unsigned int valence(Vertex v)
const;
846 Edge
find_edge(Vertex a, Vertex b)
const;
868 const std::vector<vec3>&
points()
const override {
return vpoint_.vector(); }
871 std::vector<vec3>&
points()
override {
return vpoint_.vector(); }
892 return Edge(
static_cast<int>(
edges_size() - 1));
898 PropertyContainer vprops_;
899 PropertyContainer eprops_;
900 PropertyContainer mprops_;
902 VertexProperty<VertexConnectivity> vconn_;
903 EdgeProperty<EdgeConnectivity> econn_;
905 VertexProperty<bool> vdeleted_;
906 EdgeProperty<bool> edeleted_;
908 VertexProperty<vec3> vpoint_;
910 unsigned int deleted_vertices_;
911 unsigned int deleted_edges_;
922 return (os <<
'v' << v.
idx());
928 return (os <<
'e' << e.
idx());
bool operator!=(const BaseHandle &_rhs) const
are two handles different?
Definition: graph.h:80
bool is_valid() const
return whether the handle is valid, i.e., the index is not equal to -1.
Definition: graph.h:72
int idx() const
Get the underlying index of this handle.
Definition: graph.h:66
bool operator<(const BaseHandle &_rhs) const
compare operator useful for sorting handles
Definition: graph.h:85
bool operator==(const BaseHandle &_rhs) const
are two handles equal?
Definition: graph.h:75
void reset()
reset handle to be invalid (index=-1)
Definition: graph.h:69
BaseHandle(int _idx=-1)
constructor
Definition: graph.h:63
EdgeAroundVertexCirculator & operator--()
pre-decrement
Definition: graph.h:389
EdgeAroundVertexCirculator & operator++()
pre-increment
Definition: graph.h:378
bool operator==(const EdgeAroundVertexCirculator &rhs) const
are two circulators equal?
Definition: graph.h:366
Edge operator*() const
get the edge the circulator refers to
Definition: graph.h:397
Vertex vertex() const
return current vertex
Definition: graph.h:407
bool operator!=(const EdgeAroundVertexCirculator &rhs) const
are two circulators different?
Definition: graph.h:373
EdgeAroundVertexCirculator(const Graph *g, Vertex v=Vertex())
default constructor
Definition: graph.h:358
bool operator!=(const EdgeIterator &rhs) const
are two iterators different?
Definition: graph.h:289
EdgeIterator & operator++()
pre-increment iterator
Definition: graph.h:295
EdgeIterator(Edge e=Edge(), const Graph *g=nullptr)
Default constructor.
Definition: graph.h:274
Edge operator*() const
get the edge the iterator refers to
Definition: graph.h:280
bool operator==(const EdgeIterator &rhs) const
are two iterators equal?
Definition: graph.h:283
EdgeIterator & operator--()
pre-decrement iterator
Definition: graph.h:304
EdgeProperty()=default
default constructor
Property< T >::reference operator[](Edge e)
access the data stored for edge e
Definition: graph.h:175
Property< T >::const_reference operator[](Edge e) const
access the data stored for edge e
Definition: graph.h:181
ModelProperty()=default
default constructor
Property< T >::const_reference operator[](size_t idx) const
access the data stored for the graph
Definition: graph.h:204
Property< T >::reference operator[](size_t idx)
access the data stored for the graph
Definition: graph.h:198
bool operator!=(const VertexAroundVertexCirculator &rhs) const
are two circulators different?
Definition: graph.h:448
VertexAroundVertexCirculator & operator++()
pre-increment
Definition: graph.h:453
Vertex operator*() const
get the vertex the circulator refers to
Definition: graph.h:472
Vertex vertex() const
return current vertex
Definition: graph.h:486
bool operator==(const VertexAroundVertexCirculator &rhs) const
are two circulators equal?
Definition: graph.h:441
VertexAroundVertexCirculator & operator--()
pre-decrement
Definition: graph.h:464
VertexAroundVertexCirculator(const Graph *g, Vertex v=Vertex())
default constructor
Definition: graph.h:433
Vertex operator*() const
get the vertex the iterator refers to
Definition: graph.h:228
VertexIterator(Vertex v=Vertex(), const Graph *g=nullptr)
Default constructor.
Definition: graph.h:222
bool operator==(const VertexIterator &rhs) const
are two iterators equal?
Definition: graph.h:231
VertexIterator & operator--()
pre-decrement iterator
Definition: graph.h:252
bool operator!=(const VertexIterator &rhs) const
are two iterators different?
Definition: graph.h:237
VertexIterator & operator++()
pre-increment iterator
Definition: graph.h:243
Property< T >::const_reference operator[](Vertex v) const
access the data stored for vertex v
Definition: graph.h:157
Property< T >::reference operator[](Vertex v)
access the data stored for vertex v
Definition: graph.h:151
VertexProperty()=default
default constructor
A Graph data structure with easy property management.
Definition: graph.h:51
const vec3 & position(Vertex v) const
position of a vertex (read only)
Definition: graph.h:862
EdgeContainer edges() const
returns edge container for C++11 range-based for-loops
Definition: graph.h:816
Graph(const Graph &rhs)
copy constructor: copies rhs to *this. performs a deep copy of all properties.
Definition: graph.h:522
const std::type_info & get_vertex_property_type(const std::string &name) const
Definition: graph.h:740
bool remove_vertex_property(const std::string &n)
remove the vertex property named n
Definition: graph.h:707
EdgeIterator edges_end() const
returns end iterator for edges
Definition: graph.h:810
EdgeAroundVertexCirculator edges(Vertex v) const
returns circulator for edges around vertex v
Definition: graph.h:828
VertexAroundVertexCirculator vertices(Vertex v) const
returns circulator for vertices around vertex v
Definition: graph.h:822
unsigned int n_edges() const
returns number of edges in the graph
Definition: graph.h:557
bool is_valid(Edge e) const
return whether edge e is valid, i.e. the index is stores it within the array bounds.
Definition: graph.h:599
bool rename_vertex_property(const std::string &old_name, const std::string &new_name)
rename a vertex property given its name
Definition: graph.h:724
unsigned int valence(Vertex v) const
Definition: graph.cpp:248
VertexContainer vertices() const
returns vertex container for C++11 range-based for-loops
Definition: graph.h:798
~Graph() override=default
destructor
const std::type_info & get_edge_property_type(const std::string &name) const
Definition: graph.h:746
Graph & assign(const Graph &rhs)
assign rhs to *this. does not copy custom properties.
Definition: graph.cpp:82
bool has_garbage() const
are there deleted vertices or edges?
Definition: graph.h:574
VertexProperty< T > vertex_property(const std::string &name, const T t=T())
Definition: graph.h:684
unsigned int edges_size() const
returns number of (deleted and valid)edges in the graph
Definition: graph.h:552
void reserve(unsigned int nvertices, unsigned int nedges)
reserve memory (mainly used in file readers)
Definition: graph.cpp:151
ModelProperty< T > get_model_property(const std::string &name) const
Definition: graph.h:676
std::vector< std::string > vertex_properties() const
returns the names of all vertex properties
Definition: graph.h:759
EdgeProperty< T > get_edge_property(const std::string &name) const
Definition: graph.h:670
void resize(unsigned int nv, unsigned int ne)
Definition: graph.h:568
EdgeIterator edges_begin() const
returns start iterator for edges
Definition: graph.h:804
Edge find_edge(Vertex a, Vertex b) const
find the edge (a,b)
Definition: graph.cpp:232
VertexProperty< T > get_vertex_property(const std::string &name) const
Definition: graph.h:664
Vertex source(Edge e) const
returns the starting vertex of an edge, which is equal to vertex(e, 0).
Definition: graph.h:628
std::vector< std::string > edge_properties() const
returns the names of all edge properties
Definition: graph.h:764
bool is_deleted(Vertex v) const
Definition: graph.h:582
std::vector< std::string > model_properties() const
returns the names of all model properties
Definition: graph.h:769
void collect_garbage()
remove deleted vertices/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:729
bool is_deleted(Edge e) const
Definition: graph.h:588
const std::type_info & get_model_property_type(const std::string &name) const
Definition: graph.h:752
std::vector< vec3 > & points() override
vector of vertex positions
Definition: graph.h:871
bool remove_edge_property(const std::string &n)
remove the edge property named n
Definition: graph.h:714
ModelProperty< T > model_property(const std::string &name, const T t=T())
Definition: graph.h:697
VertexProperty< T > add_vertex_property(const std::string &name, const T t=T())
Definition: graph.h:642
bool remove_model_property(ModelProperty< T > &p)
remove the model property p
Definition: graph.h:718
bool rename_model_property(const std::string &old_name, const std::string &new_name)
rename a model property given its name
Definition: graph.h:734
ModelProperty< T > add_model_property(const std::string &name, const T t=T())
Definition: graph.h:656
EdgeProperty< T > edge_property(const std::string &name, const T t=T())
Definition: graph.h:690
bool remove_edge_property(EdgeProperty< T > &p)
remove the edge property p
Definition: graph.h:711
void property_stats(std::ostream &output) const override
prints the names of all properties to an output stream (e.g., std::cout)
Definition: graph.cpp:162
void delete_edge(Edge e)
deletes the edge e 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 garbage state).
Definition: graph.cpp:123
bool is_isolated(Vertex v) const
returns whether v is isolated, i.e., not incident to any edge
Definition: graph.h:612
vec3 & position(Vertex v)
position of a vertex
Definition: graph.h:865
unsigned int vertices_size() const
returns number of (deleted and valid) vertices in the graph
Definition: graph.h:550
bool remove_model_property(const std::string &n)
remove the model property named n
Definition: graph.h:721
float edge_length(Edge e) const
compute the length of edge e.
Definition: graph.cpp:256
EdgeProperty< T > add_edge_property(const std::string &name, const T t=T())
Definition: graph.h:649
Vertex target(Edge e) const
returns the ending vertex of an edge, which is equal to vertex(e, 1).
Definition: graph.h:631
void delete_vertex(Vertex v)
deletes the vertex v from the graph
Definition: graph.cpp:264
VertexIterator vertices_begin() const
returns start iterator for vertices
Definition: graph.h:786
bool remove_vertex_property(VertexProperty< T > &p)
remove the vertex property p
Definition: graph.h:704
VertexIterator vertices_end() const
returns end iterator for vertices
Definition: graph.h:792
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
return whether vertex v is valid, i.e. the index is stores it within the array bounds.
Definition: graph.h:594
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
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
vector of vertex positions (read only)
Definition: graph.h:868
unsigned int n_vertices() const
returns number of vertices in the graph
Definition: graph.h:555
The base class of renderable 3D models.
Definition: model.h:49
const std::string & name() const
The name of a model.
Definition: model.h:60
Implementation of a generic property.
Definition: property.h:253
Definition: collider.cpp:182
std::ostream & operator<<(std::ostream &os, Graph::Vertex v)
Definition: graph.h:920
helper structure to be able to use std::unordered_map
Definition: graph.h:90
Edge(int _idx=-1)
default constructor (with invalid index)
Definition: graph.h:114
std::vector< Edge > edges_
all edges connected with the vertex
Definition: graph.h:125
Vertex(int _idx=-1)
default constructor (with invalid index)
Definition: graph.h:105