GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
getblock.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5 #include "G3d_intern.h"
6 
7 /*---------------------------------------------------------------------------*/
8 
9 void
10 G3d_getBlockNocache(G3D_Map * map, int x0, int y0, int z0, int nx, int ny,
11  int nz, void *block, int type)
12 {
13  void *tile;
14  int tileX0, tileY0, tileZ0, tileOffsX0, tileOffsY0, tileOffsZ0;
15  int tileX1, tileY1, tileZ1, tileOffsX1, tileOffsY1, tileOffsZ1;
16  int tx, ty, tz, dx, dy, dz, x, y, z, rows, cols, depths;
17  int tileIndex;
18 
19  if (!map->useCache)
20  tile = G3d_allocTilesType(map, 1, type);
21  if (tile == NULL)
22  G3d_fatalError("G3d_getBlockNocache: error in G3d_allocTiles");
23 
24  G3d_coord2tileCoord(map, x0, y0, z0, &tileX0, &tileY0, &tileZ0,
25  &tileOffsX0, &tileOffsY0, &tileOffsZ0);
26  G3d_coord2tileCoord(map, x0 + nx - 1, y0 + ny - 1, z0 + nz - 1,
27  &tileX1, &tileY1, &tileZ1,
28  &tileOffsX1, &tileOffsY1, &tileOffsZ1);
29 
30  for (tz = tileZ0; tz <= tileZ1; tz++) {
31  dz = (tz - tileZ0) * map->tileZ - tileOffsZ0;
32  for (ty = tileY0; ty <= tileY1; ty++) {
33  dy = (ty - tileY0) * map->tileY - tileOffsY0;
34  for (tx = tileX0; tx <= tileX1; tx++) {
35  dx = (tx - tileX0) * map->tileX - tileOffsX0;
36 
37  tileIndex = G3d_tile2tileIndex(map, tx, ty, tz);
38 
39  if (G3d_tileIndexInRange(map, tileIndex))
40  if (map->useCache) {
41  tile = G3d_getTilePtr(map, tileIndex);
42  if (tile == NULL)
44  ("G3d_getBlockNocache: error in G3d_getTilePtr");
45  }
46  else {
47  if (!G3d_readTile
48  (map, tileIndex, tile, map->typeIntern))
50  ("G3d_getBlockNocache: error in G3d_readTile");
51  }
52 
53  else
54  G3d_setNullTile(map, tile);
55 
56  cols = (tx == tileX1 ? tileOffsX1 : map->tileX - 1);
57  rows = (ty == tileY1 ? tileOffsY1 : map->tileY - 1);
58  depths = (tz == tileZ1 ? tileOffsZ1 : map->tileZ - 1);
59 
60  x = (tx == tileX0 ? tileOffsX0 : 0);
61 
62  for (z = (tz == tileZ0 ? tileOffsZ0 : 0); z <= depths; z++)
63  for (y = (ty == tileY0 ? tileOffsY0 : 0); y <= rows; y++) {
64  G3d_copyValues(tile,
65  z * map->tileXY + y * map->tileX + x,
66  map->typeIntern,
67  block,
68  (z + dz) * nx * ny + (y + dy) * nx +
69  (x + dx), type, cols - x + 1);
70  }
71  }
72  }
73  }
74 
75  if (!map->useCache)
76  G3d_freeTiles(tile);
77 }
78 
79 /*---------------------------------------------------------------------------*/
80 
81 
102 void
103 G3d_getBlock(G3D_Map * map, int x0, int y0, int z0, int nx, int ny, int nz,
104  void *block, int type)
105 {
106  int x, y, z, nNull, x1, y1, z1, length;
107 
108  if (!map->useCache) {
109  G3d_getBlockNocache(map, x0, y0, z0, nx, ny, nz, block, type);
110  return;
111  }
112 
113  x1 = G3D_MIN(x0 + nx, map->region.cols);
114  y1 = G3D_MIN(y0 + ny, map->region.rows);
115  z1 = G3D_MIN(z0 + nz, map->region.depths);
116 
117  length = G3d_length(type);
118 
119  for (z = z0; z < z1; z++) {
120  for (y = y0; y < y1; y++) {
121  for (x = x0; x < x1; x++) {
122  G3d_getValueRegion(map, x, y, z, block, type);
123  block = G_incr_void_ptr(block, length);
124  }
125  nNull = x0 + nx - x;
126  G3d_setNullValue(block, nNull, type);
127  block = G_incr_void_ptr(block, length * nNull);
128  }
129  nNull = (y0 + ny - y) * nx;
130  G3d_setNullValue(block, nNull, type);
131  block = G_incr_void_ptr(block, length * nNull);
132  }
133  nNull = (z0 + nz - z) * ny * nx;
134  G3d_setNullValue(block, nNull, type);
135 }
void G3d_getValueRegion(G3D_Map *map, int x, int y, int z, void *value, int type)
Returns in *value the cell-value of the cell with region-coordinate (x, y, z). The value returned is ...
Definition: tileio.c:244
void G3d_setNullValue(void *c, int nofElts, int type)
Fills the vector pointed to by c with nofElts NULL-values of type.
Definition: g3dnull.c:32
#define G3D_MIN(a, b)
Definition: G3d_intern.h:25
int y
Definition: plot.c:34
void * G_incr_void_ptr(const void *ptr, const size_t size)
Advance void pointer.
Definition: gis/raster.c:33
void G3d_copyValues(const void *src, int offsSrc, int typeSrc, void *dst, int offsDst, int typeDst, int nElts)
Definition: g3dmisc.c:52
void * G3d_getTilePtr(G3D_Map *map, int tileIndex)
This function returns a pointer to a tile which contains the data for the tile with index tileIndex...
Definition: tileio.c:71
void G3d_setNullTile(G3D_Map *map, void *tile)
Is equivalent to G3d_setNullTileType (map, tile, G3d_fileTypeMap (map)).
Definition: tilenull.c:41
void * G3d_allocTilesType(G3D_Map *map, int nofTiles, int type)
Allocates a vector of nofTiles tiles with the same dimensions as the tiles of map and large enough to...
Definition: tilealloc.c:26
int G3d_tileIndexInRange(G3D_Map *map, int tileIndex)
Returns 1 if tileIndex is a valid index for map. Returns 0 otherwise.
Definition: tilemath.c:215
return NULL
Definition: dbfopen.c:1394
tuple cols
void G3d_freeTiles(void *tiles)
Is equivalent to G3d_free (tiles);
Definition: tilealloc.c:77
int G3d_length(int t)
Definition: g3dmisc.c:77
for(cat=0;;cat++)
Definition: g3dcats.c:140
void G3d_getBlockNocache(G3D_Map *map, int x0, int y0, int z0, int nx, int ny, int nz, void *block, int type)
Definition: getblock.c:10
void G3d_coord2tileCoord(G3D_Map *map, int x, int y, int z, int *xTile, int *yTile, int *zTile, int *xOffs, int *yOffs, int *zOffs)
Converts cell-coordinates (x, y, z) into tile-coordinates (xTile, yTile, zTile) and the coordinate of...
Definition: tilemath.c:136
void G3d_getBlock(G3D_Map *map, int x0, int y0, int z0, int nx, int ny, int nz, void *block, int type)
Copies the cells contained in the block (cube) with vertices (x0, y0, z0) and (x0 + nx - 1...
Definition: getblock.c:103
int G3d_readTile(G3D_Map *map, int tileIndex, void *tile, int type)
Reads tile with index tileIndex into the tile buffer. The cells are stored with type type which must ...
Definition: tileread.c:145
int G3d_tile2tileIndex(G3D_Map *map, int xTile, int yTile, int zTile)
Returns tile-index corresponding to tile-coordinates (xTile, yTile, zTile).
Definition: tilemath.c:52
void G3d_fatalError(const char *,...)
This function prints the error message msg, and terminates the program with an error status...
Definition: g3derror.c:58