[GRASS-SVN] r46716 - in grass/trunk: include lib/g3d lib/g3d/test raster/r.to.rast3 raster/r.to.rast3elev raster3d/r3.cross.rast raster3d/r3.in.ascii raster3d/r3.out.ascii raster3d/r3.out.vtk raster3d/r3.to.rast vector/v.random vector/v.to.rast3 vector/v.vol.rst

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jun 15 10:13:35 EDT 2011


Author: huhabla
Date: 2011-06-15 07:13:34 -0700 (Wed, 15 Jun 2011)
New Revision: 46716

Added:
   grass/trunk/lib/g3d/g3dgetvalue.c
   grass/trunk/lib/g3d/g3dputvalue.c
   grass/trunk/lib/g3d/test/
   grass/trunk/lib/g3d/test/Makefile
   grass/trunk/lib/g3d/test/test.g3d.lib.html
   grass/trunk/lib/g3d/test/test_coordinate_transform.c
   grass/trunk/lib/g3d/test/test_g3d_lib.h
   grass/trunk/lib/g3d/test/test_main.c
   grass/trunk/lib/g3d/test/test_put_get_value.c
   grass/trunk/lib/g3d/test/test_tools.c
Modified:
   grass/trunk/include/G3d.h
   grass/trunk/lib/g3d/g3dregion.c
   grass/trunk/lib/g3d/g3dresample.c
   grass/trunk/lib/g3d/g3dvolume.c
   grass/trunk/lib/g3d/g3dwindow.c
   grass/trunk/lib/g3d/tileio.c
   grass/trunk/lib/g3d/tilemath.c
   grass/trunk/lib/g3d/tilewrite.c
   grass/trunk/lib/g3d/writeascii.c
   grass/trunk/raster/r.to.rast3/main.c
   grass/trunk/raster/r.to.rast3elev/main.c
   grass/trunk/raster3d/r3.cross.rast/main.c
   grass/trunk/raster3d/r3.in.ascii/r3.in.ascii.html
   grass/trunk/raster3d/r3.out.ascii/main.c
   grass/trunk/raster3d/r3.out.ascii/r3.out.ascii.html
   grass/trunk/raster3d/r3.out.vtk/writeVTKData.c
   grass/trunk/raster3d/r3.to.rast/main.c
   grass/trunk/vector/v.random/main.c
   grass/trunk/vector/v.to.rast3/main.c
   grass/trunk/vector/v.to.rast3/v.to.rast3.html
   grass/trunk/vector/v.vol.rst/dataoct.h
   grass/trunk/vector/v.vol.rst/main.c
   grass/trunk/vector/v.vol.rst/user.h
   grass/trunk/vector/v.vol.rst/user1.c
Log:
Major G3d libarary update. Cleaned and reordered the put and get value functions in G3D library. Added several tests to verify the right hand side cube coordinate system. Modified most of the g3d modules to use the correct row order when interacting with raster maps. Modified v.random to create db column in -z mode.

Modified: grass/trunk/include/G3d.h
===================================================================
--- grass/trunk/include/G3d.h	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/include/G3d.h	2011-06-15 14:13:34 UTC (rev 46716)
@@ -448,6 +448,9 @@
 void G3d_extract2dRegion(G3D_Region *, struct Cell_head *);
 void G3d_regionToCellHead(G3D_Region *, struct Cell_head *);
 int G3d_readRegionMap(const char *, const char *, G3D_Region *);
+int G3d_isValidLocation(G3D_Map *, double, double, double);
+void G3d_location2coord(G3D_Map *, double, double, double, int *, int *, int *);
+void G3d_location2coord2(G3D_Map *, double, double, double, int *, int *, int *);
 
 /* grass/src/libes/g3d/g3dresample.c */
 void G3d_nearestNeighbor(G3D_Map *, int, int, int, void *, int);
@@ -468,10 +471,15 @@
 void G3d_getValue(G3D_Map *, int, int, int, void *, int);
 float G3d_getFloat(G3D_Map *, int, int, int);
 double G3d_getDouble(G3D_Map *, int, int, int);
+void G3d_getWindowValue(G3D_Map *, double, double, double, void *, int);
+
+
 G3D_Region *G3d_windowPtr(void);
 void G3d_setWindow(G3D_Region *);
 void G3d_setWindowMap(G3D_Map *, G3D_Region *);
 void G3d_getWindow(G3D_Region *);
+void G3d_location2WindowCoord(G3D_Map *, double, double, double, int *, int *, int *);
+int G3d_isValidLocationWindow(G3D_Map *, double, double, double);
 
 /* grass/src/libes/g3d/g3dwindowio.c */
 void G3d_useWindowParams(void);
@@ -553,9 +561,7 @@
 int G3d_tileInRange(G3D_Map *, int, int, int);
 int G3d_computeClippedTileDimensions(G3D_Map *, int, int *, int *, int *,
 				     int *, int *, int *);
-int G3d_isValidLocation(G3D_Map *, double, double, double);
-void G3d_location2coord(G3D_Map *, double, double, double, int *, int *,
-			int *);
+
 /* grass/src/libes/g3d/tilenull.c */
 void G3d_setNullTileType(G3D_Map *, void *, int);
 void G3d_setNullTile(G3D_Map *, void *);

Added: grass/trunk/lib/g3d/g3dgetvalue.c
===================================================================
--- grass/trunk/lib/g3d/g3dgetvalue.c	                        (rev 0)
+++ grass/trunk/lib/g3d/g3dgetvalue.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -0,0 +1,245 @@
+#include <grass/raster.h>
+#include "G3d_intern.h"
+
+/*---------------------------------------------------------------------------*/
+
+/*!
+ * \brief 
+ *
+ * Returns in <em>*value</em> the resampled cell-value of the cell with
+ * window-coordinate <em>(x, y, z)</em>.  The value returned is of <em>type</em>.
+ * This function invokes a fatal error if an error occurs.
+ *
+ *  \param map
+ *  \param x
+ *  \param y
+ *  \param z
+ *  \param value
+ *  \param type
+ *  \return void
+ */
+
+void G3d_getValue(G3D_Map * map, int x, int y, int z, void *value, int type)
+{
+    /* get the resampled value */
+    map->resampleFun(map, x, y, z, value, type);
+}
+
+/*---------------------------------------------------------------------------*/
+
+/*!
+ * \brief 
+ *
+ * Is equivalent to
+ * <tt>G3d_getValue (map, x, y, z, &value, FCELL_TYPE);</tt> return value.
+ *
+ *  \param map
+ *  \param x
+ *  \param y
+ *  \param z
+ *  \return float
+ */
+
+float G3d_getFloat(G3D_Map * map, int x, int y, int z)
+{
+    float value;
+
+    G3d_getValue(map, x, y, z, &value, FCELL_TYPE);
+    return value;
+}
+
+/*---------------------------------------------------------------------------*/
+
+/*!
+ * \brief 
+ *
+ * Is equivalent
+ * to <tt>G3d_getValue (map, x, y, z, &value, DCELL_TYPE);</tt> return value.
+ *
+ *  \param map
+ *  \param x
+ *  \param y
+ *  \param z
+ *  \return double
+ */
+
+double G3d_getDouble(G3D_Map * map, int x, int y, int z)
+{
+    double value;
+
+    G3d_getValue(map, x, y, z, &value, DCELL_TYPE);
+    return value;
+}
+
+/*---------------------------------------------------------------------------*/
+
+/*!
+ * \brief 
+ *
+ * Returns in <em>value</em> the value of the <em>map</em> which corresponds to 
+ * window coordinates <em>(north, east, top)</em>.  The
+ * value is resampled using the resampling function specified for <em>map</em>. The
+ * <em>value</em> is of <em>type</em>.
+ *
+ *  \param map
+ *  \param north
+ *  \param east
+ *  \param top
+ *  \param value
+ *  \param type
+ *  \return void
+ */
+
+void
+G3d_getWindowValue(G3D_Map * map, double north, double east, double top,
+		   void *value, int type)
+{
+    int col, row, depth;
+
+    G3d_location2WindowCoord(map, north, east, top, &col, &row, &depth);
+
+    /* if (row, col, depth) outside window return NULL value */
+    if ((row < 0) || (row >= map->window.rows) ||
+	(col < 0) || (col >= map->window.cols) ||
+	(depth < 0) || (depth >= map->window.depths)) {
+	G3d_setNullValue(value, 1, type);
+	return;
+    }
+
+    /* Get the value from the map in map-region resolution */
+	map->resampleFun(map, col, row, depth, value, type);
+}
+
+/*---------------------------------------------------------------------------*/
+
+/*!
+ * \brief 
+ *
+ * Returns in <em>value</em> the value of the <em>map</em> which corresponds to 
+ * region coordinates <em>(north, east, top)</em>.
+ *
+ *  \param map
+ *  \param north
+ *  \param east
+ *  \param top
+ *  \param value
+ *  \param type
+ *  \return void
+ */
+
+void
+G3d_getRegionValue(G3D_Map * map, double north, double east, double top,
+		   void *value, int type)
+{
+    int row, col, depth;
+
+    G3d_location2coord(map, north, east, top, &col, &row, &depth);
+
+    /* if (row, col, depth) outside region return NULL value */
+    if ((row < 0) || (row >= map->region.rows) ||
+	(col < 0) || (col >= map->region.cols) ||
+	(depth < 0) || (depth >= map->region.depths)) {
+	G3d_setNullValue(value, 1, type);
+	return;
+    }
+
+    /* Get the value from the map in map-region resolution */
+	G3d_getValueRegion(map, col, row, depth, value, type);
+}
+
+/*---------------------------------------------------------------------------*/
+
+/*!
+ * \brief 
+ *
+ * Is equivalent to <tt>G3d_getValueRegion (map, x, y, z, &value, FCELL_TYPE);</tt>
+ * return value.
+ *
+ *  \param map
+ *  \param x
+ *  \param y
+ *  \param z
+ *  \return float
+ */
+
+float G3d_getFloatRegion(G3D_Map * map, int x, int y, int z)
+{
+    int tileIndex, offs;
+    float *tile;
+
+    if (map->typeIntern == DCELL_TYPE)
+	return (float)G3d_getDoubleRegion(map, x, y, z);
+
+    G3d_coord2tileIndex(map, x, y, z, &tileIndex, &offs);
+    tile = (float *)G3d_getTilePtr(map, tileIndex);
+
+    if (tile == NULL)
+	G3d_fatalError("G3d_getFloatRegion: error in G3d_getTilePtr");
+
+    return tile[offs];
+}
+
+/*---------------------------------------------------------------------------*/
+
+
+/*!
+ * \brief 
+ *
+ * Is equivalent to <tt>G3d_getValueRegion (map, x, y, z, &value,
+ * DCELL_TYPE);</tt> return value.
+ *
+ *  \param map
+ *  \param x
+ *  \param y
+ *  \param z
+ *  \return double
+ */
+
+double G3d_getDoubleRegion(G3D_Map * map, int x, int y, int z)
+{
+    int tileIndex, offs;
+    double *tile;
+
+    if (map->typeIntern == FCELL_TYPE)
+	return (double)G3d_getFloatRegion(map, x, y, z);
+
+    G3d_coord2tileIndex(map, x, y, z, &tileIndex, &offs);
+    tile = (double *)G3d_getTilePtr(map, tileIndex);
+
+    if (tile == NULL)
+	G3d_fatalError("G3d_getDoubleRegion: error in G3d_getTilePtr");
+
+    return tile[offs];
+}
+
+/*---------------------------------------------------------------------------*/
+
+
+/*!
+ * \brief 
+ *
+ * Returns in <em>*value</em> the cell-value of the cell with
+ * 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.
+ * This function invokes a fatal error if an error occurs.
+ *
+ *  \param map
+ *  \param x
+ *  \param y
+ *  \param z
+ *  \param value
+ *  \param type
+ *  \return void
+ */
+
+void
+G3d_getValueRegion(G3D_Map * map, int x, int y, int z, void *value, int type)
+{
+    if (type == FCELL_TYPE) {
+	*((float *)value) = G3d_getFloatRegion(map, x, y, z);
+	return;
+    }
+
+    *((double *)value) = G3d_getDoubleRegion(map, x, y, z);
+}

Added: grass/trunk/lib/g3d/g3dputvalue.c
===================================================================
--- grass/trunk/lib/g3d/g3dputvalue.c	                        (rev 0)
+++ grass/trunk/lib/g3d/g3dputvalue.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -0,0 +1,117 @@
+#include <grass/raster.h>
+#include "G3d_intern.h"
+
+
+/*!
+ * \brief 
+ *
+ * Is equivalent to G3d_putValue (map, x, y, z, &value, FCELL_TYPE).
+ *
+ *  \param map
+ *  \param x
+ *  \param y
+ *  \param z
+ *  \param value
+ *  \return int
+ */
+
+int G3d_putFloat(G3D_Map * map, int x, int y, int z, float value)
+{
+    int tileIndex, offs;
+    float *tile;
+
+    if (map->typeIntern == DCELL_TYPE) {
+	if (!G3d_putDouble(map, x, y, z, (double)value)) {
+	    G3d_error("G3d_putFloat: error in G3d_putDouble");
+	    return 0;
+	}
+	return 1;
+    }
+
+    G3d_coord2tileIndex(map, x, y, z, &tileIndex, &offs);
+    tile = (float *)G3d_getTilePtr(map, tileIndex);
+    if (tile == NULL) {
+	G3d_error("G3d_putFloat: error in G3d_getTilePtr");
+	return 0;
+    }
+
+    tile[offs] = value;
+    return 1;
+}
+
+/*---------------------------------------------------------------------------*/
+
+
+/*!
+ * \brief 
+ *
+ *  Is equivalent to G3d_putValue (map, x, y, z, &value, DCELL_TYPE).
+ *
+ *  \param map
+ *  \param x
+ *  \param y
+ *  \param z
+ *  \param value
+ *  \return int
+ */
+
+int G3d_putDouble(G3D_Map * map, int x, int y, int z, double value)
+{
+    int tileIndex, offs;
+    double *tile;
+
+    if (map->typeIntern == FCELL_TYPE) {
+	if (!G3d_putFloat(map, x, y, z, (float)value)) {
+	    G3d_error("G3d_putDouble: error in G3d_putFloat");
+	    return 0;
+	}
+	return 1;
+    }
+
+    G3d_coord2tileIndex(map, x, y, z, &tileIndex, &offs);
+    tile = (double *)G3d_getTilePtr(map, tileIndex);
+    if (tile == NULL) {
+	G3d_error("G3d_putDouble: error in G3d_getTilePtr");
+	return 0;
+    }
+
+    tile[offs] = value;
+    return 1;
+}
+
+/*---------------------------------------------------------------------------*/
+
+/*!
+ * \brief 
+ *
+ * After converting <em>*value</em> of <em>type</em> into the type specified
+ * at the initialization time (i.e. <em>typeIntern</em>) this function writes the
+ * value into the tile buffer corresponding to cell-coordinate <em>(x, y, z)</em>.
+ *
+ *  \param map
+ *  \param x
+ *  \param y
+ *  \param z
+ *  \param value
+ *  \param type
+ *  \return 1 ... if successful,  
+ *          0 ... otherwise.
+ */
+
+int
+G3d_putValue(G3D_Map * map, int x, int y, int z, const void *value, int type)
+{
+    if (type == FCELL_TYPE) {
+	if (!G3d_putFloat(map, x, y, z, *((float *)value))) {
+	    G3d_error("G3d_putValue: error in G3d_putFloat");
+	    return 0;
+	}
+	return 1;
+    }
+
+    if (!G3d_putDouble(map, x, y, z, *((double *)value))) {
+	G3d_error("G3d_putValue: error in G3d_putDouble");
+	return 0;
+    }
+    return 1;
+}

