QGIS API Documentation  2.99.0-Master (7705179)
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  mGraphVertexes.append( QgsGraphVertex( pt ) );
26  return mGraphVertexes.size() - 1;
27 }
28 
29 int QgsGraph::addEdge( int outVertexIdx, int inVertexIdx, const QVector< QVariant > &strategies )
30 {
31  QgsGraphEdge e;
32 
33  e.mStrategies = strategies;
34  e.mOut = outVertexIdx;
35  e.mIn = inVertexIdx;
36  mGraphEdges.push_back( e );
37  int edgeIdx = mGraphEdges.size() - 1;
38 
39  mGraphVertexes[ outVertexIdx ].mOutEdges.push_back( edgeIdx );
40  mGraphVertexes[ inVertexIdx ].mInEdges.push_back( edgeIdx );
41 
42  return mGraphEdges.size() - 1;
43 }
44 
45 const QgsGraphVertex &QgsGraph::vertex( int idx ) const
46 {
47  return mGraphVertexes[ idx ];
48 }
49 
50 const QgsGraphEdge &QgsGraph::edge( int idx ) const
51 {
52  return mGraphEdges[ idx ];
53 }
54 
56 {
57  return mGraphVertexes.size();
58 }
59 
61 {
62  return mGraphEdges.size();
63 }
64 
65 int QgsGraph::findVertex( const QgsPointXY &pt ) const
66 {
67  int i = 0;
68  for ( i = 0; i < mGraphVertexes.size(); ++i )
69  {
70  if ( mGraphVertexes[ i ].point() == pt )
71  {
72  return i;
73  }
74  }
75  return -1;
76 }
77 
79  : mOut( 0 )
80  , mIn( 0 )
81 {
82 
83 }
84 
85 QVariant QgsGraphEdge::cost( int i ) const
86 {
87  return mStrategies[ i ];
88 }
89 
90 QVector< QVariant > QgsGraphEdge::strategies() const
91 {
92  return mStrategies;
93 }
94 
96 {
97  return mIn;
98 }
99 
101 {
102  return mOut;
103 }
104 
106  : mCoordinate( point )
107 {
108 
109 }
110 
112 {
113  return mOutEdges;
114 }
115 
117 {
118  return mInEdges;
119 }
120 
122 {
123  return mCoordinate;
124 }
const QgsGraphEdge & edge(int idx) const
Returns edge at given index.
Definition: qgsgraph.cpp:50
int vertexCount() const
Returns number of graph vertices.
Definition: qgsgraph.cpp:55
A class to represent a 2D point.
Definition: qgspointxy.h:42
QgsPointXY point() const
Returns point associated with graph vertex.
Definition: qgsgraph.cpp:121
int outVertex() const
Returns index of the outgoing vertex.
Definition: qgsgraph.cpp:100
QgsGraphEdgeIds outEdges() const
Returns outgoing edges ids.
Definition: qgsgraph.cpp:111
QVector< QVariant > strategies() const
Returns array of available strategies.
Definition: qgsgraph.cpp:90
int findVertex(const QgsPointXY &pt) const
Find vertex by associated point.
Definition: qgsgraph.cpp:65
QVariant cost(int strategyIndex) const
Returns edge cost calculated using specified strategy.
Definition: qgsgraph.cpp:85
int inVertex() const
Returns index of the incoming vertex.
Definition: qgsgraph.cpp:95
This class implements a graph vertex.
Definition: qgsgraph.h:87
QgsGraphVertex()
Default constructor.
Definition: qgsgraph.h:94
int addEdge(int outVertexIdx, int inVertexIdx, const QVector< QVariant > &strategies)
Add an edge to the graph.
Definition: qgsgraph.cpp:29
const QgsGraphVertex & vertex(int idx) const
Returns vertex at given index.
Definition: qgsgraph.cpp:45
int addVertex(const QgsPointXY &pt)
Add a vertex to the graph.
Definition: qgsgraph.cpp:23
QList< int > QgsGraphEdgeIds
Definition: qgsgraph.h:80
int edgeCount() const
Returns number of graph edges.
Definition: qgsgraph.cpp:60
QgsGraphEdgeIds inEdges() const
Return incoming edges ids.
Definition: qgsgraph.cpp:116
This class implements a graph edge.
Definition: qgsgraph.h:43