GRASS Programmer's Manual  6.4.2(2012)
g3dmask.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <grass/gis.h>
00003 #include "G3d_intern.h"
00004 
00005 /*--------------------------------------------------------------------------*/
00006 
00007 /* the standard g3d file format is used to store the mask values. a NULL-value
00008    is stored for values which are masked out and a "0." is stored for values 
00009    which are not masked out. to improve compression, the precision is set to 
00010    0 and RLE encoding is used.
00011  */
00012 
00013 /*--------------------------------------------------------------------------*/
00014 
00015 static int G3d_maskMapExistsVar = 0;
00016 static G3D_Map *G3d_maskMap;
00017 
00018 /*--------------------------------------------------------------------------*/
00019 static void dummy(void)
00020 {
00021     return;
00022 }
00023 
00024 
00025 static float G3D_MASKNUMmaskValue;
00026 
00027 /* Call to dummy() to match void return type of G3d_setNullValue() */
00028 #define G3D_MASKNUM(map,Xmask,Ymask,Zmask,VALUEmask,TYPEmask) \
00029 \
00030    (G3D_MASKNUMmaskValue = G3d_getMaskFloat (map, Xmask, Ymask, Zmask), \
00031     ((G3d_isNullValueNum (&G3D_MASKNUMmaskValue, FCELL_TYPE)) ? \
00032       G3d_setNullValue (VALUEmask, 1, TYPEmask) : dummy()))
00033 
00034 /*--------------------------------------------------------------------------*/
00035 
00036 int G3d_maskClose()
00037 {
00038     /* No Idea if this is correct return value */
00039     if (!G3d_maskMapExistsVar)
00040         return 1;
00041 
00042     G3d_maskMapExistsVar = 0;
00043 
00044     if (!G3d_closeCell(G3d_maskMap)) {
00045         G3d_error("G3d_maskClose: error closing mask");
00046 
00047         return 0;
00048     }
00049 
00050     return 1;
00051 }
00052 
00053 /*--------------------------------------------------------------------------*/
00054 
00055 
00064 int G3d_maskFileExists()
00065 {
00066     char buf[200];
00067 
00068     sprintf(buf, "%s/%s", G3D_DIRECTORY, G3D_MASK_MAP);
00069     return (G_find_file(buf, G3D_CELL_ELEMENT, G_mapset()) != NULL);
00070 }
00071 
00072 /*--------------------------------------------------------------------------*/
00073 
00074 static int maskOpenOldCacheDefault = G3D_USE_CACHE_DEFAULT;
00075 
00076 int G3d_maskOpenOld()
00077 {
00078     G3D_Region region;
00079 
00080     /* No Idea if this is correct return value */
00081     if (G3d_maskMapExistsVar)
00082         return 1;
00083 
00084     G3d_maskMapExistsVar = G3d_maskFileExists();
00085 
00086     if (!G3d_maskMapExistsVar)
00087         return 1;
00088 
00089     if ((G3d_maskMap = G3d_openCellOld(G3D_MASK_MAP, G_mapset(),
00090                                        G3D_DEFAULT_WINDOW, FCELL_TYPE,
00091                                        maskOpenOldCacheDefault))
00092         == NULL) {
00093         G3d_error("G3d_maskOpenOld: cannot open mask");
00094 
00095         return 0;
00096     }
00097 
00098     G3d_getRegionStructMap(G3d_maskMap, &region);
00099     G3d_setWindowMap(G3d_maskMap, &region);
00100 
00101     return 1;
00102 }
00103 
00104 /*--------------------------------------------------------------------------*/
00105 
00106 static float G3d_getMaskFloat(G3D_Map * map, int x, int y, int z)
00107 {
00108     double north, east, top;
00109     float value;
00110 
00111     north = ((double)map->window.rows - y - 0.5) / (double)map->window.rows *
00112         (map->window.north - map->window.south) + map->window.south;
00113     east = ((double)x + 0.5) / (double)map->window.cols *
00114         (map->window.east - map->window.west) + map->window.west;
00115     top = ((double)z + 0.5) / (double)map->window.depths *
00116         (map->window.top - map->window.bottom) + map->window.bottom;
00117 
00118     G3d_getRegionValue(G3d_maskMap, north, east, top, &value, FCELL_TYPE);
00119     return value;
00120 }
00121 
00122 /*--------------------------------------------------------------------------*/
00123 
00124 
00137 int G3d_maskReopen(int cache)
00138 {
00139     int tmp;
00140 
00141     if (G3d_maskMapExistsVar)
00142         if (!G3d_maskClose()) {
00143             G3d_error("G3d_maskReopen: error closing mask");
00144 
00145             return 0;
00146         }
00147 
00148     tmp = maskOpenOldCacheDefault;
00149     maskOpenOldCacheDefault = cache;
00150 
00151     if (!G3d_maskOpenOld()) {
00152         G3d_error("G3d_maskReopen: error opening mask");
00153 
00154         return 0;
00155     }
00156 
00157     maskOpenOldCacheDefault = tmp;
00158     return 1;
00159 }
00160 
00161 /*--------------------------------------------------------------------------*/
00162 
00163 
00176 int G3d_isMasked(G3D_Map * map, int x, int y, int z)
00177 {
00178     if (!G3d_maskMapExistsVar)
00179         return 0;
00180 
00181     G3D_MASKNUMmaskValue = G3d_getMaskFloat(map, x, y, z);
00182     return (G3d_isNullValueNum(&G3D_MASKNUMmaskValue, FCELL_TYPE));
00183 }
00184 
00185 /*--------------------------------------------------------------------------*/
00186 
00187 
00203 void G3d_maskNum(G3D_Map * map, int x, int y, int z, void *value, int type)
00204 {
00205     if (!G3d_maskMapExistsVar)
00206         return;
00207     G3D_MASKNUM(map, x, y, z, value, type);
00208 }
00209 
00210 /*--------------------------------------------------------------------------*/
00211 
00212 
00225 void G3d_maskFloat(G3D_Map * map, int x, int y, int z, float *value)
00226 {
00227     if (!G3d_maskMapExistsVar)
00228         return;
00229     G3D_MASKNUM(map, x, y, z, value, FCELL_TYPE);
00230 }
00231 
00232 /*--------------------------------------------------------------------------*/
00233 
00234 
00247 void G3d_maskDouble(G3D_Map * map, int x, int y, int z, double *value)
00248 {
00249     if (!G3d_maskMapExistsVar)
00250         return;
00251     G3D_MASKNUM(map, x, y, z, value, DCELL_TYPE);
00252 }
00253 
00254 /*--------------------------------------------------------------------------*/
00255 
00256 
00274 void G3d_maskTile(G3D_Map * map, int tileIndex, void *tile, int type)
00275 {
00276     int nofNum, rows, cols, depths, xRedundant, yRedundant, zRedundant;
00277     int x, y, z, xLength, yLength, dx, dy, dz, length;
00278 
00279     if (!G3d_maskMapExistsVar)
00280         return;
00281 
00282     nofNum = G3d_computeClippedTileDimensions(map, tileIndex,
00283                                               &rows, &cols, &depths,
00284                                               &xRedundant, &yRedundant,
00285                                               &zRedundant);
00286     G3d_tileIndexOrigin(map, tileIndex, &x, &y, &z);
00287 
00288     if (nofNum == map->tileSize) {
00289          /*AV*/
00290             /* BEGIN OF ORIGINAL CODE */
00291             /*
00292              *    G3d_getTileDimensionsMap (map, &rows, &cols, &depths);
00293              */
00294              /*AV*/
00295             /* BEGIN OF MY CODE */
00296             G3d_getTileDimensionsMap(map, &cols, &rows, &depths);
00297         /* END OF MY CODE */
00298         xRedundant = yRedundant = 0;
00299     }
00300 
00301     rows += y;
00302     cols += x;
00303     depths += z;
00304     length = G3d_length(type);
00305     xLength = xRedundant * length;
00306     yLength = map->tileX * yRedundant * length;
00307 
00308     for (dz = z; dz < depths; dz++) {
00309         for (dy = y; dy < rows; dy++) {
00310             for (dx = x; dx < cols; dx++) {
00311                 G3D_MASKNUM(map, dx, dy, dz, tile, type);
00312                 tile += length;
00313             }
00314 
00315             tile += xLength;
00316         }
00317         tile += yLength;
00318     }
00319 }
00320 
00321 /*--------------------------------------------------------------------------*/
00322 
00323 
00335 void G3d_maskOn(G3D_Map * map)
00336 {
00337     map->useMask = 1;
00338 }
00339 
00340 
00352 void G3d_maskOff(G3D_Map * map)
00353 {
00354     map->useMask = 0;
00355 }
00356 
00357 
00368 int G3d_maskIsOn(G3D_Map * map)
00369 {
00370     return map->useMask;
00371 }
00372 
00373 
00383 int G3d_maskIsOff(G3D_Map * map)
00384 {
00385     return !map->useMask;
00386 }
00387 
00388 
00397 const char *G3d_maskFile(void)
00398 {
00399     return G3D_MASK_MAP;
00400 }
00401 
00402 
00411 int G3d_maskMapExists(void)
00412 {
00413     return G3d_maskMapExistsVar;
00414 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines