GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
g3dmask.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <grass/gis.h>
3 #include "G3d_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 G3d_maskMapExistsVar = 0;
16 static G3D_Map *G3d_maskMap;
17 
18 /*--------------------------------------------------------------------------*/
19 static void dummy(void)
20 {
21  return;
22 }
23 
24 
25 static float G3D_MASKNUMmaskValue;
26 
27 /* Call to dummy() to match void return type of G3d_setNullValue() */
28 #define G3D_MASKNUM(map,Xmask,Ymask,Zmask,VALUEmask,TYPEmask) \
29 \
30  (G3D_MASKNUMmaskValue = G3d_getMaskFloat (map, Xmask, Ymask, Zmask), \
31  ((G3d_isNullValueNum (&G3D_MASKNUMmaskValue, FCELL_TYPE)) ? \
32  G3d_setNullValue (VALUEmask, 1, TYPEmask) : dummy()))
33 
34 /*--------------------------------------------------------------------------*/
35 
37 {
38  /* No Idea if this is correct return value */
39  if (!G3d_maskMapExistsVar)
40  return 1;
41 
42  G3d_maskMapExistsVar = 0;
43 
44  if (!G3d_closeCell(G3d_maskMap)) {
45  G3d_error("G3d_maskClose: error closing mask");
46 
47  return 0;
48  }
49 
50  return 1;
51 }
52 
53 /*--------------------------------------------------------------------------*/
54 
55 
65 {
66  char buf[200];
67 
68  sprintf(buf, "%s/%s", G3D_DIRECTORY, G3D_MASK_MAP);
69  return (G_find_file(buf, G3D_CELL_ELEMENT, G_mapset()) != NULL);
70 }
71 
72 /*--------------------------------------------------------------------------*/
73 
74 static int maskOpenOldCacheDefault = G3D_USE_CACHE_DEFAULT;
75 
77 {
78  G3D_Region region;
79 
80  /* No Idea if this is correct return value */
81  if (G3d_maskMapExistsVar)
82  return 1;
83 
84  G3d_maskMapExistsVar = G3d_maskFileExists();
85 
86  if (!G3d_maskMapExistsVar)
87  return 1;
88 
89  if ((G3d_maskMap = G3d_openCellOld(G3D_MASK_MAP, G_mapset(),
90  G3D_DEFAULT_WINDOW, FCELL_TYPE,
91  maskOpenOldCacheDefault))
92  == NULL) {
93  G3d_error("G3d_maskOpenOld: cannot open mask");
94 
95  return 0;
96  }
97 
98  G3d_getRegionStructMap(G3d_maskMap, &region);
99  G3d_setWindowMap(G3d_maskMap, &region);
100 
101  return 1;
102 }
103 
104 /*--------------------------------------------------------------------------*/
105 
106 static float G3d_getMaskFloat(G3D_Map * map, int x, int y, int z)
107 {
108  double north, east, top;
109  float value;
110 
111  north = ((double)map->window.rows - y - 0.5) / (double)map->window.rows *
112  (map->window.north - map->window.south) + map->window.south;
113  east = ((double)x + 0.5) / (double)map->window.cols *
114  (map->window.east - map->window.west) + map->window.west;
115  top = ((double)z + 0.5) / (double)map->window.depths *
116  (map->window.top - map->window.bottom) + map->window.bottom;
117 
118  G3d_getRegionValue(G3d_maskMap, north, east, top, &value, FCELL_TYPE);
119  return value;
120 }
121 
122 /*--------------------------------------------------------------------------*/
123 
124 
137 int G3d_maskReopen(int cache)
138 {
139  int tmp;
140 
141  if (G3d_maskMapExistsVar)
142  if (!G3d_maskClose()) {
143  G3d_error("G3d_maskReopen: error closing mask");
144 
145  return 0;
146  }
147 
148  tmp = maskOpenOldCacheDefault;
149  maskOpenOldCacheDefault = cache;
150 
151  if (!G3d_maskOpenOld()) {
152  G3d_error("G3d_maskReopen: error opening mask");
153 
154  return 0;
155  }
156 
157  maskOpenOldCacheDefault = tmp;
158  return 1;
159 }
160 
161 /*--------------------------------------------------------------------------*/
162 
163 
176 int G3d_isMasked(G3D_Map * map, int x, int y, int z)
177 {
178  if (!G3d_maskMapExistsVar)
179  return 0;
180 
181  G3D_MASKNUMmaskValue = G3d_getMaskFloat(map, x, y, z);
182  return (G3d_isNullValueNum(&G3D_MASKNUMmaskValue, FCELL_TYPE));
183 }
184 
185 /*--------------------------------------------------------------------------*/
186 
187 
203 void G3d_maskNum(G3D_Map * map, int x, int y, int z, void *value, int type)
204 {
205  if (!G3d_maskMapExistsVar)
206  return;
207  G3D_MASKNUM(map, x, y, z, value, type);
208 }
209 
210 /*--------------------------------------------------------------------------*/
211 
212 
225 void G3d_maskFloat(G3D_Map * map, int x, int y, int z, float *value)
226 {
227  if (!G3d_maskMapExistsVar)
228  return;
229  G3D_MASKNUM(map, x, y, z, value, FCELL_TYPE);
230 }
231 
232 /*--------------------------------------------------------------------------*/
233 
234 
247 void G3d_maskDouble(G3D_Map * map, int x, int y, int z, double *value)
248 {
249  if (!G3d_maskMapExistsVar)
250  return;
251  G3D_MASKNUM(map, x, y, z, value, DCELL_TYPE);
252 }
253 
254 /*--------------------------------------------------------------------------*/
255 
256 
274 void G3d_maskTile(G3D_Map * map, int tileIndex, void *tile, int type)
275 {
276  int nofNum, rows, cols, depths, xRedundant, yRedundant, zRedundant;
277  int x, y, z, xLength, yLength, dx, dy, dz, length;
278 
279  if (!G3d_maskMapExistsVar)
280  return;
281 
282  nofNum = G3d_computeClippedTileDimensions(map, tileIndex,
283  &rows, &cols, &depths,
284  &xRedundant, &yRedundant,
285  &zRedundant);
286  G3d_tileIndexOrigin(map, tileIndex, &x, &y, &z);
287 
288  if (nofNum == map->tileSize) {
289  /*AV*/
290  /* BEGIN OF ORIGINAL CODE */
291  /*
292  * G3d_getTileDimensionsMap (map, &rows, &cols, &depths);
293  */
294  /*AV*/
295  /* BEGIN OF MY CODE */
296  G3d_getTileDimensionsMap(map, &cols, &rows, &depths);
297  /* END OF MY CODE */
298  xRedundant = yRedundant = 0;
299  }
300 
301  rows += y;
302  cols += x;
303  depths += z;
304  length = G3d_length(type);
305  xLength = xRedundant * length;
306  yLength = map->tileX * yRedundant * length;
307 
308  for (dz = z; dz < depths; dz++) {
309  for (dy = y; dy < rows; dy++) {
310  for (dx = x; dx < cols; dx++) {
311  G3D_MASKNUM(map, dx, dy, dz, tile, type);
312  tile += length;
313  }
314 
315  tile += xLength;
316  }
317  tile += yLength;
318  }
319 }
320 
321 /*--------------------------------------------------------------------------*/
322 
323 
335 void G3d_maskOn(G3D_Map * map)
336 {
337  map->useMask = 1;
338 }
339 
340 
352 void G3d_maskOff(G3D_Map * map)
353 {
354  map->useMask = 0;
355 }
356 
357 
368 int G3d_maskIsOn(G3D_Map * map)
369 {
370  return map->useMask;
371 }
372 
373 
383 int G3d_maskIsOff(G3D_Map * map)
384 {
385  return !map->useMask;
386 }
387 
388 
397 const char *G3d_maskFile(void)
398 {
399  return G3D_MASK_MAP;
400 }
401 
402 
412 {
413  return G3d_maskMapExistsVar;
414 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
void G3d_maskFloat(G3D_Map *map, int x, int y, int z, float *value)
Same as G3d_maskNum (x, y, z, value, FCELL_TYPE).
Definition: g3dmask.c:225
void G3d_maskOff(G3D_Map *map)
Turns off the mask for map. This is the default. Do not invoke this function after the first tile has...
Definition: g3dmask.c:352
void G3d_getRegionValue(G3D_Map *map, double north, double east, double top, void *value, int type)
Returns in value the value of the map which corresponds to region coordinates (north, east, top). The value is resampled using the resampling function specified for map. The value is of type.
Definition: g3dregion.c:239
int G3d_maskClose()
Definition: g3dmask.c:36
void G3d_error(const char *msg,...)
Definition: g3derror.c:75
void G3d_tileIndexOrigin(G3D_Map *map, int tileIndex, int *x, int *y, int *z)
Computes the cell-coordinates (x, y, z) which correspond to the origin of the tile with tileIndex...
Definition: tilemath.c:103
void G3d_getTileDimensionsMap(G3D_Map *map, int *x, int *y, int *z)
Returns the tile dimensions used for map.
Definition: headerinfo.c:142
void G3d_getRegionStructMap(G3D_Map *map, G3D_Region *region)
Returns in region the region of map.
Definition: headerinfo.c:115
const char * G3d_maskFile(void)
Returns the name of the 3d mask file.
Definition: g3dmask.c:397
int G3d_isNullValueNum(const void *n, int type)
Definition: g3dnull.c:9
void G3d_maskNum(G3D_Map *map, int x, int y, int z, void *value, int type)
Replaces the value stored in value with the NULL-value if G3d_isMasked (x, y, z) returns 1...
Definition: g3dmask.c:203
#define G3D_MASKNUM(map, Xmask, Ymask, Zmask, VALUEmask, TYPEmask)
Definition: g3dmask.c:28
int y
Definition: plot.c:34
void G3d_maskOn(G3D_Map *map)
Turns on the mask for map. Do not invoke this function after the first tile has been read since the r...
Definition: g3dmask.c:335
int G3d_maskMapExists(void)
Returns 1 if the 3d mask is loaded.
Definition: g3dmask.c:411
int G3d_maskIsOff(G3D_Map *map)
Returns 1 if the mask for map is turned off. Returns 0 otherwise.
Definition: g3dmask.c:383
char * value
Definition: env.c:30
int G3d_closeCell(G3D_Map *map)
Closes g3d-file. If map is new and cache-mode is used for map then every tile which is not flushed be...
Definition: g3dclose.c:144
int G3d_maskReopen(int cache)
This function should be used to adjust the cache size used for the 3d-mask. First the open 3d-mask is...
Definition: g3dmask.c:137
int G3d_maskOpenOld()
Definition: g3dmask.c:76
int G3d_maskIsOn(G3D_Map *map)
Returns 1 if the mask for map is turned on. Returns 0 otherwise.
Definition: g3dmask.c:368
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
int G3d_computeClippedTileDimensions(G3D_Map *map, int tileIndex, int *rows, int *cols, int *depths, int *xRedundant, int *yRedundant, int *zRedundant)
Computes the dimensions of the tile when clipped to fit the region of map. The clipped dimensions are...
Definition: tilemath.c:267
return NULL
Definition: dbfopen.c:1394
void G3d_setWindowMap(G3D_Map *map, G3D_Region *window)
Sets the window for map to window. Can be used multiple times for the same map.
Definition: g3dwindow.c:24
void * G3d_openCellOld(const char *name, const char *mapset, G3D_Region *window, int typeIntern, int cache)
Opens existing g3d-file name in mapset. Tiles are stored in memory with type which must be any of FCE...
Definition: g3dopen.c:85
tuple cols
int G3d_isMasked(G3D_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: g3dmask.c:176
int G3d_length(int t)
Definition: g3dmisc.c:77
int G3d_maskFileExists()
Returns 1 if the 3d mask file exists.
Definition: g3dmask.c:64
char * G_find_file(const char *element, char *name, const char *mapset)
searches for a file from the mapset search list or in a specified mapset. returns the mapset name whe...
Definition: find_file.c:159
void G3d_maskDouble(G3D_Map *map, int x, int y, int z, double *value)
Same as G3d_maskNum (x, y, z, value, DCELL_TYPE).
Definition: g3dmask.c:247
void G3d_maskTile(G3D_Map *map, int tileIndex, void *tile, int type)
Replaces the values stored in tile (with tileIndex) for which G3d_isMasked returns 1 with NULL-values...
Definition: g3dmask.c:274