mask.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #include <stdio.h>
  2. #include <grass/gis.h>
  3. #include "raster3d_intern.h"
  4. /*--------------------------------------------------------------------------*/
  5. /* the standard g3d file format is used to store the mask values. a NULL-value
  6. is stored for values which are masked out and a "0." is stored for values
  7. which are not masked out. to improve compression, the precision is set to
  8. 0 and RLE encoding is used.
  9. */
  10. /*--------------------------------------------------------------------------*/
  11. static int Rast3d_maskMapExistsVar = 0;
  12. static RASTER3D_Map *Rast3d_maskMap;
  13. /*--------------------------------------------------------------------------*/
  14. static void dummy(void)
  15. {
  16. return;
  17. }
  18. static float RASTER3D_MASKNUMmaskValue;
  19. /* Call to dummy() to match void return type of Rast3d_set_null_value() */
  20. #define RASTER3D_MASKNUM(map,Xmask,Ymask,Zmask,VALUEmask,TYPEmask) \
  21. \
  22. (RASTER3D_MASKNUMmaskValue = Rast3d_getMaskFloat (map, Xmask, Ymask, Zmask), \
  23. ((Rast3d_is_null_value_num (&RASTER3D_MASKNUMmaskValue, FCELL_TYPE)) ? \
  24. Rast3d_set_null_value (VALUEmask, 1, TYPEmask) : dummy()))
  25. /*--------------------------------------------------------------------------*/
  26. int Rast3d_mask_close()
  27. {
  28. /* No Idea if this is correct return value */
  29. if (!Rast3d_maskMapExistsVar)
  30. return 1;
  31. Rast3d_maskMapExistsVar = 0;
  32. if (!Rast3d_close(Rast3d_maskMap)) {
  33. Rast3d_error("Rast3d_mask_close: error closing mask");
  34. return 0;
  35. }
  36. return 1;
  37. }
  38. /*--------------------------------------------------------------------------*/
  39. /*!
  40. * \brief
  41. *
  42. * Returns 1 if the 3d mask file exists.
  43. *
  44. * \return int
  45. */
  46. int Rast3d_mask_file_exists(void)
  47. {
  48. return G_find_file_misc(RASTER3D_DIRECTORY, RASTER3D_CELL_ELEMENT, RASTER3D_MASK_MAP, G_mapset()) != NULL;
  49. }
  50. /*--------------------------------------------------------------------------*/
  51. static int maskOpenOldCacheDefault = RASTER3D_USE_CACHE_DEFAULT;
  52. int Rast3d_mask_open_old(void)
  53. {
  54. RASTER3D_Region region;
  55. /* No Idea if this is correct return value */
  56. if (Rast3d_maskMapExistsVar)
  57. return 1;
  58. Rast3d_maskMapExistsVar = Rast3d_mask_file_exists();
  59. if (!Rast3d_maskMapExistsVar)
  60. return 1;
  61. if ((Rast3d_maskMap = Rast3d_open_cell_old(RASTER3D_MASK_MAP, G_mapset(),
  62. RASTER3D_DEFAULT_WINDOW, FCELL_TYPE,
  63. maskOpenOldCacheDefault))
  64. == NULL) {
  65. Rast3d_error("Rast3d_mask_open_old: cannot open mask");
  66. return 0;
  67. }
  68. Rast3d_get_region_struct_map(Rast3d_maskMap, &region);
  69. Rast3d_set_window_map(Rast3d_maskMap, &region);
  70. return 1;
  71. }
  72. /*--------------------------------------------------------------------------*/
  73. static float Rast3d_getMaskFloat(RASTER3D_Map * map, int x, int y, int z)
  74. {
  75. double north, east, top;
  76. float value;
  77. north = ((double)map->window.rows - y - 0.5) / (double)map->window.rows *
  78. (map->window.north - map->window.south) + map->window.south;
  79. east = ((double)x + 0.5) / (double)map->window.cols *
  80. (map->window.east - map->window.west) + map->window.west;
  81. top = ((double)z + 0.5) / (double)map->window.depths *
  82. (map->window.top - map->window.bottom) + map->window.bottom;
  83. Rast3d_get_region_value(Rast3d_maskMap, north, east, top, &value, FCELL_TYPE);
  84. return value;
  85. }
  86. /*--------------------------------------------------------------------------*/
  87. /*!
  88. * \brief
  89. *
  90. * This function should be used to adjust the cache size used for the
  91. * 3d-mask. First the open 3d-mask is closed and then opened again with
  92. * a cache size as specified with <em>cache</em>.
  93. *
  94. * \param cache
  95. * \return 1 ... if successful
  96. * 0 ... otherwise.
  97. */
  98. int Rast3d_mask_reopen(int cache)
  99. {
  100. int tmp;
  101. if (Rast3d_maskMapExistsVar)
  102. if (!Rast3d_mask_close()) {
  103. Rast3d_error("Rast3d_mask_reopen: error closing mask");
  104. return 0;
  105. }
  106. tmp = maskOpenOldCacheDefault;
  107. maskOpenOldCacheDefault = cache;
  108. if (!Rast3d_mask_open_old()) {
  109. Rast3d_error("Rast3d_mask_reopen: error opening mask");
  110. return 0;
  111. }
  112. maskOpenOldCacheDefault = tmp;
  113. return 1;
  114. }
  115. /*--------------------------------------------------------------------------*/
  116. /*!
  117. * \brief
  118. *
  119. * Returns 1 if the cell with cell-coordinates <em>(x, y, z)</em> is masked
  120. * out. Returns 0 otherwise.
  121. *
  122. * \param x
  123. * \param y
  124. * \param z
  125. * \return int
  126. */
  127. int Rast3d_is_masked(RASTER3D_Map * map, int x, int y, int z)
  128. {
  129. if (!Rast3d_maskMapExistsVar)
  130. return 0;
  131. RASTER3D_MASKNUMmaskValue = Rast3d_getMaskFloat(map, x, y, z);
  132. return (Rast3d_is_null_value_num(&RASTER3D_MASKNUMmaskValue, FCELL_TYPE));
  133. }
  134. /*--------------------------------------------------------------------------*/
  135. /*!
  136. * \brief
  137. *
  138. * Replaces the value stored in <em>value</em> with the NULL-value if
  139. * <em>Rast3d_is_masked (x, y, z)</em> returns 1. Does nothing otherwise.
  140. * <em>value</em> is assumed to be of<em>type</em>.
  141. *
  142. * \param x
  143. * \param y
  144. * \param z
  145. * \param value
  146. * \param type
  147. * \return void
  148. */
  149. void Rast3d_mask_num(RASTER3D_Map * map, int x, int y, int z, void *value, int type)
  150. {
  151. if (!Rast3d_maskMapExistsVar)
  152. return;
  153. RASTER3D_MASKNUM(map, x, y, z, value, type);
  154. }
  155. /*--------------------------------------------------------------------------*/
  156. /*!
  157. * \brief
  158. *
  159. * Same as <em>Rast3d_mask_num (x, y, z, value, FCELL_TYPE)</em>.
  160. *
  161. * \param x
  162. * \param y
  163. * \param z
  164. * \param value
  165. * \return void
  166. */
  167. void Rast3d_mask_float(RASTER3D_Map * map, int x, int y, int z, float *value)
  168. {
  169. if (!Rast3d_maskMapExistsVar)
  170. return;
  171. RASTER3D_MASKNUM(map, x, y, z, value, FCELL_TYPE);
  172. }
  173. /*--------------------------------------------------------------------------*/
  174. /*!
  175. * \brief
  176. *
  177. * Same as <em>Rast3d_mask_num (x, y, z, value, DCELL_TYPE)</em>.
  178. *
  179. * \param x
  180. * \param y
  181. * \param z
  182. * \param value
  183. * \return void
  184. */
  185. void Rast3d_mask_double(RASTER3D_Map * map, int x, int y, int z, double *value)
  186. {
  187. if (!Rast3d_maskMapExistsVar)
  188. return;
  189. RASTER3D_MASKNUM(map, x, y, z, value, DCELL_TYPE);
  190. }
  191. /*--------------------------------------------------------------------------*/
  192. /*!
  193. * \brief
  194. *
  195. * Replaces the values stored in <em>tile</em> (with <em>tileIndex</em>) for
  196. * which <em>Rast3d_is_masked</em> returns 1 with NULL-values. Does not change
  197. * the remaining values. The values are assumed to be of <em>type</em>.
  198. * Whether replacement is performed or not only depends on location of the
  199. * cells of the tile and not on the status of the mask for <em>map</em>
  200. * (i.e. turned on or off).
  201. *
  202. * \param map
  203. * \param tileIndex
  204. * \param tile
  205. * \param type
  206. * \return void
  207. */
  208. void Rast3d_mask_tile(RASTER3D_Map * map, int tileIndex, void *tile, int type)
  209. {
  210. int nofNum, rows, cols, depths, xRedundant, yRedundant, zRedundant;
  211. int x, y, z, xLength, yLength, dx, dy, dz, length;
  212. if (!Rast3d_maskMapExistsVar)
  213. return;
  214. nofNum = Rast3d_compute_clipped_tile_dimensions(map, tileIndex,
  215. &rows, &cols, &depths,
  216. &xRedundant, &yRedundant,
  217. &zRedundant);
  218. Rast3d_tile_index_origin(map, tileIndex, &x, &y, &z);
  219. if (nofNum == map->tileSize) {
  220. /*AV*/
  221. /* BEGIN OF ORIGINAL CODE */
  222. /*
  223. * Rast3d_get_tile_dimensions_map (map, &rows, &cols, &depths);
  224. */
  225. /*AV*/
  226. /* BEGIN OF MY CODE */
  227. Rast3d_get_tile_dimensions_map(map, &cols, &rows, &depths);
  228. /* END OF MY CODE */
  229. xRedundant = yRedundant = 0;
  230. }
  231. rows += y;
  232. cols += x;
  233. depths += z;
  234. length = Rast3d_length(type);
  235. xLength = xRedundant * length;
  236. yLength = map->tileX * yRedundant * length;
  237. for (dz = z; dz < depths; dz++) {
  238. for (dy = y; dy < rows; dy++) {
  239. for (dx = x; dx < cols; dx++) {
  240. RASTER3D_MASKNUM(map, dx, dy, dz, tile, type);
  241. tile += length;
  242. }
  243. tile += xLength;
  244. }
  245. tile += yLength;
  246. }
  247. }
  248. /*--------------------------------------------------------------------------*/
  249. /*!
  250. * \brief
  251. *
  252. * Turns on the mask for <em>map</em>. Do
  253. * not invoke this function after the first tile has been read since the result
  254. * might be inconsistent cell-values.
  255. *
  256. * \param map
  257. * \return void
  258. */
  259. void Rast3d_mask_on(RASTER3D_Map * map)
  260. {
  261. map->useMask = 1;
  262. }
  263. /*!
  264. * \brief
  265. *
  266. * Turns off the mask for <em>map</em>.
  267. * This is the default. Do not invoke this function after the first tile has
  268. * been read since the result might be inconsistent cell-values.
  269. *
  270. * \param map
  271. * \return void
  272. */
  273. void Rast3d_mask_off(RASTER3D_Map * map)
  274. {
  275. map->useMask = 0;
  276. }
  277. /*!
  278. * \brief
  279. *
  280. * Returns 1 if the mask for <em>map</em>
  281. * is turned on. Returns 0 otherwise.
  282. *
  283. * \param map
  284. * \return int
  285. */
  286. int Rast3d_mask_is_on(RASTER3D_Map * map)
  287. {
  288. return map->useMask;
  289. }
  290. /*!
  291. * \brief
  292. *
  293. * Returns 1 if the mask for <em>map</em> is turned off. Returns 0 otherwise.
  294. *
  295. * \param map
  296. * \return int
  297. */
  298. int Rast3d_mask_is_off(RASTER3D_Map * map)
  299. {
  300. return !map->useMask;
  301. }
  302. /*!
  303. * \brief
  304. *
  305. * Returns the name of the 3d mask file.
  306. *
  307. * \return char *
  308. */
  309. const char *Rast3d_mask_file(void)
  310. {
  311. return RASTER3D_MASK_MAP;
  312. }
  313. /*!
  314. * \brief
  315. *
  316. * Returns 1 if the 3d mask is loaded.
  317. *
  318. * \return int
  319. */
  320. int Rast3d_mask_map_exists(void)
  321. {
  322. return Rast3d_maskMapExistsVar;
  323. }