GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
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  */
64 {
67 }
68 
69 /*--------------------------------------------------------------------------*/
70 
71 static int maskOpenOldCacheDefault = RASTER3D_USE_CACHE_DEFAULT;
72 
74 {
75  RASTER3D_Region region;
76 
77  /* No Idea if this is correct return value */
78  if (Rast3d_maskMapExistsVar)
79  return 1;
80 
81  Rast3d_maskMapExistsVar = Rast3d_mask_file_exists();
82 
83  if (!Rast3d_maskMapExistsVar)
84  return 1;
85 
86  if ((Rast3d_maskMap = Rast3d_open_cell_old(
88  maskOpenOldCacheDefault)) == NULL) {
89  Rast3d_error("Rast3d_mask_open_old: cannot open mask");
90 
91  return 0;
92  }
93 
94  Rast3d_get_region_struct_map(Rast3d_maskMap, &region);
95  Rast3d_set_window_map(Rast3d_maskMap, &region);
96 
97  return 1;
98 }
99 
100 /*--------------------------------------------------------------------------*/
101 
102 static float Rast3d_getMaskFloat(RASTER3D_Map *map, int x, int y, int z)
103 {
104  double north, east, top;
105  float value;
106 
107  north = ((double)map->window.rows - y - 0.5) / (double)map->window.rows *
108  (map->window.north - map->window.south) +
109  map->window.south;
110  east = ((double)x + 0.5) / (double)map->window.cols *
111  (map->window.east - map->window.west) +
112  map->window.west;
113  top = ((double)z + 0.5) / (double)map->window.depths *
114  (map->window.top - map->window.bottom) +
115  map->window.bottom;
116 
117  Rast3d_get_region_value(Rast3d_maskMap, north, east, top, &value,
118  FCELL_TYPE);
119  return value;
120 }
121 
122 /*--------------------------------------------------------------------------*/
123 
124 /*!
125  * \brief
126  *
127  * This function should be used to adjust the cache size used for the
128  * 3d-mask. First the open 3d-mask is closed and then opened again with
129  * a cache size as specified with <em>cache</em>.
130  *
131  * \param cache
132  * \return 1 ... if successful
133  * 0 ... otherwise.
134  */
135 int Rast3d_mask_reopen(int cache)
136 {
137  int tmp;
138 
139  if (Rast3d_maskMapExistsVar)
140  if (!Rast3d_mask_close()) {
141  Rast3d_error("Rast3d_mask_reopen: error closing mask");
142 
143  return 0;
144  }
145 
146  tmp = maskOpenOldCacheDefault;
147  maskOpenOldCacheDefault = cache;
148 
149  if (!Rast3d_mask_open_old()) {
150  Rast3d_error("Rast3d_mask_reopen: error opening mask");
151 
152  return 0;
153  }
154 
155  maskOpenOldCacheDefault = tmp;
156  return 1;
157 }
158 
159 /*--------------------------------------------------------------------------*/
160 
161 /*!
162  * \brief
163  *
164  * Returns 1 if the cell with cell-coordinates <em>(x, y, z)</em> is masked
165  * out. Returns 0 otherwise.
166  *
167  * \param x
168  * \param y
169  * \param z
170  * \return int
171  */
172 int Rast3d_is_masked(RASTER3D_Map *map, int x, int y, int z)
173 {
174  if (!Rast3d_maskMapExistsVar)
175  return 0;
176 
177  RASTER3D_MASKNUMmaskValue = Rast3d_getMaskFloat(map, x, y, z);
178  return (Rast3d_is_null_value_num(&RASTER3D_MASKNUMmaskValue, FCELL_TYPE));
179 }
180 
181 /*--------------------------------------------------------------------------*/
182 
183 /*!
184  * \brief
185  *
186  * Replaces the value stored in <em>value</em> with the NULL-value if
187  * <em>Rast3d_is_masked (x, y, z)</em> returns 1. Does nothing otherwise.
188  * <em>value</em> is assumed to be of<em>type</em>.
189  *
190  * \param map
191  * \param x
192  * \param y
193  * \param z
194  * \param value
195  * \param type
196  * \return void
197  */
198 void Rast3d_mask_num(RASTER3D_Map *map, int x, int y, int z, void *value,
199  int type)
200 {
201  if (!Rast3d_maskMapExistsVar)
202  return;
203  RASTER3D_MASKNUM(map, x, y, z, value, type);
204 }
205 
206 /*--------------------------------------------------------------------------*/
207 
208 /*!
209  * \brief
210  *
211  * Same as <em>Rast3d_mask_num (x, y, z, value, FCELL_TYPE)</em>.
212  *
213  * \param map
214  * \param x
215  * \param y
216  * \param z
217  * \param value
218  * \return void
219  */
220 void Rast3d_mask_float(RASTER3D_Map *map, int x, int y, int z, float *value)
221 {
222  if (!Rast3d_maskMapExistsVar)
223  return;
224  RASTER3D_MASKNUM(map, x, y, z, value, FCELL_TYPE);
225 }
226 
227 /*--------------------------------------------------------------------------*/
228 
229 /*!
230  * \brief
231  *
232  * Same as <em>Rast3d_mask_num (x, y, z, value, DCELL_TYPE)</em>.
233  *
234  * \param map
235  * \param x
236  * \param y
237  * \param z
238  * \param value
239  * \return void
240  */
241 void Rast3d_mask_double(RASTER3D_Map *map, int x, int y, int z, double *value)
242 {
243  if (!Rast3d_maskMapExistsVar)
244  return;
245  RASTER3D_MASKNUM(map, x, y, z, value, DCELL_TYPE);
246 }
247 
248 /*--------------------------------------------------------------------------*/
249 
250 /*!
251  * \brief
252  *
253  * Replaces the values stored in <em>tile</em> (with <em>tileIndex</em>) for
254  * which <em>Rast3d_is_masked</em> returns 1 with NULL-values. Does not change
255  * the remaining values. The values are assumed to be of <em>type</em>.
256  * Whether replacement is performed or not only depends on location of the
257  * cells of the tile and not on the status of the mask for <em>map</em>
258  * (i.e. turned on or off).
259  *
260  * \param map
261  * \param tileIndex
262  * \param tile
263  * \param type
264  * \return void
265  */
266 void Rast3d_mask_tile(RASTER3D_Map *map, int tileIndex, void *tile, int type)
267 {
268  int nofNum, rows, cols, depths, xRedundant, yRedundant, zRedundant;
269  int x, y, z, xLength, yLength, dx, dy, dz, length;
270 
271  if (!Rast3d_maskMapExistsVar)
272  return;
273 
274  nofNum = Rast3d_compute_clipped_tile_dimensions(map, tileIndex, &rows,
275  &cols, &depths, &xRedundant,
276  &yRedundant, &zRedundant);
277  Rast3d_tile_index_origin(map, tileIndex, &x, &y, &z);
278 
279  if (nofNum == map->tileSize) {
280  /*AV*/
281  /* BEGIN OF ORIGINAL CODE */
282  /*
283  * Rast3d_get_tile_dimensions_map (map, &rows, &cols, &depths);
284  */
285  /*AV*/
286  /* BEGIN OF MY CODE */
287  Rast3d_get_tile_dimensions_map(map, &cols, &rows, &depths);
288  /* END OF MY CODE */
289  xRedundant = yRedundant = 0;
290  }
291 
292  rows += y;
293  cols += x;
294  depths += z;
295  length = Rast3d_length(type);
296  xLength = xRedundant * length;
297  yLength = map->tileX * yRedundant * length;
298 
299  for (dz = z; dz < depths; dz++) {
300  for (dy = y; dy < rows; dy++) {
301  for (dx = x; dx < cols; dx++) {
302  RASTER3D_MASKNUM(map, dx, dy, dz, tile, type);
303  tile = (char *)tile + length;
304  }
305 
306  tile = (char *)tile + xLength;
307  }
308  tile = (char *)tile + yLength;
309  }
310 }
311 
312 /*--------------------------------------------------------------------------*/
313 
314 /*!
315  * \brief
316  *
317  * Turns on the mask for <em>map</em>. Do
318  * not invoke this function after the first tile has been read since the result
319  * might be inconsistent cell-values.
320  *
321  * \param map
322  * \return void
323  */
325 {
326  map->useMask = 1;
327 }
328 
329 /*!
330  * \brief
331  *
332  * Turns off the mask for <em>map</em>.
333  * This is the default. Do not invoke this function after the first tile has
334  * been read since the result might be inconsistent cell-values.
335  *
336  * \param map
337  * \return void
338  */
340 {
341  map->useMask = 0;
342 }
343 
344 /*!
345  * \brief
346  *
347  * Returns 1 if the mask for <em>map</em>
348  * is turned on. Returns 0 otherwise.
349  *
350  * \param map
351  * \return int
352  */
354 {
355  return map->useMask;
356 }
357 
358 /*!
359  * \brief
360  *
361  * Returns 1 if the mask for <em>map</em> is turned off. Returns 0 otherwise.
362  *
363  * \param map
364  * \return int
365  */
367 {
368  return !map->useMask;
369 }
370 
371 /*!
372  * \brief
373  *
374  * Returns the name of the 3d mask file.
375  *
376  * \return char *
377  */
378 const char *Rast3d_mask_file(void)
379 {
380  return RASTER3D_MASK_MAP;
381 }
382 
383 /*!
384  * \brief
385  *
386  * Returns 1 if the 3d mask is loaded.
387  *
388  * \return int
389  */
391 {
392  return Rast3d_maskMapExistsVar;
393 }
#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:79
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:126
void Rast3d_get_tile_dimensions_map(RASTER3D_Map *, int *, int *, int *)
Returns the tile dimensions used for map.
Definition: headerinfo.c:133
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:93
void Rast3d_get_region_struct_map(RASTER3D_Map *, RASTER3D_Region *)
Returns in region the region of map.
Definition: headerinfo.c:108
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:243
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:324
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:339
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:241
#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:390
int Rast3d_mask_open_old(void)
Definition: mask.c:73
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:172
int Rast3d_mask_file_exists(void)
Returns 1 if the 3d mask file exists.
Definition: mask.c:63
const char * Rast3d_mask_file(void)
Returns the name of the 3d mask file.
Definition: mask.c:378
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:353
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:135
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:220
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:266
int Rast3d_mask_is_off(RASTER3D_Map *map)
Returns 1 if the mask for map is turned off. Returns 0 otherwise.
Definition: mask.c:366
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:198
#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