main.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /***************************************************************************
  2. * MODULE: r3.mask
  3. *
  4. * AUTHOR(S): Roman Waupotitsch, Michael Shapiro, Helena Mitasova,
  5. * Bill Brown, Lubos Mitas, Jaro Hofierka
  6. *
  7. * PURPOSE: Establishes the current working 3D raster mask.
  8. *
  9. * COPYRIGHT: (C) 2005 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <grass/gis.h>
  20. #include <grass/raster3d.h>
  21. #include <grass/glocale.h>
  22. /*--------------------------------------------------------------------------*/
  23. typedef struct
  24. {
  25. struct Option *map, *maskVals;
  26. } paramType;
  27. static paramType params;
  28. /*--------------------------------------------------------------------------*/
  29. void getParams(char **name, d_Mask ** maskRules)
  30. {
  31. *name = params.map->answer;
  32. Rast3d_parse_vallist(params.maskVals->answers, maskRules);
  33. }
  34. /*-------------------------------------------------------------------------*/
  35. #define MAX(a,b) (a > b ? a : b)
  36. static void makeMask(char *name, d_Mask * maskRules)
  37. {
  38. void *map, *mask;
  39. RASTER3D_Region region;
  40. int tileX, tileY, tileZ, x, y, z, cacheSize;
  41. double value;
  42. float floatNull;
  43. cacheSize = Rast3d_cache_size_encode(RASTER3D_USE_CACHE_XY, 1);
  44. if (NULL == G_find_raster3d(name, ""))
  45. Rast3d_fatal_error(_("3D raster map <%s> not found"), name);
  46. map = Rast3d_open_cell_old(name, G_mapset(), RASTER3D_DEFAULT_WINDOW,
  47. DCELL_TYPE, cacheSize);
  48. if (map == NULL)
  49. Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), name);
  50. Rast3d_get_region_struct_map(map, &region);
  51. Rast3d_get_tile_dimensions_map(map, &tileX, &tileY, &tileZ);
  52. mask = Rast3d_open_new_param(Rast3d_mask_file(), FCELL_TYPE, cacheSize,
  53. &region, FCELL_TYPE, RASTER3D_COMPRESSION, 0,
  54. tileX, tileY, tileZ);
  55. if (mask == NULL)
  56. Rast3d_fatal_error(_("Unable to open 3D raster mask file"));
  57. Rast3d_min_unlocked(map, RASTER3D_USE_CACHE_X);
  58. Rast3d_autolock_on(map);
  59. Rast3d_unlock_all(map);
  60. Rast3d_min_unlocked(mask, RASTER3D_USE_CACHE_X);
  61. Rast3d_autolock_on(mask);
  62. Rast3d_unlock_all(mask);
  63. Rast3d_set_null_value(&floatNull, 1, FCELL_TYPE);
  64. for (z = 0; z < region.depths; z++) {
  65. if ((z % tileZ) == 0) {
  66. Rast3d_unlock_all(map);
  67. Rast3d_unlock_all(mask);
  68. }
  69. for (y = 0; y < region.rows; y++) /* We count from north to south in the cube coordinate system */
  70. for (x = 0; x < region.cols; x++) {
  71. value = Rast3d_get_double_region(map, x, y, z);
  72. if (Rast3d_mask_d_select((DCELL *) & value, maskRules))
  73. Rast3d_put_float(mask, x, y, z, (float)floatNull); /* mask-out value */
  74. else
  75. Rast3d_put_float(mask, x, y, z, (float)0.0); /* not mask-out value */
  76. }
  77. if ((z % tileZ) == 0) {
  78. if (!Rast3d_flush_tiles_in_cube
  79. (mask, 0, 0, MAX(0, z - tileZ), region.rows - 1,
  80. region.cols - 1, z))
  81. Rast3d_fatal_error(_("makeMask: error flushing tiles in cube"));
  82. }
  83. }
  84. if (!Rast3d_flush_all_tiles(mask))
  85. Rast3d_fatal_error(_("makeMask: error flushing all tiles"));
  86. Rast3d_autolock_off(map);
  87. Rast3d_unlock_all(map);
  88. Rast3d_autolock_off(mask);
  89. Rast3d_unlock_all(mask);
  90. if (!Rast3d_close(mask))
  91. Rast3d_fatal_error(_("Unable to close 3D raster mask file"));
  92. if (!Rast3d_close(map))
  93. Rast3d_fatal_error(_("Unable to close raster map <%s>"), name);
  94. }
  95. /*--------------------------------------------------------------------------*/
  96. int main(int argc, char *argv[])
  97. {
  98. char *name;
  99. d_Mask *maskRules;
  100. struct GModule *module;
  101. G_gisinit(argv[0]);
  102. module = G_define_module();
  103. G_add_keyword(_("raster3d"));
  104. G_add_keyword(_("mask"));
  105. G_add_keyword(_("voxel"));
  106. module->description =
  107. _("Establishes the current working 3D raster mask.");
  108. params.map = G_define_option();
  109. params.map->key = "map";
  110. params.map->type = TYPE_STRING;
  111. params.map->required = YES;
  112. params.map->multiple = NO;
  113. params.map->gisprompt = "old,grid3,3d-raster";
  114. params.map->description = _("3D raster map with reference values");
  115. params.maskVals = G_define_option();
  116. params.maskVals->key = "maskvalues";
  117. params.maskVals->key_desc = "val[-val]";
  118. params.maskVals->type = TYPE_STRING;
  119. params.maskVals->required = NO;
  120. params.maskVals->multiple = YES;
  121. params.maskVals->description = _("List of cell values to be masked out");
  122. if (G_parser(argc, argv))
  123. exit(EXIT_FAILURE);
  124. if (Rast3d_mask_file_exists())
  125. G_fatal_error(_("Cannot create mask file: RASTER3D_MASK already exists"));
  126. getParams(&name, &maskRules);
  127. makeMask(name, maskRules);
  128. exit(EXIT_SUCCESS);
  129. }
  130. /*--------------------------------------------------------------------------*/
  131. /*--------------------------------------------------------------------------*/
  132. /*--------------------------------------------------------------------------*/