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)23int 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)29int 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) const45const QgsGraphVertex &QgsGraph::vertex( int idx ) const 46 { 47 return mGraphVertices[ idx ]; 48 } 49 edge(int idx) const50const QgsGraphEdge &QgsGraph::edge( int idx ) const 51 { 52 return mGraphEdges[ idx ]; 53 } 54 vertexCount() const55int QgsGraph::vertexCount() const 56 { 57 return mGraphVertices.size(); 58 } 59 edgeCount() const60int QgsGraph::edgeCount() const 61 { 62 return mGraphEdges.size(); 63 } 64 findVertex(const QgsPointXY & pt) const65int 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) const78QVariant QgsGraphEdge::cost( int i ) const 79 { 80 return mStrategies[ i ]; 81 } 82 strategies() const83QVector< QVariant > QgsGraphEdge::strategies() const 84 { 85 return mStrategies; 86 } 87 fromVertex() const88int QgsGraphEdge::fromVertex() const 89 { 90 return mFromIdx; 91 } 92 toVertex() const93int QgsGraphEdge::toVertex() const 94 { 95 return mToIdx; 96 } 97 QgsGraphVertex(const QgsPointXY & point)98QgsGraphVertex::QgsGraphVertex( const QgsPointXY &point ) 99 : mCoordinate( point ) 100 { 101 102 } 103 incomingEdges() const104QgsGraphEdgeIds QgsGraphVertex::incomingEdges() const 105 { 106 return mIncomingEdges; 107 } 108 outgoingEdges() const109QgsGraphEdgeIds QgsGraphVertex::outgoingEdges() const 110 { 111 return mOutgoingEdges; 112 } 113 point() const114QgsPointXY QgsGraphVertex::point() const 115 { 116 return mCoordinate; 117 } 118