QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgsgraph.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgraph.h
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 
16 /*
17  * This file describes the built-in QGIS classes for modeling a mathematical graph.
18  * Vertices are identified by their geographic coordinates and have no additional
19  * properties. Number of strategies for calculating edge cost is not limited.
20  * Graph may have incidence edges.
21  *
22  * \file qgsgraph.h
23  */
24 
25 #ifndef QGSGRAPH_H
26 #define QGSGRAPH_H
27 
28 #include <QList>
29 #include <QVector>
30 #include <QVariant>
31 
32 #include "qgspointxy.h"
33 #include "qgis_analysis.h"
34 
35 class QgsGraphVertex;
36 
37 
44 class ANALYSIS_EXPORT QgsGraphEdge
45 {
46  public:
47 
51  QgsGraphEdge() = default;
52 
57  QVariant cost( int strategyIndex ) const;
58 
62  QVector< QVariant > strategies() const;
63 
68  int toVertex() const;
69 
74  int fromVertex() const;
75 
76  private:
77 
78  QVector< QVariant > mStrategies;
79 
80  int mToIdx = 0;
81  int mFromIdx = 0;
82 
83  friend class QgsGraph;
84 };
85 
86 
87 typedef QList< int > QgsGraphEdgeIds;
88 
95 class ANALYSIS_EXPORT QgsGraphVertex
96 {
97  public:
98 
102  QgsGraphVertex() = default;
103 
108  QgsGraphVertex( const QgsPointXY &point );
109 
114  QgsGraphEdgeIds incomingEdges() const;
115 
120  QgsGraphEdgeIds outgoingEdges() const;
121 
125  QgsPointXY point() const;
126 
127  private:
128  QgsPointXY mCoordinate;
129  QgsGraphEdgeIds mIncomingEdges;
130  QgsGraphEdgeIds mOutgoingEdges;
131 
132  friend class QgsGraph;
133 };
134 
142 class ANALYSIS_EXPORT QgsGraph
143 {
144  public:
145 
149  QgsGraph() = default;
150 
151  // Graph constructing methods
152 
156  int addVertex( const QgsPointXY &pt );
157 
162  int addEdge( int fromVertexIdx, int toVertexIdx, const QVector< QVariant > &strategies );
163 
167  int vertexCount() const;
168 
169 #ifndef SIP_RUN
170 
174  const QgsGraphVertex &vertex( int idx ) const;
175 #else
176 
182  QgsGraphVertex vertex( int idx ) const;
183  % MethodCode
184  if ( sipCpp->hasVertex( a0 ) )
185  {
186  return sipConvertFromNewType( new QgsGraphVertex( sipCpp->vertex( a0 ) ), sipType_QgsGraphVertex, Py_None );
187  }
188  else
189  {
190  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
191  sipIsErr = 1;
192  }
193  % End
194 #endif
195 
196 #ifndef SIP_RUN
197 
205  void removeVertex( int index );
206 #else
207 
216  void removeVertex( int index );
217  % MethodCode
218  if ( sipCpp->hasVertex( a0 ) )
219  {
220  sipCpp->removeVertex( a0 );
221  }
222  else
223  {
224  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
225  sipIsErr = 1;
226  }
227  % End
228 #endif
229 
233  int edgeCount() const;
234 
235 #ifndef SIP_RUN
236 
240  const QgsGraphEdge &edge( int idx ) const;
241 #else
242 
248  QgsGraphEdge edge( int idx ) const;
249  % MethodCode
250  if ( sipCpp->hasEdge( a0 ) )
251  {
252  return sipConvertFromNewType( new QgsGraphEdge( sipCpp->edge( a0 ) ), sipType_QgsGraphEdge, Py_None );
253  }
254  else
255  {
256  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
257  sipIsErr = 1;
258  }
259  % End
260 #endif
261 
262 
263 #ifndef SIP_RUN
264 
273  void removeEdge( int index );
274 #else
275 
285  void removeEdge( int index );
286  % MethodCode
287  if ( sipCpp->hasEdge( a0 ) )
288  {
289  sipCpp->removeEdge( a0 );
290  }
291  else
292  {
293  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
294  sipIsErr = 1;
295  }
296  % End
297 #endif
298 
303  int findVertex( const QgsPointXY &pt ) const;
304 
305 #ifndef SIP_RUN
306 
318  int findOppositeEdge( int index ) const;
319 #else
320 
334  int findOppositeEdge( int index ) const;
335  % MethodCode
336  if ( sipCpp->hasEdge( a0 ) )
337  {
338  sipRes = sipCpp->findOppositeEdge( a0 );
339  }
340  else
341  {
342  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
343  sipIsErr = 1;
344  }
345  % End
346 #endif
347 
353  bool hasEdge( int index ) const;
354 
360  bool hasVertex( int index ) const;
361 
362  protected:
363 
364 #ifndef SIP_RUN
366  QHash<int, QgsGraphVertex> mGraphVertices;
367 
369  QHash<int, QgsGraphEdge> mGraphEdges;
370 #endif
371 
372 
373  private:
374 
375  int mNextVertexId = 0;
376  int mNextEdgeId = 0;
377 };
378 
379 #endif // QGSGRAPH_H
This class implements a graph edge.
Definition: qgsgraph.h:45
QgsGraphEdge()=default
Constructor for QgsGraphEdge.
This class implements a graph vertex.
Definition: qgsgraph.h:96
QgsGraphVertex()=default
Default constructor.
Mathematical graph representation.
Definition: qgsgraph.h:143
QHash< int, QgsGraphVertex > mGraphVertices
Graph vertices.
Definition: qgsgraph.h:366
QHash< int, QgsGraphEdge > mGraphEdges
Graph edges.
Definition: qgsgraph.h:369
QgsGraph()=default
Constructor for QgsGraph.
A class to represent a 2D point.
Definition: qgspointxy.h:59
QList< int > QgsGraphEdgeIds
Definition: qgsgraph.h:87