graph.c

Go to the documentation of this file.
00001 /****************************************************************************
00002 *
00003 * MODULE:       Vector library 
00004 *               
00005 * AUTHOR(S):    Radim Blazek
00006 *
00007 * PURPOSE:      Higher level functions for reading/writing/manipulating vectors.
00008 *
00009 *               This program is free software under the GNU General Public
00010 *               License (>=v2). Read the file COPYING that comes with GRASS
00011 *               for details.
00012 *
00013 *****************************************************************************/
00014 #include <stdlib.h>
00015 #include <string.h>
00016 #include "gis.h"
00017 #include "dbmi.h"
00018 #include "Vect.h"
00019 
00020 /* TODO: Vect_graph_free ( GRAPH *graph ) */
00021 
00022 static int From_node;   /* from node set in SP and used by clipper for first arc */  
00023 
00024 static int clipper ( dglGraph_s    *pgraph ,
00025                      dglSPClipInput_s  * pargIn ,
00026                      dglSPClipOutput_s * pargOut ,
00027                      void *          pvarg )         /* caller's pointer */
00028 {
00029     dglInt32_t cost;
00030     dglInt32_t from;
00031     
00032     G_debug ( 3, "Net: clipper()" );
00033     
00034     from = dglNodeGet_Id(pgraph, pargIn->pnNodeFrom);
00035     
00036     G_debug ( 3, "  Edge = %d NodeFrom = %d NodeTo = %d edge cost = %d", 
00037                      dglEdgeGet_Id (pgraph, pargIn->pnEdge), 
00038                      from, dglNodeGet_Id(pgraph, pargIn->pnNodeTo),
00039                      pargOut->nEdgeCost);
00040 
00041     if ( from != From_node ) { /* do not clip first */
00042         if ( dglGet_NodeAttrSize(pgraph) > 0 ) {
00043             memcpy( &cost, dglNodeGet_Attr(pgraph, pargIn->pnNodeFrom), sizeof(cost) );
00044             if ( cost == -1 ) { /* closed, cannot go from this node except it is 'from' node */
00045                 G_debug ( 3, "  closed node" );
00046                 return 1;
00047             } else {
00048                 G_debug ( 3, "  EdgeCost += %d (node)", cost );
00049                 pargOut->nEdgeCost += cost;
00050             }
00051         }
00052     } else {
00053         G_debug ( 3, "  don't clip first node" );
00054     }
00055 
00056     return 0;   
00057 }
00058 
00067 void
00068 Vect_graph_init ( GRAPH *graph, int nodes_costs )
00069 {
00070     dglInt32_t opaqueset[ 16 ] = { 360000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
00071 
00072     G_debug (3, "Vect_graph_init()" ); 
00073 
00074     if ( nodes_costs ) dglInitialize ( graph, 1, sizeof(dglInt32_t), 0, opaqueset ); 
00075     else dglInitialize ( graph, 1, 0, 0, opaqueset ); 
00076 }
00077 
00086 void
00087 Vect_graph_build ( GRAPH *graph )
00088 {
00089     int ret;
00090 
00091     G_debug (3, "Vect_graph_build()" ); 
00092 
00093     ret = dglFlatten ( graph );
00094     if ( ret < 0 ) G_fatal_error ("GngFlatten error");
00095 }
00096 
00108 void
00109 Vect_graph_add_edge ( GRAPH *graph, int from, int to, double costs, int id  )
00110 {
00111     int ret;
00112     dglInt32_t dglcosts;
00113     
00114     G_debug (3, "Vect_add_edge() from = %d to = %d, costs = %f, id = %d", from, to, costs, id ); 
00115 
00116     dglcosts = (dglInt32_t) costs * 1000;
00117             
00118     ret = dglAddEdge ( graph, from, to, dglcosts, id );
00119     if ( ret < 0 ) G_fatal_error ("Cannot add network arc");
00120 }
00121 
00131 void
00132 Vect_graph_set_node_costs ( GRAPH *graph, int node, double costs )
00133 {
00134     dglInt32_t dglcosts;
00135     
00136     /* TODO: Not tested! */
00137     G_debug (3, "Vect_graph_set_node_costs()" ); 
00138 
00139     dglcosts = (dglInt32_t) costs * 1000;
00140             
00141     dglNodeSet_Attr(graph, dglGetNode(graph,node), &dglcosts); 
00142 }
00143 
00157 int
00158 Vect_graph_shortest_path ( GRAPH *graph, int from, int to, struct ilist *List, double *cost ) 
00159 {
00160     int i, line, *pclip, cArc, nRet;
00161     dglSPReport_s * pSPReport;
00162     dglInt32_t nDistance;
00163 
00164     G_debug (3, "Vect_graph_shortest_path(): from = %d, to = %d", from, to ); 
00165     
00166     /* Note : if from == to dgl goes to nearest node and returns back (dgl feature) => 
00167     *         check here for from == to */
00168     
00169     if ( List != NULL ) Vect_reset_list ( List);
00170 
00171     /* Check if from and to are identical, otherwise dglib returns path to neares node and back! */
00172     if ( from == to ) {
00173         if ( cost != NULL ) *cost = 0;
00174         return 0;
00175     }
00176 
00177     From_node = from;
00178 
00179     pclip = NULL;
00180     if ( List != NULL ) {
00181         nRet = dglShortestPath ( graph, &pSPReport, from, to, clipper, pclip, NULL);
00182     } else {
00183         nRet = dglShortestDistance ( graph, &nDistance, from, to, clipper, pclip, NULL);
00184     }
00185 
00186     if ( nRet == 0 ) {
00187         if ( cost != NULL )
00188            *cost = PORT_DOUBLE_MAX;
00189         return -1;
00190     }
00191     else if ( nRet < 0 ) {
00192         fprintf( stderr , "dglShortestPath error: %s\n", dglStrerror( graph ) );
00193         return -1;
00194     }
00195     
00196     if ( List != NULL ) {
00197         for( i = 0 ; i < pSPReport->cArc ; i ++ ) {
00198             line = dglEdgeGet_Id(graph, pSPReport->pArc[i].pnEdge);
00199             G_debug( 2, "From %ld to %ld - cost %ld user %d distance %ld" ,
00200                           pSPReport->pArc[i].nFrom,
00201                           pSPReport->pArc[i].nTo,
00202                           /* this is the cost from clip() */
00203                           dglEdgeGet_Cost(graph, pSPReport->pArc[i].pnEdge) / 1000, 
00204                           line,
00205                           pSPReport->pArc[i].nDistance );
00206             Vect_list_append ( List, line );
00207         }
00208     }
00209 
00210     if ( cost != NULL ) {
00211         if ( List != NULL ) 
00212             *cost = (double) pSPReport->nDistance /  1000;
00213         else    
00214             *cost = (double) nDistance / 1000;
00215     }
00216         
00217     if ( List != NULL ) {
00218         cArc = pSPReport->cArc;
00219         dglFreeSPReport( graph, pSPReport );
00220     } else
00221         cArc = 0;
00222 
00223     return (cArc);
00224 }
00225 

Generated on Mon Jan 1 19:49:16 2007 for GRASS by  doxygen 1.5.1