Skip to content

Commit

Permalink
Fix extremely confusing network graph API naming
Browse files Browse the repository at this point in the history
Since names of api calls were backwards to expectations
  • Loading branch information
nyalldawson committed Oct 31, 2017
1 parent b337335 commit ab05fe7
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 115 deletions.
29 changes: 18 additions & 11 deletions python/analysis/network/qgsgraph.sip
Expand Up @@ -41,15 +41,17 @@ class QgsGraphEdge
:rtype: list of QVariant
%End

int outVertex() const;
int toVertex() const;
%Docstring
Returns index of the outgoing vertex
Returns the index of the vertex at the end of this edge.
.. seealso:: fromVertex()
:rtype: int
%End

int inVertex() const;
int fromVertex() const;
%Docstring
Returns index of the incoming vertex
Returns the index of the vertex at the start of this edge.
.. seealso:: toVertex()
:rtype: int
%End

Expand All @@ -62,6 +64,7 @@ class QgsGraphVertex
{
%Docstring
This class implements a graph vertex
.. versionadded:: 3.0
%End

%TypeHeaderCode
Expand All @@ -80,21 +83,23 @@ class QgsGraphVertex
This constructor initializes QgsGraphVertex object and associates a vertex with a point
%End

QgsGraphEdgeIds outEdges() const;
QgsGraphEdgeIds incomingEdges() const;
%Docstring
Returns outgoing edges ids
Returns the incoming edge ids, i.e. edges which end at this node.
.. seealso:: outgoingEdges()
:rtype: QgsGraphEdgeIds
%End

QgsGraphEdgeIds inEdges() const;
QgsGraphEdgeIds outgoingEdges() const;
%Docstring
Return incoming edges ids
Returns outgoing edge ids, i.e. edges which start at this node.
.. seealso:: incomingEdges()
:rtype: QgsGraphEdgeIds
%End

QgsPointXY point() const;
%Docstring
Returns point associated with graph vertex
Returns point associated with graph vertex.
:rtype: QgsPointXY
%End

Expand All @@ -105,6 +110,7 @@ class QgsGraph
{
%Docstring
Mathematical graph representation
.. versionadded:: 3.0
%End

%TypeHeaderCode
Expand All @@ -124,9 +130,10 @@ class QgsGraph
:rtype: int
%End

int addEdge( int outVertexIdx, int inVertexIdx, const QVector< QVariant > &strategies );
int addEdge( int fromVertexIdx, int toVertexIdx, const QVector< QVariant > &strategies );
%Docstring
Add an edge to the graph
Add an edge to the graph, going from the ``fromVertexIdx``
to ``toVertexIdx``.
:rtype: int
%End

Expand Down
2 changes: 2 additions & 0 deletions python/analysis/network/qgsgraphbuilder.sip
Expand Up @@ -30,12 +30,14 @@ class QgsGraphBuilder : QgsGraphBuilderInterface
~QgsGraphBuilder();

virtual void addVertex( int id, const QgsPointXY &pt );

%Docstring
MANDATORY BUILDER PROPERTY DECLARATION
%End

virtual void addEdge( int pt1id, const QgsPointXY &pt1, int pt2id, const QgsPointXY &pt2, const QVector< QVariant > &prop );


QgsGraph *graph() /Factory/;
%Docstring
Returns generated QgsGraph
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/qgis/ServiceAreaFromLayer.py
Expand Up @@ -245,13 +245,13 @@ def processAlgorithm(self, parameters, context, feedback):
tree, cost = QgsGraphAnalyzer.dijkstra(graph, idxStart, 0)
for j, v in enumerate(cost):
if v > travelCost and tree[j] != -1:
vertexId = graph.edge(tree[j]).outVertex()
vertexId = graph.edge(tree[j]).toVertex()
if cost[vertexId] <= travelCost:
vertices.append(j)

for j in vertices:
upperBoundary.append(graph.vertex(graph.edge(tree[j]).inVertex()).point())
lowerBoundary.append(graph.vertex(graph.edge(tree[j]).outVertex()).point())
upperBoundary.append(graph.vertex(graph.edge(tree[j]).fromVertex()).point())
lowerBoundary.append(graph.vertex(graph.edge(tree[j]).toVertex()).point())

geomUpper = QgsGeometry.fromMultiPointXY(upperBoundary)
geomLower = QgsGeometry.fromMultiPointXY(lowerBoundary)
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/qgis/ServiceAreaFromPoint.py
Expand Up @@ -208,15 +208,15 @@ def processAlgorithm(self, parameters, context, feedback):
vertices = []
for i, v in enumerate(cost):
if v > travelCost and tree[i] != -1:
vertexId = graph.edge(tree[i]).outVertex()
vertexId = graph.edge(tree[i]).toVertex()
if cost[vertexId] <= travelCost:
vertices.append(i)

upperBoundary = []
lowerBoundary = []
for i in vertices:
upperBoundary.append(graph.vertex(graph.edge(tree[i]).inVertex()).point())
lowerBoundary.append(graph.vertex(graph.edge(tree[i]).outVertex()).point())
upperBoundary.append(graph.vertex(graph.edge(tree[i]).fromVertex()).point())
lowerBoundary.append(graph.vertex(graph.edge(tree[i]).toVertex()).point())

feedback.pushInfo(self.tr('Writing results...'))

Expand Down
Expand Up @@ -233,7 +233,6 @@ def processAlgorithm(self, parameters, context, feedback):

feedback.setProgress(int(current * total))


feedback.pushInfo(self.tr('Building graph...'))
snappedPoints = director.makeGraph(builder, points, feedback)

Expand Down Expand Up @@ -268,8 +267,8 @@ def processAlgorithm(self, parameters, context, feedback):
current = idxEnd
while current != idxStart:
cost += graph.edge(tree[current]).cost(0)
route.append(graph.vertex(graph.edge(tree[current]).inVertex()).point())
current = graph.edge(tree[current]).outVertex()
route.append(graph.vertex(graph.edge(tree[current]).fromVertex()).point())
current = graph.edge(tree[current]).toVertex()

route.append(snappedPoints[i])
route.reverse()
Expand Down
Expand Up @@ -267,8 +267,8 @@ def processAlgorithm(self, parameters, context, feedback):
current = idxEnd
while current != idxStart:
cost += graph.edge(tree[current]).cost(0)
route.append(graph.vertex(graph.edge(tree[current]).inVertex()).point())
current = graph.edge(tree[current]).outVertex()
route.append(graph.vertex(graph.edge(tree[current]).fromVertex()).point())
current = graph.edge(tree[current]).toVertex()

route.append(snappedPoints[0])
route.reverse()
Expand Down
Expand Up @@ -227,8 +227,8 @@ def processAlgorithm(self, parameters, context, feedback):
current = idxEnd
while current != idxStart:
cost += graph.edge(tree[current]).cost(0)
route.append(graph.vertex(graph.edge(tree[current]).inVertex()).point())
current = graph.edge(tree[current]).outVertex()
route.append(graph.vertex(graph.edge(tree[current]).fromVertex()).point())
current = graph.edge(tree[current]).toVertex()

route.append(snappedPoints[0])
route.reverse()
Expand Down
26 changes: 13 additions & 13 deletions src/analysis/network/qgsgraph.cpp
Expand Up @@ -26,18 +26,18 @@ int QgsGraph::addVertex( const QgsPointXY &pt )
return mGraphVertices.size() - 1;
}

int QgsGraph::addEdge( int outVertexIdx, int inVertexIdx, const QVector< QVariant > &strategies )
int QgsGraph::addEdge( int fromVertexIdx, int toVertexIdx, const QVector< QVariant > &strategies )
{
QgsGraphEdge e;

e.mStrategies = strategies;
e.mOut = outVertexIdx;
e.mIn = inVertexIdx;
e.mToIdx = toVertexIdx;
e.mFromIdx = fromVertexIdx;
mGraphEdges.push_back( e );
int edgeIdx = mGraphEdges.size() - 1;

mGraphVertices[ outVertexIdx ].mOutEdges.push_back( edgeIdx );
mGraphVertices[ inVertexIdx ].mInEdges.push_back( edgeIdx );
mGraphVertices[ toVertexIdx ].mIncomingEdges.push_back( edgeIdx );
mGraphVertices[ fromVertexIdx ].mOutgoingEdges.push_back( edgeIdx );

return mGraphEdges.size() - 1;
}
Expand Down Expand Up @@ -85,14 +85,14 @@ QVector< QVariant > QgsGraphEdge::strategies() const
return mStrategies;
}

