GRASS Programmer's Manual  6.4.2(2012)
getblock.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <sys/types.h>
00004 #include <unistd.h>
00005 #include "G3d_intern.h"
00006 
00007 /*---------------------------------------------------------------------------*/
00008 
00009 void
00010 G3d_getBlockNocache(G3D_Map * map, int x0, int y0, int z0, int nx, int ny,
00011                     int nz, void *block, int type)
00012 {
00013     void *tile;
00014     int tileX0, tileY0, tileZ0, tileOffsX0, tileOffsY0, tileOffsZ0;
00015     int tileX1, tileY1, tileZ1, tileOffsX1, tileOffsY1, tileOffsZ1;
00016     int tx, ty, tz, dx, dy, dz, x, y, z, rows, cols, depths;
00017     int tileIndex;
00018 
00019     if (!map->useCache)
00020         tile = G3d_allocTilesType(map, 1, type);
00021     if (tile == NULL)
00022         G3d_fatalError("G3d_getBlockNocache: error in G3d_allocTiles");
00023 
00024     G3d_coord2tileCoord(map, x0, y0, z0, &tileX0, &tileY0, &tileZ0,
00025                         &tileOffsX0, &tileOffsY0, &tileOffsZ0);
00026     G3d_coord2tileCoord(map, x0 + nx - 1, y0 + ny - 1, z0 + nz - 1,
00027                         &tileX1, &tileY1, &tileZ1,
00028                         &tileOffsX1, &tileOffsY1, &tileOffsZ1);
00029 
00030     for (tz = tileZ0; tz <= tileZ1; tz++) {
00031         dz = (tz - tileZ0) * map->tileZ - tileOffsZ0;
00032         for (ty = tileY0; ty <= tileY1; ty++) {
00033             dy = (ty - tileY0) * map->tileY - tileOffsY0;
00034             for (tx = tileX0; tx <= tileX1; tx++) {
00035                 dx = (tx - tileX0) * map->tileX - tileOffsX0;
00036 
00037                 tileIndex = G3d_tile2tileIndex(map, tx, ty, tz);
00038 
00039                 if (G3d_tileIndexInRange(map, tileIndex))
00040                     if (map->useCache) {
00041                         tile = G3d_getTilePtr(map, tileIndex);
00042                         if (tile == NULL)
00043                             G3d_fatalError
00044                                 ("G3d_getBlockNocache: error in G3d_getTilePtr");
00045                     }
00046                     else {
00047                         if (!G3d_readTile
00048                             (map, tileIndex, tile, map->typeIntern))
00049                             G3d_fatalError
00050                                 ("G3d_getBlockNocache: error in G3d_readTile");
00051                     }
00052 
00053                 else
00054                     G3d_setNullTile(map, tile);
00055 
00056                 cols = (tx == tileX1 ? tileOffsX1 : map->tileX - 1);
00057                 rows = (ty == tileY1 ? tileOffsY1 : map->tileY - 1);
00058                 depths = (tz == tileZ1 ? tileOffsZ1 : map->tileZ - 1);
00059 
00060                 x = (tx == tileX0 ? tileOffsX0 : 0);
00061 
00062                 for (z = (tz == tileZ0 ? tileOffsZ0 : 0); z <= depths; z++)
00063                     for (y = (ty == tileY0 ? tileOffsY0 : 0); y <= rows; y++) {
00064                         G3d_copyValues(tile,
00065                                        z * map->tileXY + y * map->tileX + x,
00066                                        map->typeIntern,
00067                                        block,
00068                                        (z + dz) * nx * ny + (y + dy) * nx +
00069                                        (x + dx), type, cols - x + 1);
00070                     }
00071             }
00072         }
00073     }
00074 
00075     if (!map->useCache)
00076         G3d_freeTiles(tile);
00077 }
00078 
00079 /*---------------------------------------------------------------------------*/
00080 
00081 
00102 void
00103 G3d_getBlock(G3D_Map * map, int x0, int y0, int z0, int nx, int ny, int nz,
00104              void *block, int type)
00105 {
00106     int x, y, z, nNull, x1, y1, z1, length;
00107 
00108     if (!map->useCache) {
00109         G3d_getBlockNocache(map, x0, y0, z0, nx, ny, nz, block, type);
00110         return;
00111     }
00112 
00113     x1 = G3D_MIN(x0 + nx, map->region.cols);
00114     y1 = G3D_MIN(y0 + ny, map->region.rows);
00115     z1 = G3D_MIN(z0 + nz, map->region.depths);
00116 
00117     length = G3d_length(type);
00118 
00119     for (z = z0; z < z1; z++) {
00120         for (y = y0; y < y1; y++) {
00121             for (x = x0; x < x1; x++) {
00122                 G3d_getValueRegion(map, x, y, z, block, type);
00123                 block = G_incr_void_ptr(block, length);
00124             }
00125             nNull = x0 + nx - x;
00126             G3d_setNullValue(block, nNull, type);
00127             block = G_incr_void_ptr(block, length * nNull);
00128         }
00129         nNull = (y0 + ny - y) * nx;
00130         G3d_setNullValue(block, nNull, type);
00131         block = G_incr_void_ptr(block, length * nNull);
00132     }
00133     nNull = (z0 + nz - z) * ny * nx;
00134     G3d_setNullValue(block, nNull, type);
00135 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines