GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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 }