int QgsGraphEdge::inVertex() const
int QgsGraphEdge::fromVertex() const
{
return mIn;
return mFromIdx;
}

int QgsGraphEdge::outVertex() const
int QgsGraphEdge::toVertex() const
{
return mOut;
return mToIdx;
}

QgsGraphVertex::QgsGraphVertex( const QgsPointXY &point )
Expand All @@ -101,14 +101,14 @@ QgsGraphVertex::QgsGraphVertex( const QgsPointXY &point )

}

QgsGraphEdgeIds QgsGraphVertex::outEdges() const
QgsGraphEdgeIds QgsGraphVertex::incomingEdges() const
{
return mOutEdges;
return mIncomingEdges;
}

QgsGraphEdgeIds QgsGraphVertex::inEdges() const
QgsGraphEdgeIds QgsGraphVertex::outgoingEdges() const
{
return mInEdges;
return mOutgoingEdges;
}

QgsPointXY QgsGraphVertex::point() const
Expand Down
37 changes: 22 additions & 15 deletions src/analysis/network/qgsgraph.h
Expand Up @@ -61,21 +61,23 @@ class ANALYSIS_EXPORT QgsGraphEdge
QVector< QVariant > strategies() const;

/**
* Returns index of the outgoing vertex
* Returns the index of the vertex at the end of this edge.
* \see fromVertex()
*/
int outVertex() const;
int toVertex() const;

