[GRASS-SVN] r70861 - in grass-addons/grass7/raster/r.pi: r.pi.nlm r.pi.nlm.stats

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Apr 11 06:05:55 PDT 2017


Author: mmetz
Date: 2017-04-11 06:05:55 -0700 (Tue, 11 Apr 2017)
New Revision: 70861

Modified:
   grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/fractal.c
   grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/func.c
   grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/local_proto.h
   grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/main.c
   grass-addons/grass7/raster/r.pi/r.pi.nlm/func.c
   grass-addons/grass7/raster/r.pi/r.pi.nlm/local_proto.h
   grass-addons/grass7/raster/r.pi/r.pi.nlm/main.c
Log:
r.pi.nlm: fix bugs

Modified: grass-addons/grass7/raster/r.pi/r.pi.nlm/func.c
===================================================================
--- grass-addons/grass7/raster/r.pi/r.pi.nlm/func.c	2017-04-11 12:32:44 UTC (rev 70860)
+++ grass-addons/grass7/raster/r.pi/r.pi.nlm/func.c	2017-04-11 13:05:55 UTC (rev 70861)
@@ -1,14 +1,11 @@
 #include "local_proto.h"
 
-int GetCell(double *map, int x, int y, int size, double *res)
+int GetCell(double *map, int x, int y, int size, double val, double *res)
 {
     if ((x >= 0) && (x < size) && (y >= 0) && (y < size)) {
 	*res = map[x + y * size];
 	if (Rast_is_d_null_value(res)) {
-	    double min, max;
-
-	    MinMax(map, &min, &max, size * size);
-	    *res = min;
+	    *res = val;
 	}
 	return 1;
     }
@@ -26,7 +23,7 @@
     /*      fprintf(stderr, "map[%d,%d] = %f\n", x, y, map[x + y * size]); */
 }
 
