QGIS API Documentation  3.20.0-Odense (decaadbb31)
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  mGraphVertices.append( QgsGraphVertex( pt ) );
26  return mGraphVertices.size() - 1;
27 }
28 
29 int QgsGraph::addEdge( int fromVertexIdx, int toVertexIdx, const QVector< QVariant > &strategies )
30 {
31  QgsGraphEdge e;
32 
33  e.mStrategies = strategies;
34  e.mToIdx = toVertexIdx;
35  e.mFromIdx = fromVertexIdx;
36  mGraphEdges.push_back( e );
37  int edgeIdx = mGraphEdges.size() - 1;
38 
39  mGraphVertices[ toVertexIdx ].mIncomingEdges.push_back( edgeIdx );
40  mGraphVertices[ fromVertexIdx ].mOutgoingEdges.push_back( edgeIdx );
41 
42  return mGraphEdges.size() - 1;
43 }
44 
45 const QgsGraphVertex &QgsGraph::vertex( int idx ) const
46 {
47  return mGraphVertices[ idx ];
48 }
49 
50 const QgsGraphEdge &QgsGraph::edge( int idx ) const
51 {
52  return mGraphEdges[ idx ];
53 }
54 
56 {
57  return mGraphVertices.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 < mGraphVertices.size(); ++i )
69  {
70  if ( mGraphVertices[ i ].point() == pt )
71  {
72  return i;
73  }
74  }
75  return -1;
76 }
77 
78 QVariant QgsGraphEdge::cost( int i ) const
79 {
80  return mStrategies[ i ];
81 }
82 
83 QVector< QVariant > QgsGraphEdge::strategies() const
84 {
85  return mStrategies;
86 }
87 
89 {
90  return mFromIdx;
91 }
92 
94 {
95  return mToIdx;
96 }
97 
99  : mCoordinate( point )
100 {
101 
102 }
103 
105 {
106  return mIncomingEdges;
107 }
108 
110 {
111  return mOutgoingEdges;
112 }
113 
115 {
116  return mCoordinate;
117 }
This class implements a graph edge.
Definition: qgsgraph.h:44
int fromVertex() const
Returns the index of the vertex at the start of this edge.
Definition: qgsgraph.cpp:88
QVector< QVariant > strategies() const
Returns array of available strategies.
Definition: qgsgraph.cpp:83
int toVertex() const
Returns the index of the vertex at the end of this edge.
Definition: qgsgraph.cpp:93
QVariant cost(int strategyIndex) const
Returns edge cost calculated using specified strategy.
Definition: qgsgraph.cpp:78
This class implements a graph vertex.
Definition: qgsgraph.h:95
QgsGraphEdgeIds outgoingEdges() const
Returns outgoing edge ids, i.e.
Definition: qgsgraph.cpp:109
QgsGraphEdgeIds incomingEdges() const
Returns the incoming edge ids, i.e.
Definition: qgsgraph.cpp:104
QgsPointXY point() const
Returns point associated with graph vertex.
Definition: qgsgraph.cpp:114
QgsGraphVertex()=default
Default constructor.
const QgsGraphVertex & vertex(int idx) const
Returns vertex at given index.
Definition: qgsgraph.cpp:45
int addEdge(int fromVertexIdx, int toVertexIdx, const QVector< QVariant > &strategies)
Add an edge to the graph, going from the fromVertexIdx to toVertexIdx.
Definition: qgsgraph.cpp:29
int findVertex(const QgsPointXY &pt) const
Find vertex by associated point.
Definition: qgsgraph.cpp:65
int edgeCount() const
Returns number of graph edges.
Definition: qgsgraph.cpp:60
const QgsGraphEdge & edge(int idx) const
Returns edge at given index.
Definition: qgsgraph.cpp:50
int addVertex(const QgsPointXY &pt)
Add a vertex to the graph.
Definition: qgsgraph.cpp:23
int vertexCount() const
Returns number of graph vertices.
Definition: qgsgraph.cpp:55
A class to represent a 2D point.
Definition: qgspointxy.h:59
QList< int > QgsGraphEdgeIds
Definition: qgsgraph.h:86