GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-535c39c9fc
mask.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <grass/gis.h>
3 #include "raster3d_intern.h"
4 
5 /*--------------------------------------------------------------------------*/
6 
7 /* the standard g3d file format is used to store the mask values. a NULL-value
8  is stored for values which are masked out and a "0." is stored for values
9  which are not masked out. to improve compression, the precision is set to
10  0 and RLE encoding is used.
11  */
12 
13 /*--------------------------------------------------------------------------*/
14 
15 static int Rast3d_maskMapExistsVar = 0;
16 static RASTER3D_Map *Rast3d_maskMap;
17 
18 /*--------------------------------------------------------------------------*/
19 static void dummy(void)
20 {
21  return;
22 }
23 
24 static float RASTER3D_MASKNUMmaskValue;
25 
26 /* Call to dummy() to match void return type of Rast3d_set_null_value() */
27 #define RASTER3D_MASKNUM(map, Xmask, Ymask, Zmask, VALUEmask, TYPEmask) \
28  \
29  (RASTER3D_MASKNUMmaskValue = \
30  Rast3d_getMaskFloat(map, Xmask, Ymask, Zmask), \
31  ((Rast3d_is_null_value_num(&RASTER3D_MASKNUMmaskValue, FCELL_TYPE)) \
32  ? Rast3d_set_null_value(VALUEmask, 1, TYPEmask) \
33  : dummy()))
34 
35 /*--------------------------------------------------------------------------*/
36 
38 {
39  /* No Idea if this is correct return value */
40  if (!Rast3d_maskMapExistsVar)
41  return 1;
42 
43  Rast3d_maskMapExistsVar = 0;
44 
45  if (!Rast3d_close(Rast3d_maskMap)) {
46  Rast3d_error("Rast3d_mask_close: error closing mask");
47 
48  return 0;
49  }
50 
51  return 1;
52 }
53 
54 /*--------------------------------------------------------------------------*/
55 
56 /*!
57  * \brief
58  *
59  * Returns 1 if the 3d mask file exists.
60  *
61  * \return int
62  */
63 
65 {
68 }
69 
70 /*--------------------------------------------------------------------------*/
71 
72 static int maskOpenOldCacheDefault = RASTER3D_USE_CACHE_DEFAULT;
73 
75 {
76  RASTER3D_Region region;
77 
78  /* No Idea if this is correct return value */
79  if (Rast3d_maskMapExistsVar)
80  return 1;
81 
82  Rast3d_maskMapExistsVar = Rast3d_mask_file_exists();
83 
84  if (!Rast3d_maskMapExistsVar)
85  return 1;
86 
87  if ((Rast3d_maskMap = Rast3d_open_cell_old(
89  maskOpenOldCacheDefault)) == NULL) {
90  Rast3d_error("Rast3d_mask_open_old: cannot open mask");
91 
92  return 0;
93  }
94 
95  Rast3d_get_region_struct_map(Rast3d_maskMap, &region);
96  Rast3d_set_window_map(Rast3d_maskMap, &region);
97 
98  return 1;
99 }
100 
101 /*--------------------------------------------------------------------------*/
102 
103 static float Rast3d_getMaskFloat(RASTER3D_Map *map, int x, int y, int z)
104 {
105  double north, east, top;
106  float value;
107 
108  north = ((double)map->window.rows - y - 0.5) / (double)map->window.rows *
109  (map->window.north - map->window.south) +
110  map->window.south;
111  east = ((double)x + 0.5) / (double)map->window.cols *
112  (map->window.east - map->window.west) +
113  map->window.west;
114  top = ((double)z + 0.5) / (double)map->window.depths *
115  (map->window.top - map->window.bottom) +
116  map->window.bottom;
117 
118  Rast3d_get_region_value(Rast3d_maskMap, north, east, top, &value,
119  FCELL_TYPE);
120  return value;
121 }
122 
123 /*--------------------------------------------------------------------------*/
124 
125 /*!
126  * \brief
127  *
128  * This function should be used to adjust the cache size used for the
129  * 3d-mask. First the open 3d-mask is closed and then opened again with
130  * a cache size as specified with <em>cache</em>.
131  *
132  * \param cache
133  * \return 1 ... if successful
134  * 0 ... otherwise.
135  */
136 
137 int Rast3d_mask_reopen(int cache)
138 {
139  int tmp;
140 
141  if (Rast3d_maskMapExistsVar)
142  if (!Rast3d_mask_close()) {
143  Rast3d_error("Rast3d_mask_reopen: error closing mask");
144 
145  return 0;
146  }
147 
148  tmp = maskOpenOldCacheDefault;
149  maskOpenOldCacheDefault = cache;
150 
151  if (!Rast3d_mask_open_old()) {
152  Rast3d_error("Rast3d_mask_reopen: error opening mask");
153 
154  return 0;
155  }
156 
157  maskOpenOldCacheDefault = tmp;
158  return 1;
159 }
160 
161 /*--------------------------------------------------------------------------*/
162 
163 /*!
164  * \brief
165  *
166  * Returns 1 if the cell with cell-coordinates <em>(x, y, z)</em> is masked
167  * out. Returns 0 otherwise.
168  *
169  * \param x
170  * \param y
171  * \param z
172  * \return int
173  */
174 
175 int Rast3d_is_masked(RASTER3D_Map *map, int x, int y, int z)
176 {
177  if (!Rast3d_maskMapExistsVar)
178  return 0;
179 
180  RASTER3D_MASKNUMmaskValue = Rast3d_getMaskFloat(map, x, y, z);
181  return (Rast3d_is_null_value_num(&RASTER3D_MASKNUMmaskValue, FCELL_TYPE));
182 }
183 
184 /*--------------------------------------------------------------------------*/
185 
186 /*!
187  * \brief
188  *
189  * Replaces the value stored in <em>value</em> with the NULL-value if
190  * <em>Rast3d_is_masked (x, y, z)</em> returns 1. Does nothing otherwise.
191  * <em>value</em> is assumed to be of<em>type</em>.
192  *
193  * \param x
194  * \param y
195  * \param z
196  * \param value
197  * \param type
198  * \return void
199  */
200 
201 void Rast3d_mask_num(RASTER3D_Map *map, int x, int y, int z, void *value,
202  int type)
203 {
204  if (!Rast3d_maskMapExistsVar)
205  return;
206  RASTER3D_MASKNUM(map, x, y, z, value, type);
207 }
208 
209 /*--------------------------------------------------------------------------*/
210 
211 /*!
212  * \brief
213  *
214  * Same as <em>Rast3d_mask_num (x, y, z, value, FCELL_TYPE)</em>.
215  *
216  * \param x
217  * \param y
218  * \param z
219  * \param value
220  * \return void
221  */
222 
223 void Rast3d_mask_float(RASTER3D_Map *map, int x, int y, int z, float *value)
224 {
225  if (!Rast3d_maskMapExistsVar)
226  return;
227  RASTER3D_MASKNUM(map, x, y, z, value, FCELL_TYPE);
228 }
229 
230 /*--------------------------------------------------------------------------*/
231 
232 /*!
233  * \brief
234  *
235  * Same as <em>Rast3d_mask_num (x, y, z, value, DCELL_TYPE)</em>.
236  *
237  * \param x
238  * \param y
239  * \param z
240  * \param value
241  * \return void
242  */
243 
244 void Rast3d_mask_double(RASTER3D_Map *map, int x, int y, int z, double *value)
245 {
246  if (!Rast3d_maskMapExistsVar)
247  return;
248  RASTER3D_MASKNUM(map, x, y, z, value, DCELL_TYPE);
249 }
250 
251 /*--------------------------------------------------------------------------*/
252 
253 /*!
254  * \brief
255  *
256  * Replaces the values stored in <em>tile</em> (with <em>tileIndex</em>) for
257  * which <em>Rast3d_is_masked</em> returns 1 with NULL-values. Does not change
258  * the remaining values. The values are assumed to be of <em>type</em>.
259  * Whether replacement is performed or not only depends on location of the
260  * cells of the tile and not on the status of the mask for <em>map</em>
261  * (i.e. turned on or off).
262  *
263  * \param map
264  * \param tileIndex
265  * \param tile
266  * \param type
267  * \return void
268  */
269 
270 void Rast3d_mask_tile(RASTER3D_Map *map, int tileIndex, void *tile, int type)
271 {
272  int nofNum, rows, cols, depths, xRedundant, yRedundant, zRedundant;
273  int x, y, z, xLength, yLength, dx, dy, dz, length;
274 
275  if (!Rast3d_maskMapExistsVar)
276  return;
277 
278  nofNum = Rast3d_compute_clipped_tile_dimensions(map, tileIndex, &rows,
279  &cols, &depths, &xRedundant,
280  &yRedundant, &zRedundant);
281  Rast3d_tile_index_origin(map, tileIndex, &x, &y, &z);
282 
283  if (nofNum == map->tileSize) {
284  /*AV*/
285  /* BEGIN OF ORIGINAL CODE */
286  /*
287  * Rast3d_get_tile_dimensions_map (map, &rows, &cols, &depths);
288  */
289  /*AV*/
290  /* BEGIN OF MY CODE */
291  Rast3d_get_tile_dimensions_map(map, &cols, &rows, &depths);
292  /* END OF MY CODE */
293  xRedundant = yRedundant = 0;
294  }
295 
296  rows += y;
297  cols += x;
298  depths += z;
299  length = Rast3d_length(type);
300  xLength = xRedundant * length;
301  yLength = map->tileX * yRedundant * length;
302 
303  for (dz = z; dz < depths; dz++) {
304  for (dy = y; dy < rows; dy++) {
305  for (dx = x; dx < cols; dx++) {
306  RASTER3D_MASKNUM(map, dx, dy, dz, tile, type);
307  tile = (char *)tile + length;
308  }
309 
310  tile = (char *)tile + xLength;
311  }
312  tile = (char *)tile + yLength;
313  }
314 }
315 
316 /*--------------------------------------------------------------------------*/
317 
318 /*!
319  * \brief
320  *
321  * Turns on the mask for <em>map</em>. Do
322  * not invoke this function after the first tile has been read since the result
323  * might be inconsistent cell-values.
324  *
325  * \param map
326  * \return void
327  */
328 
330 {
331  map->useMask = 1;
332 }
333 
334 /*!
335  * \brief
336  *
337  * Turns off the mask for <em>map</em>.
338  * This is the default. Do not invoke this function after the first tile has
339  * been read since the result might be inconsistent cell-values.
340  *
341  * \param map
342  * \return void
343  */
344 
346 {
347  map->useMask = 0;
348 }
349 
350 /*!
351  * \brief
352  *
353  * Returns 1 if the mask for <em>map</em>
354  * is turned on. Returns 0 otherwise.
355  *
356  * \param map
357  * \return int
358  */
359 
361 {
362  return map->useMask;
363 }
364 
365 /*!
366  * \brief
367  *
368  * Returns 1 if the mask for <em>map</em> is turned off. Returns 0 otherwise.
369  *
370  * \param map
371  * \return int
372  */
373 
375 {
376  return !map->useMask;
377 }
378 
379 /*!
380  * \brief
381  *
382  * Returns the name of the 3d mask file.
383  *
384  * \return char *
385  */
386 
387 const char *Rast3d_mask_file(void)
388 {
389  return RASTER3D_MASK_MAP;
390 }
391 
392 /*!
393  * \brief
394  *
395  * Returns 1 if the 3d mask is loaded.
396  *
397  * \return int
398  */
399 
401 {
402  return Rast3d_maskMapExistsVar;
403 }
#define NULL
Definition: ccmath.h:32
const char * G_find_file_misc(const char *, const char *, char *, const char *)
Searches for a misc file from the mapset search list or in a specified mapset.
Definition: find_file.c:208
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
void * Rast3d_open_cell_old(const char *, const char *, RASTER3D_Region *, int, int)
Opens existing g3d-file name in mapset. Tiles are stored in memory with type which must be any of FCE...
Definition: raster3d/open.c:80
void Rast3d_get_region_value(RASTER3D_Map *, double, double, double, void *, int)
Returns in value the value of the map which corresponds to region coordinates (north,...
Definition: getvalue.c:131
void Rast3d_get_tile_dimensions_map(RASTER3D_Map *, int *, int *, int *)
Returns the tile dimensions used for map.
Definition: headerinfo.c:138
void Rast3d_tile_index_origin(RASTER3D_Map *, int, int *, int *, int *)
Computes the cell-coordinates (x, y, z) which correspond to the origin of the tile with tileIndex.
Definition: tilemath.c:97
void Rast3d_get_region_struct_map(RASTER3D_Map *, RASTER3D_Region *)
Returns in region the region of map.
Definition: headerinfo.c:112
int Rast3d_is_null_value_num(const void *, int)
Definition: null.c:12
int Rast3d_length(int)
Definition: raster3d/misc.c:75
int Rast3d_compute_clipped_tile_dimensions(RASTER3D_Map *, int, int *, int *, int *, int *, int *, int *)
Computes the dimensions of the tile when clipped to fit the region of map. The clipped dimensions are...
Definition: tilemath.c:253
void Rast3d_error(const char *,...) __attribute__((format(printf
int Rast3d_close(RASTER3D_Map *)
Close 3D raster map files.
void Rast3d_set_window_map(RASTER3D_Map *, RASTER3D_Region *)
Sets the window for map to window. Can be used multiple times for the same map.
void Rast3d_mask_on(RASTER3D_Map *map)
Turns on the mask for map. Do not invoke this function after the first tile has been read since the r...
Definition: mask.c:329
void Rast3d_mask_off(RASTER3D_Map *map)
Turns off the mask for map. This is the default. Do not invoke this function after the first tile has...
Definition: mask.c:345
void Rast3d_mask_double(RASTER3D_Map *map, int x, int y, int z, double *value)
Same as Rast3d_mask_num (x, y, z, value, DCELL_TYPE).
Definition: mask.c:244
#define RASTER3D_MASKNUM(map, Xmask, Ymask, Zmask, VALUEmask, TYPEmask)
Definition: mask.c:27
int Rast3d_mask_map_exists(void)
Returns 1 if the 3d mask is loaded.
Definition: mask.c:400
int Rast3d_mask_open_old(void)
Definition: mask.c:74
int Rast3d_is_masked(RASTER3D_Map *map, int x, int y, int z)
Returns 1 if the cell with cell-coordinates (x, y, z) is masked out. Returns 0 otherwise.
Definition: mask.c:175
int Rast3d_mask_file_exists(void)
Returns 1 if the 3d mask file exists.
Definition: mask.c:64
const char * Rast3d_mask_file(void)
Returns the name of the 3d mask file.
Definition: mask.c:387
int Rast3d_mask_close(void)
Definition: mask.c:37
int Rast3d_mask_is_on(RASTER3D_Map *map)
Returns 1 if the mask for map is turned on. Returns 0 otherwise.
Definition: mask.c:360
int Rast3d_mask_reopen(int cache)
This function should be used to adjust the cache size used for the 3d-mask. First the open 3d-mask is...
Definition: mask.c:137
void Rast3d_mask_float(RASTER3D_Map *map, int x, int y, int z, float *value)
Same as Rast3d_mask_num (x, y, z, value, FCELL_TYPE).
Definition: mask.c:223
void Rast3d_mask_tile(RASTER3D_Map *map, int tileIndex, void *tile, int type)
Replaces the values stored in tile (with tileIndex) for which Rast3d_is_masked returns 1 with NULL-va...
Definition: mask.c:270
int Rast3d_mask_is_off(RASTER3D_Map *map)
Returns 1 if the mask for map is turned off. Returns 0 otherwise.
Definition: mask.c:374
void Rast3d_mask_num(RASTER3D_Map *map, int x, int y, int z, void *value, int type)
Replaces the value stored in value with the NULL-value if Rast3d_is_masked (x, y, z) returns 1....
Definition: mask.c:201
#define RASTER3D_DIRECTORY
Definition: raster3d.h:31
#define RASTER3D_DEFAULT_WINDOW
Definition: raster3d.h:29
#define RASTER3D_USE_CACHE_DEFAULT
Definition: raster3d.h:19
#define RASTER3D_MASK_MAP
Definition: raster3d.h:39
#define RASTER3D_CELL_ELEMENT
Definition: raster3d.h:32
#define FCELL_TYPE
Definition: raster.h:12
#define DCELL_TYPE
Definition: raster.h:13
RASTER3D_Region window
Definition: raster3d.h:85
int tileSize
Definition: raster3d.h:180
double north
Definition: raster3d.h:49
double south
Definition: raster3d.h:49
double east
Definition: raster3d.h:50
double bottom
Definition: raster3d.h:51
double top
Definition: raster3d.h:51
double west
Definition: raster3d.h:50
#define x