GRASS Programmer's Manual  6.4.2(2012)
tilemath.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <sys/types.h>
00004 #include <unistd.h>
00005 #include "G3d_intern.h"
00006 
00007 /*---------------------------------------------------------------------------*/
00008 
00009 
00024 void
00025 G3d_tileIndex2tile(G3D_Map * map, int tileIndex, int *xTile, int *yTile,
00026                    int *zTile)
00027 {
00028     int tileIndex2d;
00029 
00030     *zTile = tileIndex / map->nxy;
00031     tileIndex2d = tileIndex % map->nxy;
00032     *yTile = tileIndex2d / map->nx;
00033     *xTile = tileIndex2d % map->nx;
00034 }
00035 
00036 /*---------------------------------------------------------------------------*/
00037 
00038 
00052 int G3d_tile2tileIndex(G3D_Map * map, int xTile, int yTile, int zTile)
00053 {
00054     return map->nxy * zTile + map->nx * yTile + xTile;
00055 }
00056 
00057 /*---------------------------------------------------------------------------*/
00058 
00059 
00077 void
00078 G3d_tileCoordOrigin(G3D_Map * map, int xTile, int yTile, int zTile, int *x,
00079                     int *y, int *z)
00080 {
00081     *x = map->tileX * xTile;
00082     *y = map->tileY * yTile;
00083     *z = map->tileZ * zTile;
00084 }
00085 
00086 /*---------------------------------------------------------------------------*/
00087 
00088 
00103 void G3d_tileIndexOrigin(G3D_Map * map, int tileIndex, int *x, int *y, int *z)
00104 {
00105     int xTile, yTile, zTile;
00106 
00107     G3d_tileIndex2tile(map, tileIndex, &xTile, &yTile, &zTile);
00108     G3d_tileCoordOrigin(map, xTile, yTile, zTile, x, y, z);
00109 }
00110 
00111 /*---------------------------------------------------------------------------*/
00112 
00113 
00135 void
00136 G3d_coord2tileCoord(G3D_Map * map, int x, int y, int z, int *xTile,
00137                     int *yTile, int *zTile, int *xOffs, int *yOffs,
00138                     int *zOffs)
00139 {
00140     *xTile = x / map->tileX;
00141     *xOffs = x % map->tileX;
00142     *yTile = y / map->tileY;
00143     *yOffs = y % map->tileY;
00144     *zTile = z / map->tileZ;
00145     *zOffs = z % map->tileZ;
00146 }
00147 
00148 /*---------------------------------------------------------------------------*/
00149 
00150 
00166 void
00167 G3d_coord2tileIndex(G3D_Map * map, int x, int y, int z, int *tileIndex,
00168                     int *offset)
00169 {
00170     int xTile, yTile, zTile, xOffs, yOffs, zOffs;
00171 
00172     G3d_coord2tileCoord(map, x, y, z,
00173                         &xTile, &yTile, &zTile, &xOffs, &yOffs, &zOffs);
00174     *tileIndex = G3d_tile2tileIndex(map, xTile, yTile, zTile);
00175     *offset = zOffs * map->tileXY + yOffs * map->tileX + xOffs;
00176 }
00177 
00178 /*---------------------------------------------------------------------------*/
00179 
00180 
00195 int G3d_coordInRange(G3D_Map * map, int x, int y, int z)
00196 {
00197     return (x >= 0) && (x < map->region.cols) && (y >= 0) &&
00198         (y < map->region.rows) && (z >= 0) && (z < map->region.depths);
00199 }
00200 
00201 /*---------------------------------------------------------------------------*/
00202 
00203 
00215 int G3d_tileIndexInRange(G3D_Map * map, int tileIndex)
00216 {
00217     return (tileIndex < map->nTiles) && (tileIndex >= 0);
00218 }
00219 
00220 /*---------------------------------------------------------------------------*/
00221 
00222 
00237 int G3d_tileInRange(G3D_Map * map, int x, int y, int z)
00238 {
00239     return (x >= 0) && (x < map->nx) && (y >= 0) && (y < map->ny) &&
00240         (z >= 0) && (z < map->nz);
00241 }
00242 
00243 /*---------------------------------------------------------------------------*/
00244 
00245 
00266 int
00267 G3d_computeClippedTileDimensions(G3D_Map * map, int tileIndex, int *rows,
00268                                  int *cols, int *depths, int *xRedundant,
00269                                  int *yRedundant, int *zRedundant)
00270 {
00271     int x, y, z;
00272 
00273     G3d_tileIndex2tile(map, tileIndex, &x, &y, &z);
00274 
00275     if ((x != map->clipX) && (y != map->clipY) && (z != map->clipZ)) {
00276         return map->tileSize;
00277     }
00278 
00279     if (x != map->clipX) {
00280         *cols = map->tileX;
00281         *xRedundant = 0;
00282     }
00283     else {
00284         *cols = (map->region.cols - 1) % map->tileX + 1;
00285         *xRedundant = map->tileX - *cols;
00286     }
00287     if (y != map->clipY) {
00288         *rows = map->tileY;
00289         *yRedundant = 0;
00290     }
00291     else {
00292         *rows = (map->region.rows - 1) % map->tileY + 1;
00293         *yRedundant = map->tileY - *rows;
00294     }
00295     if (z != map->clipZ) {
00296         *depths = map->tileZ;
00297         *zRedundant = 0;
00298     }
00299     else {
00300         *depths = (map->region.depths - 1) % map->tileZ + 1;
00301         *zRedundant = map->tileZ - *depths;
00302     }
00303 
00304     /* printf ("%d (%d %d %d): (%d %d) (%d %d) (%d %d), %d\n", */
00305     /*      tileIndex, x, y, z, *rows, *xRedundant, *cols, *yRedundant,  */
00306     /*      *depths, *zRedundant, *depths * *cols * *rows); */
00307 
00308     return *depths * *cols * *rows;
00309 }
00310 
00311 /*---------------------------------------------------------------------------*/
00312 
00313 
00327 int G3d_isValidLocation(G3D_Map * map, double north, double east, double top)
00328 {
00329     return ((north >= map->region.south) && (north <= map->region.north) &&
00330             (east >= map->region.west) && (east <= map->region.east) &&
00331             (((top >= map->region.bottom) && (top <= map->region.top)) ||
00332              ((top <= map->region.bottom) && (top >= map->region.top))));
00333 }
00334 
00335 /*---------------------------------------------------------------------------*/
00336 
00337 
00354 void
00355 G3d_location2coord(G3D_Map * map, double north, double east, double top,
00356                    int *x, int *y, int *z)
00357 {
00358     if (!G3d_isValidLocation(map, north, east, top))
00359         G3d_fatalError("location2coord: location not in region");
00360 
00361     *y = (north - map->region.south) / (map->region.north -
00362                                         map->region.south) *
00363         (map->region.rows - 1);
00364     *x = (east - map->region.west) / (map->region.east -
00365                                       map->region.west) * (map->region.cols -
00366                                                            1);
00367     *z = (top - map->region.bottom) / (map->region.top -
00368                                        map->region.bottom) *
00369         (map->region.depths - 1);
00370 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines