ソースを参照

raster3dlib: Using Macros to reduce function nesting in coordinate to index conversion.
Updated the testsuite.


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@61287 15284696-431f-4ddb-bdfa-cd5b030d7da7

Soeren Gebbert 10 年 前
コミット
2d99b2ca78

+ 43 - 0
lib/raster3d/raster3d_intern.h

@@ -1,3 +1,6 @@
+#ifndef RASTER3D_INTERN_H
+#define RASTER3D_INTERN_H
+
 #include <grass/raster3d.h>
 #include <grass/raster3d.h>
 #include <grass/gis.h>
 #include <grass/gis.h>
 
 
@@ -77,3 +80,43 @@ extern void Rast3d_fatal_error_noargs(const char * /* msg */ );
 #define RASTER3D_REGION_EWRES "e-w resol"
 #define RASTER3D_REGION_EWRES "e-w resol"
 #define RASTER3D_REGION_NSRES "n-s resol"
 #define RASTER3D_REGION_NSRES "n-s resol"
 #define RASTER3D_REGION_TBRES "t-b resol"
 #define RASTER3D_REGION_TBRES "t-b resol"
+
+/* Coordinates to index conversion will return double.
+ * Use floor() and integer casting to receive col,row and depth
+ *
+ * double cold = EASTERN_TO_COL(east, region)
+ * int col = (int)floor(cold)
+ *
+ */
+#define EASTERN_TO_COL(east, region) (east - region->west) / (region->ew_res);
+#define NORTHERN_TO_ROW(north, region) (region->north - north) / (region->ns_res);
+#define TOP_TO_DEPTH(top, region) (top - region->bottom) / (region->tb_res);
+/* Location coordinates to index coordinates
+ * region is a pointer to the RASTER3D_Region structure
+ * north, east and top are double values
+ * x, y, and z are pointer to double values
+ */
+#define LOCATION_TO_COORD(region, north, east, top, x, y, z) \
+    { \
+        *x = EASTERN_TO_COL(east, region) \
+        *y = NORTHERN_TO_ROW(north, region) \
+        *z = TOP_TO_DEPTH(top, region) \
+    }
+
+/* Row to north, col to east and depth to top macros
+ * region is a pointer to the RASTER3D_Region structure
+ * north, east and top are pointer to double values,
+ * x, y and z are double values
+ */
+#define COL_TO_EASTERN(region, x)  region->west + x * region->ew_res;
+#define ROW_TO_NORTHERN(region, y) region->north - y * region->ns_res;
+#define DEPTH_TO_TOP(region, z)    region->bottom + z * region->tb_res;
+#define COORD_TO_LOCATION(region, x, y, z, north, east, top) \
+    { \
+        *east  = COL_TO_EASTERN(region, x) \
+        *north = ROW_TO_NORTHERN(region, y) \
+        *top   = DEPTH_TO_TOP(region, z) \
+    }
+
+
+#endif

+ 19 - 23
lib/raster3d/region.c

@@ -1,11 +1,10 @@
 #include <stdio.h>
 #include <stdio.h>
+#include <math.h>
 
 
-#include <grass/gis.h>
 #include <grass/raster.h>
 #include <grass/raster.h>
-#include <grass/raster3d.h>
-
 #include "raster3d_intern.h"
 #include "raster3d_intern.h"
 
 
+
 /*---------------------------------------------------------------------------*/
 /*---------------------------------------------------------------------------*/
 
 
 
 
