[GRASS-SVN] r56089 - in grass/trunk: include/defs include/vect lib/vector/Vlib

svn_grass at osgeo.org svn_grass at osgeo.org
Thu May 2 14:36:24 PDT 2013


Author: martinl
Date: 2013-05-02 14:36:24 -0700 (Thu, 02 May 2013)
New Revision: 56089

Modified:
   grass/trunk/include/defs/vector.h
   grass/trunk/include/vect/dig_structs.h
   grass/trunk/lib/vector/Vlib/net.c
Log:
vlib: separate graph related info to new struct Graph_info
      add Vect_net_get_graph() to public API


Modified: grass/trunk/include/defs/vector.h
===================================================================
--- grass/trunk/include/defs/vector.h	2013-05-02 20:43:48 UTC (rev 56088)
+++ grass/trunk/include/defs/vector.h	2013-05-02 21:36:24 UTC (rev 56089)
@@ -388,6 +388,7 @@
                          const char *, const char *, int, int);
 int Vect_net_shortest_path(struct Map_info *, int, int, struct ilist *,
                            double *);
+dglGraph_s *Vect_net_get_graph(const struct Map_info *);
 int Vect_net_get_line_cost(const struct Map_info *, int, int, double *);
 int Vect_net_get_node_cost(const struct Map_info *, int, double *);
 int Vect_net_nearest_nodes(struct Map_info *, double, double, double, int,

Modified: grass/trunk/include/vect/dig_structs.h
===================================================================
--- grass/trunk/include/vect/dig_structs.h	2013-05-02 20:43:48 UTC (rev 56088)
+++ grass/trunk/include/vect/dig_structs.h	2013-05-02 21:36:24 UTC (rev 56089)
@@ -1216,6 +1216,42 @@
     } uplist;
 };
 
+/*!
+  \brief Graph-related section (see \ref dblib)
+*/
+struct Graph_info {
+    /*!
+      \brief Line type used to build the graph
+    */
+    int line_type;
+    /*!
+      \brief Graph structure
+    */
+    dglGraph_s graph_s;
+    /*!
+      \brief Shortest path cache
+    */
+    dglSPCache_s spCache;
+    /*!
+      \brief Forward costs used for graph
+      
+      dglGetEdge() is not supported for _DGL_V1)
+    */
+    double *edge_fcosts;
+    /*!
+      \brief backward costs used for graph
+    */
+    double *edge_bcosts;
+    /*!
+      \brief Node costs used for graph
+    */
+    double *node_costs;
+    /*!
+      \brief Edge and node costs multiplicator
+    */
+    int cost_multip;
+};
+
 /*! \brief
   Vector map info
 
@@ -1252,37 +1288,6 @@
     struct Plus_head plus;
 
     /*!
-      \brief Graph-related section - line type used to build the graph
-     */
-    int graph_line_type;
-    /*!
-      \brief Graph-related section - graph structure
-    */
-    dglGraph_s graph;
-    /*!
-      \brief Graph-related section - shortest path cache
-    */
-    dglSPCache_s spCache;
-    /*!
-      \brief Graph-related section - forward costs used for graph
-
-      dglGetEdge() is not supported for _DGL_V1)
-    */
-    double *edge_fcosts;
-    /*!
-      \brief Graph-related section - backward costs used for graph
-    */
-    double *edge_bcosts;
-    /*!
-      \brief Graph-related section - node costs used for graph
-    */
-    double *node_costs;
-    /*!
-      \brief Graph-related section - edge and node costs multiplicator
-    */
-    int cost_multip;
-
-    /*!
       \brief Open indicator
 
       Should be 0x5522AA22 (VECT_OPEN_CODE) if opened correctly
@@ -1389,6 +1394,11 @@
     */
     FILE *hist_fp;
 
+    /*!
+      \brief Graph info (built for network analysis)
+    */
+    struct Graph_info dgraph;
+    
     /*** format specific ***/
 
     /*!

Modified: grass/trunk/lib/vector/Vlib/net.c
===================================================================
--- grass/trunk/lib/vector/Vlib/net.c	2013-05-02 20:43:48 UTC (rev 56088)
+++ grass/trunk/lib/vector/Vlib/net.c	2013-05-02 21:36:24 UTC (rev 56089)
@@ -115,7 +115,7 @@
 
     G_message(_("Building graph..."));
 
-    Map->graph_line_type = ltype;
+    Map->dgraph.line_type = ltype;
 
     Points = Vect_new_line_struct();
     Cats = Vect_new_cats_struct();
@@ -125,26 +125,26 @@
 	ll = 1;			/* LL */
 
     if (afcol == NULL && ll && !geo)
-	Map->cost_multip = 1000000;
+	Map->dgraph.cost_multip = 1000000;
     else
-	Map->cost_multip = 1000;
+	Map->dgraph.cost_multip = 1000;
 
     nlines = Vect_get_num_lines(Map);
     nnodes = Vect_get_num_nodes(Map);
 
-    gr = &(Map->graph);
+    gr = &(Map->dgraph.graph_s);
 
     /* Allocate space for costs, later replace by functions reading costs from graph */
-    Map->edge_fcosts = (double *)G_malloc((nlines + 1) * sizeof(double));
-    Map->edge_bcosts = (double *)G_malloc((nlines + 1) * sizeof(double));
-    Map->node_costs = (double *)G_malloc((nnodes + 1) * sizeof(double));
+    Map->dgraph.edge_fcosts = (double *)G_malloc((nlines + 1) * sizeof(double));
+    Map->dgraph.edge_bcosts = (double *)G_malloc((nlines + 1) * sizeof(double));
+    Map->dgraph.node_costs = (double *)G_malloc((nnodes + 1) * sizeof(double));
     /* Set to -1 initially */
     for (i = 1; i <= nlines; i++) {
-	Map->edge_fcosts[i] = -1;	/* forward */
-	Map->edge_bcosts[i] = -1;	/* backward */
+	Map->dgraph.edge_fcosts[i] = -1;	/* forward */
+	Map->dgraph.edge_bcosts[i] = -1;	/* backward */
     }
     for (i = 1; i <= nnodes; i++) {
-	Map->node_costs[i] = 0;
+	Map->dgraph.node_costs[i] = 0;
     }
 
     if (ncol != NULL)
@@ -293,27 +293,27 @@
 	    bdcost = dcost;
 	}
 	if (dofw && dcost != -1) {
-	    cost = (dglInt32_t) Map->cost_multip * dcost;
+	    cost = (dglInt32_t) Map->dgraph.cost_multip * dcost;
 	    G_debug(5, "Add arc %d from %d to %d cost = %d", i, from, to,
 		    cost);
 	    ret =
 		dglAddEdge(gr, (dglInt32_t) from, (dglInt32_t) to,
 			   (dglInt32_t) cost, (dglInt32_t) i);
-	    Map->edge_fcosts[i] = dcost;
+	    Map->dgraph.edge_fcosts[i] = dcost;
 	    if (ret < 0)
 		G_fatal_error("Cannot add network arc");
 	}
 
 	G_debug(5, "bdcost = %f edge_bcosts = %f", bdcost,
-		Map->edge_bcosts[i]);
+		Map->dgraph.edge_bcosts[i]);
 	if (dobw && bdcost != -1) {
-	    bcost = (dglInt32_t) Map->cost_multip * bdcost;
+	    bcost = (dglInt32_t) Map->dgraph.cost_multip * bdcost;
 	    G_debug(5, "Add arc %d from %d to %d bcost = %d", -i, to, from,
 		    bcost);
 	    ret =
 		dglAddEdge(gr, (dglInt32_t) to, (dglInt32_t) from,
 			   (dglInt32_t) bcost, (dglInt32_t) - i);
-	    Map->edge_bcosts[i] = bdcost;
+	    Map->dgraph.edge_bcosts[i] = bdcost;
 	    if (ret < 0)
 		G_fatal_error(_("Cannot add network arc"));
 	}
@@ -424,12 +424,12 @@
 		cost = -1;
 	    }
 	    else {
-		cost = (dglInt32_t) Map->cost_multip * dcost;
+		cost = (dglInt32_t) Map->dgraph.cost_multip * dcost;
 	    }
 	    G_debug(3, "Set node's cost to %d", cost);
 	    dglNodeSet_Attr(gr, dglGetNode(gr, (dglInt32_t) i),
 			    (dglInt32_t *) (dglInt32_t) & cost);
-	    Map->node_costs[i] = dcost;
+	    Map->dgraph.node_costs[i] = dcost;
 	}
 	db_close_database_shutdown_driver(driver);
 	db_CatValArray_free(&fvarr);
@@ -444,7 +444,7 @@
 
     /* init SP cache */
     /* disable to debug dglib cache */
-    dglInitializeSPCache(gr, &(Map->spCache));
+    dglInitializeSPCache(gr, &(Map->dgraph.spCache));
 
     G_message(_("Graph was built"));
 
@@ -500,24 +500,24 @@
     if (List != NULL) {
 	if (use_cache) {
 	    nRet =
-		dglShortestPath(&(Map->graph), &pSPReport, (dglInt32_t) from,
-				(dglInt32_t) to, clipper, pclip, &(Map->spCache));
+		dglShortestPath(&(Map->dgraph.graph_s), &pSPReport, (dglInt32_t) from,
+				(dglInt32_t) to, clipper, pclip, &(Map->dgraph.spCache));
 	}
 	else {
 	    nRet =
-		dglShortestPath(&(Map->graph), &pSPReport, (dglInt32_t) from,
+		dglShortestPath(&(Map->dgraph.graph_s), &pSPReport, (dglInt32_t) from,
 				(dglInt32_t) to, clipper, pclip, NULL);
 	}
     }
     else {
 	if (use_cache) {
 	    nRet =
-		dglShortestDistance(&(Map->graph), &nDistance, (dglInt32_t) from,
-				    (dglInt32_t) to, clipper, pclip, &(Map->spCache));
+		dglShortestDistance(&(Map->dgraph.graph_s), &nDistance, (dglInt32_t) from,
+				    (dglInt32_t) to, clipper, pclip, &(Map->dgraph.spCache));
 	}
 	else {
 	    nRet =
-		dglShortestDistance(&(Map->graph), &nDistance, (dglInt32_t) from,
+		dglShortestDistance(&(Map->dgraph.graph_s), &nDistance, (dglInt32_t) from,
 				    (dglInt32_t) to, clipper, pclip, NULL);
 	}
     }
@@ -529,14 +529,14 @@
 	return -1;
     }
     else if (nRet < 0) {
-	G_warning(_("dglShortestPath error: %s"), dglStrerror(&(Map->graph)));
+	G_warning(_("dglShortestPath error: %s"), dglStrerror(&(Map->dgraph.graph_s)));
 	return -1;
     }
 
     if (List != NULL) {
 	for (i = 0; i < pSPReport->cArc; i++) {
-	    line = dglEdgeGet_Id(&(Map->graph), pSPReport->pArc[i].pnEdge);
-	    G_debug(2, "From %ld to %ld - cost %ld user %d distance %ld", pSPReport->pArc[i].nFrom, pSPReport->pArc[i].nTo, dglEdgeGet_Cost(&(Map->graph), pSPReport->pArc[i].pnEdge) / Map->cost_multip,	/* this is the cost from clip() */
+	    line = dglEdgeGet_Id(&(Map->dgraph.graph_s), pSPReport->pArc[i].pnEdge);
+	    G_debug(2, "From %ld to %ld - cost %ld user %d distance %ld", pSPReport->pArc[i].nFrom, pSPReport->pArc[i].nTo, dglEdgeGet_Cost(&(Map->dgraph.graph_s), pSPReport->pArc[i].pnEdge) / Map->dgraph.cost_multip,	/* this is the cost from clip() */
 		    line, pSPReport->pArc[i].nDistance);
 	    Vect_list_append(List, line);
 	}
@@ -544,14 +544,14 @@
 
     if (cost != NULL) {
 	if (List != NULL)
-	    *cost = (double)pSPReport->nDistance / Map->cost_multip;
+	    *cost = (double)pSPReport->nDistance / Map->dgraph.cost_multip;
 	else
-	    *cost = (double)nDistance / Map->cost_multip;
+	    *cost = (double)nDistance / Map->dgraph.cost_multip;
     }
 
     if (List != NULL) {
 	cArc = pSPReport->cArc;
-	dglFreeSPReport(&(Map->graph), pSPReport);
+	dglFreeSPReport(&(Map->dgraph.graph_s), pSPReport);
     }
     else
 	cArc = 0;
@@ -559,6 +559,22 @@
     return (cArc);
 }
 
+/*!
+  \brief Get graph structure
+  
+  Graph is built by Vect_net_build_graph().
+  
+  Returns NULL when graph is not built.
+  
+  \param Map pointer to Map_info struct
+
+  \return pointer to dglGraph_s struct or NULL
+*/
+dglGraph_s *Vect_net_get_graph(const struct Map_info *Map)
+{
+    return &(Map->dgraph.graph_s);
+}
+
 /*! 
    \brief Returns in cost for given direction in *cost.
 
@@ -584,33 +600,33 @@
     if (direction == GV_FORWARD) {
 	/* V1 has no index by line-id -> array used */
 	/*
-	   pEdge = dglGetEdge(&(Map->graph), line);
+	   pEdge = dglGetEdge(&(Map->dgraph.graph_s), line);
 	   if (pEdge == NULL)
 		return 0;
-	   *cost = (double) dglEdgeGet_Cost(&(Map->graph), pEdge);
+	   *cost = (double) dglEdgeGet_Cost(&(Map->dgraph.graph_s), pEdge);
 	 */
-	if (Map->edge_fcosts[line] == -1) {
+	if (Map->dgraph.edge_fcosts[line] == -1) {
 	    *cost = -1;
 	    return 0;
 	}
 	else
-	    *cost = Map->edge_fcosts[line];
+            *cost = Map->dgraph.edge_fcosts[line];
     }
     else if (direction == GV_BACKWARD) {
 	/*
-	   pEdge = dglGetEdge(&(Map->graph), -line);
+	   pEdge = dglGetEdge(&(Map->dgraph.graph_s), -line);
 	   if (pEdge == NULL) 
 	    	return 0;
-	   *cost = (double) dglEdgeGet_Cost(&(Map->graph), pEdge);
+	   *cost = (double) dglEdgeGet_Cost(&(Map->dgraph.graph_s), pEdge);
 	 */
-	if (Map->edge_bcosts[line] == -1) {
+	if (Map->dgraph.edge_bcosts[line] == -1) {
 	    *cost = -1;
 	    return 0;
 	}
 	else
-	    *cost = Map->edge_bcosts[line];
+	    *cost = Map->dgraph.edge_bcosts[line];
 	G_debug(5, "Vect_net_get_line_cost(): edge_bcosts = %f",
-		Map->edge_bcosts[line]);
+		Map->dgraph.edge_bcosts[line]);
     }
     else {
 	G_fatal_error(_("Wrong line direction in Vect_net_get_line_cost()"));
@@ -632,7 +648,7 @@
 {
     G_debug(3, "Vect_net_get_node_cost(): node = %d", node);
 
-    *cost = Map->node_costs[node];
+    *cost = Map->dgraph.node_costs[node];
 
     G_debug(3, "  -> cost = %f", *cost);
 
@@ -697,7 +713,7 @@
 	Points = Vect_new_line_struct();
 
     /* Find nearest line */
-    line = Vect_find_line(Map, x, y, z, Map->graph_line_type, maxdist, 0, 0);
+    line = Vect_find_line(Map, x, y, z, Map->dgraph.line_type, maxdist, 0, 0);
 
     if (line < 1)
 	return 0;



More information about the grass-commit mailing list