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>
71 int idx()
const {
return idx_; }
90 return idx_ == _rhs.idx_;
99 return idx_ != _rhs.idx_;
108 return idx_ < _rhs.idx_;
143 std::ostream&
operator<<(std::ostream& os)
const {
return os <<
'v' <<
idx(); }
316 if (graph_ && graph_->has_garbage()) while (graph_->is_valid(hnd_) && graph_->is_deleted(hnd_)) ++hnd_.idx_;
332 return (hnd_ == rhs.hnd_);
353 while (graph_->has_garbage() && graph_->is_valid(hnd_) && graph_->is_deleted(hnd_)) ++hnd_.idx_;
365 while (graph_->has_garbage() && graph_->is_valid(hnd_) && graph_->is_deleted(hnd_)) --hnd_.idx_;
387 if (graph_ && graph_->has_garbage()) while (graph_->is_valid(hnd_) && graph_->is_deleted(hnd_)) ++hnd_.idx_;
403 return (hnd_ == rhs.hnd_);
424 while (graph_->has_garbage() && graph_->is_valid(hnd_) && graph_->is_deleted(hnd_)) ++hnd_.idx_;
436 while (graph_->has_garbage() && graph_->is_valid(hnd_) && graph_->is_deleted(hnd_)) --hnd_.idx_;
507 : graph_(g), vertex_(v), finished_(false)
509 iterator_ = graph_->vconn_[vertex_].edges_.begin();
510 end_ = graph_->vconn_[vertex_].edges_.end();
520 return ((graph_ == rhs.graph_) && (vertex_ == rhs.vertex_) && (iterator_ == rhs.iterator_))
540 if (iterator_ == end_) {
541 iterator_ = graph_->vconn_[vertex_].edges_.begin();
564 if (iterator_ != end_)
return *iterator_;
572 operator bool()
const {
return !graph_->vconn_[vertex_].edges_.empty(); }
588 std::vector<Edge>::const_iterator iterator_;
590 std::vector<Edge>::const_iterator end_;
606 : graph_(g), vertex_(v), finished_(false)
608 iterator_ = graph_->vconn_[vertex_].edges_.begin();
609 end_ = graph_->vconn_[vertex_].edges_.end();
619 return ((graph_ == rhs.graph_) && (vertex_ == rhs.vertex_) && (iterator_ == rhs.iterator_))
639 if (iterator_ == end_) {
640 iterator_ = graph_->vconn_[vertex_].edges_.begin();
663 if (iterator_ != end_) {
664 Vertex v = graph_->vertex(*iterator_, 1);
665 if (v != vertex_)
return v;
666 else return graph_->vertex(*iterator_, 0);
675 operator bool()
const {
return !graph_->vconn_[vertex_].edges_.empty(); }
685 iterator_ = graph_->vconn_[vertex_].edges_.
begin();
697 std::vector<Edge>::const_iterator iterator_;
699 std::vector<Edge>::const_iterator end_;
756 Edge
add_edge(
const Vertex& v1,
const Vertex& v2);
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()); }
796 void reserve(
unsigned int nvertices,
unsigned int nedges);
802 void resize(
unsigned int nv,
unsigned int ne) {
865 return vconn_[v].edges_.empty();
878 return econn_[e].source_;
880 return econn_[e].target_;
1029 return vprops_.rename(old_name, new_name);
1034 return eprops_.rename(old_name, new_name);
1039 return mprops_.rename(old_name, new_name);
1050 return vprops_.get_type(
name);
1060 return eprops_.get_type(
name);
1070 return mprops_.get_type(
name);
1077 return vprops_.properties();
1082 return eprops_.properties();
1087 return mprops_.properties();
1190 unsigned int valence(Vertex v)
const;
1198 Edge
find_edge(Vertex a, Vertex b)
const;
1237 const std::vector<vec3>&
points()
const override {
return vpoint_.vector(); }
1243 std::vector<vec3>&
points()
override {
return vpoint_.vector(); }
1268 return Edge(
static_cast<int>(
edges_size() - 1));
1274 PropertyContainer vprops_;
1275 PropertyContainer eprops_;
1276 PropertyContainer mprops_;
1286 unsigned int deleted_vertices_;
1287 unsigned int deleted_edges_;
1301 return (os <<
'v' << v.
idx());
1310 return (os <<
'e' << e.
idx());
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