GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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 }