/**
* Returns index of the incoming vertex
* Returns the index of the vertex at the start of this edge.
* \see toVertex()
*/
int inVertex() const;
int fromVertex() const;

private:

QVector< QVariant > mStrategies;

int mOut = 0;
int mIn = 0;
int mToIdx = 0;
int mFromIdx = 0;

friend class QgsGraph;
};
Expand All @@ -87,6 +89,7 @@ typedef QList< int > QgsGraphEdgeIds;
* \ingroup analysis
* \class QgsGraphVertex
* \brief This class implements a graph vertex
* \since QGIS 3.0
*/
class ANALYSIS_EXPORT QgsGraphVertex
{
Expand All @@ -104,24 +107,26 @@ class ANALYSIS_EXPORT QgsGraphVertex
QgsGraphVertex( const QgsPointXY &point );

/**
* Returns outgoing edges ids
* Returns the incoming edge ids, i.e. edges which end at this node.
* \see outgoingEdges()
*/
QgsGraphEdgeIds outEdges() const;
QgsGraphEdgeIds incomingEdges() const;

/**
* Return incoming edges ids
* Returns outgoing edge ids, i.e. edges which start at this node.
* \see incomingEdges()
*/
QgsGraphEdgeIds inEdges() const;
QgsGraphEdgeIds outgoingEdges() const;

/**
* Returns point associated with graph vertex
* Returns point associated with graph vertex.
*/
QgsPointXY point() const;

private:
QgsPointXY mCoordinate;
QgsGraphEdgeIds mOutEdges;
QgsGraphEdgeIds mInEdges;
QgsGraphEdgeIds mIncomingEdges;
QgsGraphEdgeIds mOutgoingEdges;

friend class QgsGraph;
};
Expand All @@ -130,6 +135,7 @@ class ANALYSIS_EXPORT QgsGraphVertex
* \ingroup analysis
* \class QgsGraph
* \brief Mathematical graph representation
* \since QGIS 3.0
*/