Modified: grass/trunk/lib/g3d/g3dregion.c
===================================================================
--- grass/trunk/lib/g3d/g3dregion.c	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/lib/g3d/g3dregion.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -210,71 +210,112 @@
     *regionDest = *regionSrc;
 }
 
+
 /*---------------------------------------------------------------------------*/
 
+int
+G3d_readRegionMap(const char *name, const char *mapset, G3D_Region * region)
+{
+    char fullName[GPATH_MAX];
+    char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
 
+    if (G_name_is_fully_qualified(name, xname, xmapset))
+	G3d_filename(fullName, G3D_HEADER_ELEMENT, xname, xmapset);
+    else {
+	if (!mapset || !*mapset)
+	    mapset = G_find_grid3(name, "");
+	G3d_filename(fullName, G3D_HEADER_ELEMENT, name, mapset);
+    }
+    return G3d_readWindow(region, fullName);
+}
+
+/*---------------------------------------------------------------------------*/
+
+
 /*!
  * \brief 
  *
- *  Returns in <em>value</em> the value of the <em>map</em> which corresponds to 
- * region coordinates <em>(north, east, top)</em>.  The
- * value is resampled using the resampling function specified for <em>map</em>. The
- * <em>value</em> is of <em>type</em>.
+ *  Returns 1 if region-coordinates <em>(north, west, bottom)</em> are
+ * inside the region of <em>map</em>. Returns 0 otherwise.
  *
  *  \param map
  *  \param north
  *  \param east
  *  \param top
- *  \param value
- *  \param type
- *  \return void
+ *  \return int
  */
 
-void
-G3d_getRegionValue(G3D_Map * map, double north, double east, double top,
-		   void *value, int type)
+int G3d_isValidLocation(G3D_Map * map, double north, double east, double top)
 {
-    int row, col, depth;
+    return ((north >= map->region.south) && (north <= map->region.north) &&
+	    (east >= map->region.west) && (east <= map->region.east) &&
+	    (((top >= map->region.bottom) && (top <= map->region.top)) ||
+	     ((top <= map->region.bottom) && (top >= map->region.top))));
+}
 
-    /* convert (north, east, top) into (row, col, depth) */
+/*---------------------------------------------------------------------------*/
 
-    row = map->region.rows -
-	(north - map->region.south) / (map->region.north -
-				       map->region.south) * map->region.rows;
-    col =
-	(east - map->region.west) / (map->region.east -
-				     map->region.west) * map->region.cols;
-    depth =
-	(top - map->region.bottom) / (map->region.top -
-				      map->region.bottom) *
-	map->region.depths;
+/*!
+ * \brief 
+ *
+ *  Converts region-coordinates <em>(north, east,
+ *  top)</em> into cell-coordinates <em>(x, y, z)</em>.
+ *
+ *  \param map
+ *  \param north
+ *  \param east
+ *  \param top
+ *  \param x
+ *  \param y
+ *  \param z
+ *  \return void
+ */
 
-    /* if (row, col, depth) outside window return NULL value */
-    if ((row < 0) || (row >= map->region.rows) ||
-	(col < 0) || (col >= map->region.cols) ||
-	(depth < 0) || (depth >= map->region.depths)) {
-	G3d_setNullValue(value, 1, type);
-	return;
-    }
-
-    /* get value */
-    map->resampleFun(map, row, col, depth, value, type);
+void
+G3d_location2coord(G3D_Map * map, double north, double east, double top,
+		   int *x, int *y, int *z)
+{
+    double col, row, depth;
+    
+    col = (east - map->region.west) / (map->region.east -
+				      map->region.west) * (double)(map->region.cols);
+    row = (north - map->region.south) / (map->region.north -
+					map->region.south) * (double)(map->region.rows);
+    depth = (top - map->region.bottom) / (map->region.top -
+				       map->region.bottom) * (double)(map->region.depths);
+    /*
+    printf("G3d_location2coord col %g row %g depth %g\n", col, row, depth);
+    */  
+    
+    *x = (int)col;
+    *y = (int)row;
+    *z = (int)depth;
 }
 
-/*---------------------------------------------------------------------------*/
 
-int
-G3d_readRegionMap(const char *name, const char *mapset, G3D_Region * region)
+/*!
+ * \brief 
+ *
+ *  Converts region-coordinates <em>(north, east,
+ *  top)</em> into cell-coordinates <em>(x, y, z)</em>.
+ *  This function calls G3d_fatalError in case location is not in window.
+ *
+ *  \param map
+ *  \param north
+ *  \param east
+ *  \param top
+ *  \param x
+ *  \param y
+ *  \param z
+ *  \return void
+ */
+
+void
+G3d_location2coord2(G3D_Map * map, double north, double east, double top,
+		   int *x, int *y, int *z)
 {
-    char fullName[GPATH_MAX];
-    char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
+    if (!G3d_isValidLocation(map, north, east, top))
+	G3d_fatalError("G3d_location2coord2: location not in region");
 
-    if (G_name_is_fully_qualified(name, xname, xmapset))
-	G3d_filename(fullName, G3D_HEADER_ELEMENT, xname, xmapset);
-    else {
-	if (!mapset || !*mapset)
-	    mapset = G_find_grid3(name, "");
-	G3d_filename(fullName, G3D_HEADER_ELEMENT, name, mapset);
-    }
-    return G3d_readWindow(region, fullName);
+    G3d_location2coord(map, north, east, top, x, y, z);
 }

Modified: grass/trunk/lib/g3d/g3dresample.c
===================================================================
--- grass/trunk/lib/g3d/g3dresample.c	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/lib/g3d/g3dresample.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -9,11 +9,12 @@
  * \brief 
  *
  * The default resampling function which uses nearest
- * neighbor resampling.
+ * neighbor resampling. This method converts the window coordinates
+ * x, y, and z into region coordinates and returned the nearest neighbor.
  *
  *  \param map
+ *  \param col
  *  \param row
- *  \param col
  *  \param depth
  *  \param value
  *  \param type
@@ -21,22 +22,36 @@
  */
 
 void
