GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
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 
6 #include <grass/raster.h>
7 #include "raster3d_intern.h"
8 
9 /*---------------------------------------------------------------------------*/
10 
11 void Rast3d_get_block_nocache(RASTER3D_Map *map, int x0, int y0, int z0, int nx,
12  int ny, int nz, void *block, int type)
13 {
14  void *tile = NULL;
15  int tileX0, tileY0, tileZ0, tileOffsX0, tileOffsY0, tileOffsZ0;
16  int tileX1, tileY1, tileZ1, tileOffsX1, tileOffsY1, tileOffsZ1;
17  int tx, ty, tz, dx, dy, dz, x, y, z, rows, cols, depths;
18  int tileIndex;
19 
20  if (!map->useCache)
21  tile = Rast3d_alloc_tiles_type(map, 1, type);
22  if (tile == NULL)
24  "Rast3d_get_block_nocache: error in Rast3d_alloc_tiles");
25 
26  Rast3d_coord2tile_coord(map, x0, y0, z0, &tileX0, &tileY0, &tileZ0,
27  &tileOffsX0, &tileOffsY0, &tileOffsZ0);
28  Rast3d_coord2tile_coord(map, x0 + nx - 1, y0 + ny - 1, z0 + nz - 1, &tileX1,
29  &tileY1, &tileZ1, &tileOffsX1, &tileOffsY1,
30  &tileOffsZ1);
31 
32  for (tz = tileZ0; tz <= tileZ1; tz++) {
33  dz = (tz - tileZ0) * map->tileZ - tileOffsZ0;
34  for (ty = tileY0; ty <= tileY1; ty++) {
35  dy = (ty - tileY0) * map->tileY - tileOffsY0;
36  for (tx = tileX0; tx <= tileX1; tx++) {
37  dx = (tx - tileX0) * map->tileX - tileOffsX0;
38 
39  tileIndex = Rast3d_tile2tile_index(map, tx, ty, tz);
40 
41  if (Rast3d_tile_index_in_range(map, tileIndex))
42  if (map->useCache) {
43  tile = Rast3d_get_tile_ptr(map, tileIndex);
44  if (tile == NULL)
45  Rast3d_fatal_error("Rast3d_get_block_nocache: "
46  "error in Rast3d_get_tile_ptr");
47  }
48  else {
49  if (!Rast3d_read_tile(map, tileIndex, tile,
50  map->typeIntern))
51  Rast3d_fatal_error("Rast3d_get_block_nocache: "
52  "error in Rast3d_read_tile");
53  }
54 
55  else
56  Rast3d_set_null_tile(map, tile);
57 
58  cols = (tx == tileX1 ? tileOffsX1 : map->tileX - 1);
59  rows = (ty == tileY1 ? tileOffsY1 : map->tileY - 1);
60  depths = (tz == tileZ1 ? tileOffsZ1 : map->tileZ - 1);
61 
62  x = (tx == tileX0 ? tileOffsX0 : 0);
63 
64  for (z = (tz == tileZ0 ? tileOffsZ0 : 0); z <= depths; z++)
65  for (y = (ty == tileY0 ? tileOffsY0 : 0); y <= rows; y++) {
67  tile, z * map->tileXY + y * map->tileX + x,
68  map->typeIntern, block,
69  (z + dz) * nx * ny + (y + dy) * nx + (x + dx), type,
70  cols - x + 1);
71  }
72  }
73  }
74  }
75 
76  if (!map->useCache)
77  Rast3d_free_tiles(tile);
78 }
79 
80 /*---------------------------------------------------------------------------*/
81 
82 /*!
83  * \brief
84  *
85  * Copies the cells contained in the block (cube) with vertices
86  * <em>(x0, y0, z0)</em> and <em>(x0 + nx - 1, y0 + ny - 1, z0 + nz - 1)</em>
87  * into <em>block</em>. The cell-values in <em>block</em> are of <em>type</em>.
88  * The source code can be found in <em>getblock.c</em>.
89  *
90  * \param map
91  * \param x0
92  * \param y0
93  * \param z0
94  * \param nx
95  * \param ny
96  * \param nz
97  * \param block
98  * \param type
99  * \return void
100  */
101 
102 void Rast3d_get_block(RASTER3D_Map *map, int x0, int y0, int z0, int nx, int ny,
103  int nz, void *block, int type)
104 {
105  int x, y, z, nNull, x1, y1, z1, length;
106 
107  if (!map->useCache) {
108  Rast3d_get_block_nocache(map, x0, y0, z0, nx, ny, nz, block, type);
109  return;
110  }
111 
112  x1 = RASTER3D_MIN(x0 + nx, map->region.cols);
113  y1 = RASTER3D_MIN(y0 + ny, map->region.rows);
114  z1 = RASTER3D_MIN(z0 + nz, map->region.depths);
115 
116  length = Rast3d_length(type);
117 
118  for (z = z0; z < z1; z++) {
119  for (y = y0; y < y1; y++) {
120  for (x = x0; x < x1; x++) {
121  Rast3d_get_value_region(map, x, y, z, block, type);
122  block = G_incr_void_ptr(block, length);
123  }
124  nNull = x0 + nx - x;
125  Rast3d_set_null_value(block, nNull, type);
126  block = G_incr_void_ptr(block, length * nNull);
127  }
128  nNull = (y0 + ny - y) * nx;
129  Rast3d_set_null_value(block, nNull, type);
130  block = G_incr_void_ptr(block, length * nNull);
131  }
132  nNull = (z0 + nz - z) * ny * nx;
133  Rast3d_set_null_value(block, nNull, type);
134 }
#define NULL
Definition: ccmath.h:32
#define G_incr_void_ptr(ptr, size)
Definition: defs/gis.h:81
void Rast3d_copy_values(const void *, int, int, void *, int, int, int)
Definition: raster3d/misc.c:50
int Rast3d_tile_index_in_range(RASTER3D_Map *, int)
Returns 1 if tileIndex is a valid index for map. Returns 0 otherwise.
Definition: tilemath.c:204
void Rast3d_set_null_value(void *, int, int)
Fills the vector pointed to by c with nofElts NULL-values of type.
Definition: null.c:34
void Rast3d_free_tiles(void *)
Is equivalent to Rast3d_free (tiles);
Definition: tilealloc.c:73
void Rast3d_set_null_tile(RASTER3D_Map *, void *)
Is equivalent to Rast3d_set_null_tile_type (map, tile, Rast3d_file_type_map (map)).
Definition: tilenull.c:40
void * Rast3d_get_tile_ptr(RASTER3D_Map *, int)
This function returns a pointer to a tile which contains the data for the tile with index tileIndex....
Definition: tileio.c:79
int Rast3d_tile2tile_index(RASTER3D_Map *, int, int, int)
Returns tile-index corresponding to tile-coordinates (xTile, yTile, zTile).
Definition: tilemath.c:49
void Rast3d_get_value_region(RASTER3D_Map *, int, int, int, void *, int)
Returns in *value the cell-value of the cell with region-coordinate (x, y, z). The value returned is ...
Definition: getvalue.c:257
void Rast3d_coord2tile_coord(RASTER3D_Map *, int, int, int, int *, int *, int *, int *, int *, int *)
Converts cell-coordinates (x, y, z) into tile-coordinates (xTile, yTile, zTile) and the coordinate of...
Definition: tilemath.c:129
int Rast3d_length(int)
Definition: raster3d/misc.c:75
int Rast3d_read_tile(RASTER3D_Map *, int, void *, int)
Reads tile with index tileIndex into the tile buffer. The cells are stored with type type which must ...
Definition: tileread.c:151
void * Rast3d_alloc_tiles_type(RASTER3D_Map *, int, int)
Allocates a vector of nofTiles tiles with the same dimensions as the tiles of map and large enough to...
Definition: tilealloc.c:23
void Rast3d_fatal_error(const char *,...) __attribute__((format(printf
void Rast3d_get_block(RASTER3D_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:102
void Rast3d_get_block_nocache(RASTER3D_Map *map, int x0, int y0, int z0, int nx, int ny, int nz, void *block, int type)
Definition: getblock.c:11
#define RASTER3D_MIN(a, b)
RASTER3D_Region region
Definition: raster3d.h:82
int useCache
Definition: raster3d.h:160
int typeIntern
Definition: raster3d.h:150
#define x