[GRASS-SVN] r41778 - grass/trunk/lib/vector/neta
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Apr 10 11:16:06 EDT 2010
Author: martinl
Date: 2010-04-10 11:16:06 -0400 (Sat, 10 Apr 2010)
New Revision: 41778
Modified:
grass/trunk/lib/vector/neta/allpairs.c
grass/trunk/lib/vector/neta/articulation_point.c
grass/trunk/lib/vector/neta/bridge.c
grass/trunk/lib/vector/neta/centrality.c
grass/trunk/lib/vector/neta/components.c
grass/trunk/lib/vector/neta/flow.c
grass/trunk/lib/vector/neta/path.c
grass/trunk/lib/vector/neta/spanningtree.c
grass/trunk/lib/vector/neta/timetables.c
grass/trunk/lib/vector/neta/utils.c
Log:
run grass_indent.sh
Modified: grass/trunk/lib/vector/neta/allpairs.c
===================================================================
--- grass/trunk/lib/vector/neta/allpairs.c 2010-04-10 15:12:32 UTC (rev 41777)
+++ grass/trunk/lib/vector/neta/allpairs.c 2010-04-10 15:16:06 UTC (rev 41778)
@@ -1,19 +1,19 @@
/*!
- \file vector/neta/allpairs.c
-
- \brief Network Analysis library - shortest path between all pairs
-
- Computes the length of the shortest path between all pairs of nodes
- in the network.
-
- (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
-
- This program is free software under the GNU General Public License
- (>=v2). Read the file COPYING that comes with GRASS for details.
-
- \author Daniel Bundala (Google Summer of Code 2009)
-*/
+ \file vector/neta/allpairs.c
+ \brief Network Analysis library - shortest path between all pairs
+
+ Computes the length of the shortest path between all pairs of nodes
+ in the network.
+
+ (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
+
+ This program is free software under the GNU General Public License
+ (>=v2). Read the file COPYING that comes with GRASS for details.
+
+ \author Daniel Bundala (Google Summer of Code 2009)
+ */
+
#include <stdio.h>
#include <stdlib.h>
#include <grass/gis.h>
@@ -22,18 +22,18 @@
#include <grass/dgl/graph.h>
/*!
- \brief Stores the directed distance between every pair
+ \brief Stores the directed distance between every pair
- \todo Use only O(W*W) memory where W is the number of nodes present in the graph
+ \todo Use only O(W*W) memory where W is the number of nodes present in the graph
- Upon the completion, dist stores the directed distance between every pair (i,j)
- or -1 if the nodes are unreachable. It must be an array of dimension [nodes+1]*[nodes+1]
-
- \param graph input graph
- \param[out] dist list of directed distance
-
- \return -1 on error
- \return 0 on success
+ Upon the completion, dist stores the directed distance between every pair (i,j)
+ or -1 if the nodes are unreachable. It must be an array of dimension [nodes+1]*[nodes+1]
+
+ \param graph input graph
+ \param[out] dist list of directed distance
+
+ \return -1 on error
+ \return 0 on success
*/
int NetA_allpairs(dglGraph_s * graph, dglInt32_t ** dist)
{
@@ -41,8 +41,10 @@
dglEdgesetTraverser_s et;
dglNodeTraverser_s nt;
dglInt32_t *node;
+
nnodes = dglGet_NodeCount(graph);
dglInt32_t *node_indices;
+
node_indices = (dglInt32_t *) G_calloc(nnodes, sizeof(dglInt32_t));
if (!node_indices) {
G_fatal_error(_("Out of memory"));
@@ -57,9 +59,12 @@
indices = 0;
for (node = dglNode_T_First(&nt); node; node = dglNode_T_Next(&nt)) {
dglInt32_t node_id = dglNodeGet_Id(graph, node);
+
node_indices[indices++] = node_id;
dglInt32_t *edge;
- dglEdgeset_T_Initialize(&et, graph, dglNodeGet_OutEdgeset(graph, node));
+
+ dglEdgeset_T_Initialize(&et, graph,
+ dglNodeGet_OutEdgeset(graph, node));
for (edge = dglEdgeset_T_First(&et); edge;
edge = dglEdgeset_T_Next(&et))
if (dglEdgeGet_Id(graph, edge) < 0) /*ignore backward edges */
@@ -71,16 +76,20 @@
dglNode_T_Release(&nt);
for (k = 0; k < indices; k++) {
dglInt32_t k_index = node_indices[k];
+
G_percent(k + 1, indices, 1);
for (i = 0; i < indices; i++) {
dglInt32_t i_index = node_indices[i];
+
if (dist[i_index][k_index] == -1)
continue; /*no reason to proceed along infinite path */
for (j = 0; j < indices; j++) {
dglInt32_t j_index = node_indices[j];
+
if (dist[k_index][j_index] != -1 &&
(dist[i_index][k_index] + dist[k_index][j_index] <
- dist[i_index][j_index] || dist[i_index][j_index] == -1)) {
+ dist[i_index][j_index] ||
+ dist[i_index][j_index] == -1)) {
dist[i_index][j_index] =
dist[i_index][k_index] + dist[k_index][j_index];
}
Modified: grass/trunk/lib/vector/neta/articulation_point.c
===================================================================
--- grass/trunk/lib/vector/neta/articulation_point.c 2010-04-10 15:12:32 UTC (rev 41777)
+++ grass/trunk/lib/vector/neta/articulation_point.c 2010-04-10 15:16:06 UTC (rev 41778)
@@ -1,18 +1,18 @@
/*!
- \file vector/neta/articulation_point.c
-
- \brief Network Analysis library - connected components
+ \file vector/neta/articulation_point.c
- Computes strongly and weakly connected components.
-
- (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
-
- This program is free software under the GNU General Public License
- (>=v2). Read the file COPYING that comes with GRASS for details.
-
- \author Daniel Bundala (Google Summer of Code 2009)
-*/
+ \brief Network Analysis library - connected components
+ Computes strongly and weakly connected components.
+
+ (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
+
+ This program is free software under the GNU General Public License
+ (>=v2). Read the file COPYING that comes with GRASS for details.
+
+ \author Daniel Bundala (Google Summer of Code 2009)
+ */
+
#include <stdio.h>
#include <stdlib.h>
#include <grass/gis.h>
@@ -21,13 +21,13 @@
#include <grass/dgl/graph.h>
/*!
- \brief Get number of articulation points in the graph
+ \brief Get number of articulation points in the graph
- \param graph input graph
- \param[out] articulation_list list of articulation points
-
- \return number of points
- \return -1 on error
+ \param graph input graph
+ \param[out] articulation_list list of articulation points
+
+ \return number of points
+ \return -1 on error
*/
int NetA_articulation_points(dglGraph_s * graph,
struct ilist *articulation_list)
@@ -45,6 +45,7 @@
dglInt32_t *current_node;
int stack_size;
int i, time;
+
nnodes = dglGet_NodeCount(graph);
current =
(dglEdgesetTraverser_s *) G_calloc(nnodes + 1,
@@ -74,14 +75,17 @@
for (current_node = dglNode_T_First(&nt); current_node;
current_node = dglNode_T_Next(&nt)) {
dglInt32_t current_id = dglNodeGet_Id(graph, current_node);
+
if (tin[current_id] == 0) {
int children = 0; /*number of subtrees rooted at the root/current_node */
+
stack[0] = current_node;
stack_size = 1;
parent[current_id] = NULL;
while (stack_size) {
dglInt32_t *node = stack[stack_size - 1];
dglInt32_t node_id = dglNodeGet_Id(graph, node);
+
if (tin[node_id] == 0) /*vertex visited for the first time */
min_tin[node_id] = tin[node_id] = ++time;
else { /*return from the recursion */
@@ -102,6 +106,7 @@
if (to == parent[node_id])
continue; /*skip parrent */
int to_id = dglNodeGet_Id(graph, to);
+
if (tin[to_id]) { /*back edge, cannot be a bridge/articualtion point */
if (tin[to_id] < min_tin[node_id])
min_tin[node_id] = tin[to_id];
Modified: grass/trunk/lib/vector/neta/bridge.c
===================================================================
--- grass/trunk/lib/vector/neta/bridge.c 2010-04-10 15:12:32 UTC (rev 41777)
+++ grass/trunk/lib/vector/neta/bridge.c 2010-04-10 15:16:06 UTC (rev 41778)
@@ -1,18 +1,18 @@
/*!
- \file vector/neta/bridges.c
-
- \brief Network Analysis library - bridges
+ \file vector/neta/bridges.c
- Computes number of bridges in the graph.
-
- (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
-
- This program is free software under the GNU General Public License
- (>=v2). Read the file COPYING that comes with GRASS for details.
-
- \author Daniel Bundala (Google Summer of Code 2009)
-*/
+ \brief Network Analysis library - bridges
+ Computes number of bridges in the graph.
+
+ (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
+
+ This program is free software under the GNU General Public License
+ (>=v2). Read the file COPYING that comes with GRASS for details.
+
+ \author Daniel Bundala (Google Summer of Code 2009)
+ */
+
#include <stdio.h>
#include <stdlib.h>
#include <grass/gis.h>
@@ -21,16 +21,16 @@
#include <grass/dgl/graph.h>
/*!
- \brief Get number of bridges in the graph.
-
- Bridge is an array containing the indices of the bridges.
+ \brief Get number of bridges in the graph.
- \param graph input graph
- \param[out] bridges_list list of bridges
+ Bridge is an array containing the indices of the bridges.
- \return number of bridges
- \return -1 on error
-*/
+ \param graph input graph
+ \param[out] bridges_list list of bridges
+
+ \return number of bridges
+ \return -1 on error
+ */
int NetA_compute_bridges(dglGraph_s * graph, struct ilist *bridge_list)
{
int nnodes;
@@ -45,6 +45,7 @@
dglInt32_t *current_node;
int stack_size;
int i, time;
+
nnodes = dglGet_NodeCount(graph);
current =
(dglEdgesetTraverser_s *) G_calloc(nnodes + 1,
@@ -73,6 +74,7 @@
for (current_node = dglNode_T_First(&nt); current_node;
current_node = dglNode_T_Next(&nt)) {
dglInt32_t current_id = dglNodeGet_Id(graph, current_node);
+
if (tin[current_id] == 0) {
stack[0] = current_node;
stack_size = 1;
@@ -80,6 +82,7 @@
while (stack_size) {
dglInt32_t *node = stack[stack_size - 1];
dglInt32_t node_id = dglNodeGet_Id(graph, node);
+
if (tin[node_id] == 0) /*vertex visited for the first time */
min_tin[node_id] = tin[node_id] = ++time;
else { /*return from the recursion */
@@ -103,6 +106,7 @@
if (abs(edge_id) == parent[node_id])
continue; /*skip edge we used to travel to this node */
int to_id = dglNodeGet_Id(graph, to);
+
if (tin[to_id]) { /*back edge, cannot be a bridge/articualtion point */
if (tin[to_id] < min_tin[node_id])
min_tin[node_id] = tin[to_id];
Modified: grass/trunk/lib/vector/neta/centrality.c
===================================================================
--- grass/trunk/lib/vector/neta/centrality.c 2010-04-10 15:12:32 UTC (rev 41777)
+++ grass/trunk/lib/vector/neta/centrality.c 2010-04-10 15:16:06 UTC (rev 41778)
@@ -1,18 +1,18 @@
/*!
- \file vector/neta/centality.c
-
- \brief Network Analysis library - centrality
+ \file vector/neta/centality.c
- Centrality measures
-
- (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
-
- This program is free software under the GNU General Public License
- (>=v2). Read the file COPYING that comes with GRASS for details.
-
- \author Daniel Bundala (Google Summer of Code 2009)
-*/
+ \brief Network Analysis library - centrality
+ Centrality measures
+
+ (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
+
+ This program is free software under the GNU General Public License
+ (>=v2). Read the file COPYING that comes with GRASS for details.
+
+ \author Daniel Bundala (Google Summer of Code 2009)
+ */
+
#include <stdio.h>
#include <stdlib.h>
#include <grass/gis.h>
@@ -22,17 +22,18 @@
#include <grass/neta.h>
/*!
- \brief Computes degree centrality measure.
+ \brief Computes degree centrality measure.
- Array degree has to be properly initialised to nnodes+1 elements
+ Array degree has to be properly initialised to nnodes+1 elements
- \param graph input graph
- \param[out] array of degrees
-*/
+ \param graph input graph
+ \param[out] array of degrees
+ */
void NetA_degree_centrality(dglGraph_s * graph, double *degree)
{
int i;
double nnodes = dglGet_NodeCount(graph);
+
for (i = 1; i <= nnodes; i++)
degree[i] =
dglNodeGet_OutDegree(graph,
@@ -40,21 +41,22 @@
}
/*!
- \brief Computes eigenvector centrality using edge costs as weights.
+ \brief Computes eigenvector centrality using edge costs as weights.
- \param graph input graph
- \param iterations number of iterations
- \param error ?
- \param[out] eigenvector eigen vector value
-
- \return 0 on success
- \return -1 on failure
-*/
+ \param graph input graph
+ \param iterations number of iterations
+ \param error ?
+ \param[out] eigenvector eigen vector value
+
+ \return 0 on success
+ \return -1 on failure
+ */
int NetA_eigenvector_centrality(dglGraph_s * graph, int iterations,
double error, double *eigenvector)
{
int i, iter, nnodes;
double *tmp;
+
nnodes = dglGet_NodeCount(graph);
tmp = (double *)G_calloc(nnodes + 1, sizeof(double));
if (!tmp) {
@@ -71,11 +73,13 @@
dglInt32_t *node;
dglNodeTraverser_s nt;
dglEdgesetTraverser_s et;
+
dglNode_T_Initialize(&nt, graph);
for (node = dglNode_T_First(&nt); node; node = dglNode_T_Next(&nt)) {
dglInt32_t node_id = dglNodeGet_Id(graph, node);
double cur_value = eigenvector[node_id];
dglInt32_t *edge;
+
dglEdgeset_T_Initialize(&et, graph,
dglNodeGet_OutEdgeset(graph, node));
for (edge = dglEdgeset_T_First(&et); edge;
@@ -87,12 +91,14 @@
}
dglNode_T_Release(&nt);
double cum_error = 0, max_value = tmp[1];
+
for (i = 2; i <= nnodes; i++)
if (tmp[i] > max_value)
max_value = tmp[i];
for (i = 1; i <= nnodes; i++) {
tmp[i] /= max_value;
- cum_error += (tmp[i] - eigenvector[i]) * (tmp[i] - eigenvector[i]);
+ cum_error +=
+ (tmp[i] - eigenvector[i]) * (tmp[i] - eigenvector[i]);
eigenvector[i] = tmp[i];
}
if (cum_error < error)
@@ -105,18 +111,18 @@
}
/*!
- \brief Computes betweenness and closeness centrality measure using Brandes algorithm.
+ \brief Computes betweenness and closeness centrality measure using Brandes algorithm.
- Edge costs must be nonnegative. If some edge costs are negative then
- the behaviour of this method is undefined.
-
- \param graph input graph
- \param[out] betweenness betweeness values
- \param[out] closeness cloneness values
-
- \return 0 on success
- \return -1 on failure
-*/
+ Edge costs must be nonnegative. If some edge costs are negative then
+ the behaviour of this method is undefined.
+
+ \param graph input graph
+ \param[out] betweenness betweeness values
+ \param[out] closeness cloneness values
+
+ \return 0 on success
+ \return -1 on failure
+ */
int NetA_betweenness_closeness(dglGraph_s * graph, double *betweenness,
double *closeness)
{
@@ -172,6 +178,7 @@
dglHeapInsertMin(&heap, 0, ' ', heap_data);
while (1) {
dglInt32_t v, dist;
+
if (!dglHeapExtractMin(&heap, &heap_node))
break;
v = heap_node.value.ul;
@@ -181,6 +188,7 @@
stack[stack_size++] = v;
dglInt32_t *edge;
+
dglEdgeset_T_Initialize(&et, graph,
dglNodeGet_OutEdgeset(graph,
dglGetNode(graph,
@@ -190,6 +198,7 @@
dglInt32_t *to = dglEdgeGet_Tail(graph, edge);
dglInt32_t to_id = dglNodeGet_Id(graph, to);
dglInt32_t d = dglEdgeGet_Cost(graph, edge);
+
if (dst[to_id] == -1 || dst[to_id] > dist + d) {
dst[to_id] = dist + d;
Vect_reset_list(prev[to_id]);
@@ -209,11 +218,13 @@
delta[i] = 0;
for (i = stack_size - 1; i >= 0; i--) {
dglInt32_t w = stack[i];
+
if (closeness)
closeness[s] += dst[w];
for (j = 0; j < prev[w]->n_values; j++) {
dglInt32_t v = prev[w]->value[j];
+
delta[v] += (cnt[v] / (double)cnt[w]) * (1.0 + delta[w]);
}
if (w != s && betweenness)
Modified: grass/trunk/lib/vector/neta/components.c
===================================================================
--- grass/trunk/lib/vector/neta/components.c 2010-04-10 15:12:32 UTC (rev 41777)
+++ grass/trunk/lib/vector/neta/components.c 2010-04-10 15:16:06 UTC (rev 41778)
@@ -1,18 +1,18 @@
/*!
- \file vector/neta/components.c
-
- \brief Network Analysis library - graph componets
+ \file vector/neta/components.c
- Computes strongly and weakly connected components.
-
- (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
-
- This program is free software under the GNU General Public License
- (>=v2). Read the file COPYING that comes with GRASS for details.
-
- \author Daniel Bundala (Google Summer of Code 2009)
-*/
+ \brief Network Analysis library - graph componets
+ Computes strongly and weakly connected components.
+
+ (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
+
+ This program is free software under the GNU General Public License
+ (>=v2). Read the file COPYING that comes with GRASS for details.
+
+ \author Daniel Bundala (Google Summer of Code 2009)
+ */
+
#include <stdio.h>
#include <stdlib.h>
#include <grass/gis.h>
@@ -21,14 +21,14 @@
#include <grass/dgl/graph.h>
/*!
- \brief Computes weekly connected components
+ \brief Computes weekly connected components
- \param graph input graph
- \param[out] component list of components
+ \param graph input graph
+ \param[out] component list of components
- \return number of components
- \return -1 on failure
-*/
+ \return number of components
+ \return -1 on failure
+ */
int NetA_weakly_connected_components(dglGraph_s * graph, int *component)
{
int nnodes;
@@ -52,6 +52,7 @@
for (cur_node = dglNode_T_First(&nt); cur_node;
cur_node = dglNode_T_Next(&nt)) {
dglInt32_t node_id = dglNodeGet_Id(graph, cur_node);
+
if (!visited[node_id]) {
visited[node_id] = 1;
stack[0] = node_id;
@@ -60,12 +61,14 @@
while (stack_size) {
dglInt32_t *node, *edgeset, *edge;
dglEdgesetTraverser_s et;
+
node = dglGetNode(graph, stack[--stack_size]);
edgeset = dglNodeGet_OutEdgeset(graph, node);
dglEdgeset_T_Initialize(&et, graph, edgeset);
for (edge = dglEdgeset_T_First(&et); edge;
edge = dglEdgeset_T_Next(&et)) {
dglInt32_t to;
+
to = dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge));
if (!visited[to]) {
visited[to] = 1;
@@ -83,14 +86,14 @@
}
/*!
- \brief Computes strongly connected components
+ \brief Computes strongly connected components
- \param graph input graph
- \param[out] component list of components
+ \param graph input graph
+ \param[out] component list of components
- \return number of components
- \return -1 on failure
-*/
+ \return number of components
+ \return -1 on failure
+ */
int NetA_strongly_connected_components(dglGraph_s * graph, int *component)
{
int nnodes;
@@ -116,6 +119,7 @@
for (node = dglNode_T_First(&nt); node; node = dglNode_T_Next(&nt)) {
dglInt32_t node_id = dglNodeGet_Id(graph, node);
+
component[node_id] = 0;
if (!visited[node_id]) {
visited[node_id] = 1;
@@ -125,6 +129,7 @@
dglInt32_t *node, *edgeset, *edge;
dglEdgesetTraverser_s et;
dglInt32_t cur_node_id = stack[stack_size - 1];
+
if (processed[cur_node_id]) {
stack_size--;
order[order_size++] = cur_node_id;
@@ -137,6 +142,7 @@
for (edge = dglEdgeset_T_First(&et); edge;
edge = dglEdgeset_T_Next(&et)) {
dglInt32_t to;
+
if (dglEdgeGet_Id(graph, edge) < 0)
continue; /*ignore backward edges */
to = dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge));
@@ -154,6 +160,7 @@
while (order_size) {
dglInt32_t node_id = order[--order_size];
+
if (component[node_id])
continue;
components++;
@@ -164,12 +171,14 @@
dglInt32_t *node, *edgeset, *edge;
dglEdgesetTraverser_s et;
dglInt32_t cur_node_id = stack[--stack_size];
+
node = dglGetNode(graph, cur_node_id);
edgeset = dglNodeGet_OutEdgeset(graph, node);
dglEdgeset_T_Initialize(&et, graph, edgeset);
for (edge = dglEdgeset_T_First(&et); edge;
edge = dglEdgeset_T_Next(&et)) {
dglInt32_t to;
+
if (dglEdgeGet_Id(graph, edge) > 0)
continue; /*ignore forward edges */
to = dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge));
Modified: grass/trunk/lib/vector/neta/flow.c
===================================================================
--- grass/trunk/lib/vector/neta/flow.c 2010-04-10 15:12:32 UTC (rev 41777)
+++ grass/trunk/lib/vector/neta/flow.c 2010-04-10 15:16:06 UTC (rev 41778)
@@ -1,19 +1,19 @@
/*!
- \file vector/neta/flow.c
-
- \brief Network Analysis library - flow in graph
+ \file vector/neta/flow.c
- Computes the length of the shortest path between all pairs of nodes
- in the network.
-
- (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
-
- This program is free software under the GNU General Public License
- (>=v2). Read the file COPYING that comes with GRASS for details.
-
- \author Daniel Bundala (Google Summer of Code 2009)
-*/
+ \brief Network Analysis library - flow in graph
+ Computes the length of the shortest path between all pairs of nodes
+ in the network.
+
+ (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
+
+ This program is free software under the GNU General Public License
+ (>=v2). Read the file COPYING that comes with GRASS for details.
+
+ \author Daniel Bundala (Google Summer of Code 2009)
+ */
+
#include <stdio.h>
#include <stdlib.h>
#include <grass/gis.h>
@@ -30,20 +30,20 @@
}
/*!
- \brief Get max flow from source to sink.
+ \brief Get max flow from source to sink.
- Array flow stores flow for each edge. Negative flow corresponds to a
- flow in opposite direction The function assumes that the edge costs
- correspond to edge capacities.
+ Array flow stores flow for each edge. Negative flow corresponds to a
+ flow in opposite direction The function assumes that the edge costs
+ correspond to edge capacities.
- \param graph input graph
- \param source_list list of sources
- \param sink_list list of sinks
- \param[out] flow max flows
+ \param graph input graph
+ \param source_list list of sources
+ \param sink_list list of sinks
+ \param[out] flow max flows
- \return number of flows
- \return -1 on failure
-*/
+ \return number of flows
+ \return -1 on failure
+ */
int NetA_flow(dglGraph_s * graph, struct ilist *source_list,
struct ilist *sink_list, int *flow)
{
@@ -77,6 +77,7 @@
while (1) {
dglInt32_t node, edge_id, min_residue;
int found = -1;
+
begin = end = 0;
for (i = 0; i < source_list->n_values; i++)
queue[end++] = source_list->value[i];
@@ -87,6 +88,7 @@
while (begin != end && found == -1) {
dglInt32_t vertex = queue[begin++];
dglInt32_t *edge, *node = dglGetNode(graph, vertex);
+
dglEdgeset_T_Initialize(&et, graph,
dglNodeGet_OutEdgeset(graph, node));
for (edge = dglEdgeset_T_First(&et); edge;
@@ -117,6 +119,7 @@
prev[node]) - sign(edge_id) * flow[abs(edge_id)];
while (!is_source[node]) {
dglInt32_t residue;
+
edge_id = dglEdgeGet_Id(graph, prev[node]);
residue =
dglEdgeGet_Cost(graph,
@@ -145,20 +148,20 @@
}
/*!
- \brief Calculates minimum cut between source(s) and sink(s).
+ \brief Calculates minimum cut between source(s) and sink(s).
- Flow is the array produced by NetA_flow() method when called with
- source_list and sink_list as the input. The output of this and
- NetA_flow() method should be the same.
+ Flow is the array produced by NetA_flow() method when called with
+ source_list and sink_list as the input. The output of this and
+ NetA_flow() method should be the same.
- \param graph input graph
- \param source_list list of sources
- \param sink_list list of sinks
- \param[out] cut list of edges (cut)
+ \param graph input graph
+ \param source_list list of sources
+ \param sink_list list of sinks
+ \param[out] cut list of edges (cut)
- \return number of edges
- \return -1 on failure
-*/
+ \return number of edges
+ \return -1 on failure
+ */
int NetA_min_cut(dglGraph_s * graph, struct ilist *source_list,
struct ilist *sink_list, int *flow, struct ilist *cut)
{
@@ -190,12 +193,15 @@
while (begin != end) {
dglInt32_t vertex = queue[begin++];
dglInt32_t *edge, *node = dglGetNode(graph, vertex);
- dglEdgeset_T_Initialize(&et, graph, dglNodeGet_OutEdgeset(graph, node));
+
+ dglEdgeset_T_Initialize(&et, graph,
+ dglNodeGet_OutEdgeset(graph, node));
for (edge = dglEdgeset_T_First(&et); edge;
edge = dglEdgeset_T_Next(&et)) {
dglInt32_t cap = dglEdgeGet_Cost(graph, edge);
dglInt32_t id = dglEdgeGet_Id(graph, edge);
- dglInt32_t to = dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge));
+ dglInt32_t to =
+ dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge));
if (!visited[to] && cap > sign(id) * flow[abs(id)]) {
visited[to] = 1;
queue[end++] = to;
@@ -209,12 +215,14 @@
if (!visited[i])
continue;
dglInt32_t *node, *edgeset, *edge;
+
node = dglGetNode(graph, i);
edgeset = dglNodeGet_OutEdgeset(graph, node);
dglEdgeset_T_Initialize(&et, graph, edgeset);
for (edge = dglEdgeset_T_First(&et); edge;
edge = dglEdgeset_T_Next(&et)) {
dglInt32_t to, edge_id;
+
to = dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge));
edge_id = abs(dglEdgeGet_Id(graph, edge));
if (!visited[to] && flow[edge_id] != 0) {
@@ -231,22 +239,22 @@
}
/*!
- \brief Splits each vertex of in graph into two vertices
+ \brief Splits each vertex of in graph into two vertices
- The method splits each vertex of in graph into two vertices: in
- vertex and out vertex. Also, it adds an edge from an in vertex to
- the corresponding out vertex (capacity=2) and it adds an edge from
- out vertex to in vertex for each edge present in the in graph
- (forward capacity=1, backward capacity=0). If the id of a vertex is
- v then id of in vertex is 2*v-1 and of out vertex 2*v.
-
- \param in from graph
- \param out to graph
- \param node_cost list of node costs
+ The method splits each vertex of in graph into two vertices: in
+ vertex and out vertex. Also, it adds an edge from an in vertex to
+ the corresponding out vertex (capacity=2) and it adds an edge from
+ out vertex to in vertex for each edge present in the in graph
+ (forward capacity=1, backward capacity=0). If the id of a vertex is
+ v then id of in vertex is 2*v-1 and of out vertex 2*v.
- \return number of undirected edges in the graph
- \return -1 on failure
-*/
+ \param in from graph
+ \param out to graph
+ \param node_cost list of node costs
+
+ \return number of undirected edges in the graph
+ \return -1 on failure
+ */
int NetA_split_vertices(dglGraph_s * in, dglGraph_s * out, int *node_costs)
{
dglInt32_t opaqueset[16] =
@@ -254,17 +262,21 @@
dglNodeTraverser_s nt;
dglInt32_t nnodes, edge_cnt;
dglInt32_t *cur_node;
+
nnodes = dglGet_NodeCount(in);
dglInitialize(out, (dglByte_t) 1, (dglInt32_t) 0, (dglInt32_t) 0,
opaqueset);
dglNode_T_Initialize(&nt, in);
edge_cnt = 0;
dglInt32_t max_node_cost = 0;
+
for (cur_node = dglNode_T_First(&nt); cur_node;
cur_node = dglNode_T_Next(&nt)) {
dglInt32_t v = dglNodeGet_Id(in, cur_node);
+
edge_cnt++;
dglInt32_t cost = 1;
+
if (node_costs)
cost = node_costs[v];
if (cost > max_node_cost)
@@ -279,10 +291,12 @@
dglEdgesetTraverser_s et;
dglInt32_t *edge;
dglInt32_t v = dglNodeGet_Id(in, cur_node);
+
dglEdgeset_T_Initialize(&et, in, dglNodeGet_OutEdgeset(in, cur_node));
for (edge = dglEdgeset_T_First(&et); edge;
edge = dglEdgeset_T_Next(&et)) {
dglInt32_t to;
+
to = dglNodeGet_Id(in, dglEdgeGet_Tail(in, edge));
edge_cnt++;
dglAddEdge(out, 2 * v, 2 * to - 1, max_node_cost + 1, edge_cnt);
Modified: grass/trunk/lib/vector/neta/path.c
===================================================================
--- grass/trunk/lib/vector/neta/path.c 2010-04-10 15:12:32 UTC (rev 41777)
+++ grass/trunk/lib/vector/neta/path.c 2010-04-10 15:16:06 UTC (rev 41778)
@@ -1,18 +1,18 @@
/*!
- \file vector/neta/path.c
-
- \brief Network Analysis library - shortest path
+ \file vector/neta/path.c
- Shortest paths from a set of nodes.
+ \brief Network Analysis library - shortest path
- (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
-
- This program is free software under the GNU General Public License
- (>=v2). Read the file COPYING that comes with GRASS for details.
-
- \author Daniel Bundala (Google Summer of Code 2009)
-*/
+ Shortest paths from a set of nodes.
+ (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
+
+ This program is free software under the GNU General Public License
+ (>=v2). Read the file COPYING that comes with GRASS for details.
+
+ \author Daniel Bundala (Google Summer of Code 2009)
+ */
+
#include <stdio.h>
#include <stdlib.h>
#include <grass/gis.h>
@@ -22,27 +22,29 @@
#include <grass/neta.h>
/*!
- \brief Computes shortests paths to every node from nodes in "from".
+ \brief Computes shortests paths to every node from nodes in "from".
- Array "dst" contains the length of the path or -1 if the node is not
- reachable. Prev contains edges from predecessor along the shortest
- path.
+ Array "dst" contains the length of the path or -1 if the node is not
+ reachable. Prev contains edges from predecessor along the shortest
+ path.
- \param graph input graph
- \param from list of 'from' positions
- \param dst list of 'to' positions
- \param[out] prev list of edges from predecessor along the shortest path
-
- \return 0 on success
- \return -1 on failure
-*/
-int NetA_distance_from_points(dglGraph_s * graph, struct ilist *from, int *dst,
- dglInt32_t ** prev)
+ \param graph input graph
+ \param from list of 'from' positions
+ \param dst list of 'to' positions
+ \param[out] prev list of edges from predecessor along the shortest path
+
+ \return 0 on success
+ \return -1 on failure
+ */
+int NetA_distance_from_points(dglGraph_s * graph, struct ilist *from,
+ int *dst, dglInt32_t ** prev)
{
int i, nnodes;
dglHeap_s heap;
+
nnodes = dglGet_NodeCount(graph);
dglEdgesetTraverser_s et;
+
for (i = 1; i <= nnodes; i++) {
dst[i] = -1;
prev[i] = NULL;
@@ -52,10 +54,12 @@
for (i = 0; i < from->n_values; i++) {
int v = from->value[i];
+
if (dst[v] == 0)
continue; /*ingore duplicates */
dst[v] = 0;
dglHeapData_u heap_data;
+
heap_data.ul = v;
dglHeapInsertMin(&heap, 0, ' ', heap_data);
}
@@ -63,6 +67,7 @@
dglInt32_t v, dist;
dglHeapNode_s heap_node;
dglHeapData_u heap_data;
+
if (!dglHeapExtractMin(&heap, &heap_node))
break;
v = heap_node.value.ul;
@@ -71,6 +76,7 @@
continue;
dglInt32_t *edge;
+
dglEdgeset_T_Initialize(&et, graph,
dglNodeGet_OutEdgeset(graph,
dglGetNode(graph, v)));
@@ -79,6 +85,7 @@
dglInt32_t *to = dglEdgeGet_Tail(graph, edge);
dglInt32_t to_id = dglNodeGet_Id(graph, to);
dglInt32_t d = dglEdgeGet_Cost(graph, edge);
+
if (dst[to_id] == -1 || dst[to_id] > dist + d) {
dst[to_id] = dist + d;
prev[to_id] = edge;
@@ -96,21 +103,21 @@
}
/*!
- \brief Find a path (minimum number of edges) from 'from' to 'to' using only edges in 'edges'.
+ \brief Find a path (minimum number of edges) from 'from' to 'to' using only edges in 'edges'.
- Precisely, edge with id I is used iff edges[abs(i)] == 1. List
- stores the indices of lines on the path. Method return number of
- edges or -1 if no path exist.
+ Precisely, edge with id I is used iff edges[abs(i)] == 1. List
+ stores the indices of lines on the path. Method return number of
+ edges or -1 if no path exist.
- \param graph input graph
- \param from 'from' position
- \param to 'to' position
- \param edges list of available edges
- \param[out] list list of edges
-
- \return number of edges
- \return -1 on failure
-*/
+ \param graph input graph
+ \param from 'from' position
+ \param to 'to' position
+ \param edges list of available edges
+ \param[out] list list of edges
+
+ \return number of edges
+ \return -1 on failure
+ */
int NetA_find_path(dglGraph_s * graph, int from, int to, int *edges,
struct ilist *list)
{
@@ -136,14 +143,18 @@
prev[from] = NULL;
while (begin != end) {
dglInt32_t vertex = queue[begin++];
+
if (vertex == to)
break;
dglInt32_t *edge, *node = dglGetNode(graph, vertex);
- dglEdgeset_T_Initialize(&et, graph, dglNodeGet_OutEdgeset(graph, node));
+
+ dglEdgeset_T_Initialize(&et, graph,
+ dglNodeGet_OutEdgeset(graph, node));
for (edge = dglEdgeset_T_First(&et); edge;
edge = dglEdgeset_T_Next(&et)) {
dglInt32_t id = abs(dglEdgeGet_Id(graph, edge));
- dglInt32_t to = dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge));
+ dglInt32_t to =
+ dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge));
if (edges[id] && !vis[to]) {
vis[to] = 'y';
prev[to] = edge;
Modified: grass/trunk/lib/vector/neta/spanningtree.c
===================================================================
--- grass/trunk/lib/vector/neta/spanningtree.c 2010-04-10 15:12:32 UTC (rev 41777)
+++ grass/trunk/lib/vector/neta/spanningtree.c 2010-04-10 15:16:06 UTC (rev 41778)
@@ -1,18 +1,18 @@
/*!
- \file vector/neta/spanningtree.c
-
- \brief Network Analysis library - spanning tree
+ \file vector/neta/spanningtree.c
- Computes minimum spanning tree in the network.
-
- (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
-
- This program is free software under the GNU General Public License
- (>=v2). Read the file COPYING that comes with GRASS for details.
-
- \author Daniel Bundala (Google Summer of Code 2009)
-*/
+ \brief Network Analysis library - spanning tree
+ Computes minimum spanning tree in the network.
+
+ (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
+
+ This program is free software under the GNU General Public License
+ (>=v2). Read the file COPYING that comes with GRASS for details.
+
+ \author Daniel Bundala (Google Summer of Code 2009)
+ */
+
#include <stdio.h>
#include <stdlib.h>
#include <grass/gis.h>
@@ -44,6 +44,7 @@
static int uf_find(struct union_find *uf, int v)
{
int cur = v, tmp;
+
while (uf->parent[cur] != cur)
cur = uf->parent[cur];
while (uf->parent[v] != v) {
@@ -59,6 +60,7 @@
{
int parent_u = uf_find(uf, u);
int parent_v = uf_find(uf, v);
+
if (parent_u != parent_v)
uf->parent[parent_u] = parent_v;
}
@@ -75,20 +77,21 @@
}
/*!
- \brief Get number of edges in the spanning forest
+ \brief Get number of edges in the spanning forest
- \param graph input graph
- \param[out] list of edges
+ \param graph input graph
+ \param[out] list of edges
- \return number of edges
- \return -1 on failure
-*/
+ \return number of edges
+ \return -1 on failure
+ */
int NetA_spanning_tree(dglGraph_s * graph, struct ilist *tree_list)
{
int nnodes, edges, nedges, i, index;
edge_cost_pair *perm; /*permutaion of edges in ascending order */
struct union_find uf;
dglEdgesetTraverser_s et;
+
nnodes = dglGet_NodeCount(graph);
nedges = dglGet_EdgeCount(graph);
perm = (edge_cost_pair *) G_calloc(nedges, sizeof(edge_cost_pair));
@@ -103,6 +106,7 @@
for (i = 1; i <= nnodes; i++) {
G_percent(i, nnodes + nedges, 1);
dglInt32_t *edge;
+
dglEdgeset_T_Initialize(&et, graph,
dglNodeGet_OutEdgeset(graph,
dglGetNode(graph,
Modified: grass/trunk/lib/vector/neta/timetables.c
===================================================================
--- grass/trunk/lib/vector/neta/timetables.c 2010-04-10 15:12:32 UTC (rev 41777)
+++ grass/trunk/lib/vector/neta/timetables.c 2010-04-10 15:16:06 UTC (rev 41778)
@@ -1,18 +1,18 @@
/*!
- \file vector/neta/timetables.c
-
- \brief Network Analysis library - timetables
+ \file vector/neta/timetables.c
- Shortest path using timetables.
-
- (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
-
- This program is free software under the GNU General Public License
- (>=v2). Read the file COPYING that comes with GRASS for details.
-
- \author Daniel Bundala (Google Summer of Code 2009)
-*/
+ \brief Network Analysis library - timetables
+ Shortest path using timetables.
+
+ (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
+
+ This program is free software under the GNU General Public License
+ (>=v2). Read the file COPYING that comes with GRASS for details.
+
+ \author Daniel Bundala (Google Summer of Code 2009)
+ */
+
#include <stdio.h>
#include <stdlib.h>
#include <grass/gis.h>
@@ -23,16 +23,16 @@
#include <grass/neta.h>
/*!
- \brief Get number of distinct elements
+ \brief Get number of distinct elements
- \param driver DB driver
- \param sql SQl string
- \param[out] list of lengths
- \param[out] list of ids
-
- \return number of distinct elements
- \return -1 on failure
-*/
+ \param driver DB driver
+ \param sql SQl string
+ \param[out] list of lengths
+ \param[out] list of ids
+
+ \return number of distinct elements
+ \return -1 on failure
+ */
int NetA_init_distinct(dbDriver * driver, dbString * sql, int **lengths,
int **ids)
{
@@ -94,22 +94,22 @@
}
/*!
- \brief Initialises timetable from a database
+ \brief Initialises timetable from a database
- \param In pointer to Map_info structure
- \param route_layer layer number of routes
- \param walk_layer layer number of walkers
- \param route_id id of route
- \param times list of timestamps
- \param to_stop ?
- \param walk_length walk length as string
- \param timetable pointer to neta_timetable
- \param route_ids list of route ids
- \param stop_ids lits of stop ids
+ \param In pointer to Map_info structure
+ \param route_layer layer number of routes
+ \param walk_layer layer number of walkers
+ \param route_id id of route
+ \param times list of timestamps
+ \param to_stop ?
+ \param walk_length walk length as string
+ \param timetable pointer to neta_timetable
+ \param route_ids list of route ids
+ \param stop_ids lits of stop ids
- \return 0 on success
- \return non-zero value on failure
-*/
+ \return 0 on success
+ \return non-zero value on failure
+ */
int NetA_init_timetable_from_db(struct Map_info *In, int route_layer,
int walk_layer, char *route_id, char *times,
char *to_stop, char *walk_length,
@@ -126,6 +126,7 @@
dbDriver *driver;
struct field_info *Fi;
+
Fi = Vect_get_field(In, route_layer);
driver = db_start_driver_open_database(Fi->driver, Fi->database);
if (driver == NULL)
@@ -138,20 +139,25 @@
route_id);
db_set_string(&sql, buf);
timetable->routes =
- NetA_init_distinct(driver, &sql, &(timetable->route_length), route_ids);
+ NetA_init_distinct(driver, &sql, &(timetable->route_length),
+ route_ids);
if (timetable->routes < 0)
return 1;
- sprintf(buf, "select %s from %s order by %s", Fi->key, Fi->table, Fi->key);
+ sprintf(buf, "select %s from %s order by %s", Fi->key, Fi->table,
+ Fi->key);
db_set_string(&sql, buf);
timetable->stops =
NetA_init_distinct(driver, &sql, &(timetable->stop_length), stop_ids);
if (timetable->stops < 0)
return 1;
- timetable->route_stops = (int **)G_calloc(timetable->routes, sizeof(int *));
- timetable->route_times = (int **)G_calloc(timetable->routes, sizeof(int *));
- timetable->stop_routes = (int **)G_calloc(timetable->stops, sizeof(int *));
+ timetable->route_stops =
+ (int **)G_calloc(timetable->routes, sizeof(int *));
+ timetable->route_times =
+ (int **)G_calloc(timetable->routes, sizeof(int *));
+ timetable->stop_routes =
+ (int **)G_calloc(timetable->stops, sizeof(int *));
timetable->stop_times = (int **)G_calloc(timetable->stops, sizeof(int *));
timetable->walk_length = (int *)G_calloc(timetable->stops, sizeof(int));
timetable->walk_stops = (int **)G_calloc(timetable->stops, sizeof(int *));
@@ -221,15 +227,16 @@
timetable->stop_times[stop][timetable->stop_length[stop]++] = time;
timetable->route_stops[route][timetable->route_length[route]] = stop;
- timetable->route_times[route][timetable->route_length[route]++] = time;
+ timetable->route_times[route][timetable->route_length[route]++] =
+ time;
}
db_close_cursor(&cursor);
if (walk_layer != -1) {
Fi = Vect_get_field(In, walk_layer);
- sprintf(buf, "select %s, %s, %s from %s", Fi->key, to_stop, walk_length,
- Fi->table);
+ sprintf(buf, "select %s, %s, %s from %s", Fi->key, to_stop,
+ walk_length, Fi->table);
db_set_string(&sql, buf);
if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) !=
@@ -246,8 +253,8 @@
value = db_get_column_value(column2);
stop = db_get_value_int(value);
stop_pnt =
- (int *)bsearch(&stop, *stop_ids, timetable->stops, sizeof(int),
- cmp_int);
+ (int *)bsearch(&stop, *stop_ids, timetable->stops,
+ sizeof(int), cmp_int);
if (stop_pnt) {
value = db_get_column_value(column1);
stop = db_get_value_int(value);
@@ -289,8 +296,8 @@
value = db_get_column_value(column2);
stop = db_get_value_int(value);
stop_pnt =
- (int *)bsearch(&stop, *stop_ids, timetable->stops, sizeof(int),
- cmp_int);
+ (int *)bsearch(&stop, *stop_ids, timetable->stops,
+ sizeof(int), cmp_int);
if (stop_pnt) {
stop2 = stop_pnt - (*stop_ids);
value = db_get_column_value(column1);
@@ -324,26 +331,27 @@
static neta_heap_data *new_heap_data(int conns, int v)
{
- neta_heap_data *d = (neta_heap_data *) G_calloc(1, sizeof(neta_heap_data));
+ neta_heap_data *d =
+ (neta_heap_data *) G_calloc(1, sizeof(neta_heap_data));
d->v = v;
d->conns = conns;
return d;
}
/*!
- \brief Update Dijkstra structures
+ \brief Update Dijkstra structures
- \param olc_conns old connection
- \param new_conns new connection
- \param to old 'to' node
- \param new_dst new 'to' node
- \param v ?
- \param route id of route
- \param rows ?
- \param update ?
- \param[out] result pointer to neta_timetable_result structure
- \param heap ?
-*/
+ \param olc_conns old connection
+ \param new_conns new connection
+ \param to old 'to' node
+ \param new_dst new 'to' node
+ \param v ?
+ \param route id of route
+ \param rows ?
+ \param update ?
+ \param[out] result pointer to neta_timetable_result structure
+ \param heap ?
+ */
void NetA_update_dijkstra(int old_conns, int new_conns, int to, int new_dst,
int v, int route, int rows, int update,
neta_timetable_result * result, dglHeap_s * heap)
@@ -356,6 +364,7 @@
result->prev_conn[new_conns][to] = old_conns;
if (update) {
dglHeapData_u heap_data;
+
heap_data.pv = (void *)new_heap_data(new_conns, to);
dglHeapInsertMin(heap, new_dst, ' ', heap_data);
}
@@ -363,23 +372,23 @@
}
/*!
- \brief Computes the earliest arrival time.
+ \brief Computes the earliest arrival time.
- Computes the earliest arrival time to to_stop from from_stop
- starting at start_time, or -1 if no path exists
+ Computes the earliest arrival time to to_stop from from_stop
+ starting at start_time, or -1 if no path exists
- \param timetable pointer to neta_timetable structure
- \param from_stop 'from' node
- \param to_stop 'to' stop
- \param start_time start timestamp
- \param min_change ?
- \param max_changes ?
- \param walking_change ?
- \param[out] result pointer to neta_timetable_result
+ \param timetable pointer to neta_timetable structure
+ \param from_stop 'from' node
+ \param to_stop 'to' stop
+ \param start_time start timestamp
+ \param min_change ?
+ \param max_changes ?
+ \param walking_change ?
+ \param[out] result pointer to neta_timetable_result
- \return ?
- \return -1 on error
-*/
+ \return ?
+ \return -1 on error
+ */
int NetA_timetable_shortest_path(neta_timetable * timetable, int from_stop,
int to_stop, int start_time, int min_change,
int max_changes, int walking_change,
@@ -389,6 +398,7 @@
dglHeap_s heap;
int opt_conns, rows = 1;
+
if (max_changes != -1)
rows = max_changes + 2;
@@ -407,7 +417,8 @@
for (i = 0; i < rows; i++) {
result->dst[i] = (int *)G_calloc(timetable->stops, sizeof(int));
result->prev_stop[i] = (int *)G_calloc(timetable->stops, sizeof(int));
- result->prev_route[i] = (int *)G_calloc(timetable->stops, sizeof(int));
+ result->prev_route[i] =
+ (int *)G_calloc(timetable->stops, sizeof(int));
result->prev_conn[i] = (int *)G_calloc(timetable->stops, sizeof(int));
if (!result->dst[i] || !result->prev_stop[i] || !result->prev_route[i]
|| !result->prev_conn[i]) {
@@ -438,6 +449,7 @@
result->dst[0][from_stop] = start_time - min_change;
result->prev_stop[0][from_stop] = result->prev_route[0][from_stop] = -1;
dglHeapData_u heap_data;
+
heap_data.pv = (void *)new_heap_data(0, from_stop);
dglHeapInsertMin(&heap, start_time - min_change, ' ', heap_data);
@@ -445,6 +457,7 @@
dglInt32_t v, dist, conns;
dglHeapNode_s heap_node;
int new_conns, walk_conns, update;
+
if (!dglHeapExtractMin(&heap, &heap_node))
break;
v = ((neta_heap_data *) (heap_node.value.pv))->v;
@@ -465,6 +478,7 @@
for (i = 0; i < timetable->walk_length[v]; i++) {
int to = timetable->walk_stops[v][i];
int new_dst = dist + timetable->walk_times[v][i];
+
NetA_update_dijkstra(conns, walk_conns, to, new_dst, v, -2,
rows, update, result, &heap);
}
@@ -476,6 +490,7 @@
for (i = 0; i < timetable->stop_length[v]; i++)
if (timetable->stop_times[v][i] >= dist + min_change) {
int route = timetable->stop_routes[v][i];
+
/*find the index of v on the route */
for (j = 0; j < timetable->route_length[route]; j++)
if (timetable->route_stops[route][j] == v)
@@ -483,6 +498,7 @@
j++;
for (; j < timetable->route_length[route]; j++) {
int to = timetable->route_stops[route][j];
+
NetA_update_dijkstra(conns, new_conns, to,
timetable->route_times[route][j], v,
route, rows, 1, result, &heap);
@@ -504,21 +520,22 @@
}
/*!
- \brief Get time
+ \brief Get time
- Get time when route "route" arrives at stop "stop" or -1.
+ Get time when route "route" arrives at stop "stop" or -1.
- \param timetable pointer to neta_timetable structure
- \param stop 'stop' node id
- \param route route id
+ \param timetable pointer to neta_timetable structure
+ \param stop 'stop' node id
+ \param route route id
- \return time
- \return -1 if not found
-*/
+ \return time
+ \return -1 if not found
+ */
int NetA_timetable_get_route_time(neta_timetable * timetable, int stop,
int route)
{
int i;
+
for (i = 0; i < timetable->route_length[route]; i++)
if (timetable->route_stops[route][i] == stop)
return timetable->route_times[route][i];
@@ -526,13 +543,14 @@
}
/*!
- \brief Free neta_timetable_result structure
-
- \param result pointer to neta_timetable_result structure
-*/
+ \brief Free neta_timetable_result structure
+
+ \param result pointer to neta_timetable_result structure
+ */
void NetA_timetable_result_release(neta_timetable_result * result)
{
int i;
+
for (i = 0; i < result->rows; i++) {
G_free(result->dst[i]);
G_free(result->prev_stop[i]);
Modified: grass/trunk/lib/vector/neta/utils.c
===================================================================
--- grass/trunk/lib/vector/neta/utils.c 2010-04-10 15:12:32 UTC (rev 41777)
+++ grass/trunk/lib/vector/neta/utils.c 2010-04-10 15:16:06 UTC (rev 41778)
@@ -1,18 +1,18 @@
/*!
- \file vector/neta/timetables.c
-
- \brief Network Analysis library - utils
+ \file vector/neta/timetables.c
- Utils subroutines.
-
- (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
-
- This program is free software under the GNU General Public License
- (>=v2). Read the file COPYING that comes with GRASS for details.
-
- \author Daniel Bundala (Google Summer of Code 2009)
-*/
+ \brief Network Analysis library - utils
+ Utils subroutines.
+
+ (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
+
+ This program is free software under the GNU General Public License
+ (>=v2). Read the file COPYING that comes with GRASS for details.
+
+ \author Daniel Bundala (Google Summer of Code 2009)
+ */
+
#include <stdio.h>
#include <stdlib.h>
#include <grass/gis.h>
@@ -23,20 +23,21 @@
/*!
- \brief Writes point
+ \brief Writes point
- Writes GV_POINT to Out at the position of the node in <em>In</em>.
-
- \param In pointer to Map_info structure (input vector map)
- \param[in,out] Out pointer to Map_info structure (output vector map)
- \param node node id
- \param Cats pointer to line_cats structures
-*/
-void NetA_add_point_on_node(struct Map_info *In, struct Map_info *Out, int node,
- struct line_cats *Cats)
+ Writes GV_POINT to Out at the position of the node in <em>In</em>.
+
+ \param In pointer to Map_info structure (input vector map)
+ \param[in,out] Out pointer to Map_info structure (output vector map)
+ \param node node id
+ \param Cats pointer to line_cats structures
+ */
+void NetA_add_point_on_node(struct Map_info *In, struct Map_info *Out,
+ int node, struct line_cats *Cats)
{
static struct line_pnts *Points;
double x, y, z;
+
Points = Vect_new_line_struct();
Vect_get_node_coor(In, node, &x, &y, &z);
Vect_reset_line(Points);
@@ -62,16 +63,17 @@
*/
/*!
- \brief Finds node
+ \brief Finds node
- Find the node corresponding to each point in the point_list
-
- \param In pointer to Map_info structure
- \param point_list list of points (their ids)
-*/
+ Find the node corresponding to each point in the point_list
+
+ \param In pointer to Map_info structure
+ \param point_list list of points (their ids)
+ */
void NetA_points_to_nodes(struct Map_info *In, struct ilist *point_list)
{
int i, node;
+
for (i = 0; i < point_list->n_values; i++) {
Vect_get_line_nodes(In, point_list->value[i], &node, NULL);
point_list->value[i] = node;
@@ -79,23 +81,23 @@
}
/*!
- \brief Get node cost
+ \brief Get node cost
- For each node in the map, finds the category of the point on it (if
- there is any) and stores the value associated with this category in
- the array node_costs. If there is no point with a category,
- node_costs=0.
-
- node_costs are multiplied by 1000000 and truncated to integers (as
- is done in Vect_net_build_graph)
-
- \param In pointer to Map_info structure
- \param layer layer number
- \param column name of column
- \param[out] node_costs list of node costs
+ For each node in the map, finds the category of the point on it (if
+ there is any) and stores the value associated with this category in
+ the array node_costs. If there is no point with a category,
+ node_costs=0.
- \returns 1 on success
- \return 0 on failure
+ node_costs are multiplied by 1000000 and truncated to integers (as
+ is done in Vect_net_build_graph)
+
+ \param In pointer to Map_info structure
+ \param layer layer number
+ \param column name of column
+ \param[out] node_costs list of node costs
+
+ \returns 1 on success
+ \return 0 on failure
*/
int NetA_get_node_costs(struct Map_info *In, int layer, char *column,
int *node_costs)
@@ -107,6 +109,7 @@
dbDriver *driver;
struct field_info *Fi;
+
Fi = Vect_get_field(In, layer);
driver = db_start_driver_open_database(Fi->driver, Fi->database);
if (driver == NULL)
@@ -127,9 +130,11 @@
return 0;
for (i = 1; i <= nlines; i++) {
int type = Vect_read_line(In, Points, Cats, i);
+
if (type == GV_POINT) {
int node, cat;
double value;
+
if (!Vect_cat_get(Cats, layer, &cat))
continue;
Vect_get_line_nodes(In, i, &node, NULL);
@@ -145,23 +150,24 @@
}
/*!
- \brief Get list of nodes from varray
+ \brief Get list of nodes from varray
- Returns the list of all nodes on features selected by varray.
- nodes_to_features conains the index of a feature adjecent to each
- node or -1 if no such feature specified by varray
- exists. Nodes_to_features might be NULL, in which case it is left
- unitialised.
+ Returns the list of all nodes on features selected by varray.
+ nodes_to_features conains the index of a feature adjecent to each
+ node or -1 if no such feature specified by varray
+ exists. Nodes_to_features might be NULL, in which case it is left
+ unitialised.
- \param map pointer to Map_info structure
- \param varray pointer to varray structure
- \param[out] nodes list of node ids
- \param node_to_features ?
-*/
-void NetA_varray_to_nodes(struct Map_info *map, struct varray * varray,
+ \param map pointer to Map_info structure
+ \param varray pointer to varray structure
+ \param[out] nodes list of node ids
+ \param node_to_features ?
+ */
+void NetA_varray_to_nodes(struct Map_info *map, struct varray *varray,
struct ilist *nodes, int *nodes_to_features)
{
int nlines, nnodes, i;
+
nlines = Vect_get_num_lines(map);
nnodes = Vect_get_num_nodes(map);
if (nodes_to_features)
@@ -171,8 +177,10 @@
for (i = 1; i <= nlines; i++)
if (varray->c[i]) {
int type = Vect_read_line(map, NULL, NULL, i);
+
if (type == GV_POINT) {
int node;
+
Vect_get_line_nodes(map, i, &node, NULL);
Vect_list_append(nodes, node);
if (nodes_to_features)
@@ -180,6 +188,7 @@
}
else {
int node1, node2;
+
Vect_get_line_nodes(map, i, &node1, &node2);
Vect_list_append(nodes, node1);
Vect_list_append(nodes, node2);
@@ -190,27 +199,26 @@
}
/*!
- \brief Initialize varray
+ \brief Initialize varray
- \param In pointer to Map_info structure
- \param layer layer number
- \param mask_type ?
- \param where where statement
- \param cat ?
- \param[out] pointer to varray structure
+ \param In pointer to Map_info structure
+ \param layer layer number
+ \param mask_type ?
+ \param where where statement
+ \param cat ?
+ \param[out] pointer to varray structure
- \return ?
-*/
+ \return ?
+ */
int NetA_initialise_varray(struct Map_info *In, int layer, int mask_type,
- char *where, char *cat, struct varray ** varray)
+ char *where, char *cat, struct varray **varray)
{
/* parse filter option and select appropriate lines */
if (where) {
if (layer < 1)
G_fatal_error(_("'%s' must be > 0 for '%s'"), "layer", "where");
if (cat)
- G_warning(_
- ("'where' and 'cats' parameters were supplied, cat will be ignored"));
+ G_warning(_("'where' and 'cats' parameters were supplied, cat will be ignored"));
*varray = Vect_new_varray(Vect_get_num_lines(In));
if (Vect_set_varray_from_db
(In, layer, where, mask_type, 1, *varray) == -1) {
More information about the grass-commit
mailing list