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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
|
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <rpc/types.h>
#include <rpc/xdr.h>
#include "G3d_intern.h"
/*---------------------------------------------------------------------------*/
static int
G3d_tile2xdrTile (map, tile, rows, cols, depths,
xRedundant, yRedundant, zRedundant, nofNum, type)
G3D_Map *map;
char *tile;
int rows, cols, depths, xRedundant, yRedundant, zRedundant, nofNum;
int type;
{
int y, z;
if (! G3d_initCopyToXdr (map, type)) {
G3d_error ("G3d_tile2xdrTile: error in G3d_initCopyToXdr");
return 0;
}
if (nofNum == map->tileSize) {
if (! G3d_copyToXdr (tile, map->tileSize)) {
G3d_error ("G3d_tile2xdrTile: error in G3d_copyToXdr");
return 0;
}
return 1;
}
if (xRedundant) {
for (z = 0; z < depths; z++) {
for (y = 0; y < rows; y++) {
if (! G3d_copyToXdr (tile, cols)) {
G3d_error ("G3d_tile2xdrTile: error in G3d_copyToXdr");
return 0;
}
tile += map->tileX * G3d_length (type);
}
if (yRedundant)
tile += map->tileX * yRedundant * G3d_length (type);
}
return 1;
}
if (yRedundant) {
for (z = 0; z < depths; z++) {
if (! G3d_copyToXdr (tile, map->tileX * rows)) {
G3d_error ("G3d_tile2xdrTile: error in G3d_copyToXdr");
return 0;
}
tile += map->tileXY * G3d_length (type);
}
return 1;
}
if (! G3d_copyToXdr (tile, map->tileXY * depths)) {
G3d_error ("G3d_tile2xdrTile: error in G3d_copyToXdr");
return 0;
}
return 1;
}
/*---------------------------------------------------------------------------*/
static int
G3d_writeTileUncompressed (map, nofNum)
G3D_Map *map;
int nofNum;
{
if (write (map->data_fd, xdr, map->numLengthExtern * nofNum) !=
map->numLengthExtern * nofNum) {
G3d_error ("G3d_writeTileUncompressed: can't write file.");
return 0;
}
return 1;
}
/*---------------------------------------------------------------------------*/
static int
G3d_writeTileCompressed (map, nofNum)
G3D_Map *map;
int nofNum;
{
if (! G_fpcompress_writeXdrNums (map->data_fd, xdr, nofNum, map->precision,
tmpCompress, map->type == G3D_FLOAT,
map->useRle, map->useLzw)) {
G3d_error (
"G3d_writeTileCompressed: error in G_fpcompress_writeXdrNums");
return 0;
}
return 1;
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* EXPORTED FUNCTIONS */
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
*
* Writes tile with index <em>tileIndex</em> to the file corresponding to <em>map</em>.
* It is assumed that the cells in <em>tile</em> are of <em>type</em> which
* must be one of G3D_FLOAT and G3D_DOUBLE. The actual type used to write the
* tile depends on the type specified at the time when <em>map</em> is initialized.
* A tile can only be written once. Subsequent attempts to write the same tile
* are ignored.
*
* \param map
* \param tileIndex
* \param tile
* \param type
* \return 1 ... if successful,
* 2 ... if write request was ignored,
* 0 ... otherwise.
*/
int
G3d_writeTile (map, tileIndex, tile, type)
G3D_Map *map;
int tileIndex;
char *tile;
int type;
{
int rows, cols, depths, xRedundant, yRedundant, zRedundant, nofNum;
/* valid tileIndex ? */
if ((tileIndex >= map->nTiles) || (tileIndex < 0))
G3d_fatalError ("G3d_writeTile: tileIndex out of range");
/* already written ? */
if (map->index[tileIndex] != -1) return 2;
/* save the file position */
map->index[tileIndex] = lseek (map->data_fd, (long) 0, SEEK_END);
if (map->index[tileIndex] == -1) {
G3d_error ("G3d_writeTile: can't position file");
return 0;
}
nofNum = G3d_computeClippedTileDimensions (map, tileIndex,
&rows, &cols, &depths,
&xRedundant, &yRedundant,
&zRedundant);
G3d_range_updateFromTile (map, tile, rows, cols, depths,
xRedundant, yRedundant, zRedundant, nofNum, type);
if (! G3d_tile2xdrTile (map, tile, rows, cols, depths,
xRedundant, yRedundant, zRedundant, nofNum, type)) {
G3d_error ("G3d_writeTileCompressed: error in G3d_tile2xdrTile");
return 0;
}
if (map->compression == G3D_NO_COMPRESSION) {
if (! G3d_writeTileUncompressed (map, nofNum)) {
G3d_error ("G3d_writeTile: error in G3d_writeTileUncompressed");
return 0;
}
} else
if (! G3d_writeTileCompressed (map, nofNum)) {
G3d_error ("G3d_writeTile: error in G3d_writeTileCompressed");
return 0;
}
/* compute the length */
map->tileLength[tileIndex] = lseek (map->data_fd, (long) 0, SEEK_END) -
map->index[tileIndex];
return 1;
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* Is equivalent to <tt>G3d_writeTile (map, tileIndex, tile, G3D_FLOAT).</tt>
*
* \param map
* \param tileIndex
* \param tile
* \return int
*/
int
G3d_writeTileFloat (map, tileIndex, tile)
G3D_Map *map;
int tileIndex;
char *tile;
{
int status;
if ((status = G3d_writeTile (map, tileIndex, tile, G3D_FLOAT))) return status;
G3d_error ("G3d_writeTileFloat: error in G3d_writeTile");
return 0;
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* Is equivalent to <tt>G3d_writeTile (map, tileIndex, tile, G3D_DOUBLE).</tt>
*
* \param map
* \param tileIndex
* \param tile
* \return int
*/
int
G3d_writeTileDouble (map, tileIndex, tile)
G3D_Map *map;
int tileIndex;
char *tile;
{
int status;
if ((status = G3d_writeTile (map, tileIndex, tile, G3D_DOUBLE))) return status;
G3d_error ("G3d_writeTileDouble: error in G3d_writeTile");
return 0;
}
/*---------------------------------------------------------------------------*/
/* CACHE-MODE-ONLY FUNCTIONS */
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* Writes the tile with
* <em>tileIndex</em> to the file corresponding to <em>map</em> and removes the tile
* from the cache (in non-cache mode the buffer provided by the map-structure is
* written).
* If this tile has already been written before the write request is ignored.
* If the tile was never referred to before the invokation of G3d_flushTile, a
* tile filled with NULL-values is written.
*
* \param map
* \param tileIndex
* \return 1 ... if successful,
* 0 ... otherwise.
*/
int
G3d_flushTile (map, tileIndex)
G3D_Map *map;
int tileIndex;
{
char *tile;
tile = G3d_getTilePtr (map, tileIndex);
if (tile == NULL) {
G3d_error ("G3d_flushTile: error in G3d_getTilePtr");
return 0;
}
if (! G3d_writeTile (map, tileIndex, tile, map->typeIntern)) {
G3d_error ("G3d_flushTile: error in G3d_writeTile");
return 0;
}
if (! G3d__removeTile (map, tileIndex)) {
G3d_error ("G3d_flushTile: error in G3d__removeTile");
return 0;
}
return 1;
}
/*---------------------------------------------------------------------------*/
#ifndef MIN
#define MIN(a,b) (a < b ? a : b)
#define MAX(a,b) (a > b ? a : b)
#endif
/*!
* \brief
*
* Writes the tiles with tile-coordinates
* contained in the axis-parallel cube with vertices <em>(xMin, yMin, zMin)</em>
* and <em>(xMax, yMax, zMax</em>). Tiles which are not stored in the cache are
* written as NULL-tiles. Write attempts for tiles which have already been
* written earlier are ignored.
*
* \param map
* \param xMin
* \param yMin
* \param zMin
* \param xMax
* \param yMax
* \param zMax
* \return 1 ... if successful,
* 0 ... otherwise.
*/
int
G3d_flushTileCube (map, xMin, yMin, zMin, xMax, yMax, zMax)
G3D_Map *map;
int xMin, yMin, zMin, xMax, yMax, zMax;
{
int x, y, z;
if (! map->useCache)
G3d_fatalError ("G3d_flushTileCube: function invalid in non-cache mode");
for (x = xMin; x <= xMax; x++)
for (y = yMin; y <= yMax; y++)
for (z = zMin; z <= zMax; z++)
if (! G3d_flushTile (map, G3d_tile2tileIndex (map, x, y, z))) {
G3d_error ("G3d_flushTileCube: error in G3d_flushTile");
return 0;
}
return 1;
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* Writes those tiles for which
* <em>every</em> cell has coordinate contained in the axis-parallel cube
* defined by the vertices with cell-coordinates <em>(xMin, yMin, zMin)</em>
* and <em>(xMax, yMax, zMax)</em>.
* Tiles which are not stored in the cache are written as NULL-tiles.
* Write attempts for tiles which have already been written earlier are
* ignored.
*
* \param map
* \param xMin
* \param yMin
* \param zMin
* \param xMax
* \param yMax
* \param zMax
* \return 1 ... if successful,
* 0 ... otherwise.
*/
int
G3d_flushTilesInCube (map, xMin, yMin, zMin, xMax, yMax, zMax)
G3D_Map *map;
int xMin, yMin, zMin, xMax, yMax, zMax;
{
int xTileMin, yTileMin, zTileMin, xTileMax, yTileMax, zTileMax;
int xOffs, yOffs, zOffs;
int regionMaxX, regionMaxY, regionMaxZ;
if (! map->useCache)
G3d_fatalError ("G3d_flushTilesInCube: function invalid in non-cache mode");
/*AV*/
/*BEGIN OF ORIGINAL CODE */
/*
* G3d_getCoordsMap (map, ®ionMaxX, ®ionMaxY, ®ionMaxZ);
*/
/*AV*/
/* BEGIN OF MY CODE */
G3d_getCoordsMap (map, ®ionMaxY, ®ionMaxX, ®ionMaxZ);
/* END OF MY CODE */
if ((xMin < 0) && (xMax < 0))
G3d_fatalError ("G3d_flushTilesInCube: coordinate out of Range");
if ((xMin >= regionMaxX) && (xMax >= regionMaxX))
G3d_fatalError ("G3d_flushTilesInCube: coordinate out of Range");
xMin = MIN (MAX (0, xMin), regionMaxX - 1);
if ((yMin < 0) && (yMax < 0))
G3d_fatalError ("G3d_flushTilesInCube: coordinate out of Range");
if ((yMin >= regionMaxY) && (yMax >= regionMaxY))
G3d_fatalError ("G3d_flushTilesInCube: coordinate out of Range");
yMin = MIN (MAX (0, yMin), regionMaxY - 1);
if ((zMin < 0) && (zMax < 0))
G3d_fatalError ("G3d_flushTilesInCube: coordinate out of Range");
if ((zMin >= regionMaxZ) && (zMax >= regionMaxZ))
G3d_fatalError ("G3d_flushTilesInCube: coordinate out of Range");
zMin = MIN (MAX (0, zMin), regionMaxZ - 1);
G3d_coord2tileCoord (map, xMin, yMin, zMin,
&xTileMin, &yTileMin, &zTileMin,
&xOffs, &yOffs, &zOffs);
if (xOffs != 0) xTileMin++;
if (yOffs != 0) yTileMin++;
if (zOffs != 0) zTileMin++;
G3d_coord2tileCoord (map, xMax + 1, yMax + 1, zMax + 1,
&xTileMax, &yTileMax, &zTileMax,
&xOffs, &yOffs, &zOffs);
xTileMax--;
yTileMax--;
zTileMax--;
if (! G3d_flushTileCube (map, xTileMin, yTileMin, zTileMin,
xTileMax, yTileMax, zTileMax)) {
G3d_error ("G3d_flushTilesInCube: error in G3d_flushTileCube");
return 0;
}
return 1;
}
#undef MIN
#undef MAX
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* Is equivalent to G3d_putValue (map, x, y, z, &value, G3D_DOUBLE).
*
* \param map
* \param x
* \param y
* \param z
* \param value
* \return int
*/
int
G3d_putDouble ();
/*!
* \brief
*
* Is equivalent to G3d_putValue (map, x, y, z, &value, G3D_FLOAT).
*
* \param map
* \param x
* \param y
* \param z
* \param value
* \return int
*/
int
G3d_putFloat (G3D_Map *map, int x, int y, int z, float value)
{
int tileIndex, offs;
float *tile;
if (map->typeIntern == G3D_DOUBLE) {
if (! G3d_putDouble (map, x, y, z, (double) value)) {
G3d_error ("G3d_putFloat: error in G3d_putDouble");
return 0;
}
return 1;
}
G3d_coord2tileIndex (map, x, y, z, &tileIndex, &offs);
tile = (float *) G3d_getTilePtr (map, tileIndex);
if (tile == NULL) {
G3d_error ("G3d_putFloat: error in G3d_getTilePtr");
return 0;
}
tile[offs] = value;
return 1;
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* Is equivalent to G3d_putValue (map, x, y, z, &value, G3D_DOUBLE).
*
* \param map
* \param x
* \param y
* \param z
* \param value
* \return int
*/
int
G3d_putDouble (map, x, y, z, value)
G3D_Map *map;
int x, y, z;
double value;
{
int tileIndex, offs;
double *tile;
if (map->typeIntern == G3D_FLOAT) {
if (! G3d_putFloat (map, x, y, z, (float) value)) {
G3d_error ("G3d_putDouble: error in G3d_putFloat");
return 0;
}
return 1;
}
G3d_coord2tileIndex (map, x, y, z, &tileIndex, &offs);
tile = (double *) G3d_getTilePtr (map, tileIndex);
if (tile == NULL) {
G3d_error ("G3d_putDouble: error in G3d_getTilePtr");
return 0;
}
tile[offs] = value;
return 1;
}
/*---------------------------------------------------------------------------*/
/*!
* \brief
*
* After converting <em>*value</em> of <em>type</em> into the type specified
* at the initialization time (i.e. <em>typeIntern</em>) this function writes the
* value into the tile buffer corresponding to cell-coordinate <em>(x, y, z)</em>.
*
* \param map
* \param x
* \param y
* \param z
* \param value
* \param type
* \return 1 ... if successful,
* 0 ... otherwise.
*/
int
G3d_putValue (map, x, y, z, value, type)
G3D_Map *map;
int x, y, z;
char *value;
int type;
{
if (type == G3D_FLOAT) {
if (! G3d_putFloat (map, x, y, z, *((float *) value))) {
G3d_error ("G3d_putValue: error in G3d_putFloat");
return 0;
}
return 1;
}
if (! G3d_putDouble (map, x, y, z, *((double *) value))) {
G3d_error ("G3d_putValue: error in G3d_putDouble");
return 0;
}
return 1;
}
|