@@ -254,7 +253,7 @@ Rast3d_read_region_map(const char *name, const char *mapset, RASTER3D_Region * r
  *  Returns 1 if region-coordinates <em>(north, east, top)</em> are
  *  Returns 1 if region-coordinates <em>(north, east, top)</em> are
  * inside the region of <em>map</em>. Returns 0 otherwise.
  * inside the region of <em>map</em>. Returns 0 otherwise.
  *
  *
- *  \param REgion
+ *  \param region
  *  \param north
  *  \param north
  *  \param east
  *  \param east
  *  \param top
  *  \param top
@@ -277,7 +276,7 @@ int Rast3d_is_valid_location(RASTER3D_Region *region, double north, double east,
  *  Converts region-coordinates <em>(north, east,
  *  Converts region-coordinates <em>(north, east,
  *  top)</em> into cell-coordinates <em>(x, y, z)</em>.
  *  top)</em> into cell-coordinates <em>(x, y, z)</em>.
  *
  *
- *  \param map
+ *  \param region
  *  \param north
  *  \param north
  *  \param east
  *  \param east
  *  \param top
  *  \param top
@@ -292,12 +291,11 @@ Rast3d_location2coord(RASTER3D_Region *region, double north, double east, double
            int *x, int *y, int *z)
            int *x, int *y, int *z)
 {
 {
     double col, row, depth;
     double col, row, depth;
+    LOCATION_TO_COORD(region, north, east, top, &col, &row, &depth);
 
 
-    Rast3d_location2coord_double(region, north, east, top, &col, &row, &depth);
-
-    *x = (int)col;
-    *y = (int)row;
-    *z = (int)depth;
+    *x = (int)floor(col);
+    *y = (int)floor(row);
+    *z = (int)floor(depth);
 }
 }
 
 
 /*!
 /*!
@@ -309,7 +307,7 @@ Rast3d_location2coord(RASTER3D_Region *region, double north, double east, double
  * <b>Note:</b> The results are <i>double</i> numbers. Casting them to
  * <b>Note:</b> The results are <i>double</i> numbers. Casting them to
  * <i>int</i> will give the column, row and depth number.
  * <i>int</i> will give the column, row and depth number.
  *
  *
- *  \param map
+ *  \param region
  *  \param north
  *  \param north
  *  \param east
  *  \param east
  *  \param top
  *  \param top
@@ -323,11 +321,7 @@ void
 Rast3d_location2coord_double(RASTER3D_Region *region, double north, double east, double top,
 Rast3d_location2coord_double(RASTER3D_Region *region, double north, double east, double top,
            double *x, double *y, double *z)
            double *x, double *y, double *z)
 {
 {
-    double row;
-
-    *x = (east - region->west) / (region->ew_res);
-    *y = (region->north - north) / (region->ns_res);
-    *z = (top - region->bottom) / (region->tb_res);
+    LOCATION_TO_COORD(region, north, east, top, x, y, z);
 
 
     G_debug(4, "Rast3d_location2coord_double x %f y %f z %f\n", *x, *y, *z);
     G_debug(4, "Rast3d_location2coord_double x %f y %f z %f\n", *x, *y, *z);
 }
 }
@@ -339,7 +333,7 @@ Rast3d_location2coord_double(RASTER3D_Region *region, double north, double east,
  *  top)</em> into cell-coordinates <em>(x, y, z)</em>.
  *  top)</em> into cell-coordinates <em>(x, y, z)</em>.
  *  This function calls Rast3d_fatal_error in case location is not in window.
  *  This function calls Rast3d_fatal_error in case location is not in window.
  *
  *
- *  \param map
+ *  \param region
  *  \param north
  *  \param north
  *  \param east
  *  \param east
  *  \param top
  *  \param top
@@ -356,7 +350,12 @@ Rast3d_location2coord2(RASTER3D_Region *region, double north, double east, doubl
     if (!Rast3d_is_valid_location(region, north, east, top))
     if (!Rast3d_is_valid_location(region, north, east, top))
     Rast3d_fatal_error("Rast3d_location2coord2: location not in region");
     Rast3d_fatal_error("Rast3d_location2coord2: location not in region");
 
 
-    Rast3d_location2coord(region, north, east, top, x, y, z);
+    double col, row, depth;
+    LOCATION_TO_COORD(region, north, east, top, &col, &row, &depth);
+
+    *x = (int)floor(col);
+    *y = (int)floor(row);
+    *z = (int)floor(depth);
 }
 }
 
 
 /*!
 /*!
@@ -379,8 +378,7 @@ Rast3d_location2coord2(RASTER3D_Region *region, double north, double east, doubl
  *  - z+1.0 will return the top for the upper edge of the column.
  *  - z+1.0 will return the top for the upper edge of the column.
  *
  *
 *
 *
- *
- *  \param map
+ *  \param region
  *  \param x
  *  \param x
  *  \param y
  *  \param y
  *  \param z
  *  \param z
@@ -393,9 +391,7 @@ Rast3d_location2coord2(RASTER3D_Region *region, double north, double east, doubl
 void
 void
 Rast3d_coord2location(RASTER3D_Region * region, double x, double y, double z, double *north, double *east, double *top)
 Rast3d_coord2location(RASTER3D_Region * region, double x, double y, double z, double *north, double *east, double *top)
 {
 {
-    *north = region->north - y * region->ns_res;
-    *east = region->west + x * region->ew_res;
-    *top = region->bottom + z * region->tb_res;
+    COORD_TO_LOCATION(region, x, y, z, north, east, top);
 
 
     G_debug(4, "Rast3d_coord2location north %g east %g top %g\n", *north, *east, *top);
     G_debug(4, "Rast3d_coord2location north %g east %g top %g\n", *north, *east, *top);
 }
 }

+ 1 - 8
lib/raster3d/test/test_main.c

@@ -42,14 +42,6 @@ void set_params(void) {
     param.unit->options = "coord,putget,large";
     param.unit->options = "coord,putget,large";
     param.unit->description = "Choose the unit tests to run";
     param.unit->description = "Choose the unit tests to run";
 
 
-/* No integration test for now
-    param.integration = G_define_option();
-    param.integration->key = "integration";
-    param.integration->type = TYPE_STRING;
-    param.integration->required = NO;
-    param.integration->options = "";
-    param.integration->description = "Choose the integration tests to run";
-*/
     param.depths = G_define_option();
     param.depths = G_define_option();
     param.depths->key = "depths";
     param.depths->key = "depths";
     param.depths->type = TYPE_INTEGER;
     param.depths->type = TYPE_INTEGER;
@@ -103,6 +95,7 @@ int main(int argc, char *argv[]) {
     module = G_define_module();
     module = G_define_module();
     module->description
     module->description
             = "Performs unit and integration tests for the raster3d library";
             = "Performs unit and integration tests for the raster3d library";
+    G_add_keyword(_("raster3d"));
     G_add_keyword(_("test"));
     G_add_keyword(_("test"));
 
 
 
 

+ 20 - 9
lib/raster3d/test/test_put_get_value.c

@@ -37,8 +37,8 @@ int unit_test_put_get_value()
 
 
     G_message("\n++ Running raster3d put/get value unit tests ++");
     G_message("\n++ Running raster3d put/get value unit tests ++");
 
 
-    //sum += test_put_get_value_dcell();
-    //sum += test_put_get_value_fcell();
+    sum += test_put_get_value_dcell();
+    sum += test_put_get_value_fcell();
     sum += test_put_get_value_resampling();
     sum += test_put_get_value_resampling();
 
 
 
 
@@ -107,6 +107,15 @@ int test_put_get_value_dcell(void)
     for(z = 0; z < region.depths; z++) {
     for(z = 0; z < region.depths; z++) {
         for(y = 0; y < region.rows; y++) { /* From the north to the south */
         for(y = 0; y < region.rows; y++) { /* From the north to the south */
             for(x = 0; x < region.cols; x++) {
             for(x = 0; x < region.cols; x++) {
+                value = -1;
+                Rast3d_put_value(map, x, y, z, &value, DCELL_TYPE);
+            }
+        }
+    }
+
+    for(z = 0; z < region.depths; z++) {
+        for(y = 0; y < region.rows; y++) { /* From the north to the south */
+            for(x = 0; x < region.cols; x++) {
                 /* Add cols, rows and depths and put this in the map */
                 /* Add cols, rows and depths and put this in the map */
                 value = x + y + z;
                 value = x + y + z;
                 Rast3d_put_value(map, x, y, z, &value, DCELL_TYPE);
                 Rast3d_put_value(map, x, y, z, &value, DCELL_TYPE);
@@ -114,19 +123,14 @@ int test_put_get_value_dcell(void)
         }
         }
     }
     }
 
 
-    /* Write everything to the disk */
-    Rast3d_flush_all_tiles(map);
-    Rast3d_close(map);
 
 
-    map = Rast3d_open_cell_old("test_put_get_value_dcell", G_mapset(), &region, DCELL_TYPE, RASTER3D_USE_CACHE_XY);
-
-    /* Reread the map and compare the expected results */
+    /* Reread the new open map and compare the expected results */
 
 
     G_message("Get the value of the upper left corner -> 0");
     G_message("Get the value of the upper left corner -> 0");
 
 
 
 
     col = row = depth = 0;
     col = row = depth = 0;
-    north = region.north - 0.1; /* north would be out of bounds therefor -0.1 */
+    north = region.north - region.ns_res * row;
     east = region.west + region.ew_res * col;
     east = region.west + region.ew_res * col;
     top = region.bottom + region.tb_res * depth;
     top = region.bottom + region.tb_res * depth;
 
 
@@ -153,6 +157,13 @@ int test_put_get_value_dcell(void)
 
 
     sum += test_resampling_dcell(map, north, east, top, col, row, depth, 1);
     sum += test_resampling_dcell(map, north, east, top, col, row, depth, 1);
 
 
+
+    /* Write everything to the disk and reopen the map */
+    Rast3d_flush_all_tiles(map);
+    Rast3d_close(map);
+    map = Rast3d_open_cell_old("test_put_get_value_dcell", G_mapset(), &region, DCELL_TYPE, RASTER3D_USE_CACHE_XY);
+
+
     G_message("Get the value of x == 9 y == 14 z == 4 -> x + y + z = 27");
     G_message("Get the value of x == 9 y == 14 z == 4 -> x + y + z = 27");
 
 
     col = 9;
     col = 9;

+ 20 - 10
lib/raster3d/test/test_put_get_value_large_file.c

@@ -36,12 +36,12 @@ int unit_test_put_get_value_large_file(int depths, int rows, int cols, int tile_
 
 
     G_message("\n++ Running raster3d put/get value large file unit tests ++");
     G_message("\n++ Running raster3d put/get value large file unit tests ++");
 
 
+    sum += test_large_file(depths, rows, cols, tile_size);
     sum += test_large_file_random(depths, rows, cols, tile_size);
     sum += test_large_file_random(depths, rows, cols, tile_size);
     sum += test_large_file_sparse_random(depths, rows, cols, tile_size);
     sum += test_large_file_sparse_random(depths, rows, cols, tile_size);
     sum += test_large_file(depths, rows, cols, tile_size);
     sum += test_large_file(depths, rows, cols, tile_size);
-    sum += test_large_file(depths, rows, cols, tile_size);
-    sum += test_large_file(depths, rows, cols, tile_size);
     sum += test_large_file_zeros(depths, rows, cols, tile_size);
     sum += test_large_file_zeros(depths, rows, cols, tile_size);
+    sum += test_large_file(depths, rows, cols, tile_size);
 
 
 
 
     if (sum > 0)
     if (sum > 0)
@@ -57,7 +57,7 @@ int unit_test_put_get_value_large_file(int depths, int rows, int cols, int tile_
 int test_large_file(int depths, int rows, int cols, int tile_size)
 int test_large_file(int depths, int rows, int cols, int tile_size)
 {
 {
     int sum = 0; 
     int sum = 0; 
-    int x, y, z;
+    int x, y, z, count;
     DCELL value;
     DCELL value;
     
     
     G_message("Testing DCELL put function for large files");
     G_message("Testing DCELL put function for large files");
@@ -89,12 +89,28 @@ int test_large_file(int depths, int rows, int cols, int tile_size)
     /* The window is the same as the map region ... of course */
     /* The window is the same as the map region ... of course */
     Rast3d_set_window_map(map, &region);
     Rast3d_set_window_map(map, &region);
     
     
-    int count = 1;
+    /*Write -1 first to see if the tile handling works correctly */
     for(z = 0; z < region.depths; z++) {
     for(z = 0; z < region.depths; z++) {
 	G_percent(z, region.depths, 1);
 	G_percent(z, region.depths, 1);
         for(y = 0; y < region.rows; y++) {
         for(y = 0; y < region.rows; y++) {
             for(x = 0; x < region.cols; x++) {
             for(x = 0; x < region.cols; x++) {
               /* Put the counter as cell value */
               /* Put the counter as cell value */
+                value = -1;
+                Rast3d_put_value(map, x, y, z, &value, DCELL_TYPE);
+            }
+        }
+    }
+
+    G_percent(1, 1, 1);
+    G_message("Rewriting the values");
+
+    /* Now write the values to be evaluated */
+    count = 1;
+    for(z = 0; z < region.depths; z++) {
+        G_percent(z, region.depths, 1);
+        for(y = 0; y < region.rows; y++) {
+            for(x = 0; x < region.cols; x++) {
+              /* Put the counter as cell value */
                 value = count;
                 value = count;
                 Rast3d_put_value(map, x, y, z, &value, DCELL_TYPE);
                 Rast3d_put_value(map, x, y, z, &value, DCELL_TYPE);
                 count++;
                 count++;
@@ -103,15 +119,9 @@ int test_large_file(int depths, int rows, int cols, int tile_size)
     }
     }
 
 
     G_percent(1, 1, 1);
     G_percent(1, 1, 1);
-    /* Write everything to the disk */
-    Rast3d_flush_all_tiles(map);
-    Rast3d_close(map);
          
          
     G_message("Verifying 3D raster map");
     G_message("Verifying 3D raster map");
 
 
-    map = Rast3d_open_cell_old("test_put_get_value_dcell_large",
-    		G_mapset(), &region, DCELL_TYPE, RASTER3D_USE_CACHE_XYZ);
-    
     count = 1;
     count = 1;
     for(z = 0; z < region.depths; z++) {
     for(z = 0; z < region.depths; z++) {
 	G_percent(z, region.depths, 1);
 	G_percent(z, region.depths, 1);

+ 1 - 0
lib/raster3d/testsuite/raster3d_lib_test.py

@@ -26,6 +26,7 @@ class Raster3dLibraryTest(TestCase):
         self.assertModule("test.raster3d.lib",  unit="large",  depths=91,  rows=89,  cols=87,  tile_size=1024)
         self.assertModule("test.raster3d.lib",  unit="large",  depths=91,  rows=89,  cols=87,  tile_size=1024)
         self.assertModule("test.raster3d.lib",  unit="large",  depths=91,  rows=89,  cols=87,  tile_size=32768)
         self.assertModule("test.raster3d.lib",  unit="large",  depths=91,  rows=89,  cols=87,  tile_size=32768)
 
 
+        # Enable zlib compression
         self.assertModule("test.raster3d.lib",  flags="l",  unit="large",  depths=91,  rows=89,  cols=87)
         self.assertModule("test.raster3d.lib",  flags="l",  unit="large",  depths=91,  rows=89,  cols=87)
         self.assertModule("test.raster3d.lib",  flags="l",  unit="large",  depths=91,  rows=89,  cols=87,  tile_size=8)
         self.assertModule("test.raster3d.lib",  flags="l",  unit="large",  depths=91,  rows=89,  cols=87,  tile_size=8)
         self.assertModule("test.raster3d.lib",  flags="l",  unit="large",  depths=91,  rows=89,  cols=87,  tile_size=512)
         self.assertModule("test.raster3d.lib",  flags="l",  unit="large",  depths=91,  rows=89,  cols=87,  tile_size=512)