getblock.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <grass/raster.h>
  6. #include "raster3d_intern.h"
  7. /*---------------------------------------------------------------------------*/
  8. void
  9. Rast3d_get_block_nocache(RASTER3D_Map * map, int x0, int y0, int z0, int nx, int ny,
  10. int nz, void *block, int type)
  11. {
  12. void *tile;
  13. int tileX0, tileY0, tileZ0, tileOffsX0, tileOffsY0, tileOffsZ0;
  14. int tileX1, tileY1, tileZ1, tileOffsX1, tileOffsY1, tileOffsZ1;
  15. int tx, ty, tz, dx, dy, dz, x, y, z, rows, cols, depths;
  16. int tileIndex;
  17. if (!map->useCache)
  18. tile = Rast3d_alloc_tiles_type(map, 1, type);
  19. if (tile == NULL)
  20. Rast3d_fatal_error("Rast3d_get_block_nocache: error in Rast3d_alloc_tiles");
  21. Rast3d_coord2tile_coord(map, x0, y0, z0, &tileX0, &tileY0, &tileZ0,
  22. &tileOffsX0, &tileOffsY0, &tileOffsZ0);
  23. Rast3d_coord2tile_coord(map, x0 + nx - 1, y0 + ny - 1, z0 + nz - 1,
  24. &tileX1, &tileY1, &tileZ1,
  25. &tileOffsX1, &tileOffsY1, &tileOffsZ1);
  26. for (tz = tileZ0; tz <= tileZ1; tz++) {
  27. dz = (tz - tileZ0) * map->tileZ - tileOffsZ0;
  28. for (ty = tileY0; ty <= tileY1; ty++) {
  29. dy = (ty - tileY0) * map->tileY - tileOffsY0;
  30. for (tx = tileX0; tx <= tileX1; tx++) {
  31. dx = (tx - tileX0) * map->tileX - tileOffsX0;
  32. tileIndex = Rast3d_tile2tile_index(map, tx, ty, tz);
  33. if (Rast3d_tile_index_in_range(map, tileIndex))
  34. if (map->useCache) {
  35. tile = Rast3d_get_tile_ptr(map, tileIndex);
  36. if (tile == NULL)
  37. Rast3d_fatal_error
  38. ("Rast3d_get_block_nocache: error in Rast3d_get_tile_ptr");
  39. }
  40. else {
  41. if (!Rast3d_read_tile
  42. (map, tileIndex, tile, map->typeIntern))
  43. Rast3d_fatal_error
  44. ("Rast3d_get_block_nocache: error in Rast3d_read_tile");
  45. }
  46. else
  47. Rast3d_set_null_tile(map, tile);
  48. cols = (tx == tileX1 ? tileOffsX1 : map->tileX - 1);
  49. rows = (ty == tileY1 ? tileOffsY1 : map->tileY - 1);
  50. depths = (tz == tileZ1 ? tileOffsZ1 : map->tileZ - 1);
  51. x = (tx == tileX0 ? tileOffsX0 : 0);
  52. for (z = (tz == tileZ0 ? tileOffsZ0 : 0); z <= depths; z++)
  53. for (y = (ty == tileY0 ? tileOffsY0 : 0); y <= rows; y++) {
  54. Rast3d_copy_values(tile,
  55. z * map->tileXY + y * map->tileX + x,
  56. map->typeIntern,
  57. block,
  58. (z + dz) * nx * ny + (y + dy) * nx +
  59. (x + dx), type, cols - x + 1);
  60. }
  61. }
  62. }
  63. }
  64. if (!map->useCache)
  65. Rast3d_free_tiles(tile);
  66. }
  67. /*---------------------------------------------------------------------------*/
  68. /*!
  69. * \brief
  70. *
  71. * Copies the cells contained in the block (cube) with vertices
  72. * <em>(x0, y0, z0)</em> and <em>(x0 + nx - 1, y0 + ny - 1, z0 + nz - 1)</em>
  73. * into <em>block</em>. The cell-values in <em>block</em> are of <em>type</em>.
  74. * The source code can be found in <em>getblock.c</em>.
  75. *
  76. * \param map
  77. * \param x0
  78. * \param y0
  79. * \param z0
  80. * \param nx
  81. * \param ny
  82. * \param nz
  83. * \param block
  84. * \param type
  85. * \return void
  86. */
  87. void
  88. Rast3d_get_block(RASTER3D_Map * map, int x0, int y0, int z0, int nx, int ny, int nz,
  89. void *block, int type)
  90. {
  91. int x, y, z, nNull, x1, y1, z1, length;
  92. if (!map->useCache) {
  93. Rast3d_get_block_nocache(map, x0, y0, z0, nx, ny, nz, block, type);
  94. return;
  95. }
  96. x1 = RASTER3D_MIN(x0 + nx, map->region.cols);
  97. y1 = RASTER3D_MIN(y0 + ny, map->region.rows);
  98. z1 = RASTER3D_MIN(z0 + nz, map->region.depths);
  99. length = Rast3d_length(type);
  100. for (z = z0; z < z1; z++) {
  101. for (y = y0; y < y1; y++) {
  102. for (x = x0; x < x1; x++) {
  103. Rast3d_get_value_region(map, x, y, z, block, type);
  104. block = G_incr_void_ptr(block, length);
  105. }
  106. nNull = x0 + nx - x;
  107. Rast3d_set_null_value(block, nNull, type);
  108. block = G_incr_void_ptr(block, length * nNull);
  109. }
  110. nNull = (y0 + ny - y) * nx;
  111. Rast3d_set_null_value(block, nNull, type);
  112. block = G_incr_void_ptr(block, length * nNull);
  113. }
  114. nNull = (z0 + nz - z) * ny * nx;
  115. Rast3d_set_null_value(block, nNull, type);
  116. }