-double DownSample(double *map, int x, int y, int newcols, int newrows,
+double DownSample(double *map, double min, int x, int y, int newcols, int newrows,
 		  int oldsize)
 {
     int topleftX = oldsize * x / newcols;
@@ -41,7 +38,7 @@
 
     for (i = topleftX; i < bottomrightX; i++) {
 	for (j = topleftY; j < bottomrightY; j++) {
-	    if (GetCell(map, i, j, oldsize, &cell)) {
+	    if (GetCell(map, i, j, oldsize, min, &cell)) {
 		cnt++;
 		sum += cell;
 	    }
@@ -115,6 +112,8 @@
 
     /* get parameters */
     MinMax(map, &min, &max, size);
+    if (min == max)
+	G_fatal_error("CutValues(): min %g == max %g", min, max);
     span = max - min;
     pixels = Round(size * mapcover);
 
@@ -159,7 +158,7 @@
     }
 }
 
-void FractalStep(double *map, Point v1, Point v2, Point v3, Point v4,
+void FractalStep(double *map, double min, Point v1, Point v2, Point v3, Point v4,
 		 double d, int size)
 {
     Point mid;
@@ -170,13 +169,13 @@
     /* get values */
     int cnt = 0;
 
-    if (GetCell(map, v1.x, v1.y, size, &val1))
+    if (GetCell(map, v1.x, v1.y, size, min, &val1))
 	cnt++;
-    if (GetCell(map, v2.x, v2.y, size, &val2))
+    if (GetCell(map, v2.x, v2.y, size, min, &val2))
 	cnt++;
-    if (GetCell(map, v3.x, v3.y, size, &val3))
+    if (GetCell(map, v3.x, v3.y, size, min, &val3))
 	cnt++;
-    if (GetCell(map, v4.x, v4.y, size, &val4))
+    if (GetCell(map, v4.x, v4.y, size, min, &val4))
 	cnt++;
 
     /* calculate midpoints */
@@ -198,7 +197,10 @@
     int i, x, y, dx;
     double actd = d;
     int xdisp;
+    double min, max;
 
+    MinMax(map, &min, &max, size * size);
+
     /* initialize corners */
     SetCell(map, 0, 0, size, 2 * (Randomf() - 0.5));
     SetCell(map, size - 1, 0, size, 2 * (Randomf() - 0.5));
@@ -206,7 +208,7 @@
     SetCell(map, size - 1, size - 1, size, 2 * (Randomf() - 0.5));
 
     /* calculate starting step width */
-    step = size;
+    step = size - 1;
 
     for (i = 0; i < n; i++) {
 	/* do diamond step */
@@ -220,7 +222,8 @@
 		v3.y = (y + 1) * step;
 		v4.x = (x + 1) * step;
 		v4.y = (y + 1) * step;
-		FractalStep(map, v1, v2, v3, v4, actd, size);
+
+		FractalStep(map, min, v1, v2, v3, v4, actd, size);
 	    }
 	}
 
@@ -229,8 +232,8 @@
 
 	/* do square step */
 	xdisp = 1;
-	for (y = 0; y <= (1 << (i + 1)); y++) {
-	    for (x = 0; x <= (1 << i) - xdisp; x++) {
+	for (y = 1; y <= (1 << (i + 0)); y++) {
+	    for (x = 1; x <= (1 << i) - xdisp - 1; x++) {
 		dx = 2 * x + xdisp;
 		v1.x = dx * step;
 		v1.y = (y - 1) * step;
@@ -240,7 +243,8 @@
 		v3.y = (y + 1) * step;
 		v4.x = (dx - 1) * step;
 		v4.y = y * step;
-		FractalStep(map, v1, v2, v3, v4, actd, size);
+
+		FractalStep(map, min, v1, v2, v3, v4, actd, size);
 	    }
 
 	    /* switch row offset */

Modified: grass-addons/grass7/raster/r.pi/r.pi.nlm/local_proto.h
===================================================================
--- grass-addons/grass7/raster/r.pi/r.pi.nlm/local_proto.h	2017-04-11 12:32:44 UTC (rev 70860)
+++ grass-addons/grass7/raster/r.pi/r.pi.nlm/local_proto.h	2017-04-11 13:05:55 UTC (rev 70861)
@@ -4,12 +4,12 @@
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <math.h>
+#include <time.h>
 #include <grass/gis.h>
 #include <grass/glocale.h>
 #include <grass/stats.h>
 #include "../r.pi.library/r_pi.h"
-#include <math.h>
-#include <time.h>
 
 typedef struct
 {
@@ -42,7 +42,7 @@
 
 /* func.c */
 void FractalIter(double *map, double d, double dmod, int n, int size);
-double DownSample(double *map, int x, int y, int newcols, int newrows,
+double DownSample(double *map, double min, int x, int y, int newcols, int newrows,
 		  int oldsize);
 double CutValues(double *map, double mapcover, int size);
 double UpSample(int *map, int x, int y, int oldcols, int oldrows,

Modified: grass-addons/grass7/raster/r.pi/r.pi.nlm/main.c
===================================================================
--- grass-addons/grass7/raster/r.pi/r.pi.nlm/main.c	2017-04-11 12:32:44 UTC (rev 70860)
+++ grass-addons/grass7/raster/r.pi/r.pi.nlm/main.c	2017-04-11 13:05:55 UTC (rev 70861)
@@ -6,7 +6,7 @@
  *               Markus Metz (update to GRASS 7)
  * PURPOSE:      Generation of Neutral Landscapes (fractal landscapes), similar to r.pi.nlm, but fractal landscapes instead of circular growth
  *
- * COPYRIGHT:    (C) 2009-2011 by the GRASS Development Team
+ * COPYRIGHT:    (C) 2009-2011,2017 by the GRASS Development Team
  *
  *               This program is free software under the GNU General Public
  *               License (>=v2). Read the file COPYING that comes with GRASS
@@ -196,7 +196,7 @@
 
     /* init buffers */
     memset(bigbuf, 0, size * size * sizeof(double));
-    memset(buffer, TYPE_NOTHING, sx * sy * sizeof(int));
+    memset(buffer, 0, sx * sy * sizeof(int));
 
     /* load values from input file */
     if (oldname) {
@@ -255,7 +255,7 @@
     /* resample map to desired size */
     for (i = 0; i < sx; i++) {
 	for (j = 0; j < sy; j++) {
-	    double val = DownSample(bigbuf, i, j, sx, sy, size);
+	    double val = DownSample(bigbuf, min, i, j, sx, sy, size);
 	    double old = buffer[i + j * sx];
 
 	    if (val >= edge && old == 0) {

Modified: grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/fractal.c
===================================================================
--- grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/fractal.c	2017-04-11 12:32:44 UTC (rev 70860)
+++ grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/fractal.c	2017-04-11 13:05:55 UTC (rev 70861)
@@ -22,10 +22,12 @@
     /* find edge */
     edge = CutValues(fractbuf, landcover, size * size);
 
+    MinMax(fractbuf, &min, &max, size * size);
+
     /* resample map to desired size */
     for (i = 0; i < sx; i++) {
 	for (j = 0; j < sy; j++) {
-	    double val = DownSample(fractbuf, i, j, sx, sy, size);
+	    double val = DownSample(fractbuf, min, i, j, sx, sy, size);
 	    double old = buffer[i + j * sx];
 
 	    if (val >= edge && old == 0) {

Modified: grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/func.c
===================================================================
--- grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/func.c	2017-04-11 12:32:44 UTC (rev 70860)
+++ grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/func.c	2017-04-11 13:05:55 UTC (rev 70861)
@@ -1,14 +1,11 @@
 #include "local_proto.h"
 
-int GetCell(double *map, int x, int y, int size, double *res)
+int GetCell(double *map, int x, int y, int size, double val, double *res)
 {
     if ((x >= 0) && (x < size) && (y >= 0) && (y < size)) {
 	*res = map[x + y * size];
 	if (Rast_is_d_null_value(res)) {
-	    double min, max;
-
-	    MinMax(map, &min, &max, size * size);
-	    *res = min;
+	    *res = val;
 	}
 	return 1;
     }
@@ -26,7 +23,7 @@
     /*      fprintf(stderr, "map[%d,%d] = %f\n", x, y, map[x + y * size]); */
 }
 
-double DownSample(double *map, int x, int y, int newcols, int newrows,
+double DownSample(double *map, double min, int x, int y, int newcols, int newrows,
 		  int oldsize)
 {
     int topleftX = oldsize * x / newcols;
@@ -41,7 +38,7 @@
 
     for (i = topleftX; i < bottomrightX; i++) {
 	for (j = topleftY; j < bottomrightY; j++) {
-	    if (GetCell(map, i, j, oldsize, &cell)) {
+	    if (GetCell(map, i, j, oldsize, min, &cell)) {
 		cnt++;
 		sum += cell;
 	    }
@@ -115,6 +112,8 @@
 
     /* get parameters */
     MinMax(map, &min, &max, size);
+    if (min == max)
+	G_fatal_error("CutValues(): min %g == max %g", min, max);
     span = max - min;
     pixels = Round(size * mapcover);
 
@@ -159,7 +158,7 @@
     }
 }
 
-void FractalStep(double *map, Point v1, Point v2, Point v3, Point v4,
+void FractalStep(double *map, double min, Point v1, Point v2, Point v3, Point v4,
 		 double d, int size)
 {
     Point mid;
@@ -170,13 +169,13 @@
     /* get values */
     int cnt = 0;
 
-    if (GetCell(map, v1.x, v1.y, size, &val1))
+    if (GetCell(map, v1.x, v1.y, size, min, &val1))
 	cnt++;
-    if (GetCell(map, v2.x, v2.y, size, &val2))
+    if (GetCell(map, v2.x, v2.y, size, min, &val2))
 	cnt++;
-    if (GetCell(map, v3.x, v3.y, size, &val3))
+    if (GetCell(map, v3.x, v3.y, size, min, &val3))
 	cnt++;
-    if (GetCell(map, v4.x, v4.y, size, &val4))
+    if (GetCell(map, v4.x, v4.y, size, min, &val4))
 	cnt++;
 
     /* calculate midpoints */
@@ -198,7 +197,10 @@
     int i, x, y, dx;
     double actd = d;
     int xdisp;
+    double min, max;
 
+    MinMax(map, &min, &max, size * size);
+
     /* initialize corners */
     SetCell(map, 0, 0, size, 2 * (Randomf() - 0.5));
     SetCell(map, size - 1, 0, size, 2 * (Randomf() - 0.5));
@@ -206,7 +208,7 @@
     SetCell(map, size - 1, size - 1, size, 2 * (Randomf() - 0.5));
 
     /* calculate starting step width */
-    step = size;
+    step = size - 1;
 
     for (i = 0; i < n; i++) {
 	/* do diamond step */
@@ -220,7 +222,8 @@
 		v3.y = (y + 1) * step;
 		v4.x = (x + 1) * step;
 		v4.y = (y + 1) * step;
-		FractalStep(map, v1, v2, v3, v4, actd, size);
+
+		FractalStep(map, min, v1, v2, v3, v4, actd, size);
 	    }
 	}
 
@@ -229,8 +232,8 @@
 
 	/* do square step */
 	xdisp = 1;
-	for (y = 0; y <= (1 << (i + 1)); y++) {
-	    for (x = 0; x <= (1 << i) - xdisp; x++) {
+	for (y = 1; y <= (1 << (i + 0)); y++) {
+	    for (x = 1; x <= (1 << i) - xdisp - 1; x++) {
 		dx = 2 * x + xdisp;
 		v1.x = dx * step;
 		v1.y = (y - 1) * step;
@@ -240,7 +243,8 @@
 		v3.y = (y + 1) * step;
 		v4.x = (dx - 1) * step;
 		v4.y = y * step;
-		FractalStep(map, v1, v2, v3, v4, actd, size);
+
+		FractalStep(map, min, v1, v2, v3, v4, actd, size);
 	    }
 
 	    /* switch row offset */

Modified: grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/local_proto.h
===================================================================
--- grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/local_proto.h	2017-04-11 12:32:44 UTC (rev 70860)
+++ grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/local_proto.h	2017-04-11 13:05:55 UTC (rev 70861)
@@ -30,7 +30,7 @@
 
 /* func.c */
 void FractalIter(double *map, double d, double dmod, int n, int size);
-double DownSample(double *map, int x, int y, int newcols, int newrows,
+double DownSample(double *map, double min, int x, int y, int newcols, int newrows,
 		  int oldsize);
 double CutValues(double *map, double mapcover, int size);
 double UpSample(int *map, int x, int y, int oldcols, int oldrows,

Modified: grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/main.c
===================================================================
--- grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/main.c	2017-04-11 12:32:44 UTC (rev 70860)
+++ grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/main.c	2017-04-11 13:05:55 UTC (rev 70861)
@@ -7,7 +7,7 @@
  * PURPOSE:      Generation of Neutral Landscapes and statistical analysis 
  *                               of fragmentation indices
  *
- * COPYRIGHT:    (C) 2009-2011 by the GRASS Development Team
+ * COPYRIGHT:    (C) 2009-2011,2017 by the GRASS Development Team
  *
  *               This program is free software under the GNU General Public
  *               License (>=v2). Read the file COPYING that comes with GRASS
@@ -327,7 +327,6 @@
     /* init fragments structure */
     fragments[0] = cells;
 
-
     /* init buffers */
     memset(bigbuf, 0, size * size * sizeof(double));
     memset(buffer, 0, sx * sy * sizeof(int));



More information about the grass-commit mailing list