1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include "G3d_intern.h"
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* Converts index <em>tileIndex</em> into tile-coordinates
* <em>(xTile, yTile, zTile)</em>.
*
* \param map
* \param tileIndex
* \param xTile
* \param yTile
* \param zTile
* \return void
*/
void
G3d_tileIndex2tile(G3D_Map * map, int tileIndex, int *xTile, int *yTile,
int *zTile)
{
int tileIndex2d;
*zTile = tileIndex / map->nxy;
tileIndex2d = tileIndex % map->nxy;
*yTile = tileIndex2d / map->nx;
*xTile = tileIndex2d % map->nx;
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* Returns tile-index corresponding to tile-coordinates <em>(xTile,
* yTile, zTile)</em>.
*
* \param map
* \param xTile
* \param yTile
* \param zTile
* \return int
*/
int G3d_tile2tileIndex(G3D_Map * map, int xTile, int yTile, int zTile)
{
return map->nxy * zTile + map->nx * yTile + xTile;
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* Computes the cell-coordinates <em>(x, y, z)</em>
* which correspond to the origin of the tile with tile-coordinates <em>(xTile,
* yTile, zTile)</em>.
*
* \param map
* \param xTile
* \param yTile
* \param zTile
* \param x
* \param y
* \param z
* \return void
*/
void
G3d_tileCoordOrigin(G3D_Map * map, int xTile, int yTile, int zTile, int *x,
int *y, int *z)
{
*x = map->tileX * xTile;
*y = map->tileY * yTile;
*z = map->tileZ * zTile;
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* Computes the cell-coordinates <em>(x, y, z)</em> which correspond to
* the origin of the tile with <em>tileIndex</em>.
*
* \param map
* \param tileIndex
* \param x
* \param y
* \param z
* \return void
*/
void G3d_tileIndexOrigin(G3D_Map * map, int tileIndex, int *x, int *y, int *z)
{
int xTile, yTile, zTile;
G3d_tileIndex2tile(map, tileIndex, &xTile, &yTile, &zTile);
G3d_tileCoordOrigin(map, xTile, yTile, zTile, x, y, z);
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* Converts
* cell-coordinates <em>(x, y, z)</em> into tile-coordinates <em>(xTile, yTile,
* zTile)</em> and the coordinate of the cell <em>(xOffs, yOffs, zOffs)</em> within
* the tile.
*
* \param map
* \param x
* \param y
* \param z
* \param xTile
* \param yTile
* \param zTile
* \param xOffs
* \param yOffs
* \param zOffs
* \return void
*/
void
G3d_coord2tileCoord(G3D_Map * map, int x, int y, int z, int *xTile,
int *yTile, int *zTile, int *xOffs, int *yOffs,
int *zOffs)
{
*xTile = x / map->tileX;
*xOffs = x % map->tileX;
*yTile = y / map->tileY;
*yOffs = y % map->tileY;
*zTile = z / map->tileZ;
*zOffs = z % map->tileZ;
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* Converts cell-coordinates <em>(x, y, z)</em> into
* <em>tileIndex</em> and the <em>offset</em> of the cell within the tile.
*
* \param map
* \param x
* \param y
* \param z
* \param tileIndex
* \param offset
* \return void
*/
void
G3d_coord2tileIndex(G3D_Map * map, int x, int y, int z, int *tileIndex,
int *offset)
{
int xTile, yTile, zTile, xOffs, yOffs, zOffs;
G3d_coord2tileCoord(map, x, y, z,
&xTile, &yTile, &zTile, &xOffs, &yOffs, &zOffs);
*tileIndex = G3d_tile2tileIndex(map, xTile, yTile, zTile);
*offset = zOffs * map->tileXY + yOffs * map->tileX + xOffs;
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* Returns 1 if
* cell-coordinate <em>(x, y, z)</em> is a coordinate inside the region. Returns 0
* otherwise.
*
* \param map
* \param x
* \param y
* \param z
* \return int
*/
int G3d_coordInRange(G3D_Map * map, int x, int y, int z)
{
return (x >= 0) && (x < map->region.cols) && (y >= 0) &&
(y < map->region.rows) && (z >= 0) && (z < map->region.depths);
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* Returns 1 if <em>tileIndex</em> is a valid index for <em>map</em>.
* Returns 0 otherwise.
*
* \param map
* \param tileIndex
* \return int
*/
int G3d_tileIndexInRange(G3D_Map * map, int tileIndex)
{
return (tileIndex < map->nTiles) && (tileIndex >= 0);
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* Returns 1 if
* tile-coordinate <em>(x, y, z)</em> is a coordinate inside tile cube. Returns 0
* otherwise.
*
* \param map
* \param x
* \param y
* \param z
* \return int
*/
int G3d_tileInRange(G3D_Map * map, int x, int y, int z)
{
return (x >= 0) && (x < map->nx) && (y >= 0) && (y < map->ny) &&
(z >= 0) && (z < map->nz);
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* Computes the dimensions of the tile when clipped to fit the
* region of <em>map</em>. The clipped dimensions are returned in <em>rows</em>,
* <em>cols</em>, <em>depths</em>. The complement is returned in <em>xRedundant</em>,
* <em>yRedundant</em>, and <em>zRedundant</em>. This function returns the number of
* cells in the clipped tile.
*
* \param map
* \param tileIndex
* \param rows
* \param cols
* \param depths
* \param xRedundant
* \param yRedundant
* \param zRedundant
* \return int
*/
int
G3d_computeClippedTileDimensions(G3D_Map * map, int tileIndex, int *rows,
int *cols, int *depths, int *xRedundant,
int *yRedundant, int *zRedundant)
{
int x, y, z;
G3d_tileIndex2tile(map, tileIndex, &x, &y, &z);
if ((x != map->clipX) && (y != map->clipY) && (z != map->clipZ)) {
return map->tileSize;
}
if (x != map->clipX) {
*cols = map->tileX;
*xRedundant = 0;
}
else {
*cols = (map->region.cols - 1) % map->tileX + 1;
*xRedundant = map->tileX - *cols;
}
if (y != map->clipY) {
*rows = map->tileY;
*yRedundant = 0;
}
else {
*rows = (map->region.rows - 1) % map->tileY + 1;
*yRedundant = map->tileY - *rows;
}
if (z != map->clipZ) {
*depths = map->tileZ;
*zRedundant = 0;
}
else {
*depths = (map->region.depths - 1) % map->tileZ + 1;
*zRedundant = map->tileZ - *depths;
}
/* printf ("%d (%d %d %d): (%d %d) (%d %d) (%d %d), %d\n", */
/* tileIndex, x, y, z, *rows, *xRedundant, *cols, *yRedundant, */
/* *depths, *zRedundant, *depths * *cols * *rows); */
return *depths * *cols * *rows;
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* Returns 1 if region-coordinates <em>(north, west, bottom)</em> are
* inside the region of <em>map</em>. Returns 0 otherwise.
*
* \param map
* \param north
* \param west
* \param bottom
* \return int
*/
int G3d_isValidLocation(G3D_Map * map, double north, double east, double top)
{
return ((north >= map->region.south) && (north <= map->region.north) &&
(east >= map->region.west) && (east <= map->region.east) &&
(((top >= map->region.bottom) && (top <= map->region.top)) ||
((top <= map->region.bottom) && (top >= map->region.top))));
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* Converts region-coordinates <em>(north, west,
* bottom)</em> into cell-coordinates <em>(x, y, z)</em>.
*
* \param map
* \param north
* \param west
* \param bottom
* \param x
* \param y
* \param z
* \return void
*/
void
G3d_location2coord(G3D_Map * map, double north, double east, double top,
int *x, int *y, int *z)
{
if (!G3d_isValidLocation(map, north, east, top))
G3d_fatalError("location2coord: location not in region");
*y = (north - map->region.south) / (map->region.north -
map->region.south) *
(map->region.rows - 1);
*x = (east - map->region.west) / (map->region.east -
map->region.west) * (map->region.cols -
1);
*z = (top - map->region.bottom) / (map->region.top -
map->region.bottom) *
(map->region.depths - 1);
}
|