[GRASS-SVN] r49789 - in grass/trunk/lib/raster3d: . test

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Dec 16 14:32:29 EST 2011


Author: huhabla
Date: 2011-12-16 11:32:29 -0800 (Fri, 16 Dec 2011)
New Revision: 49789

Modified:
   grass/trunk/lib/raster3d/error.c
   grass/trunk/lib/raster3d/getvalue.c
   grass/trunk/lib/raster3d/test/Makefile
   grass/trunk/lib/raster3d/test/test_put_get_value.c
Log:
Added boundary check to get_value_region and a unit test.


Modified: grass/trunk/lib/raster3d/error.c
===================================================================
--- grass/trunk/lib/raster3d/error.c	2011-12-16 15:05:32 UTC (rev 49788)
+++ grass/trunk/lib/raster3d/error.c	2011-12-16 19:32:29 UTC (rev 49789)
@@ -40,7 +40,7 @@
 void Rast3d_print_error(const char *msg)
 {
     fprintf(stderr, "ERROR: ");
-    fprintf(stderr, msg);
+    fprintf(stderr, "%s", msg);
     fprintf(stderr, "\n");
 }
 

Modified: grass/trunk/lib/raster3d/getvalue.c
===================================================================
--- grass/trunk/lib/raster3d/getvalue.c	2011-12-16 15:05:32 UTC (rev 49788)
+++ grass/trunk/lib/raster3d/getvalue.c	2011-12-16 19:32:29 UTC (rev 49789)
@@ -166,22 +166,31 @@
 {
     int tileIndex, offs;
     float *tile;
+    float value;
 
     if (map->typeIntern == DCELL_TYPE)
 	return (float)Rast3d_get_double_region(map, x, y, z);
 
+    /* In case of region coordinates out of bounds, return the Null value */
+    if(x < 0 || y < 0 || z < 0 || x >= map->region.cols ||
+       y >= map->region.rows || z >= map->region.depths) {
+         Rast3d_set_null_value(&value, 1, FCELL_TYPE);
+         return value;
+    }   
+
     Rast3d_coord2tile_index(map, x, y, z, &tileIndex, &offs);
     tile = (float *)Rast3d_get_tile_ptr(map, tileIndex);
 
     if (tile == NULL)
-	Rast3d_fatal_error("Rast3d_get_float_region: error in Rast3d_get_tile_ptr");
+	Rast3d_fatal_error("Rast3d_get_float_region: error in Rast3d_get_tile_ptr." 
+                           "Region coordinates x %i y %i z %i  tile index %i offset %i",
+                           x, y, z, tileIndex, offs);
 
     return tile[offs];
 }
 
 /*---------------------------------------------------------------------------*/
 
-
 /*!
  * \brief 
  *
@@ -199,22 +208,31 @@
 {
     int tileIndex, offs;
     double *tile;
+    double value;
 
     if (map->typeIntern == FCELL_TYPE)
 	return (double)Rast3d_get_float_region(map, x, y, z);
 
+    /* In case of region coordinates out of bounds, return the Null value */
+    if(x < 0 || y < 0 || z < 0 || x >= map->region.cols ||
+       y >= map->region.rows || z >= map->region.depths) {
+         Rast3d_set_null_value(&value, 1, DCELL_TYPE);
+         return value;
+    } 
+
     Rast3d_coord2tile_index(map, x, y, z, &tileIndex, &offs);
     tile = (double *)Rast3d_get_tile_ptr(map, tileIndex);
 
     if (tile == NULL)
-	Rast3d_fatal_error("Rast3d_get_double_region: error in Rast3d_get_tile_ptr");
+	Rast3d_fatal_error("Rast3d_get_double_region: error in Rast3d_get_tile_ptr." 
+                           "Region coordinates x %i y %i z %i  tile index %i offset %i",
+                           x, y, z, tileIndex, offs);
 
     return tile[offs];
 }
 
 /*---------------------------------------------------------------------------*/
 