-G3d_nearestNeighbor(G3D_Map * map, int row, int col, int depth, void *value,
+G3d_nearestNeighbor(G3D_Map * map, int x, int y, int z, void *value,
 		    int type)
 {
 
-     /*AV*/
-	/* BEGIN OF ORIGINAL CODE */
-	/*
-	   G3d_getValueRegion (map, row, col, depth, value, type);
-	 */
-	/* END OF ORIGINAL CODE */
-	 /*AV*/
-	/* BEGIN OF MY CODE */
-	G3d_getValueRegion(map, col, row, depth, value, type);
-    /* END OF MY CODE */
+    double north, east, top;
+    int row, col, depth;
 
+    /* convert (x, y, z) into (north, east, top) */
 
+	north = ((double)y + 0.5) / (double)map->window.rows *
+	(map->window.north - map->window.south) + map->window.south;
+    east = ((double)x + 0.5) / (double)map->window.cols *
+	(map->window.east - map->window.west) + map->window.west;
+    top = ((double)z + 0.5) / (double)map->window.depths *
+	(map->window.top - map->window.bottom) + map->window.bottom;
+
+    /* convert (north, east, top) into (row, col, depth) */
+
+    G3d_location2coord(map, north, east, top, &col, &row, &depth);
+
+    /* if (row, col, depth) outside map region return NULL value */
+    if ((row < 0) || (row >= map->region.rows) ||
+	(col < 0) || (col >= map->region.cols) ||
+	(depth < 0) || (depth >= map->region.depths)) {
+	G3d_setNullValue(value, 1, type);
+	return;
+    }
+    
+    /* Get the value from the map in map-region resolution */
+	G3d_getValueRegion(map, col, row, depth, value, type);
 }
 
 /*--------------------------------------------------------------------------*/

Modified: grass/trunk/lib/g3d/g3dvolume.c
===================================================================
--- grass/trunk/lib/g3d/g3dvolume.c	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/lib/g3d/g3dvolume.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -119,7 +119,7 @@
 		v[1] = t * v0[1] + tp * v1[1];
 		v[2] = t * v0[2] + tp * v1[2];
 
-		G3d_location2coord(map, v[0], v[1], v[2], &x, &y, &z);
+		G3d_location2coord2(map, v[0], v[1], v[2], &x, &y, &z);
 		/* DEBUG
 		   printf ("(%d %d %d) (%lf %lf %lf) (%d %d %d) %lf\n", 
 		   (int) dx / 2, (int) dy / 2, (int) dz / 2,

Modified: grass/trunk/lib/g3d/g3dwindow.c
===================================================================
--- grass/trunk/lib/g3d/g3dwindow.c	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/lib/g3d/g3dwindow.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -70,96 +70,29 @@
     return &g3d_window;
 }
 
+
 /*---------------------------------------------------------------------------*/
 
 
 /*!
  * \brief 
  *
- * Returns in <em>*value</em> the cell-value of the cell with
- * window-coordinate <em>(x, y, z)</em>.  The value returned is of <em>type</em>.
- * This function invokes a fatal error if an error occurs.
+ *  Returns 1 if window-coordinates <em>(north, west, bottom)</em> are
+ * inside the window of <em>map</em>. Returns 0 otherwise.
  *
  *  \param map
- *  \param x
- *  \param y
- *  \param z
- *  \param value
- *  \param type
- *  \return void
+ *  \param north
+ *  \param east
+ *  \param top
+ *  \return int
  */
 
-void G3d_getValue(G3D_Map * map, int x, int y, int z, void *value, int type)
+int G3d_isValidLocationWindow(G3D_Map * map, double north, double east, double top)
 {
-    double north, east, top;
-
-     /*AV*/
-	/* BEGIN OF ORIGINAL CODE */
-	/*
-	   int row, col, depth;
-	 */
-	/* END OF ORIGINAL CODE */
-	 /*AV*/
-	/* BEGIN OF MY CODE */
-    double row, col, depth;
-
-    /* END OF MY CODE */
-
-    /* convert (x, y, z) into (north, east, top) */
-
-     /*AV*/
-	/* BEGIN OF ORIGINAL CODE */
-	/*
-	   north = ((double) map->window.rows - y - 0.5) / (double) map->window.rows *
-	   (map->window.north - map->window.south) + map->window.south;
-	 */
-	/* END OF ORIGINAL CODE */
-	 /*AV*/
-	/* BEGIN OF MY CODE */
-	north = ((double)y + 0.5) / (double)map->window.rows *
-	(map->window.north - map->window.south) + map->window.south;
-    /* END OF MY CODE */
-
-    east = ((double)x + 0.5) / (double)map->window.cols *
-	(map->window.east - map->window.west) + map->window.west;
-    top = ((double)z + 0.5) / (double)map->window.depths *
-	(map->window.top - map->window.bottom) + map->window.bottom;
-
-    /* convert (north, east, top) into (row, col, depth) */
-
-     /*AV*/
-	/* BEGIN OF ORIGINAL CODE */
-	/*
-	   row = map->region.rows -
-	   (north - map->region.south) / (map->region.north - map->region.south) *
-	   map->region.rows;
-	 */
-	/* END OF ORIGINAL CODE */
-	 /*AV*/
-	/* BEGIN OF MY CODE */
-	row =
-	(north - map->region.south) / (map->region.north -
-				       map->region.south) * map->region.rows;
-    /* END OF MY CODE */
-
-    col = (east - map->region.west) / (map->region.east - map->region.west) *
-	map->region.cols;
-    depth =
-	(top - map->region.bottom) / (map->region.top -
-				      map->region.bottom) *
-	map->region.depths;
-
-    /* if (row, col, depth) outside window return NULL value */
-    if ((row < 0) || (row >= map->region.rows) ||
-	(col < 0) || (col >= map->region.cols) ||
-	(depth < 0) || (depth >= map->region.depths)) {
-	G3d_setNullValue(value, 1, type);
-	return;
-    }
-
-    /* get value */
-    map->resampleFun(map, (int)row, (int)col, (int)depth, value, type);
-
+    return ((north >= map->window.south) && (north <= map->window.north) &&
+	    (east >= map->window.west) && (east <= map->window.east) &&
+	    (((top >= map->window.bottom) && (top <= map->window.top)) ||
+	     ((top <= map->window.bottom) && (top >= map->window.top))));
 }
 
 /*---------------------------------------------------------------------------*/
@@ -168,44 +101,64 @@
 /*!
  * \brief 
  *
- * Is equivalent to
- * <tt>G3d_getValue (map, x, y, z, &value, FCELL_TYPE);</tt> return value.
+ *  Converts window-coordinates <em>(north, east,
+ *  top)</em> into cell-coordinates <em>(x, y, z)</em>.
  *
  *  \param map
+ *  \param north
+ *  \param east
+ *  \param top
  *  \param x
  *  \param y
  *  \param z
- *  \return float
+ *  \return void
  */
 
-float G3d_getFloat(G3D_Map * map, int x, int y, int z)
+void
+G3d_location2WindowCoord(G3D_Map * map, double north, double east, double top,
+		   int *x, int *y, int *z)
 {
-    float value;
-
-    G3d_getValue(map, x, y, z, &value, FCELL_TYPE);
-    return value;
+    double col, row, depth;
+    
+    col = (east - map->window.west) / (map->window.east -
+				      map->window.west) * (double)(map->window.cols);
+    row = (north - map->window.south) / (map->window.north -
+					map->window.south) * (double)(map->window.rows);
+    depth = (top - map->window.bottom) / (map->window.top -
+				       map->window.bottom) * (double)(map->window.depths);
+    /*
+    printf("G3d_location2WindowCoord col %g row %g depth %g\n", col, row, depth);
+    */
+    
+    *x = (int)col;
+    *y = (int)row;
+    *z = (int)depth;
+    
 }
 
-/*---------------------------------------------------------------------------*/
-
-
 /*!
  * \brief 
  *
- * Is equivalent
- * to <tt>G3d_getValue (map, x, y, z, &value, DCELL_TYPE);</tt> return value.
+ *  Converts window-coordinates <em>(north, east,
+ *  top)</em> into cell-coordinates <em>(x, y, z)</em>.
+ *  This function calls G3d_fatalError in case location is not in window.
  *
  *  \param map
+ *  \param north
+ *  \param east
+ *  \param top
  *  \param x
  *  \param y
  *  \param z
- *  \return double
+ *  \return void
  */
 
-double G3d_getDouble(G3D_Map * map, int x, int y, int z)
+void
+G3d_location2WindowCoord2(G3D_Map * map, double north, double east, double top,
+		   int *x, int *y, int *z)
 {
-    double value;
-
-    G3d_getValue(map, x, y, z, &value, DCELL_TYPE);
-    return value;
+    if (!G3d_isValidLocationWindow(map, north, east, top))
+	G3d_fatalError("G3d_location2WindowCoord2: location not in window");
+    
+    G3d_location2WindowCoord(map, north, east, top, x, y, z);
 }

Added: grass/trunk/lib/g3d/test/Makefile
===================================================================
--- grass/trunk/lib/g3d/test/Makefile	                        (rev 0)
+++ grass/trunk/lib/g3d/test/Makefile	2011-06-15 14:13:34 UTC (rev 46716)
@@ -0,0 +1,9 @@
+MODULE_TOPDIR = ../../..
+
+PGM=test.g3d.lib
+
+LIBES = $(GISLIB) $(G3DLIB) 
+DEPENDENCIES = $(GISDEP) $(G3DDEP)
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd

Added: grass/trunk/lib/g3d/test/test.g3d.lib.html
===================================================================
Added: grass/trunk/lib/g3d/test/test_coordinate_transform.c
===================================================================
--- grass/trunk/lib/g3d/test/test_coordinate_transform.c	                        (rev 0)
+++ grass/trunk/lib/g3d/test/test_coordinate_transform.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -0,0 +1,301 @@
+#include <stdlib.h>
+#include <string.h>
+#include "test_g3d_lib.h"
+#include "grass/interpf.h"
+
+static int test_coordinate_transform(void);
+static int test_region(void);
+
+
+/* *************************************************************** */
+/* Perfrome the coordinate transformation tests ****************** */
+/* *************************************************************** */
+int unit_test_coordinate_transform(void)
+{
+    int sum = 0;
+
+    G_message(_("\n++ Running g3d coordinate transform unit tests ++"));
+
+    sum += test_coordinate_transform();
+    sum += test_region();
+
+    if (sum > 0)
+	G_warning(_("\n-- g3d coordinate transform unit tests failure --"));
+    else
+	G_message(_("\n-- g3d coordinate transform unit tests finished successfully --"));
+
+    return sum;
+}
+
+/* *************************************************************** */
+
+int test_coordinate_transform(void)
+{
+    int sum = 0; 
+    
+    double north, east, top;
+    int col = 0, row = 0, depth = 0;
+    
+    G3D_Region region, default_region;
+    G3D_Map *map = NULL;
+    
+    /* We need to set up a specific region for the new g3d map.
+     * First we safe the default region. */
+    G3d_getWindow(&default_region);
+    G3d_regionCopy(&region, &default_region);
+    
+    region.bottom = 0.0;
+    region.top = 1000;
+    region.south = 1000;
+    region.north = 8500;
+    region.west = 5000;
+    region.east = 10000;
+    region.rows = 15;
+    region.cols = 10;
+    region.depths = 5;
+        
+    G3d_adjustRegion(&region);
+    
+    map = G3d_openNewOptTileSize("test_coordinate_transform", G3D_USE_CACHE_XYZ, &region, FCELL_TYPE, 32);
+        
+    /* The window is the same as the map region ... of course */
+    G3d_setWindowMap(map, &region);
+    
+    G_message("Test the upper right corner, coordinates must be col = 9, row = 14, depth = 4");
+    
+    /*
+     ROWS
+  1000 1500 2000 2500 3000 3500 4000 4500 5000 5500 6500 7000 7500 8000 8500 9000
+    |....|....|....|....|....|....|....|....|....|....|....|....|....|....|....|                         
+    0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15
+          
+    COLS
+  5000 5500 6000 6500 7000 7500 8000 8500 9000 9500 10000
+    |....|....|....|....|....|....|....|....|....|....|                         
+    0    1    2    3    4    5    6    7    8    9   10
+      
+    DEPTHS
+    0   200  400  600  800  1000
+    |....|....|....|....|....|                         
+    0    1    2    3    4    5                          
+    */
+    north = 8499.9;
+    east=  9999.9;
+    top =  999.9;
+        
+    G3d_location2coord(map, north, east, top, &col, &row, &depth);    
+    printf("G3d_location2coord col %i row %i depth %i\n", col, row, depth);
+    if(region.cols - 1 != col || region.rows - 1 != row || region.depths - 1 != depth) {
+        G_message("Error in G3d_location2coord");
+        sum++;
+    }
+    
+    G3d_location2WindowCoord(map, north, east, top, &col, &row, &depth);
+    printf("G3d_location2WindowCoord col %i row %i depth %i\n", col, row, depth);
+    if(region.cols - 1 != col || region.rows - 1 != row || region.depths - 1 != depth) {
+        G_message("Error in G3d_location2WindowCoord");
+        sum++;
+    }
+    
+    G_message("Test the lower left corner, coordinates must be col = row = depth = 0");
+    
+    north = 1000.0;
+    east= 5000.0;
+    top = 0.0;
+        
+    G3d_location2coord(map, north, east, top, &col, &row, &depth);    
+    printf("G3d_location2coord col %i row %i depth %i\n", col, row, depth);
+    if(0 != col || 0 != row || 0 != depth) {
+        G_message("Error in G3d_location2coord");
+        sum++;
+    }
+    
+    G3d_location2WindowCoord(map, north, east, top, &col, &row, &depth);
+    printf("G3d_location2WindowCoord col %i row %i depth %i\n", col, row, depth);
+    if(0 != col || 0 != row || 0 != depth) {
+        G_message("Error in G3d_location2WindowCoord");
+        sum++;
+    }
+    
+    G_message("Test the center, coordinates must be col = 4 row = 7 depth = 2");
+    
+    
+    north = 4750.0;
+    east= 7499.9;
+    top = 500.0;
+        
+    G3d_location2coord(map, north, east, top, &col, &row, &depth);    
+    printf("G3d_location2coord col %i row %i depth %i\n", col, row, depth);
+    if((region.cols - 1)/2 != col || (region.rows - 1)/2 != row || (region.depths - 1)/2 != depth) {
+        G_message("Error in G3d_location2coord");
+        sum++;
+    }
+    
+    G3d_location2WindowCoord(map, north, east, top, &col, &row, &depth);
+    printf("G3d_location2WindowCoord col %i row %i depth %i\n", col, row, depth);
+    if((region.cols - 1)/2 != col || (region.rows - 1)/2 != row || (region.depths - 1)/2 != depth) {
+        G_message("Error in G3d_location2WindowCoord");
+        sum++;
+    }
+    
+    G_message("Test the n=3000.1, e=7000.1 and t=800.1, coordinates must be col = row = depth = 4");
+    
+    north = 3000.1;
+    east= 7000.1;
+    top = 800.1;
+        
+    G3d_location2coord(map, north, east, top, &col, &row, &depth);    
+    printf("G3d_location2coord col %i row %i depth %i\n", col, row, depth);
+    if(4 != col || 4 != row || 4 != depth) {
+        G_message("Error in G3d_location2coord");
+        sum++;
+    }
+    
+    G3d_location2WindowCoord(map, north, east, top, &col, &row, &depth);
+    printf("G3d_location2WindowCoord col %i row %i depth %i\n", col, row, depth);
+    if(4 != col || 4 != row || 4 != depth) {
+        G_message("Error in G3d_location2WindowCoord");
+        sum++;
+    }
+    
+    G_message("Test the n=2999.9, e=6999.9 and t=799.9, coordinates must be col = row = depth = 3");
+    
+    north = 2999.9;
+    east= 6999.9;
+    top = 799.9;
+        
+    G3d_location2coord(map, north, east, top, &col, &row, &depth);    
+    printf("G3d_location2coord col %i row %i depth %i\n", col, row, depth);
+    if(3 != col || 3 != row || 3 != depth) {
+        G_message("Error in G3d_location2coord");
+        sum++;
+    }
+    
+    G3d_location2WindowCoord(map, north, east, top, &col, &row, &depth);
+    printf("G3d_location2WindowCoord col %i row %i depth %i\n", col, row, depth);
+    if(3 != col || 3 != row || 3 != depth) {
+        G_message("Error in G3d_location2WindowCoord");
+        sum++;
+    }
+    
+    G3d_closeCell(map);
+    
+    G_remove("grid3", "test_coordinate_transform");
+    
+    return sum;
+}
+
+/* *************************************************************** */
+
+int test_region(void)
+{
+    int sum = 0;
+    G3D_Region region, new_region;
+    
+    G3d_getWindow(&region);
+    region.bottom = 0.0;
+    region.top = 1000;
+    region.south = 10000;
+    region.north = 20000;
+    region.west = 5000;
+    region.east = 10000;
+    region.rows = 20;
+    region.cols = 10;
+    region.depths = 5;
+    region.ew_res = 0;
+    region.ns_res = 0;
+    region.tb_res = 0;
+        
+    /* Test region adjustment */
+    G3d_adjustRegion(&region);
+    
+    if(region.ew_res != 500) {
+        G_message("Error in G3d_adjustRegion: region.ew_res != 500");
+        sum++;
+    }
+    
+    if(region.ns_res != 500) {
+        G_message("Error in G3d_adjustRegion: region.ew_res != 500");
+        sum++;
+    }
+    
+    if(region.tb_res != 200) {
+        G_message("Error in G3d_adjustRegion: region.ew_res != 500");
+        sum++;
+    }
+    
+    /* Test the region copy */
+    G3d_regionCopy(&new_region, &region);
+        
+    if(region.bottom != new_region.bottom) {
+        G_message("Error in G3d_regionCopy: region.bottom != new_region.bottom");
+        sum++;
+    }
+    
+    if(region.cols != new_region.cols) {
+        G_message("Error in G3d_regionCopy: region.cols != new_region.cols");
+        sum++;
+    }
+    
+    if(region.depths != new_region.depths) {
+        G_message("Error in G3d_regionCopy: region.depths != new_region.depths");
+        sum++;
+    }
+    
+    if(region.east != new_region.east) {
+        G_message("Error in G3d_regionCopy: region.east != new_region.east");
+        sum++;
+    }
+    
+    if(region.ew_res != new_region.ew_res) {
+        G_message("Error in G3d_regionCopy: region.ew_res != new_region.ew_res");
+        sum++;
+    }
+    
+    if(region.north != new_region.north) {
+        G_message("Error in G3d_regionCopy: region.north != new_region.north");
+        sum++;
+    }
+    
+    if(region.ns_res != new_region.ns_res) {
+        G_message("Error in G3d_regionCopy: region.ns_res != new_region.ns_res");
+        sum++;
+    }
+    
+    if(region.proj != new_region.proj) {
+        G_message("Error in G3d_regionCopy: region.proj != new_region.proj");
+        sum++;
+    }
+    
+    if(region.rows != new_region.rows) {
+        G_message("Error in G3d_regionCopy: region.rows != new_region.rows");
+        sum++;
+    }
+    
+    if(region.south != new_region.south) {
+        G_message("Error in G3d_regionCopy: region.south != new_region.south");
+        sum++;
+    }
+    
+    if(region.tb_res != new_region.tb_res) {
+        G_message("Error in G3d_regionCopy: region.tb_res != new_region.tb_res");
+        sum++;
+    }
+    
+    if(region.top != new_region.top) {
+        G_message("Error in G3d_regionCopy: region.top != new_region.top");
+        sum++;
+    }
+    
+    if(region.west != new_region.west) {
+        G_message("Error in G3d_regionCopy: region.west != new_region.west");
+        sum++;
+    }
+    
+    if(region.zone != new_region.zone) {
+        G_message("Error in G3d_regionCopy: region.zone != new_region.zone");
+        sum++;
+    }    
+    
+    return sum;
+}

Added: grass/trunk/lib/g3d/test/test_g3d_lib.h
===================================================================
--- grass/trunk/lib/g3d/test/test_g3d_lib.h	                        (rev 0)
+++ grass/trunk/lib/g3d/test/test_g3d_lib.h	2011-06-15 14:13:34 UTC (rev 46716)
@@ -0,0 +1,29 @@
+
+/*****************************************************************************
+*
+* MODULE:       Grass g3d Library
+* AUTHOR(S):    Soeren Gebbert, Braunschweig (GER) Jun 2011
+*               soerengebbert <at> googlemail <dot> com
+*               
+* PURPOSE:	Unit and Integration tests
+*
+* COPYRIGHT:    (C) 2000 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
+*               for details.
+*
+*****************************************************************************/
+
+#ifndef _TEST_G3D_LIB_H_
+#define _TEST_G3D_LIB_H_
+
+#include <grass/G3d.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+double compute_time_difference(struct timeval start, struct timeval end);
+int unit_test_coordinate_transform(void);
+int unit_test_put_get_value(void);
+
+#endif

Added: grass/trunk/lib/g3d/test/test_main.c
===================================================================
--- grass/trunk/lib/g3d/test/test_main.c	                        (rev 0)
+++ grass/trunk/lib/g3d/test/test_main.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -0,0 +1,134 @@
+
+/*****************************************************************************
+*
+* MODULE:       Grass g3d Library
+* AUTHOR(S):    Soeren Gebbert, Braunschweig (GER) Jun 2011
+* 		        soerengebbert <at> googlemail <dot> com
+*               
+* PURPOSE:	Unit and Integration tests
+*
+* COPYRIGHT:    (C) 2000 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
+*               for details.
+*
+*****************************************************************************/
+
+#include <stdlib.h>
+#include <string.h>
+#include "test_g3d_lib.h"
+
+/*- Parameters and global variables -----------------------------------------*/
+typedef struct {
+    struct Option *unit, *integration;
+    struct Flag *full, *testunit, *testint;
+} paramType;
+
+paramType param; /*Parameters */
+
+/*- prototypes --------------------------------------------------------------*/
+static void set_params(void); /*Fill the paramType structure */
+
+/* ************************************************************************* */
+/* Set up the arguments we are expecting ********************************** */
+
+/* ************************************************************************* */
+void set_params(void) {
+    param.unit = G_define_option();
+    param.unit->key = "unit";
+    param.unit->type = TYPE_STRING;
+    param.unit->required = NO;
+    param.unit->options = "coord,putget";
+    param.unit->description = _("Choose the unit tests to run");
+
+    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.testunit = G_define_flag();
+    param.testunit->key = 'u';
+    param.testunit->description = _("Run all unit tests");
+
+    param.testint = G_define_flag();
+    param.testint->key = 'i';
+    param.testint->description = _("Run all integration tests");
+
+    param.full = G_define_flag();
+    param.full->key = 'a';
+    param.full->description = _("Run all unit and integration tests");
+
+}
+
+/* ************************************************************************* */
+/* ************************************************************************* */
+
+/* ************************************************************************* */
+int main(int argc, char *argv[]) {
+    struct GModule *module;
+    int returnstat = 0, i;
+
+    /* Initialize GRASS */
+    G_gisinit(argv[0]);
+
+    module = G_define_module();
+    module->description
+            = _("Performs unit and integration tests for the g3d library");
+
+    /* Get parameters from user */
+    set_params();
+
+    if (G_parser(argc, argv))
+        exit(EXIT_FAILURE);
+    
+    /* Initiate the defaults for testing */
+    G3d_initDefaults();
+
+    /*Run the unit tests */
+    if (param.testunit->answer || param.full->answer) {
+        returnstat += unit_test_coordinate_transform();
+        returnstat += unit_test_put_get_value();
+    }
+
+    /*Run the integration tests */
+    if (param.testint->answer || param.full->answer) {
+        ;
+    }
+
+    /*Run single tests */
+    if (!param.full->answer) {
+        /*unit tests */
+        if (!param.testunit->answer) {
+            i = 0;
+            if (param.unit->answers)
+                while (param.unit->answers[i]) {
+                    if (strcmp(param.unit->answers[i], "coor") == 0)
+                        returnstat += unit_test_coordinate_transform();
+                    if (strcmp(param.unit->answers[i], "putget") == 0)
+                        returnstat += unit_test_put_get_value();
+                    
+                    i++;
+                }
+        }
+        /*integration tests */
+        if (!param.testint->answer) {
+            i = 0;
+            if (param.integration->answers)
+                while (param.integration->answers[i]) {
+                    ;
+                }
+
+        }
+    }
+
+    
+    if (returnstat != 0)
+        G_warning("Errors detected while testing the g3d lib");
+    else
+        G_message("\n-- g3d lib tests finished successfully --");
+
+    return (returnstat);
+}

Added: grass/trunk/lib/g3d/test/test_put_get_value.c
===================================================================
--- grass/trunk/lib/g3d/test/test_put_get_value.c	                        (rev 0)
+++ grass/trunk/lib/g3d/test/test_put_get_value.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -0,0 +1,520 @@
+
+/*****************************************************************************
+*
+* MODULE:       Grass g3d Library
+* AUTHOR(S):    Soeren Gebbert, Braunschweig (GER) Jun 2011
+* 		        soerengebbert <at> googlemail <dot> com
+*               
+* PURPOSE:	Unit and Integration tests
+*
+* COPYRIGHT:    (C) 2000 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
+*               for details.
+*
+*****************************************************************************/
+#include <stdlib.h>
+#include <string.h>
+#include "test_g3d_lib.h"
+#include "grass/interpf.h"
+
+static int test_put_get_value_dcell(void);
+static int test_put_get_value_fcell(void);
+static int test_put_get_value_resampling(void);
+static int test_resampling_dcell(G3D_Map *map, double north, double east, double 
+                                 top, int col, int row, int depth, int fact);
+static int test_resampling_fcell(G3D_Map *map, double north, double east, double 
+                                 top, int col, int row, int depth, int fact);
+
+/* *************************************************************** */
+/* Perfrome the coordinate transformation tests ****************** */
+/* *************************************************************** */
+int unit_test_put_get_value(void)
+{
+    int sum = 0;
+
+    G_message(_("\n++ Running g3d put/get value unit tests ++"));
+
+    sum += test_put_get_value_dcell();
+    sum += test_put_get_value_fcell();
+    sum += test_put_get_value_resampling();
+
+    if (sum > 0)
+	G_warning(_("\n-- g3d put/get value unit tests failure --"));
+    else
+	G_message(_("\n-- g3d put/get value unit tests finished successfully --"));
+
+    return sum;
+}
+
+/* *************************************************************** */
+
+int test_put_get_value_dcell(void)
+{
+    int sum = 0; 
+    int x, y, z;
+    DCELL value;
+    DCELL value_ref;
+    
+    G_message("Testing DCELL put get value functions");
+    
+    double north, east, top;
+    int col, row, depth;
+    
+    G3D_Region region;
+    G3D_Map *map = NULL;
+    
+    /* We need to set up a specific region for the new g3d map.
+     * First we safe the default region. */
+    G3d_getWindow(&region);
+    
+    region.bottom = 0.0;
+    region.top = 1000;
+    region.south = 1000;
+    region.north = 8500;
+    region.west = 5000;
+    region.east = 10000;
+    region.rows = 15;
+    region.cols = 10;
+    region.depths = 5;
+        
+    G3d_adjustRegion(&region);
+        
+    map = G3d_openNewOptTileSize("test_put_get_value_dcell", G3D_USE_CACHE_XY, &region, DCELL_TYPE, 32);
+    
+    /* The window is the same as the map region ... of course */
+    G3d_setWindowMap(map, &region);
+    /*
+     ROWS
+  1000 1500 2000 2500 3000 3500 4000 4500 5000 5500 6500 7000 7500 8000 8500 9000 north
+    |....|....|....|....|....|....|....|....|....|....|....|....|....|....|....|                         
+    0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15 region
+          
+    COLS
+  5000 5500 6000 6500 7000 7500 8000 8500 9000 9500 10000 east
+    |....|....|....|....|....|....|....|....|....|....|                         
+    0    1    2    3    4    5    6    7    8    9   10 region
+      
+    DEPTHS
+    0   200  400  600  800  1000  top
+    |....|....|....|....|....|                         
+    0    1    2    3    4    5 region                   
+    */
+    
+    for(z = 0; z < region.depths; z++) {
+        for(y = 0; y < region.rows; y++) {
+            for(x = 0; x < region.cols; x++) {
+                /* Add cols, rows and depths and put this in the map */
+                value = x + y + z;
+                G3d_putValue(map, x, y, z, &value, DCELL_TYPE);
+            }
+        }
+    }
+    /* Write everything to the disk */
+    G3d_flushAllTiles(map);
+    
+    /* Reread the map and compare the expected results */
+    
+    G_message("Get the value of the lower left corner -> 0");
+    
+    north = region.south;
+    east = region.west;
+    top = region.bottom;
+    
+    col = row = depth = 0;
+    north = region.south + region.ns_res * row;
+    east = region.west + region.ew_res * col;
+    top = region.bottom + region.tb_res * depth;
+    
+    sum += test_resampling_dcell(map, north, east, top, col, row, depth, 1);
+    
+    G_message("Get the value of x == y == z == 1 -> x + y + z == 3");
+    
+    col = row = depth = 1;
+    north = region.south + region.ns_res * row;
+    east = region.west + region.ew_res * col;
+    top = region.bottom + region.tb_res * depth;
+    
+    sum += test_resampling_dcell(map, north, east, top, col, row, depth, 1);
+    
+    G_message("Get the value of x == 4 y == 3 z == 2 -> x + y + z = 9");
+    
+    col = 4;
+    row = 3;
+    depth = 2;
+    north = region.south + region.ns_res * row;
+    east = region.west + region.ew_res * col;
+    top = region.bottom + region.tb_res * depth;
+    
+    sum += test_resampling_dcell(map, north, east, top, col, row, depth, 1);
+    
+    G_message("Get the value of x == 9 y == 14 z == 4 -> x + y + z = 27");
+    
+    col = 9;
+    row = 14;
+    depth = 4;
+    north = region.south + region.ns_res * row;
+    east = region.west + region.ew_res * col;
+    top = region.bottom + region.tb_res * depth;
+    
+    sum += test_resampling_dcell(map, north, east, top, col, row, depth, 1);
+        
+    G_message("Get the value of x == 10 y == 15 z == 5 -> x + y + z = NAN");
+        
+    col = 10;
+    row = 15;
+    depth = 5;
+    north = region.south + region.ns_res * 15;
+    east = region.west + region.ew_res * 10;
+    top = region.bottom + region.tb_res * 5;
+    
+    G3d_getRegionValue(map, north, east, top, &value, DCELL_TYPE);
+    G3d_getValue(map, col, row, depth, &value_ref, DCELL_TYPE);
+    /* G3d_getValueRegion does not work with coordinates outside the region */
+    printf("Value %g == %g\n", value, value_ref);
+    
+    if(value == 0 || value < 0 || value > 0) {
+        G_message("Error in G3d_getRegionValue");
+        sum++;
+    }
+    if(value_ref == 0 || value_ref < 0 || value_ref > 0) {
+        G_message("Error in G3d_getValue");
+        sum++;
+    }
+    
+    G3d_closeCell(map);
+    
+    G_remove("grid3", "test_put_get_value_dcell");
+    
+    return sum;
+}
+
+/* *************************************************************** */
+
+int test_put_get_value_fcell(void)
+{
+     int sum = 0; 
+    int x, y, z;
+    FCELL value;
+    FCELL value_ref;
+    
+    G_message("Testing FCELL put get value functions");
+    
+    double north, east, top;
+    int col, row, depth;
+    
+    G3D_Region region;
+    G3D_Map *map = NULL;
+    
+    /* We need to set up a specific region for the new g3d map.
+     * First we safe the default region. */
+    G3d_getWindow(&region);
+    
+    region.bottom = 0.0;
+    region.top = 1000;
+    region.south = 1000;
+    region.north = 8500;
+    region.west = 5000;
+    region.east = 10000;
+    region.rows = 15;
+    region.cols = 10;
+    region.depths = 5;
+        
+    G3d_adjustRegion(&region);
+        
+    map = G3d_openNewOptTileSize("test_put_get_value_dcell", G3D_USE_CACHE_XY, &region, FCELL_TYPE, 32);
+    
+    /* The window is the same as the map region ... of course */
+    G3d_setWindowMap(map, &region);
+    
+    for(z = 0; z < region.depths; z++) {
+        for(y = 0; y < region.rows; y++) {
+            for(x = 0; x < region.cols; x++) {
+                /* Add cols, rows and depths and put this in the map */
+                value = x + y + z;
+                G3d_putValue(map, x, y, z, &value, FCELL_TYPE);
+            }
+        }
+    }
+    /* Write everything to the disk */
+    G3d_flushAllTiles(map);
+    
+       /* Reread the map and compare the expected results */
+    
+    G_message("Get the value of the lower left corner -> 0");
+    
+    north = region.south;
+    east = region.west;
+    top = region.bottom;
+    
+    col = row = depth = 0;
+    north = region.south + region.ns_res * row;
+    east = region.west + region.ew_res * col;
+    top = region.bottom + region.tb_res * depth;
+    
+    sum += test_resampling_fcell(map, north, east, top, col, row, depth, 1);
+    
+    G_message("Get the value of x == y == z == 1 -> x + y + z == 3");
+    
+    col = row = depth = 1;
+    north = region.south + region.ns_res * row;
+    east = region.west + region.ew_res * col;
+    top = region.bottom + region.tb_res * depth;
+    
+    sum += test_resampling_fcell(map, north, east, top, col, row, depth, 1);
+    
+    G_message("Get the value of x == 4 y == 3 z == 2 -> x + y + z = 9");
+    
+    col = 4;
+    row = 3;
+    depth = 2;
+    north = region.south + region.ns_res * row;
+    east = region.west + region.ew_res * col;
+    top = region.bottom + region.tb_res * depth;
+    
+    sum += test_resampling_fcell(map, north, east, top, col, row, depth, 1);
+    
+    G_message("Get the value of x == 9 y == 14 z == 4 -> x + y + z = 27");
+    
+    col = 9;
+    row = 14;
+    depth = 4;
+    north = region.south + region.ns_res * row;
+    east = region.west + region.ew_res * col;
+    top = region.bottom + region.tb_res * depth;
+    
+    sum += test_resampling_fcell(map, north, east, top, col, row, depth, 1);
+        
+    G_message("Get the value of x == 10 y == 15 z == 5 -> x + y + z = NAN");
+    
+    north = region.south + region.ns_res * 15;
+    east = region.west + region.ew_res * 10;
+    top = region.bottom + region.tb_res * 5;
+    
+    G3d_getRegionValue(map, north, east, top, &value, FCELL_TYPE);
+    G3d_getValue(map, 10, 15, 5, &value_ref, FCELL_TYPE);
+    /* G3d_getValueRegion does not work with coordinates outside the region */
+    printf("Value %g == %g\n", value, value_ref);
+    
+    if(value == 0 || value < 0 || value > 0) {
+        G_message("Error in G3d_getRegionValue");
+        sum++;
+    }
+    if(value_ref == 0 || value_ref < 0 || value_ref > 0) {
+        G_message("Error in G3d_getValue");
+        sum++;
+    }
+    
+    G3d_closeCell(map);
+    
+    G_remove("grid3", "test_put_get_value_fcell");
+    
+    return sum;
+}
+
+/* *************************************************************** */
+
+int test_put_get_value_resampling(void)
+{
+    int sum = 0; 
+    int x, y, z;
+    DCELL value;
+    
+    G_message("Testing put get resample value functions");
+    
+    double north, east, top;
+    int col, row, depth;
+    
+    G3D_Region region;
+    G3D_Region window;
+    G3D_Map *map = NULL;
+    
+    /* We need to set up a specific region for the new g3d map.
+     * First we safe the default region. */
+    G3d_getWindow(&region);
+    
+    region.bottom = 0.0;
+    region.top = 1000;
+    region.south = 1000;
+    region.north = 8500;
+    region.west = 5000;
+    region.east = 10000;
+    region.rows = 15;
+    region.cols = 10;
+    region.depths = 5;
+        
+    G3d_adjustRegion(&region);
+    
+    map = G3d_openNewOptTileSize("test_put_get_value_resample", G3D_USE_CACHE_XY, &region, DCELL_TYPE, 32);
+    
+    /* We modify the window for resampling tests */
+    G3d_regionCopy(&window, &region);
+        
+    /* Double the cols, rows and depths -> 8x resolution window */
+    window.rows = 30;
+    window.cols = 20;
+    window.depths = 10;
+    
+    G3d_adjustRegion(&window);
+    
+    /* The window is the same as the map region ... of course */
+    G3d_setWindowMap(map, &window);
+    /*
+     ROWS
+  1000 1500 2000 2500 3000 3500 4000 4500 5000 5500 6500 7000 7500 8000 8500 9000 north
+    |....|....|....|....|....|....|....|....|....|....|....|....|....|....|....|                         
+    0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15 region
+    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |
+    0    2    4    6    8   10   12   14   16   18   20   22   24   26   28   30 window
+          
+    COLS
+  5000 5500 6000 6500 7000 7500 8000 8500 9000 9500 10000 east
+    |....|....|....|....|....|....|....|....|....|....|                         
+    0    1    2    3    4    5    6    7    8    9   10 region
+    |    |    |    |    |    |    |    |    |    |    |
+    0    2    4    6    8   10   12   14   16   18   20 window
+    DEPTHS
+    0   200  400  600  800  1000 top
+    |....|....|....|....|....|                         
+    0    1    2    3    4    5 region  
+    |    |    |    |    |    |
+    0    2    4    6    8   10 window                   
+    */
+    
+    for(z = 0; z < region.depths; z++) {
+        for(y = 0; y < region.rows; y++) {
+            for(x = 0; x < region.cols; x++) {
+                /* Add cols, rows and depths and put this in the map */
+                value = x + y + z;
+                G3d_putDouble(map, x, y, z, value);
+            }
+        }
+    }
+    /* Write everything to the disk */
+    G3d_flushAllTiles(map);
+    
+    /* Reread the map and compare the expected results */
+    
+    G_message("Get the value of the lower left corner -> 0");
+    
+    north = region.south;
+    east = region.west;
+    top = region.bottom;
+    
+    col = row = depth = 0;
+    
+    sum += test_resampling_dcell(map, north, east, top, col, row, depth, 2);
+    
+    G_message("Get the value of x == y == z == 1 -> x + y + z == 3");
+    
+    
+    col = row = depth = 1;
+    north = region.south + region.ns_res * row;
+    east = region.west + region.ew_res * col;
+    top = region.bottom + region.tb_res * depth;
+    
+    sum += test_resampling_dcell(map, north, east, top, col, row, depth, 2);
+    
+    G_message("Get the value of x == 7 y == 9 z == 3 -> x + y + z == 19");
+        
+    col = 7;
+    row = 9;
+    depth = 3;
+    
+    north = region.south + region.ns_res * row;
+    east = region.west + region.ew_res * col;
+    top = region.bottom + region.tb_res * depth;
+    
+    sum += test_resampling_dcell(map, north, east, top, col, row, depth, 2);
+    
+    G_message("Get the value of x == 9 y == 14 z == 4 -> x + y + z == 27");
+        
+    col = 9;
+    row = 14;
+    depth = 4;
+    
+    north = region.south + region.ns_res * row;
+    east = region.west + region.ew_res * col;
+    top = region.bottom + region.tb_res * depth;
+    
+    sum += test_resampling_dcell(map, north, east, top, col, row, depth, 2);
+    
+    G3d_closeCell(map);
+    
+    G_remove("grid3", "test_put_get_value_dcell");
+    
+    return sum;
+}
+
+/* *************************************************************** */
+
+int test_resampling_dcell(G3D_Map *map, double north, double east, double top, int col, int row, int depth, int fact)
+{
+    int sum = 0;
+    DCELL value;
+    DCELL value_ref;
+    DCELL value_reg;
+    DCELL value_win;
+    
+    G3d_getRegionValue(map, north, east, top, &value, DCELL_TYPE);
+    G3d_getWindowValue(map, north, east, top, &value_win, DCELL_TYPE);
+    G3d_getValue(map, col * fact, row * fact, depth * fact, &value_ref, DCELL_TYPE);
+    G3d_getValueRegion(map, col, row, depth, &value_reg, DCELL_TYPE);
+    printf("Value %g == %g == %g == %g\n", value, value_win, value_ref, value_reg);
+    
+    if(value != col + row + depth) {
+        G_message("Error in G3d_getRegionValue");
+        sum++;
+    }
+    if(value != col + row + depth) {
+        G_message("Error in G3d_getWindowValue");
+        sum++;
+    }
+    if(value != col + row + depth) {
+        G_message("Error in G3d_getValue");
+        sum++;
+    }
+    if(value != col + row + depth) {
+        G_message("Error in G3d_getValueRegion");
+        sum++;
+    }
+    
+    return sum;
+}
+
+/* *************************************************************** */
+
+int test_resampling_fcell(G3D_Map *map, double north, double east, double top, int col, int row, int depth, int fact)
+{
+    int sum = 0;
+    FCELL value;
+    FCELL value_ref;
+    FCELL value_reg;
+    FCELL value_win;
+    
+    G3d_getRegionValue(map, north, east, top, &value, FCELL_TYPE);
+    G3d_getWindowValue(map, north, east, top, &value_win, FCELL_TYPE);
+    G3d_getValue(map, col * fact, row * fact, depth * fact, &value_ref, FCELL_TYPE);
+    G3d_getValueRegion(map, col, row, depth, &value_reg, FCELL_TYPE);
+    printf("Value %g == %g == %g == %g\n", value, value_win, value_ref, value_reg);
+    
+    if(value != col + row + depth) {
+        G_message("Error in G3d_getRegionValue");
+        sum++;
+    }
+    if(value != col + row + depth) {
+        G_message("Error in G3d_getWindowValue");
+        sum++;
+    }
+    if(value != col + row + depth) {
+        G_message("Error in G3d_getValue");
+        sum++;
+    }
+    if(value != col + row + depth) {
+        G_message("Error in G3d_getValueRegion");
+        sum++;
+    }
+    
+    return sum;
+}
\ No newline at end of file

Added: grass/trunk/lib/g3d/test/test_tools.c
===================================================================
--- grass/trunk/lib/g3d/test/test_tools.c	                        (rev 0)
+++ grass/trunk/lib/g3d/test/test_tools.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -0,0 +1,37 @@
+
+/*****************************************************************************
+*
+* MODULE:       Grass g3d Library
+* AUTHOR(S):    Soeren Gebbert, Braunschweig (GER) Jun 2011
+* 	            soerengebbert <at> googlemail <dot> com
+*               
+* PURPOSE:	Unit and Integration tests
+*
+* COPYRIGHT:    (C) 2000 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
+*               for details.
+*
+*****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include "test_g3d_lib.h"
+#include <sys/time.h>
+
+/* *************************************************************** */
+/* Compute the difference between two time steps ***************** */
+
+/* *************************************************************** */
+double compute_time_difference(struct timeval start, struct timeval end) {
+    int sec;
+    int usec;
+
+    sec = end.tv_sec - start.tv_sec;
+    usec = end.tv_usec - start.tv_usec;
+
+    return (double) sec + (double) usec / 1000000;
+}

Modified: grass/trunk/lib/g3d/tileio.c
===================================================================
--- grass/trunk/lib/g3d/tileio.c	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/lib/g3d/tileio.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -136,117 +136,3 @@
 
     return 1;
 }
-
-/*---------------------------------------------------------------------------*/
-
-
-/*!
- * \brief 
- *
- * Is  equivalent to <tt>G3d_getValueRegion (map, x, y, z, &value,
- * DCELL_TYPE);</tt> return value.
- *
- *  \param map
- *  \param x
- *  \param y
- *  \param z
- *  \return double
- */
-
-double G3d_getDoubleRegion();
-
-
-/*!
- * \brief 
- *
- * Is equivalent to <tt>G3d_getValueRegion (map, x, y, z, &value, FCELL_TYPE);</tt>
- * return value.
- *
- *  \param map
- *  \param x
- *  \param y
- *  \param z
- *  \return float
- */
-
-float G3d_getFloatRegion(G3D_Map * map, int x, int y, int z)
-{
-    int tileIndex, offs;
-    float *tile;
-
-    if (map->typeIntern == DCELL_TYPE)
-	return (float)G3d_getDoubleRegion(map, x, y, z);
-
-    G3d_coord2tileIndex(map, x, y, z, &tileIndex, &offs);
-    tile = (float *)G3d_getTilePtr(map, tileIndex);
-
-    if (tile == NULL)
-	G3d_fatalError("G3d_getFloatRegion: error in G3d_getTilePtr");
-
-    return tile[offs];
-}
-
-/*---------------------------------------------------------------------------*/
-
-
-/*!
- * \brief 
- *
- * Is equivalent to <tt>G3d_getValueRegion (map, x, y, z, &value,
- * DCELL_TYPE);</tt> return value.
- *
- *  \param map
- *  \param x
- *  \param y
- *  \param z
- *  \return double
- */
-
-double G3d_getDoubleRegion(G3D_Map * map, int x, int y, int z)
-{
-    int tileIndex, offs;
-    double *tile;
-
-    if (map->typeIntern == FCELL_TYPE)
-	return (double)G3d_getFloatRegion(map, x, y, z);
-
-    G3d_coord2tileIndex(map, x, y, z, &tileIndex, &offs);
-    tile = (double *)G3d_getTilePtr(map, tileIndex);
-
-    if (tile == NULL)
-	G3d_fatalError("G3d_getDoubleRegion: error in G3d_getTilePtr");
-
-    return tile[offs];
-}
-
-/*---------------------------------------------------------------------------*/
-
-
-/*!
- * \brief 
- *
- * Returns in <em>*value</em> the cell-value of the cell with
- * 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.
- * This function invokes a fatal error if an error occurs.
- *
- *  \param map
- *  \param x
- *  \param y
- *  \param z
- *  \param value
- *  \param type
- *  \return void
- */
-
-void
-G3d_getValueRegion(G3D_Map * map, int x, int y, int z, void *value, int type)
-{
-    if (type == FCELL_TYPE) {
-	*((float *)value) = G3d_getFloatRegion(map, x, y, z);
-	return;
-    }
-
-    *((double *)value) = G3d_getDoubleRegion(map, x, y, z);
-}

Modified: grass/trunk/lib/g3d/tilemath.c
===================================================================
--- grass/trunk/lib/g3d/tilemath.c	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/lib/g3d/tilemath.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -312,67 +312,6 @@
 
 
 /*!
- * \brief 
- *
- *  Returns 1 if region-coordinates <em>(north, west, bottom)</em> are
- * inside the region of <em>map</em>. Returns 0 otherwise.
- *
- *  \param map
- *  \param north
- *  \param west
- *  \param bottom
- *  \return int
- */
-
-int G3d_isValidLocation(G3D_Map * map, double north, double east, double top)
-{
-    return ((north >= map->region.south) && (north <= map->region.north) &&
-	    (east >= map->region.west) && (east <= map->region.east) &&
-	    (((top >= map->region.bottom) && (top <= map->region.top)) ||
-	     ((top <= map->region.bottom) && (top >= map->region.top))));
-}
-
-/*---------------------------------------------------------------------------*/
-
-
-/*!
- * \brief 
- *
- *  Converts region-coordinates <em>(north, west,
- * bottom)</em> into cell-coordinates <em>(x, y, z)</em>.
- *
- *  \param map
- *  \param north
- *  \param west
- *  \param bottom
- *  \param x
- *  \param y
- *  \param z
- *  \return void
- */
-
-void
-G3d_location2coord(G3D_Map * map, double north, double east, double top,
-		   int *x, int *y, int *z)
-{
-    if (!G3d_isValidLocation(map, north, east, top))
-	G3d_fatalError("location2coord: location not in region");
-
-    *y = (north - map->region.south) / (map->region.north -
-					map->region.south) *
-	(map->region.rows - 1);
-    *x = (east - map->region.west) / (map->region.east -
-				      map->region.west) * (map->region.cols -
-							   1);
-    *z = (top - map->region.bottom) / (map->region.top -
-				       map->region.bottom) *
-	(map->region.depths - 1);
-}
-
-/*---------------------------------------------------------------------------*/
-
-
-/*!
  * \brief Compute the optimal tile size.
  *
  * This function computes tile sizes with an optimal ratio between tile dimensions and

Modified: grass/trunk/lib/g3d/tilewrite.c
===================================================================
--- grass/trunk/lib/g3d/tilewrite.c	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/lib/g3d/tilewrite.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -425,133 +425,3 @@
 
 /*---------------------------------------------------------------------------*/
 
-
-/*!
- * \brief 
- *
- *  Is equivalent to G3d_putValue (map, x, y, z, &value, DCELL_TYPE).
- *
- *  \param map
- *  \param x
- *  \param y
- *  \param z
- *  \param value
- *  \return int
- */
-
-int G3d_putDouble();
-
-
-/*!
- * \brief 
- *
- * Is equivalent to G3d_putValue (map, x, y, z, &value, FCELL_TYPE).
- *
- *  \param map
- *  \param x
- *  \param y
- *  \param z
- *  \param value
- *  \return int
- */
-
-int G3d_putFloat(G3D_Map * map, int x, int y, int z, float value)
-{
-    int tileIndex, offs;
-    float *tile;
-
-    if (map->typeIntern == DCELL_TYPE) {
-	if (!G3d_putDouble(map, x, y, z, (double)value)) {
-	    G3d_error("G3d_putFloat: error in G3d_putDouble");
-	    return 0;
-	}
-	return 1;
-    }
-
-    G3d_coord2tileIndex(map, x, y, z, &tileIndex, &offs);
-    tile = (float *)G3d_getTilePtr(map, tileIndex);
-    if (tile == NULL) {
-	G3d_error("G3d_putFloat: error in G3d_getTilePtr");
-	return 0;
-    }
-
-    tile[offs] = value;
-    return 1;
-}
-
-/*---------------------------------------------------------------------------*/
-
-
-/*!
- * \brief 
- *
- *  Is equivalent to G3d_putValue (map, x, y, z, &value, DCELL_TYPE).
- *
- *  \param map
- *  \param x
- *  \param y
- *  \param z
- *  \param value
- *  \return int
- */
-
-int G3d_putDouble(G3D_Map * map, int x, int y, int z, double value)
-{
-    int tileIndex, offs;
-    double *tile;
-
-    if (map->typeIntern == FCELL_TYPE) {
-	if (!G3d_putFloat(map, x, y, z, (float)value)) {
-	    G3d_error("G3d_putDouble: error in G3d_putFloat");
-	    return 0;
-	}
-	return 1;
-    }
-
-    G3d_coord2tileIndex(map, x, y, z, &tileIndex, &offs);
-    tile = (double *)G3d_getTilePtr(map, tileIndex);
-    if (tile == NULL) {
-	G3d_error("G3d_putDouble: error in G3d_getTilePtr");
-	return 0;
-    }
-
-    tile[offs] = value;
-    return 1;
-}
-
-/*---------------------------------------------------------------------------*/
-
-/*!
- * \brief 
- *
- * After converting <em>*value</em> of <em>type</em> into the type specified
- * at the initialization time (i.e. <em>typeIntern</em>) this function writes the
- * value into the tile buffer corresponding to cell-coordinate <em>(x, y, z)</em>.
- *
- *  \param map
- *  \param x
- *  \param y
- *  \param z
- *  \param value
- *  \param type
- *  \return 1 ... if successful,  
- *          0 ... otherwise.
- */
-
-int
-G3d_putValue(G3D_Map * map, int x, int y, int z, const void *value, int type)
-{
-    if (type == FCELL_TYPE) {
-	if (!G3d_putFloat(map, x, y, z, *((float *)value))) {
-	    G3d_error("G3d_putValue: error in G3d_putFloat");
-	    return 0;
-	}
-	return 1;
-    }
-
-    if (!G3d_putDouble(map, x, y, z, *((double *)value))) {
-	G3d_error("G3d_putValue: error in G3d_putDouble");
-	return 0;
-    }
-    return 1;
-}

