QGIS API Documentation  2.99.0-Master (e077efd)
qgsgraph.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgraph.cpp
3  --------------------------------------
4  Date : 2011-04-01
5  Copyright : (C) 2010 by Yakushev Sergey
6  Email : YakushevS <at> list.ru
7 ****************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15 
21 #include "qgsgraph.h"
22 
24 {
25 }
26 
27 int QgsGraph::addVertex( const QgsPoint& pt )
28 {
29  mGraphVertexes.append( QgsGraphVertex( pt ) );
30  return mGraphVertexes.size() - 1;
31 }
32 
33 int QgsGraph::addEdge( int outVertexIdx, int inVertexIdx, const QVector< QVariant >& strategies )
34 {
35  QgsGraphEdge e;
36 
37  e.mStrategies = strategies;
38  e.mOut = outVertexIdx;
39  e.mIn = inVertexIdx;
40  mGraphEdges.push_back( e );
41  int edgeIdx = mGraphEdges.size() - 1;
42 
43  mGraphVertexes[ outVertexIdx ].mOutEdges.push_back( edgeIdx );
44  mGraphVertexes[ inVertexIdx ].mInEdges.push_back( edgeIdx );
45 
46  return mGraphEdges.size() - 1;
47 }
48 
49 const QgsGraphVertex& QgsGraph::vertex( int idx ) const
50 {
51  return mGraphVertexes[ idx ];
52 }
53 
54 const QgsGraphEdge& QgsGraph::edge( int idx ) const
55 {
56  return mGraphEdges[ idx ];
57 }
58 
60 {
61  return mGraphVertexes.size();
62 }
63 
65 {
66  return mGraphEdges.size();
67 }
68 
69 int QgsGraph::findVertex( const QgsPoint& pt ) const
70 {
71  int i = 0;
72  for ( i = 0; i < mGraphVertexes.size(); ++i )
73  {
74  if ( mGraphVertexes[ i ].point() == pt )
75  {
76  return i;
77  }
78  }
79  return -1;
80 }
81 
83  : mOut( 0 )
84  , mIn( 0 )
85 {
86 
87 }
88 
89 QVariant QgsGraphEdge::cost( int i ) const
90 {
91  return mStrategies[ i ];
92 }
93 
94 QVector< QVariant > QgsGraphEdge::strategies() const
95 {
96  return mStrategies;
97 }
98 
100 {
101  return mIn;
102 }
103 
105 {
106  return mOut;
107 }
108 
110  : mCoordinate( point )
111 {
112 
113 }
114 
116 {
117  return mOutEdges;
118 }
119 
121 {
122  return mInEdges;
123 }
124 
126 {
127  return mCoordinate;
128 }
const QgsGraphEdge & edge(int idx) const
Returns edge at given index.
Definition: qgsgraph.cpp:54
QgsGraph()
Definition: qgsgraph.cpp:23
int vertexCount() const
Returns number of graph vertices.
Definition: qgsgraph.cpp:59
int outVertex() const
Returns index of the outgoing vertex.
Definition: qgsgraph.cpp:104
QgsGraphEdgeIds outEdges() const
Returns outgoing edges ids.
Definition: qgsgraph.cpp:115
QVector< QVariant > strategies() const
Returns array of available strategies.
Definition: qgsgraph.cpp:94
int findVertex(const QgsPoint &pt) const
Find vertex by associated point.
Definition: qgsgraph.cpp:69
QgsPoint point() const
Returns point associated with graph vertex.
Definition: qgsgraph.cpp:125
int addVertex(const QgsPoint &pt)
Add a vertex to the graph.
Definition: qgsgraph.cpp:27
A class to represent a point.
Definition: qgspoint.h:111
QVariant cost(int strategyIndex) const
Returns edge cost calculated using specified strategy.
Definition: qgsgraph.cpp:89
int inVertex() const
Returns index of the incoming vertex.
Definition: qgsgraph.cpp:99
This class implements a graph vertex.
Definition: qgsgraph.h:86
QgsGraphVertex()
Default constructor.
Definition: qgsgraph.h:93
int addEdge(int outVertexIdx, int inVertexIdx, const QVector< QVariant > &strategies)
Add an edge to the graph.
Definition: qgsgraph.cpp:33
const QgsGraphVertex & vertex(int idx) const
Returns vertex at given index.
Definition: qgsgraph.cpp:49
QList< int > QgsGraphEdgeIds
Definition: qgsgraph.h:79
int edgeCount() const
Returns number of graph edges.
Definition: qgsgraph.cpp:64
QgsGraphEdgeIds inEdges() const
Return incoming edges ids.
Definition: qgsgraph.cpp:120
This class implements a graph edge.
Definition: qgsgraph.h:42