-
 /*!
  * \brief 
  *
@@ -222,6 +240,7 @@
  * region-coordinate <em>(x, y, z)</em>.  The value returned is of <em>type</em>.
  * Here <em>region</em> means the coordinate in the cube of data in the file, i.e.
  * ignoring geographic coordinates.
+ * In case the region coordinates are out of bounds, the Null value will be returned.
  * This function invokes a fatal error if an error occurs.
  *
  *  \param map

Modified: grass/trunk/lib/raster3d/test/Makefile
===================================================================
--- grass/trunk/lib/raster3d/test/Makefile	2011-12-16 15:05:32 UTC (rev 49788)
+++ grass/trunk/lib/raster3d/test/Makefile	2011-12-16 19:32:29 UTC (rev 49789)
@@ -2,8 +2,8 @@
 
 PGM=test.g3d.lib
 
-LIBES = $(GISLIB) $(RASTER3DLIB) 
-DEPENDENCIES = $(GISDEP) $(RASTER3DDEP)
+LIBES = $(GISLIB) $(RASTER3DLIB) $(RASTERLIB) 
+DEPENDENCIES = $(GISDEP) $(RASTER3DDEP) $(RASTERDEP) 
 include $(MODULE_TOPDIR)/include/Make/Module.make
 
 default: cmd

Modified: grass/trunk/lib/raster3d/test/test_put_get_value.c
===================================================================
--- grass/trunk/lib/raster3d/test/test_put_get_value.c	2011-12-16 15:05:32 UTC (rev 49788)
+++ grass/trunk/lib/raster3d/test/test_put_get_value.c	2011-12-16 19:32:29 UTC (rev 49789)
@@ -435,6 +435,8 @@
     
     sum += test_resampling_dcell(map, north, east, top, col, row, depth, 2);
     
+    sum += test_get_value_region(map, region.cols, region.rows, region.depths);
+    
     Rast3d_close(map);
     
     G_remove("grid3", "test_put_get_value_dcell");
@@ -498,18 +500,55 @@
         G_message("Error in Rast3d_get_region_value");
         sum++;
     }
-    if(value != col + row + depth) {
+    if(value_win != col + row + depth) {
         G_message("Error in Rast3d_get_window_value");
         sum++;
     }
-    if(value != col + row + depth) {
+    if(value_ref != col + row + depth) {
         G_message("Error in Rast3d_get_value");
         sum++;
     }
-    if(value != col + row + depth) {
+    if(value_reg != col + row + depth) {
         G_message("Error in Rast3d_get_value_region");
         sum++;
     }
     
     return sum;
+}
+
+/* *************************************************************** */
+
+int test_get_value_region(RASTER3D_Map *map, int cols, int rows, int depths)
+{
+    int sum = 0;
+    FCELL fvalue1 = 0.0;
+    FCELL fvalue2 = 0.0;
+    DCELL dvalue1 = 0.0;
+    DCELL dvalue2 = 0.0;
+    
+    /* Test for correct Null value */
+    Rast3d_get_value_region(map, -1, -1, -1, &fvalue1, FCELL_TYPE);
+    Rast3d_get_value_region(map, cols, rows, depths, &fvalue2, FCELL_TYPE);
+    Rast3d_get_value_region(map, -1, -1, -1, &dvalue1, DCELL_TYPE);
+    Rast3d_get_value_region(map, cols, rows, depths, &dvalue2, DCELL_TYPE);
+    printf("Value %g == %g == %g == %g\n", fvalue1, fvalue2, dvalue1, dvalue2);
+    
+    if(!Rast_is_f_null_value(&fvalue1)) {
+        G_message("Error in Rast3d_get_value_region");
+        sum++;
+    }
+    if(!Rast_is_f_null_value(&fvalue2)) {
+        G_message("Error in Rast3d_get_value_region");
+        sum++;
+    }
+    if(!Rast_is_d_null_value(&dvalue1)) {
+        G_message("Error in Rast3d_get_value_region");
+        sum++;
+    }
+    if(!Rast_is_d_null_value(&dvalue2)) {
+        G_message("Error in Rast3d_get_value_region");
+        sum++;
+    }
+    
+    return sum;
 }
\ No newline at end of file



More information about the grass-commit mailing list