class ANALYSIS_EXPORT QgsGraph
Expand All @@ -149,9 +155,10 @@ class ANALYSIS_EXPORT QgsGraph
int addVertex( const QgsPointXY &pt );

/**
* Add an edge to the graph
* Add an edge to the graph, going from the \a fromVertexIdx
* to \a toVertexIdx.
*/
int addEdge( int outVertexIdx, int inVertexIdx, const QVector< QVariant > &strategies );
int addEdge( int fromVertexIdx, int toVertexIdx, const QVector< QVariant > &strategies );

/**
* Returns number of graph vertices
Expand Down
12 changes: 6 additions & 6 deletions src/analysis/network/qgsgraphanalyzer.cpp
Expand Up @@ -59,21 +59,21 @@ void QgsGraphAnalyzer::dijkstra( const QgsGraph *source, int startPointIdx, int
not_begin.erase( it );

// edge index list
QgsGraphEdgeIds l = source->vertex( curVertex ).outEdges();
QgsGraphEdgeIds l = source->vertex( curVertex ).incomingEdges();
QgsGraphEdgeIds::iterator arcIt;
for ( arcIt = l.begin(); arcIt != l.end(); ++arcIt )
{
const QgsGraphEdge arc = source->edge( *arcIt );
double cost = arc.cost( criterionNum ).toDouble() + curCost;

if ( cost < ( *result )[ arc.inVertex()] )
if ( cost < ( *result )[ arc.fromVertex()] )
{
( *result )[ arc.inVertex()] = cost;
( *result )[ arc.fromVertex()] = cost;
if ( resultTree )
{
( *resultTree )[ arc.inVertex()] = *arcIt;
( *resultTree )[ arc.fromVertex()] = *arcIt;
}
not_begin.insert( cost, arc.inVertex() );
not_begin.insert( cost, arc.fromVertex() );
}
}
}
Expand Down Expand Up @@ -111,7 +111,7 @@ QgsGraph *QgsGraphAnalyzer::shortestTree( const QgsGraph *source, int startVerte
{
const QgsGraphEdge &arc = source->edge( tree[i] );

treeResult->addEdge( source2result[ arc.outVertex()], source2result[ arc.inVertex()],
treeResult->addEdge( source2result[ arc.fromVertex()], source2result[ arc.toVertex()],
arc.strategies() );
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/network/qgsgraphbuilder.cpp
Expand Up @@ -42,7 +42,7 @@ void QgsGraphBuilder::addVertex( int, const QgsPointXY &pt )

void QgsGraphBuilder::addEdge( int pt1id, const QgsPointXY &, int pt2id, const QgsPointXY &, const QVector< QVariant > &prop )
{
mGraph->addEdge( pt1id, pt2id, prop );
mGraph->addEdge( pt2id, pt1id, prop );
}

QgsGraph *QgsGraphBuilder::graph()
Expand Down
4 changes: 2 additions & 2 deletions src/analysis/network/qgsgraphbuilder.h
Expand Up @@ -46,9 +46,9 @@ class ANALYSIS_EXPORT QgsGraphBuilder : public QgsGraphBuilderInterface
/*
* MANDATORY BUILDER PROPERTY DECLARATION
*/
virtual void addVertex( int id, const QgsPointXY &pt ) override;
void addVertex( int id, const QgsPointXY &pt ) override;

virtual void addEdge( int pt1id, const QgsPointXY &pt1, int pt2id, const QgsPointXY &pt2, const QVector< QVariant > &prop ) override;
void addEdge( int pt1id, const QgsPointXY &pt1, int pt2id, const QgsPointXY &pt2, const QVector< QVariant > &prop ) override;

/**
* Returns generated QgsGraph
Expand Down

0 comments on commit ab05fe7

Please sign in to comment.