Modified: grass/trunk/lib/g3d/writeascii.c
===================================================================
--- grass/trunk/lib/g3d/writeascii.c	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/lib/g3d/writeascii.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <grass/gis.h>
 #include <grass/G3d.h>
 
 /*!
@@ -18,9 +19,9 @@
 void G3d_writeAscii(void *map, const char *fname)
 {
     FILE *fp;
-    double d1 = 0;
-    double *d1p;
-    float *f1p;
+    DCELL d1 = 0;
+    DCELL *d1p;
+    FCELL *f1p;
     int x, y, z;
     int rows, cols, depths, typeIntern;
 
@@ -28,27 +29,28 @@
     typeIntern = G3d_tileTypeMap(map);
 
     d1p = &d1;
-    f1p = (float *)&d1;
+    f1p = (FCELL *) &d1;
 
     if (fname == NULL)
-	fp = stdout;
+        fp = stdout;
     else if ((fp = fopen(fname, "w")) == NULL)
-	G3d_fatalError("G3d_writeAscii: can't open file to write\n");
+        G3d_fatalError("G3d_writeAscii: can't open file to write\n");
 
-    for (z = 0; z < depths; z++)
-	for (y = 0; y < rows; y++) {
-	    fprintf(fp, "z y x %d %d (%d - %d)\n", z, y, 0, cols - 1);
-	    for (x = 0; x < cols; x++) {
-		G3d_getValueRegion(map, x, y, z, d1p, typeIntern);
+    for (z = 0; z < depths; z++) {
+        for (y = 0; y < rows; y++) {
+            fprintf(fp, "z y x %d %d (%d - %d)\n", z, y, 0, cols - 1);
+            for (x = 0; x < cols; x++) {
+                G3d_getValueRegion(map, x, y, z, d1p, typeIntern);
 
-		if (typeIntern == FCELL_TYPE)
-		    fprintf(fp, "%.18f ", *f1p);
-		else
-		    fprintf(fp, "%.50f ", d1);
-	    }
-	    fprintf(fp, "\n");
-	}
+                if (typeIntern == FCELL_TYPE)
+                    fprintf(fp, "%.18f ", *f1p);
+                else
+                    fprintf(fp, "%.50f ", d1);
+            }
+            fprintf(fp, "\n");
+        }
+    }
 
     if (fp != stdout)
-	fclose(fp);
+        fclose(fp);
 }

Modified: grass/trunk/raster/r.to.rast3/main.c
===================================================================
--- grass/trunk/raster/r.to.rast3/main.c	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/raster/r.to.rast3/main.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -133,9 +133,13 @@
 		    }
 		    else {
 			dvalue = *(CELL *) ptr;
-		    }
+		    }           
+            /* Because we read raster rows from north to south, but the coordinate system
+             of the g3d cube read from south to north we need to adjust the
+             Cube coordinates row = rows - y - 1.
+             */
 		    if (G3d_putValue
-			(map, x, y, z, (char *)&dvalue, DCELL_TYPE) < 0)
+			(map, x, rows - y - 1, z, (char *)&dvalue, DCELL_TYPE) < 0)
 			fatal_error(map, fd, depths,
 				    "Error writing double data");
 		}
@@ -146,11 +150,14 @@
 		    else {
 			fvalue = *(FCELL *) ptr;
 		    }
+            /* Because we read raster rows from north to south, but the coordinate system
+             of the g3d cube read from south to north we need to adjust the
+             Cube coordinates row = rows - y - 1.
+             */
 		    if (G3d_putValue
-			(map, x, y, z, (char *)&fvalue, FCELL_TYPE) < 0)
+			(map, x, rows - y - 1, z,(char *)&fvalue, FCELL_TYPE) < 0)
 			fatal_error(map, fd, depths,
 				    "Error writing float data");
-
 		}
 		else if (globalRastMapType == DCELL_TYPE) {
 		    if (Rast_is_null_value(ptr, globalRastMapType)) {
@@ -159,8 +166,12 @@
 		    else {
 			dvalue = *(DCELL *) ptr;
 		    }
+            /* Because we read raster rows from north to south, but the coordinate system
+             of the g3d cube read from south to north we need to adjust the
+             Cube coordinates row = rows - y - 1.
+             */
 		    if (G3d_putValue
-			(map, x, y, z, (char *)&dvalue, DCELL_TYPE) < 0)
+			(map, x, rows - y - 1, z,(char *)&dvalue, DCELL_TYPE) < 0)
 			fatal_error(map, fd, depths,
 				    "Error writing double data");
 

Modified: grass/trunk/raster/r.to.rast3elev/main.c
===================================================================
--- grass/trunk/raster/r.to.rast3elev/main.c	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/raster/r.to.rast3elev/main.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -226,7 +226,11 @@
 	    "elev_raster_to_g3d: Writing 3D raster map with depths %i rows %i cols %i and count %i.",
 	    depths, rows, cols, db.count);
 
-    /*The mainloop */
+    /*The mainloop */        
+    /* Because we read raster rows from north to south, but the coordinate system
+     of the g3d cube read from south to north we need to adjust the
+     Cube coordinates row = rows - y - 1.
+     */
     for (y = 0; y < rows; y++) {
 	G_percent(y, rows - 1, 10);
 
@@ -278,7 +282,7 @@
 			value = null;
 
 		    /*Write the value to the 3D map */
-		    if (G3d_putDouble(db.map, x, y, z, value) < 0)
+		    if (G3d_putDouble(db.map, x, rows - y - 1, z, value) < 0)
 			fatal_error(db, _("Error writing G3D double data"));
 		}
 	    }
@@ -292,7 +296,7 @@
 			else if (db.useUpperVal == 2)
 			    value = db.upper;
 			else
-			    value = G3d_getDouble(db.map, x, y, z);
+			    value = G3d_getDouble(db.map, x, rows - y - 1, z);
 		    }
 		    /*lower cells */
 		    if (height > ((z + 1) * tbres + bottom)) {
@@ -301,7 +305,7 @@
 			else if (db.useLowerVal == 2)
 			    value = db.lower;
 			else
-			    value = G3d_getDouble(db.map, x, y, z);
+			    value = G3d_getDouble(db.map, x, rows - y - 1, z);
 		    }
 		    /*If exactly at the border, fill upper AND lower cell */
 		    if (height >= (z * tbres + bottom) &&
@@ -309,10 +313,10 @@
 			value = inval;
 		    /*If the elevation is null, set the G3D value null */
 		    if (G3d_isNullValueNum(&height, DCELL_TYPE))
-			value = G3d_getDouble(db.map, x, y, z);
+			value = G3d_getDouble(db.map, x, rows - y - 1, z);
 
 		    /*Write the value to the 3D map */
-		    if (G3d_putDouble(db.map, x, y, z, value) < 0)
+		    if (G3d_putDouble(db.map, x, rows - y - 1, z, value) < 0)
 			fatal_error(db, _("Error writing G3D double data"));
 
 		}
@@ -451,10 +455,8 @@
 	}
     }
 
-
     G_message(_("Creating 3D raster map"));
 
-
     /*For each elevation - input map couple */
     for (i = 0; i < db.mapnum; i++) {
 

Modified: grass/trunk/raster3d/r3.cross.rast/main.c
===================================================================
--- grass/trunk/raster3d/r3.cross.rast/main.c	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/raster3d/r3.cross.rast/main.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -190,15 +190,19 @@
 		for (z = 0; z < depths; z++) {	/*From the bottom to the top */
 		    if (elevation >= z * tbres + bottom && elevation <= (z + 1) * tbres + bottom) {	/*if at the border, choose the value from the top */
 			/*Read the value and put it in the output map row */
+            /* Because we read raster rows from north to south, but the coordinate system
+             of the g3d cube read from south to north we need to adjust the
+             Cube coordinates row = rows - y - 1.
+             */
 			if (typeIntern == FCELL_TYPE) {
-			    G3d_getValue(map, x, y, z, &f1, typeIntern);
+			    G3d_getValue(map, x, rows - y - 1, z, &f1, typeIntern);
 			    if (G3d_isNullValueNum(&f1, FCELL_TYPE))
 				Rast_set_null_value(&fcell[x], 1, FCELL_TYPE);
 			    else
 				fcell[x] = (FCELL) f1;
 			}
 			else {
-			    G3d_getValue(map, x, y, z, &d1, typeIntern);
+			    G3d_getValue(map, x, rows - y - 1, z, &d1, typeIntern);
 			    if (G3d_isNullValueNum(&d1, DCELL_TYPE))
 				Rast_set_null_value(&dcell[x], 1, DCELL_TYPE);
 			    else

Modified: grass/trunk/raster3d/r3.in.ascii/r3.in.ascii.html
===================================================================
--- grass/trunk/raster3d/r3.in.ascii/r3.in.ascii.html	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/raster3d/r3.in.ascii/r3.in.ascii.html	2011-06-15 14:13:34 UTC (rev 46716)
@@ -35,6 +35,8 @@
 <H2>NOTES</H2>
 The format for the ASCII file:
 <PRE>
+version: <i>"grass7"</i>
+order:   <i>"nsbt" or "nstb" or "snbt" or "sntb"</i>
 north: <EM>floating point</EM>
 south: <EM>floating point</EM>
 east: <EM>floating point</EM>

Modified: grass/trunk/raster3d/r3.out.ascii/main.c
===================================================================
--- grass/trunk/raster3d/r3.out.ascii/main.c	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/raster3d/r3.out.ascii/main.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -1,21 +1,21 @@
 
 /****************************************************************************
-*
-* MODULE:       r3.out.ascii 
-*   	    	
-* AUTHOR(S):    Original author 
-*		Mark Astley, Bill Brown
-* 		USA CERL started 4/4/96
-*
-* PURPOSE:      Converts a 3D raster map layer into an ASCII text file  
-*
-* COPYRIGHT:    (C) 2005 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
-*   	    	for details.
-*
-*****************************************************************************/
+ *
+ * MODULE:       r3.out.ascii 
+ *   	    	
+ * AUTHOR(S):    Original author 
+ *		Mark Astley, Bill Brown
+ * 		USA CERL started 4/4/96
+ *
+ * PURPOSE:      Converts a 3D raster map layer into an ASCII text file  
+ *
+ * COPYRIGHT:    (C) 2005 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
+ *   	    	for details.
+ *
+ *****************************************************************************/
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -26,36 +26,40 @@
 #define MAX(a,b) (a > b ? a : b)
 
 /* structs */
-typedef struct
-{
+typedef struct {
     struct Option *input, *output, *decimals, *null_val;
     struct Flag *header;
+    struct Flag *row;
+    struct Flag *depth;
+    struct Flag *grass6;
     struct Flag *mask;
 } paramType;
 
 /* protos */
-void fatalError(char *errorMsg);
-void setParams();
-void getParams(char **input, char **output, int *decim);
-void writeHeaderString(FILE * fp, char *valueString, double value);
-void writeHeaderString2(FILE * fp, char *valueString, int value);
-FILE *openAscii(char *asciiFile, G3D_Region region);
-void G3dToascii(FILE * fp, G3D_Region region, int decim);
+static void fatalError(char *errorMsg);
+static void setParams();
+static void getParams(char **input, char **output, int *decim);
+static void writeHeaderString(FILE * fp, char *valueString, double value);
+static void writeHeaderString2(FILE * fp, char *valueString, int value);
+static void writeHeaderString3(FILE * fp, char *valueString, const char* value);
+static FILE *openAscii(char *asciiFile, G3D_Region region);
+static void G3dToascii(FILE * fp, G3D_Region region, int decim);
 
 /* globals */
 void *map = NULL;
 paramType param;
 
 /*---------------------------------------------------------------------------*/
+
 /* Simple error handling routine, will eventually replace this with
  * G3D_fatalError.
  */
 void fatalError(char *errorMsg)
 {
     if (map != NULL) {
-	/* should unopen map here! */
-	if (!G3d_closeCell(map))
-	    fatalError(_("Error closing 3d raster map"));
+        /* should unopen map here! */
+        if (!G3d_closeCell(map))
+            fatalError(_("Error closing 3d raster map"));
 
     }
 
@@ -63,6 +67,7 @@
 }
 
 /*---------------------------------------------------------------------------*/
+
 /* Convenient way to set up the arguments we are expecting
  */
 void setParams()
@@ -102,12 +107,25 @@
     param.header->key = 'h';
     param.header->description = _("Suppress printing of header information");
 
+    param.row = G_define_flag();
+    param.row->key = 'r';
+    param.row->description = _("Switch the row order in output from north->south to south->north");
+
+    param.depth = G_define_flag();
+    param.depth->key = 'd';
+    param.depth->description = _("Switch the depth order in output from bottom->top to top->bottom");
+
+    param.grass6 = G_define_flag();
+    param.grass6->key = 'c';
+    param.grass6->description = _("Print grass6 compatible format. Flags -d and -r are ignored.");
+
     param.mask = G_define_flag();
     param.mask->key = 'm';
     param.mask->description = _("Use G3D mask (if exists) with input map");
 }
 
 /*---------------------------------------------------------------------------*/
+
 /* Set up the input and output file names from the user's responses
  */
 void getParams(char **input, char **output, int *decim)
@@ -118,6 +136,7 @@
 }
 
 /*---------------------------------------------------------------------------*/
+
 /* This function is used to write parts of the header for the output
  * ASCII file.
  */
@@ -127,7 +146,7 @@
 
     sprintf(format, "%s %%lf\n", valueString);
     if (fprintf(fp, format, value) < 0)
-	fatalError("writeHeaderString: header value invalid");
+        fatalError("writeHeaderString: header value invalid");
 }
 
 /*---------------------------------------------------------------------------*/
@@ -137,10 +156,22 @@
 
     sprintf(format, "%s %%d\n", valueString);
     if (fprintf(fp, format, value) < 0)
-	fatalError("writeHeaderString: header value invalid");
+        fatalError("writeHeaderString: header value invalid");
 }
 
 /*---------------------------------------------------------------------------*/
+void writeHeaderString3(FILE * fp, char *valueString, const char* value)
+{
+    static char format[100];
+
+    sprintf(format, "%s %%s\n", valueString);
+    if (fprintf(fp, format, value) < 0)
+        fatalError("writeHeaderString: header value invalid");
+}
+
+
+/*---------------------------------------------------------------------------*/
+
 /* Opens the output acsii file and writes the header.
  * Returns the file handle for the output file.
  */
@@ -149,26 +180,39 @@
     FILE *fp;
 
     if (asciiFile) {
-	fp = fopen(asciiFile, "w");
-	if (fp == NULL) {
-	    perror(asciiFile);
-	    G_usage();
-	    exit(EXIT_FAILURE);
-	}
-    }
-    else
-	fp = stdout;
+        fp = fopen(asciiFile, "w");
+        if (fp == NULL) {
+            perror(asciiFile);
+            G_usage();
+            exit(EXIT_FAILURE);
+        }
+    } else
+        fp = stdout;
 
     if (!param.header->answer) {
-	writeHeaderString(fp, "north:", region.north);
-	writeHeaderString(fp, "south:", region.south);
-	writeHeaderString(fp, "east:", region.east);
-	writeHeaderString(fp, "west:", region.west);
-	writeHeaderString(fp, "top:", region.top);
-	writeHeaderString(fp, "bottom:", region.bottom);
-	writeHeaderString2(fp, "rows:", region.rows);
-	writeHeaderString2(fp, "cols:", region.cols);
-	writeHeaderString2(fp, "levels:", region.depths);
+        /* Do not print the new header in grass compatibility mode */
+        if (!param.grass6->answer) {
+            writeHeaderString3(fp, "version:", "grass7");
+
+            if (!param.depth->answer && !param.row->answer)
+                writeHeaderString3(fp, "order:", "nsbt");
+            else if (param.depth->answer && !param.row->answer)
+                writeHeaderString3(fp, "order:", "nstb");
+            else if (!param.depth->answer && param.row->answer)
+                writeHeaderString3(fp, "order:", "snbt");
+            else if (param.depth->answer && param.row->answer)
+                writeHeaderString3(fp, "order:", "sntb");
+        }
+
+        writeHeaderString(fp, "north:", region.north);
+        writeHeaderString(fp, "south:", region.south);
+        writeHeaderString(fp, "east:", region.east);
+        writeHeaderString(fp, "west:", region.west);
+        writeHeaderString(fp, "top:", region.top);
+        writeHeaderString(fp, "bottom:", region.bottom);
+        writeHeaderString2(fp, "rows:", region.rows);
+        writeHeaderString2(fp, "cols:", region.cols);
+        writeHeaderString2(fp, "levels:", region.depths);
     }
 
     return fp;
