GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tilemath.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5 #include "G3d_intern.h"
6 
7 /*---------------------------------------------------------------------------*/
8 
9 
24 void
25 G3d_tileIndex2tile(G3D_Map * map, int tileIndex, int *xTile, int *yTile,
26  int *zTile)
27 {
28  int tileIndex2d;
29 
30  *zTile = tileIndex / map->nxy;
31  tileIndex2d = tileIndex % map->nxy;
32  *yTile = tileIndex2d / map->nx;
33  *xTile = tileIndex2d % map->nx;
34 }
35 
36 /*---------------------------------------------------------------------------*/
37 
38 
52 int G3d_tile2tileIndex(G3D_Map * map, int xTile, int yTile, int zTile)
53 {
54  return map->nxy * zTile + map->nx * yTile + xTile;
55 }
56 
57 /*---------------------------------------------------------------------------*/
58 
59 
77 void
78 G3d_tileCoordOrigin(G3D_Map * map, int xTile, int yTile, int zTile, int *x,
79  int *y, int *z)
80 {
81  *x = map->tileX * xTile;
82  *y = map->tileY * yTile;
83  *z = map->tileZ * zTile;
84 }
85 
86 /*---------------------------------------------------------------------------*/
87 
88 
103 void G3d_tileIndexOrigin(G3D_Map * map, int tileIndex, int *x, int *y, int *z)
104 {
105  int xTile, yTile, zTile;
106 
107  G3d_tileIndex2tile(map, tileIndex, &xTile, &yTile, &zTile);
108  G3d_tileCoordOrigin(map, xTile, yTile, zTile, x, y, z);
109 }
110 
111 /*---------------------------------------------------------------------------*/
112 
113 
135 void
136 G3d_coord2tileCoord(G3D_Map * map, int x, int y, int z, int *xTile,
137  int *yTile, int *zTile, int *xOffs, int *yOffs,
138  int *zOffs)
139 {
140  *xTile = x / map->tileX;
141  *xOffs = x % map->tileX;
142  *yTile = y / map->tileY;
143  *yOffs = y % map->tileY;
144  *zTile = z / map->tileZ;
145  *zOffs = z % map->tileZ;
146 }
147 
148 /*---------------------------------------------------------------------------*/
149 
150 
166 void
167 G3d_coord2tileIndex(G3D_Map * map, int x, int y, int z, int *tileIndex,
168  int *offset)
169 {
170  int xTile, yTile, zTile, xOffs, yOffs, zOffs;
171 
172  G3d_coord2tileCoord(map, x, y, z,
173  &xTile, &yTile, &zTile, &xOffs, &yOffs, &zOffs);
174  *tileIndex = G3d_tile2tileIndex(map, xTile, yTile, zTile);
175  *offset = zOffs * map->tileXY + yOffs * map->tileX + xOffs;
176 }
177 
178 /*---------------------------------------------------------------------------*/
179 
180 
195 int G3d_coordInRange(G3D_Map * map, int x, int y, int z)
196 {
197  return (x >= 0) && (x < map->region.cols) && (y >= 0) &&
198  (y < map->region.rows) && (z >= 0) && (z < map->region.depths);
199 }
200 
201 /*---------------------------------------------------------------------------*/
202 
203 
215 int G3d_tileIndexInRange(G3D_Map * map, int tileIndex)
216 {
217  return (tileIndex < map->nTiles) && (tileIndex >= 0);
218 }
219 
220 /*---------------------------------------------------------------------------*/
221 
222 
237 int G3d_tileInRange(G3D_Map * map, int x, int y, int z)
238 {
239  return (x >= 0) && (x < map->nx) && (y >= 0) && (y < map->ny) &&
240  (z >= 0) && (z < map->nz);
241 }
242 
243 /*---------------------------------------------------------------------------*/
244 
245 
266 int
267 G3d_computeClippedTileDimensions(G3D_Map * map, int tileIndex, int *rows,
268  int *cols, int *depths, int *xRedundant,
269  int *yRedundant, int *zRedundant)
270 {
271  int x, y, z;
272 
273  G3d_tileIndex2tile(map, tileIndex, &x, &y, &z);
274 
275  if ((x != map->clipX) && (y != map->clipY) && (z != map->clipZ)) {
276  return map->tileSize;
277  }
278 
279  if (x != map->clipX) {
280  *cols = map->tileX;
281  *xRedundant = 0;
282  }
283  else {
284  *cols = (map->region.cols - 1) % map->tileX + 1;
285  *xRedundant = map->tileX - *cols;
286  }
287  if (y != map->clipY) {
288  *rows = map->tileY;
289  *yRedundant = 0;
290  }
291  else {
292  *rows = (map->region.rows - 1) % map->tileY + 1;
293  *yRedundant = map->tileY - *rows;
294  }
295  if (z != map->clipZ) {
296  *depths = map->tileZ;
297  *zRedundant = 0;
298  }
299  else {
300  *depths = (map->region.depths - 1) % map->tileZ + 1;
301  *zRedundant = map->tileZ - *depths;
302  }
303 
304  /* printf ("%d (%d %d %d): (%d %d) (%d %d) (%d %d), %d\n", */
305  /* tileIndex, x, y, z, *rows, *xRedundant, *cols, *yRedundant, */
306  /* *depths, *zRedundant, *depths * *cols * *rows); */
307 
308  return *depths * *cols * *rows;
309 }
310 
311 /*---------------------------------------------------------------------------*/
312 
313 
327 int G3d_isValidLocation(G3D_Map * map, double north, double east, double top)
328 {
329  return ((north >= map->region.south) && (north <= map->region.north) &&
330  (east >= map->region.west) && (east <= map->region.east) &&
331  (((top >= map->region.bottom) && (top <= map->region.top)) ||
332  ((top <= map->region.bottom) && (top >= map->region.top))));
333 }
334 
335 /*---------------------------------------------------------------------------*/
336 
337 
354 void
355 G3d_location2coord(G3D_Map * map, double north, double east, double top,
356  int *x, int *y, int *z)
357 {
358  if (!G3d_isValidLocation(map, north, east, top))
359  G3d_fatalError("location2coord: location not in region");
360 
361  *y = (north - map->region.south) / (map->region.north -
362  map->region.south) *
363  (map->region.rows - 1);
364  *x = (east - map->region.west) / (map->region.east -
365  map->region.west) * (map->region.cols -
366  1);
367  *z = (top - map->region.bottom) / (map->region.top -
368  map->region.bottom) *
369  (map->region.depths - 1);
370 }
int G3d_isValidLocation(G3D_Map *map, double north, double east, double top)
Returns 1 if region-coordinates (north, west, bottom) are inside the region of map. Returns 0 otherwise.
Definition: tilemath.c:327
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
int y
Definition: plot.c:34
void G3d_tileCoordOrigin(G3D_Map *map, int xTile, int yTile, int zTile, int *x, int *y, int *z)
Computes the cell-coordinates (x, y, z) which correspond to the origin of the tile with tile-coordina...
Definition: tilemath.c:78
void G3d_location2coord(G3D_Map *map, double north, double east, double top, int *x, int *y, int *z)
Converts region-coordinates (north, west, bottom) into cell-coordinates (x, y, z).
Definition: tilemath.c:355
int G3d_tileIndexInRange(G3D_Map *map, int tileIndex)
Returns 1 if tileIndex is a valid index for map. Returns 0 otherwise.
Definition: tilemath.c:215
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
tuple cols
void G3d_coord2tileCoord(G3D_Map *map, int x, int y, int z, int *xTile, int *yTile, int *zTile, int *xOffs, int *yOffs, int *zOffs)
Converts cell-coordinates (x, y, z) into tile-coordinates (xTile, yTile, zTile) and the coordinate of...
Definition: tilemath.c:136
int G3d_coordInRange(G3D_Map *map, int x, int y, int z)
Returns 1 if cell-coordinate (x, y, z) is a coordinate inside the region. Returns 0 otherwise...
Definition: tilemath.c:195
int G3d_tile2tileIndex(G3D_Map *map, int xTile, int yTile, int zTile)
Returns tile-index corresponding to tile-coordinates (xTile, yTile, zTile).
Definition: tilemath.c:52
void G3d_coord2tileIndex(G3D_Map *map, int x, int y, int z, int *tileIndex, int *offset)
Converts cell-coordinates (x, y, z) into tileIndex and the offset of the cell within the tile...
Definition: tilemath.c:167
void G3d_fatalError(const char *,...)
This function prints the error message msg, and terminates the program with an error status...
Definition: g3derror.c:58
void G3d_tileIndex2tile(G3D_Map *map, int tileIndex, int *xTile, int *yTile, int *zTile)
Converts index tileIndex into tile-coordinates (xTile, yTile, zTile).
Definition: tilemath.c:25
int G3d_tileInRange(G3D_Map *map, int x, int y, int z)
Returns 1 if tile-coordinate (x, y, z) is a coordinate inside tile cube. Returns 0 otherwise...
Definition: tilemath.c:237