[GRASS-SVN] r74053 - grass-addons/grass7/raster/r.accumulate

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Feb 1 14:46:32 PST 2019


Author: hcho
Date: 2019-02-01 14:46:32 -0800 (Fri, 01 Feb 2019)
New Revision: 74053

Modified:
   grass-addons/grass7/raster/r.accumulate/calculate_lfp.c
Log:
r.accumulate: Less recursion by skipping upstream cells with shorter longest lfp

Modified: grass-addons/grass7/raster/r.accumulate/calculate_lfp.c
===================================================================
--- grass-addons/grass7/raster/r.accumulate/calculate_lfp.c	2019-02-01 03:31:51 UTC (rev 74052)
+++ grass-addons/grass7/raster/r.accumulate/calculate_lfp.c	2019-02-01 22:46:32 UTC (rev 74053)
@@ -162,6 +162,7 @@
                     struct point_list *pl, struct line_list *ll)
 {
     static struct line_pnts *Points = NULL;
+    static double diag_length;
     int rows = dir_buf->rows, cols = dir_buf->cols;
     double x, y;
     int i, j, nup;
@@ -209,37 +210,42 @@
     qsort(up_accum, nup, sizeof(struct neighbor_accum),
           compare_neighbor_accum);
 
-    if (!Points)
+    if (!Points) {
         Points = Vect_new_line_struct();
+        diag_length =
+            sqrt(pow(window->ew_res, 2.0) + pow(window->ns_res, 2.0));
+    }
 
     /* trace up upstream cells */
     for (i = 0; i < nup; i++) {
-        static double diag_length;
-
         /* store pl->n to come back later */
         int split_pl_n = pl->n;
 
-        /* try to avoid unnecessary tracing */
+        /* theoretically, the longest longest flow path is when all accumulated
+         * upstream cells are diagonally flowing */
+        double max_length = up_accum[i].accum * diag_length;
+
+        /* skip the current cell if its theoretical longest upstream length is
+         * shorter than the first cell's theoretical shortest upstream length
+         */
+        if (i > 0 && max_length < up_accum[0].accum)
+            continue;
+
+        /* if the current cell's theoretical longest lfp < all existing, skip
+         * tracing because it's impossible to obtain a longer lfp */
         if (ll->n) {
-            double max_length;
-
-            /* theoretically, the longest longest flow path is when all
-             * accumulated upstream cells are diagonally flowing */
             Vect_reset_line(Points);
             Vect_copy_xyz_to_pnts(Points, pl->x, pl->y, NULL, pl->n);
-            max_length =
-                Vect_line_length(Points) + up_accum[i].accum * diag_length;
 
+            /* the current cell's downstream length + theoretical longest
+             * upstream length */
+            max_length += Vect_line_length(Points);
+
             for (j = 0; j < ll->n && max_length < ll->lines[j]->length; j++) ;
 
-            /* if the theoretical longest lfp < all existing, skip tracing
-             * because it's impossible to obtain a longer lfp */
             if (j == ll->n)
                 continue;
         }
-        else
-            diag_length =
-                sqrt(pow(window->ew_res, 2.0) + pow(window->ns_res, 2.0));
 
         /* if tracing is successful, store the line and its length */
         if (trace_up



More information about the grass-commit mailing list