@@ -177,7 +221,8 @@
 /*---------------------------------------------------------------------------*/
 /* This function does all the work.  Basically, we just output the
  * source G3d file one layer at a time.
-				     *//* * */
+ *//* * */
+
 void G3dToascii(FILE * fp, G3D_Region region, int decim)
 {
     double d1 = 0;
@@ -185,47 +230,59 @@
     float *f1p;
     int x, y, z;
     int rows, cols, depths, typeIntern;
+    int col, row, depth;
 
-     /*AV*/
-	/* BEGIN OF ORIGINAL CODE */
-	/*
-	   G3d_getCoordsMap (map, &rows, &cols, &depths);
-	 */
-	/* END OF ORIGINAL CODE */
-	 /*AV*/
-	/* BEGIN OF MY CODE */
-	rows = region.rows;
+    rows = region.rows;
     cols = region.cols;
     depths = region.depths;
-    /* END OF MY CODE */
 
     typeIntern = G3d_tileTypeMap(map);
 
     d1p = &d1;
-    f1p = (float *)&d1;
+    f1p = (float *) &d1;
 
     for (z = 0; z < depths; z++)
-	for (y = rows - 1; y >= 0; y--) {	/* north to south */
-	    for (x = 0; x < cols; x++) {
-		G3d_getValue(map, x, y, z, d1p, typeIntern);
-		if (typeIntern == FCELL_TYPE) {
-		    if (G3d_isNullValueNum(f1p, FCELL_TYPE))
-			fprintf(fp, "%s ", param.null_val->answer);
-		    else
-			fprintf(fp, "%.*f ", decim, *f1p);
-		}
-		else {
-		    if (G3d_isNullValueNum(&d1, DCELL_TYPE))
-			fprintf(fp, "%s ", param.null_val->answer);
-		    else
-			fprintf(fp, "%.*lf ", decim, d1);
-		}
-	    }
-	    fprintf(fp, "\n");
-	}
+        for (y = 0; y < rows; y++) { /* rows count from south to north */
+            for (x = 0; x < cols; x++) {
+
+                /* From west to east */
+                col = x;
+                /* The default is to write rows from north to south
+                   to be r.in.ascii compatible 
+                 */
+                row = rows - y - 1;
+                /* From bottom to the top */
+                depth = z;
+
+                /* Write rows from south to north */
+                if (param.row->answer)
+                    row = y;
+
+                /* write XY layer from top to bottom */
+                if (param.depth->answer)
+                    depth = depths - z - 1;
+
+                /* Get the data and resample if nessessary */
+                G3d_getValue(map, col, row, depth, d1p, typeIntern);
+
+                if (typeIntern == FCELL_TYPE) {
+                    if (G3d_isNullValueNum(f1p, FCELL_TYPE))
+                        fprintf(fp, "%s ", param.null_val->answer);
+                    else
+                        fprintf(fp, "%.*f ", decim, *f1p);
+                } else {
+                    if (G3d_isNullValueNum(&d1, DCELL_TYPE))
+                        fprintf(fp, "%s ", param.null_val->answer);
+                    else
+                        fprintf(fp, "%.*lf ", decim, d1);
+                }
+            }
+            fprintf(fp, "\n");
+        }
 }
 
 /*---------------------------------------------------------------------------*/
+
 /* Main function: open the input and output files, then call
  * G3dtoascii.
  */
@@ -245,46 +302,51 @@
     G_add_keyword(_("voxel"));
     G_add_keyword(_("export"));
     module->description =
-	_("Converts a 3D raster map layer into a ASCII text file.");
+        _("Converts a 3D raster map layer into a ASCII text file.");
 
     /* Get parameters from user */
     setParams();
 
     /* Have GRASS get inputs */
     if (G_parser(argc, argv))
-	exit(EXIT_FAILURE);
+        exit(EXIT_FAILURE);
 
     /* Parse input parameters */
     getParams(&input, &output, &decim);
 
+    if (param.grass6->answer) {
+        param.depth->answer = 0;
+        param.row->answer = 0;
+    }
+
     if (NULL == G_find_grid3(input, ""))
-	G3d_fatalError(_("Requested 3d raster map not found"));
+        G3d_fatalError(_("Requested 3d raster map not found"));
 
-    /*  map = G3d_openCellOld(input, G_find_grid3(input, ""), G3D_DEFAULT_WINDOW,
-       G3D_TILE_SAME_AS_FILE,
-       G3D_NO_CACHE); */
-    /* using cache mode due to bug */
-    map = G3d_openCellOld(input, G_find_grid3(input, ""), G3D_DEFAULT_WINDOW,
-			  G3D_TILE_SAME_AS_FILE, G3D_USE_CACHE_DEFAULT);
-    if (map == NULL)
-	G3d_fatalError(_("Error opening 3d raster map"));
+    /* Initiate the default settings */
+    G3d_initDefaults();
 
-    /* Figure out the region from the map */
-    /*  G3d_getRegionStructMap(map, &region); */
+    /* Figure out the current region settings */
     G3d_getWindow(&region);
 
+    /* Open the map and use XY cache mode */
+    map = G3d_openCellOld(input, G_find_grid3(input, ""), &region,
+                          G3D_TILE_SAME_AS_FILE, G3D_USE_CACHE_XY);
+
+    if (map == NULL)
+        G3d_fatalError(_("Error opening 3d raster map"));
+
     /* Open the output ascii file */
     fp = openAscii(output, region);
 
     /*if requested set the Mask on */
     if (param.mask->answer) {
-	if (G3d_maskFileExists()) {
-	    changemask = 0;
-	    if (G3d_maskIsOff(map)) {
-		G3d_maskOn(map);
-		changemask = 1;
-	    }
-	}
+        if (G3d_maskFileExists()) {
+            changemask = 0;
+            if (G3d_maskIsOff(map)) {
+                G3d_maskOn(map);
+                changemask = 1;
+            }
+        }
     }
 
     /* Now barf out the contents of the map in ascii form */
@@ -292,19 +354,18 @@
 
     /*We set the Mask off, if it was off bevor */
     if (param.mask->answer) {
-	if (G3d_maskFileExists())
-	    if (G3d_maskIsOn(map) && changemask)
-		G3d_maskOff(map);
+        if (G3d_maskFileExists())
+            if (G3d_maskIsOn(map) && changemask)
+                G3d_maskOff(map);
     }
 
     /* Close files and exit */
     if (!G3d_closeCell(map))
-	fatalError(_("Error closing 3d raster map"));
+        fatalError(_("Error closing 3d raster map"));
 
-    map = NULL;
     if (output)
-	if (fclose(fp))
-	    fatalError(_("Error closing new ASCII file"));
+        if (fclose(fp))
+            fatalError(_("Error closing new ASCII file"));
 
     return 0;
 }

Modified: grass/trunk/raster3d/r3.out.ascii/r3.out.ascii.html
===================================================================
--- grass/trunk/raster3d/r3.out.ascii/r3.out.ascii.html	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/raster3d/r3.out.ascii/r3.out.ascii.html	2011-06-15 14:13:34 UTC (rev 46716)
@@ -5,7 +5,8 @@
 an ascii file which will be written in the current working directory.
 If <I>output</I> is not specified then <B>stdout</B> is used.  The
 <I>-h</I> flag may be used to suppress header information. The module is
-sensitive to region settings (set with g.region).
+sensitive to region settings (set with g.region). The <em>-c</em> will create grass6
+r3.in.ascii compatible output.
 
 
 <H2>NOTES</H2>
@@ -17,6 +18,8 @@
 <P>
 The format for the ascii file is:
 <pre>
+version: <i>"grass7"</i>
+order:   <i>"nsbt" or "nstb" or "snbt" or "sntb"</i>
 north:   <i>floating point</i>
 south:   <i>floating point</i>
 east:    <i>floating point</i>
@@ -28,6 +31,26 @@
 levels:  <i>integer</i>
 </pre>
 
+Ther <B>version</B> and <B>order</B> options are introduced in grass7 June 2011.
+The <B>order</B> option describes the order of rows and depths in the output. 
+It is possible to create output of different row order using the <em>-r</em> flag 
+and output of different depths order using the  <em>-d</em> flag. The default order is:
+<pre>
+west -> east for columns
+north -> south for rows
+bottom -> top for depths
+</pre>
+
+This order is compatible with the r.in.ascii row -> column ordering. The column 
+order can not be changed but the row and depth order. Supported orders are:
+
+<ul>
+    <li><b>nsbt</b>:   north -> south and bottom -> top ordering which is the default (no flags)</li>
+    <li><b>snbt</b>:   south -> north and bottom -> top ordering using <em>-r</em> flag</li>
+    <li><b>nstb</b>:   north -> south and top -> bottom ordering using <em>-d</em> flag</li>
+    <li><b>nstb</b>:   south -> north and top -> bottom ordering using <em>-rd</em> flag</li>
+</ul>
+
 The header is followed by cell values in <EM>floating point</EM> format.
 Cell values are output as a series of horizontal slices in row-major
 order.  That is, 
@@ -42,8 +65,8 @@
 
 
 <P>
-One level maps can be imported with r.in.ascii (Raster 2D) after removing
-the header lines "top", "bottom" and "levels".
+One level maps can be imported with r.in.ascii (Raster 2D) using the default <B>nsbt</B> order
+and removing the header lines "version", "order", "top", "bottom" and "levels".
 
 <H2>SEE ALSO</H2>
 <EM><A HREF="r3.in.ascii.html">r3.in.ascii</A></EM><br>

