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 
16 /**
17  * \file qgsgraph.cpp
18  * \brief implementation QgsGraph, QgsGraphVertex, QgsGraphEdge
19  */
20 
21 #include "qgsgraph.h"
22 
addVertex(const QgsPointXY & pt)23 int QgsGraph::addVertex( const QgsPointXY &pt )
24 {
25   mGraphVertices.append( QgsGraphVertex( pt ) );
26   return mGraphVertices.size() - 1;
27 }
28 
addEdge(int fromVertexIdx,int toVertexIdx,const QVector<QVariant> & strategies)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   const 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 
vertex(int idx) const45 const QgsGraphVertex &QgsGraph::vertex( int idx ) const
46 {
47   return mGraphVertices[ idx ];
48 }
49 
edge(int idx) const50 const QgsGraphEdge &QgsGraph::edge( int idx ) const
51 {
52   return mGraphEdges[ idx ];
53 }
54 
vertexCount() const55 int QgsGraph::vertexCount() const
56 {
57   return mGraphVertices.size();
58 }
59 
edgeCount() const60 int QgsGraph::edgeCount() const
61 {
62   return mGraphEdges.size();
63 }
64 
findVertex(const QgsPointXY & pt) const65 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 
cost(int i) const78 QVariant QgsGraphEdge::cost( int i ) const
79 {
80   return mStrategies[ i ];
81 }
82 
strategies() const83 QVector< QVariant > QgsGraphEdge::strategies() const
84 {
85   return mStrategies;
86 }
87 
fromVertex() const88 int QgsGraphEdge::fromVertex() const
89 {
90   return mFromIdx;
91 }
92 
toVertex() const93 int QgsGraphEdge::toVertex() const
94 {
95   return mToIdx;
96 }
97 
QgsGraphVertex(const QgsPointXY & point)98 QgsGraphVertex::QgsGraphVertex( const QgsPointXY &point )
99   : mCoordinate( point )
100 {
101 
102 }
103 
incomingEdges() const104 QgsGraphEdgeIds QgsGraphVertex::incomingEdges() const
105 {
106   return mIncomingEdges;
107 }
108 
outgoingEdges() const109 QgsGraphEdgeIds QgsGraphVertex::outgoingEdges() const
110 {
111   return mOutgoingEdges;
112 }
113 
point() const114 QgsPointXY QgsGraphVertex::point() const
115 {
116   return mCoordinate;
117 }
118