r3.mask.main.c 4.9 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/G3d.h>
  21. #include <grass/glocale.h>
  22. #include "mask_functions.h"
  23. /*--------------------------------------------------------------------------*/
  24. typedef struct
  25. {
  26. struct Option *map, *maskVals;
  27. } paramType;
  28. static paramType params;
  29. /*--------------------------------------------------------------------------*/
  30. void getParams(char **name, d_Mask ** maskRules)
  31. {
  32. *name = params.map->answer;
  33. parse_vallist(params.maskVals->answers, maskRules);
  34. }
  35. /*-------------------------------------------------------------------------*/
  36. #define MAX(a,b) (a > b ? a : b)
  37. static void makeMask(char *name, d_Mask * maskRules)
  38. {
  39. void *map, *mask;
  40. G3D_Region region;
  41. int tileX, tileY, tileZ, x, y, z, cacheSize;
  42. double value;
  43. float floatNull;
  44. cacheSize = G3d_cacheSizeEncode(G3D_USE_CACHE_XY, 1);
  45. if (NULL == G_find_grid3(name, ""))
  46. G3d_fatalError(_("Requested 3d raster map not found"));
  47. map = G3d_openCellOld(name, G_mapset(), G3D_DEFAULT_WINDOW,
  48. DCELL_TYPE, cacheSize);
  49. if (map == NULL)
  50. G3d_fatalError(_("makeMask: error opening map."));
  51. G3d_getRegionStructMap(map, &region);
  52. G3d_getTileDimensionsMap(map, &tileX, &tileY, &tileZ);
  53. mask = G3d_openNewParam(G3d_maskFile(), FCELL_TYPE, cacheSize,
  54. &region, FCELL_TYPE, G3D_NO_LZW, G3D_USE_RLE, 0,
  55. tileX, tileY, tileZ);
  56. if (mask == NULL)
  57. G3d_fatalError(_("makeMask: error opening g3d mask file"));
  58. G3d_minUnlocked(map, G3D_USE_CACHE_X);
  59. G3d_autolockOn(map);
  60. G3d_unlockAll(map);
  61. G3d_minUnlocked(mask, G3D_USE_CACHE_X);
  62. G3d_autolockOn(mask);
  63. G3d_unlockAll(mask);
  64. G3d_setNullValue(&floatNull, 1, FCELL_TYPE);
  65. for (z = 0; z < region.depths; z++) {
  66. if ((z % tileZ) == 0) {
  67. G3d_unlockAll(map);
  68. G3d_unlockAll(mask);
  69. }
  70. /*for (y = region.rows-1; y >= 0; y--) *//* go north to south */
  71. for (y = 0; y < region.rows; y++) /*I dont know which one is right; soeren 04 Aug 05 */
  72. for (x = 0; x < region.cols; x++) {
  73. value = G3d_getDoubleRegion(map, x, y, z);
  74. if (mask_d_select((DCELL *) & value, maskRules))
  75. G3d_putFloat(mask, x, y, z, (float)floatNull); /* mask-out value */
  76. else
  77. G3d_putFloat(mask, x, y, z, (float)0.0); /* not mask-out value */
  78. }
  79. if ((z % tileZ) == 0) {
  80. if (!G3d_flushTilesInCube
  81. (mask, 0, 0, MAX(0, z - tileZ), region.rows - 1,
  82. region.cols - 1, z))
  83. G3d_fatalError(_("makeMask: error flushing tiles in cube"));
  84. }
  85. }
  86. if (!G3d_flushAllTiles(mask))
  87. G3d_fatalError(_("makeMask: error flushing all tiles"));
  88. G3d_autolockOff(map);
  89. G3d_unlockAll(map);
  90. G3d_autolockOff(mask);
  91. G3d_unlockAll(mask);
  92. if (!G3d_closeCell(mask))
  93. G3d_fatalError(_("makeMask: error closing g3d mask file"));
  94. if (!G3d_closeCell(map))
  95. G3d_fatalError(_("makeMask: error closing map"));
  96. }
  97. /*--------------------------------------------------------------------------*/
  98. int main(int argc, char *argv[])
  99. {
  100. char *name;
  101. d_Mask *maskRules;
  102. struct GModule *module;
  103. G_gisinit(argv[0]);
  104. module = G_define_module();
  105. G_add_keyword(_("raster3d"));
  106. G_add_keyword(_("voxel"));
  107. module->description =
  108. _("Establishes the current working 3D raster mask.");
  109. params.map = G_define_option();
  110. params.map->key = "map";
  111. params.map->type = TYPE_STRING;
  112. params.map->required = YES;
  113. params.map->multiple = NO;
  114. params.map->gisprompt = "old,grid3,3d-raster";
  115. params.map->description = _("3d raster map with reference values");
  116. params.maskVals = G_define_option();
  117. params.maskVals->key = "maskvalues";
  118. params.maskVals->key_desc = "val[-val]";
  119. params.maskVals->type = TYPE_STRING;
  120. params.maskVals->required = NO;
  121. params.maskVals->multiple = YES;
  122. params.maskVals->description = _("List of cell values to be masked out");
  123. if (G_parser(argc, argv))
  124. exit(EXIT_FAILURE);
  125. if (G3d_maskFileExists())
  126. G_fatal_error(_("Cannot create mask file: G3D_MASK already exists"));
  127. getParams(&name, &maskRules);
  128. makeMask(name, maskRules);
  129. exit(EXIT_SUCCESS);
  130. }
  131. /*--------------------------------------------------------------------------*/
  132. /*--------------------------------------------------------------------------*/
  133. /*--------------------------------------------------------------------------*/