Modified: grass/trunk/raster3d/r3.out.vtk/writeVTKData.c
===================================================================
--- grass/trunk/raster3d/r3.out.vtk/writeVTKData.c	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/raster3d/r3.out.vtk/writeVTKData.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -340,7 +340,7 @@
     status = 0;
 
     for (z = 0; z < depths; z++) {
-	if (param.structgrid->answer) {
+	if (!param.structgrid->answer) {
 	    for (y = 0; y < rows; y++) {
 		G_percent(status, (rows * depths - 1), 10);
 		status++;
@@ -403,7 +403,7 @@
     fprintf(fp, "COLOR_SCALARS %s 3\n", varname);
 
     for (z = 0; z < depths; z++) {	/*From the bottom to the top */
-	if (param.structgrid->answer) {
+	if (!param.structgrid->answer) {
 	    for (y = 0; y < rows; y++) {
 		G_percent(status, (rows * depths - 1), 10);
 		status++;
@@ -503,7 +503,7 @@
     fprintf(fp, "VECTORS %s float\n", varname);
 
     for (z = 0; z < depths; z++) {	/*From the bottom to the top */
-	if (param.structgrid->answer) {
+	if (!param.structgrid->answer) {
 	    for (y = 0; y < rows; y++) {
 		G_percent(status, (rows * depths - 1), 10);
 		status++;

Modified: grass/trunk/raster3d/r3.to.rast/main.c
===================================================================
--- grass/trunk/raster3d/r3.to.rast/main.c	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/raster3d/r3.to.rast/main.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -130,16 +130,20 @@
 	for (y = 0; y < rows; y++) {
 	    G_percent(y, rows - 1, 10);
 
-	    for (x = 0; x < cols; x++) {
+	    for (x = 0; x < cols; x++) {            
+        /* Because we write raster rows from north to south, but the coordinate system
+         of the g3d cube read from south to north we need to adjust the
+         Cube coordinates row = rows - y - 1.
+         */
 		if (typeIntern == FCELL_TYPE) {
-		    G3d_getValue(map, x, y, z, &f1, typeIntern);
+		    G3d_getValue(map, x, rows - y - 1, z, &f1, typeIntern);
 		    if (G3d_isNullValueNum(&f1, FCELL_TYPE))
 			Rast_set_null_value(&fcell[x], 1, FCELL_TYPE);
 		    else
 			fcell[x] = (FCELL) f1;
 		}
 		else {
-		    G3d_getValue(map, x, y, z, &d1, typeIntern);
+		    G3d_getValue(map, x, rows - y - 1, z, &d1, typeIntern);
 		    if (G3d_isNullValueNum(&d1, DCELL_TYPE))
 			Rast_set_null_value(&dcell[x], 1, DCELL_TYPE);
 		    else
@@ -199,6 +203,7 @@
 
     module = G_define_module();
     G_add_keyword(_("raster3d"));
+    G_add_keyword(_("raster"));
     G_add_keyword(_("voxel"));
     module->description = _("Converts 3D raster maps to 2D raster maps");
 
@@ -221,7 +226,7 @@
     /*Set the resolution of the output maps */
     if (param.res->answer) {
 
-	/*Open the map with default region */
+	/*Open the map with current region */
 	map = G3d_openCellOld(param.input->answer,
 			      G_find_grid3(param.input->answer, ""),
 			      G3D_DEFAULT_WINDOW, G3D_TILE_SAME_AS_FILE,

Modified: grass/trunk/vector/v.random/main.c
===================================================================
--- grass/trunk/vector/v.random/main.c	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/vector/v.random/main.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -115,7 +115,7 @@
     parm.zcol = G_define_standard_option(G_OPT_DB_COLUMN);
     parm.zcol->label = _("Name of column for z values");
     parm.zcol->description =
-	_("Writes z values to column instead of 3D vector");
+	_("Writes z values to column");
     parm.zcol->guisection = _("3D output");
 
     parm.ztype = G_define_option();
@@ -144,10 +144,6 @@
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
-    if (flag.z->answer && parm.zcol->answer) {
-	G_fatal_error(_("Unable to create 3D vector and attribute table at same time"));
-    }
-
     output = parm.output->answer;
     n = atoi(parm.nsites->answer);
     b = (flag.drand48->answer == '\0') ? 0 : 1;
@@ -266,17 +262,14 @@
 
 	x = rng() / max * (window.west - window.east) + window.east;
 	y = rng() / max * (window.north - window.south) + window.south;
-
-	if (flag.z->answer) {
-	    z = rng() / max * (zmax - zmin) + zmin;
+	z = rng() / max * (zmax - zmin) + zmin;
+        
+	if (flag.z->answer)
 	    Vect_append_point(Points, x, y, z);
-	}
 	else
 	    Vect_append_point(Points, x, y, 0.0);
 
 	if (parm.zcol->answer) {
-	    z = rng() / max * (zmax - zmin) + zmin;
-
 	    sprintf(buf, "insert into %s values ( %d, ", Fi->table, i + 1);
 	    db_set_string(&sql, buf);
 	    /* Round random value if column is integer type */

Modified: grass/trunk/vector/v.to.rast3/main.c
===================================================================
--- grass/trunk/vector/v.to.rast3/main.c	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/vector/v.to.rast3/main.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -63,9 +63,10 @@
     
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
+    
+    G3d_initDefaults();
 
     G3d_getWindow(&region);
-    G3d_readWindow(&region, NULL);
 
     Vect_set_open_level(2);
     Vect_open_old2(&Map, in_opt->answer, "", field_opt->answer);
@@ -96,8 +97,8 @@
 
     db_close_database_shutdown_driver(Driver);
 
-    map = G3d_openCellNew(out_opt->answer, FCELL_TYPE,
-			  G3D_USE_CACHE_DEFAULT, &region);
+    map = G3d_openNewOptTileSize(out_opt->answer,
+			  G3D_USE_CACHE_DEFAULT, &region, FCELL_TYPE, 32);
 
     if (map == NULL)
 	G_fatal_error(_("Unable to create output map"));
@@ -120,19 +121,13 @@
 	if (cat < 0) {
 	    continue;
 	}
-
-	if (Points->x[0] < region.west || Points->x[0] > region.east
-	    || Points->y[0] < region.south || Points->y[0] > region.north
-	    || Points->z[0] < region.bottom || Points->z[0] > region.top) {
+    /* Check if the coordinates are located in the cube */
+	if (!G3d_isValidLocation(map, Points->y[0], Points->x[0], Points->z[0])) {
 	    continue;
 	}
+    /* Convert the north, east and top coorindate into row, col and depth*/
+    G3d_location2coord2(map, Points->y[0], Points->x[0], Points->z[0], &col, &row, &depth);
 
-	/*Because the g3d lib is row oriented and counts therefore from north to south,
-	 * we have to do the same here*/
-	row = (int)floor((region.north - Points->y[0]) / region.ns_res);
-	col = (int)floor((Points->x[0] - region.west) / region.ew_res);
-	depth = (int)floor((Points->z[0] - region.bottom) / region.tb_res);
-
 	if (ctype == DB_C_TYPE_INT) {
 	    int ivalue;
 

Modified: grass/trunk/vector/v.to.rast3/v.to.rast3.html
===================================================================
--- grass/trunk/vector/v.to.rast3/v.to.rast3.html	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/vector/v.to.rast3/v.to.rast3.html	2011-06-15 14:13:34 UTC (rev 46716)
@@ -1,13 +1,14 @@
 <H2>DESCRIPTION</H2>
 
-<EM>v.to.rast3</EM> converts a GRASS 3D vector point map to a GRASS 3dgrid raster
-volume (voxel) map.
+<EM>v.to.rast3</EM> converts a GRASS 3D vector point map to a GRASS 3dgrid raster3d
+ (voxel) map.
 
 <H2>NOTES</H2>
 
-When converting from a 3D vector point layer to a 3dcell volume
-a vector point is converted into a single cube
-representing the location of the vector point.
+When converting from a 3D vector point layer to a raster3d map
+a vector point is converted into a single 3d cell
+representing the location of the vector point. As 3d cell value the database 
+attribute of the vector point is stored as float.
 
 <H2>SEE ALSO</H2>
 

Modified: grass/trunk/vector/v.vol.rst/dataoct.h
===================================================================
--- grass/trunk/vector/v.vol.rst/dataoct.h	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/vector/v.vol.rst/dataoct.h	2011-06-15 14:13:34 UTC (rev 46716)
@@ -36,8 +36,8 @@
 /* defined in oct.h */
 struct octtree;
 
-struct quadruple *point_new();
-struct octdata *data_new();
+struct quadruple *point_new(double, double, double, double, double);
+struct octdata *data_new(double, double, double, int, int, int, int);
 int oct_compare();
 int oct_add_data();
 int oct_division_check();

Modified: grass/trunk/vector/v.vol.rst/main.c
===================================================================
--- grass/trunk/vector/v.vol.rst/main.c	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/vector/v.vol.rst/main.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -203,8 +203,8 @@
     int LINEQS();
     int CRSTG();
     double CRST();
-    double amax1();
-    double amin1();
+    double amax1(double, double);
+    double amin1(double, double);
     int max1();
     int min1();
     int per, npmin;

Modified: grass/trunk/vector/v.vol.rst/user.h
===================================================================
--- grass/trunk/vector/v.vol.rst/user.h	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/vector/v.vol.rst/user.h	2011-06-15 14:13:34 UTC (rev 46716)
@@ -18,7 +18,7 @@
 int COGRR1();
 int POINT();
 int LINEQS();
-void clean_fatal_error();
+void clean_fatal_error(const char*);
 int point_save();
 
 #endif

Modified: grass/trunk/vector/v.vol.rst/user1.c
===================================================================
--- grass/trunk/vector/v.vol.rst/user1.c	2011-06-15 09:55:32 UTC (rev 46715)
+++ grass/trunk/vector/v.vol.rst/user1.c	2011-06-15 14:13:34 UTC (rev 46716)
@@ -423,8 +423,8 @@
   /*** Write elevation results ***/
     if (outz != NULL) {
 
-	cf1 = G3d_openCellNew(outz, FCELL_TYPE,
-			      G3D_USE_CACHE_DEFAULT, &current_region);
+	cf1 = G3d_openNewOptTileSize(outz, 
+			      G3D_USE_CACHE_DEFAULT, &current_region, FCELL_TYPE, 32);
 	if (cf1 == NULL) {
 	    sprintf(buff, "Can't open %s for writing ", outz);
 	    clean_fatal_error(buff);
@@ -442,7 +442,7 @@
 	cnt = 0;
 	for (iarc = 0; iarc < nsizl; iarc++) {
 
-	    for (y = nsizr - 1; y >= 0; y--) {	/* changed by AV */
+	    for (y = 0; y < nsizr; y++) {	
 		for (x = 0; x < nsizc; x++) {
 		    if (maskmap != NULL)
 			bmask = BM_get(bitmask, x, y);
@@ -474,8 +474,8 @@
   /*** Write out the gradient results ***/
     if (gradient != NULL) {
 
-	cf2 = G3d_openCellNew(gradient, FCELL_TYPE,
-			      G3D_USE_CACHE_DEFAULT, &current_region);
+	cf2 = G3d_openNewOptTileSize(gradient, 
+			      G3D_USE_CACHE_DEFAULT, &current_region, FCELL_TYPE, 32);
 	if (cf2 == NULL) {
 	    sprintf(buff, "Can't open %s for writing ", gradient);
 	    clean_fatal_error(buff);
@@ -493,7 +493,7 @@
 	cnt = 0;
 	for (iarc = 0; iarc < nsizl; iarc++) {
 
-	    for (y = nsizr - 1; y >= 0; y--) {	/* changed by AV */
+	    for (y = 0; y < nsizr; y++) {	
 		for (x = 0; x < nsizc; x++) {
 		    if (maskmap != NULL)
 			bmask = BM_get(bitmask, x, y);
@@ -525,8 +525,8 @@
   /*** Write out aspect1 results ***/
     if (aspect1 != NULL) {
 
-	cf3 = G3d_openCellNew(aspect1, FCELL_TYPE,
-			      G3D_USE_CACHE_DEFAULT, &current_region);
+	cf3 = G3d_openNewOptTileSize(aspect1, 
+			      G3D_USE_CACHE_DEFAULT, &current_region, FCELL_TYPE, 32);
 	if (cf3 == NULL) {
 	    sprintf(buff, "Can't open %s for writing ", aspect1);
 	    clean_fatal_error(buff);
@@ -544,7 +544,7 @@
 	cnt = 0;
 	for (iarc = 0; iarc < nsizl; iarc++) {
 
-	    for (y = nsizr - 1; y >= 0; y--) {	/* changed by AV */
+	    for (y = 0; y < nsizr; y++) {	
 		for (x = 0; x < nsizc; x++) {
 		    if (maskmap != NULL)
 			bmask = BM_get(bitmask, x, y);
@@ -576,8 +576,8 @@
   /*** Write out aspect2 results ***/
     if (aspect2 != NULL) {
 
-	cf4 = G3d_openCellNew(aspect2, FCELL_TYPE,
-			      G3D_USE_CACHE_DEFAULT, &current_region);
+	cf4 = G3d_openNewOptTileSize(aspect2, 
+			      G3D_USE_CACHE_DEFAULT, &current_region, FCELL_TYPE, 32);
 	if (cf4 == NULL) {
 	    sprintf(buff, "Can't open %s for writing ", aspect2);
 	    clean_fatal_error(buff);
@@ -595,7 +595,7 @@
 	cnt = 0;
 	for (iarc = 0; iarc < nsizl; iarc++) {
 
-	    for (y = nsizr - 1; y >= 0; y--) {	/* changed by AV */
+	    for (y = 0; y < nsizr; y++) {	
 		for (x = 0; x < nsizc; x++) {
 		    if (maskmap != NULL)
 			bmask = BM_get(bitmask, x, y);
@@ -627,8 +627,8 @@
   /*** Write out ncurv results ***/
     if (ncurv != NULL) {
 
-	cf5 = G3d_openCellNew(ncurv, FCELL_TYPE,
-			      G3D_USE_CACHE_DEFAULT, &current_region);
+	cf5 = G3d_openNewOptTileSize(ncurv, 
+			      G3D_USE_CACHE_DEFAULT, &current_region, FCELL_TYPE, 32);
 	if (cf5 == NULL) {
 	    sprintf(buff, "Can't open %s for writing ", ncurv);
 	    clean_fatal_error(buff);
@@ -646,7 +646,7 @@
 	cnt = 0;
 	for (iarc = 0; iarc < nsizl; iarc++) {
 
-	    for (y = nsizr - 1; y >= 0; y--) {	/* changed by AV */
+	    for (y = 0; y < nsizr; y++) {	
 		for (x = 0; x < nsizc; x++) {
 		    if (maskmap != NULL)
 			bmask = BM_get(bitmask, x, y);
@@ -678,8 +678,8 @@
   /*** Write out gcurv results ***/
     if (gcurv != NULL) {
 
-	cf6 = G3d_openCellNew(gcurv, FCELL_TYPE,
-			      G3D_USE_CACHE_DEFAULT, &current_region);
+	cf6 = G3d_openNewOptTileSize(gcurv, 
+			      G3D_USE_CACHE_DEFAULT, &current_region, FCELL_TYPE, 32);
 	if (cf6 == NULL) {
 	    sprintf(buff, "Can't open %s for writing ", gcurv);
 	    clean_fatal_error(buff);
@@ -697,7 +697,7 @@
 	cnt = 0;
 	for (iarc = 0; iarc < nsizl; iarc++) {
 
-	    for (y = nsizr - 1; y >= 0; y--) {	/* changed by AV */
+	    for (y = 0; y < nsizr; y++) {	
 		for (x = 0; x < nsizc; x++) {
 		    if (maskmap != NULL)
 			bmask = BM_get(bitmask, x, y);
@@ -729,8 +729,8 @@
   /*** Write mcurv results ***/
     if (mcurv != NULL) {
 
-	cf7 = G3d_openCellNew(mcurv, FCELL_TYPE,
-			      G3D_USE_CACHE_DEFAULT, &current_region);
+	cf7 = G3d_openNewOptTileSize(mcurv,
+			      G3D_USE_CACHE_DEFAULT, &current_region, FCELL_TYPE, 32);
 	if (cf7 == NULL) {
 	    sprintf(buff, "Can't open %s for writing ", mcurv);
 	    clean_fatal_error(buff);
@@ -748,7 +748,7 @@
 	cnt = 0;
 	for (iarc = 0; iarc < nsizl; iarc++) {
 
-	    for (y = nsizr - 1; y >= 0; y--) {	/* changed by AV */
+	    for (y = 0; y < nsizr; y++) {	
 		for (x = 0; x < nsizc; x++) {
 		    if (maskmap != NULL)
 			bmask = BM_get(bitmask, x, y);



More information about the grass-commit mailing list