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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkMultiBlockPLOT3DReaderInternals.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkMultiBlockPLOT3DReaderInternals.h"
#include "vtkMultiProcessController.h"
#include <cassert>
int vtkMultiBlockPLOT3DReaderInternals::ReadInts(FILE* fp, int n, int* val)
{
int retVal = static_cast<int>(fread(val, sizeof(int), n, fp));
if (this->Settings.ByteOrder == vtkMultiBlockPLOT3DReader::FILE_LITTLE_ENDIAN)
{
vtkByteSwap::Swap4LERange(val, n);
}
else
{
vtkByteSwap::Swap4BERange(val, n);
}
return retVal;
}
void vtkMultiBlockPLOT3DReaderInternals::CheckBinaryFile(FILE *fp, size_t fileSize)
{
rewind(fp);
this->Settings.BinaryFile = 0;
// The shortest binary file is 12 files: 2 ints for block dims + 1 float for
// a coordinate.
if (fileSize < 12)
{
return;
}
char bytes[12];
if (fread(bytes, 1, 12, fp) != 12)
{
return;
}
// Check the first 12 bytes. If we find non-ascii characters, then we
// assume that it is binary.
for (int i=0; i<12; i++)
{
if (!(isdigit(bytes[i]) || bytes[i] == '.' ||
bytes[i] == ' ' || bytes[i] == '\r' ||
bytes[i] == '\n' || bytes[i] == '\t'))
{
this->Settings.BinaryFile = 1;
return;
}
}
}
int vtkMultiBlockPLOT3DReaderInternals::CheckByteOrder(FILE* fp)
{
rewind(fp);
int i;
if (fread(&i, sizeof(int), 1, fp) < 1)
{
return 0;
}
char* cpy = (char*)&i;
// If binary, we can assume that the first value is going to be either a
// count (Fortran) or a number of block or a dimension. We assume that
// this number will be smaller than 2^24. Then we check the first byte.
// If it is 0 and the last byte is not 0, it is likely that this is big
// endian.
if(cpy[0] == 0 && cpy[3] != 0)
{
this->Settings.ByteOrder = vtkMultiBlockPLOT3DReader::FILE_BIG_ENDIAN;
}
else
{
this->Settings.ByteOrder = vtkMultiBlockPLOT3DReader::FILE_LITTLE_ENDIAN;
}
return 1;
}
int vtkMultiBlockPLOT3DReaderInternals::CheckByteCount(FILE* fp)
{
rewind(fp);
// Read the first integer, then skip by that many bytes, then
// read the value again. If the two match, it is likely that
// the file has byte counts.
int count;
if (!this->ReadInts(fp, 1, &count))
{
return 0;
}
if (fseek(fp, count, SEEK_CUR) != 0)
{
return 0;
}
int count2;
if (!this->ReadInts(fp, 1, &count2))
{
return 0;
}
if (count == count2)
{
this->Settings.HasByteCount = 1;
}
else
{
this->Settings.HasByteCount = 0;
}
return 1;
}
int vtkMultiBlockPLOT3DReaderInternals::CheckMultiGrid(FILE* fp)
{
if (this->Settings.HasByteCount)
{
rewind(fp);
// We read the byte count, if it is 4 (1 int),
// then this is multi-grid because the first
// value is the number of grids rather than
// an array of 2 or 3 values.
int recMarkBeg;
if (!this->ReadInts(fp, 1, &recMarkBeg))
{
return 0;
}
if(recMarkBeg == sizeof(int))
{
this->Settings.MultiGrid = 1;
}
else
{
this->Settings.MultiGrid = 0;
}
return 1;
}
return 0;
}
int vtkMultiBlockPLOT3DReaderInternals::Check2DGeom(FILE* fp)
{
if (this->Settings.HasByteCount)
{
rewind(fp);
int recMarkBeg, recMarkEnd;
int numGrids = 1;
if(this->Settings.MultiGrid)
{
if (!this->ReadInts(fp, 1, &recMarkBeg) ||
!this->ReadInts(fp, 1, &numGrids) ||
!this->ReadInts(fp, 1, &recMarkEnd))
{
return 0;
}
}
if (!this->ReadInts(fp, 1, &recMarkBeg))
{
return 0;
}
int nMax = 3*numGrids;
int ndims;
if(recMarkBeg == static_cast<int>(nMax*sizeof(int) + 2*sizeof(int)))
{
ndims = 3;
}
else
{
if(recMarkBeg == static_cast<int>(nMax*sizeof(int)))
{
ndims = 3;
}
else
{
ndims = 2;
}
}
this->Settings.NumberOfDimensions = ndims;
return 1;
}
return 0;
}
int vtkMultiBlockPLOT3DReaderInternals::CheckBlankingAndPrecision(FILE* fp)
{
int recMarkBeg, recMarkEnd, numGrids = 1, nMax, totPts;
int* jmax;
rewind(fp);
if(this->Settings.MultiGrid)
{
if (!this->ReadInts(fp, 1, &recMarkBeg) ||
!this->ReadInts(fp, 1, &numGrids) ||
!this->ReadInts(fp, 1, &recMarkEnd))
{
return 0;
}
}
if (!this->ReadInts(fp, 1, &recMarkBeg))
{
return 0;
}
nMax = this->Settings.NumberOfDimensions * numGrids;
jmax = new int[numGrids*3]; // allocate memory for jmax
if (!this->ReadInts(fp, nMax, jmax) ||
!this->ReadInts(fp, 1, &recMarkEnd))
{
delete[] jmax;
return 0;
}
totPts = 1;
for (int i=0; i<this->Settings.NumberOfDimensions; i++)
{
totPts *= jmax[i];
}
this->ReadInts(fp, 1, &recMarkBeg);
// single precision, with iblanking
if(recMarkBeg == totPts*(this->Settings.NumberOfDimensions*4 + 4))
{
this->Settings.Precision = 4;
this->Settings.IBlanking = 1;
delete[] jmax;
return 1;
}
// double precision, with iblanking
else if(recMarkBeg == totPts*(this->Settings.NumberOfDimensions*8 + 4))
{
this->Settings.Precision = 8;
this->Settings.IBlanking = 1;
delete[] jmax;
return 1;
}
// single precision, no iblanking
else if(recMarkBeg == totPts*this->Settings.NumberOfDimensions*4)
{
this->Settings.Precision = 4;
this->Settings.IBlanking = 0;
delete[] jmax;
return 1;
}
// double precision, no iblanking
else if(recMarkBeg == totPts*this->Settings.NumberOfDimensions*8)
{
this->Settings.Precision = 8;
this->Settings.IBlanking = 0;
delete[] jmax;
return 1;
}
return 0;
}
// Unfortunately, a Plot3D file written in C is trickier
// to check becaues it has no byte count markers. We need
// to do a bit more brute force checks based on reading
// data and estimating file size.
int vtkMultiBlockPLOT3DReaderInternals::CheckCFile(FILE* fp, size_t fileSize)
{
int precisions[2] = {4, 8};
int blankings[2] = {0, 1};
int dimensions[2] = {2, 3};
rewind(fp);
int gridDims[3];
if (this->ReadInts(fp, 3, gridDims) != 3)
{
return 0;
}
// Single grid
for (int i=0; i<2; i++)
{
int precision = precisions[i];
for (int j=0; j<2; j++)
{
int blanking = blankings[j];
for (int k=0; k<2; k++)
{
int dimension = dimensions[k];
if (fileSize ==
this->CalculateFileSize(false,
precision,
blanking,
dimension,
false, // always
1,
gridDims))
{
this->Settings.MultiGrid = 0;
this->Settings.Precision = precision;
this->Settings.IBlanking = blanking;
this->Settings.NumberOfDimensions = dimension;
return 1;
}
}
}
}
// Multi grid
int nGrids;
rewind(fp);
if (!this->ReadInts(fp, 1, &nGrids))
{
return 0;
}
int* gridDims2 = new int[3*nGrids];
if (this->ReadInts(fp, nGrids*3, gridDims2) != nGrids*3)
{
delete[] gridDims2;
return 0;
}
for (int i=0; i<2; i++)
{
int precision = precisions[i];
for (int j=0; j<2; j++)
{
int blanking = blankings[j];
for (int k=0; k<2; k++)
{
int dimension = dimensions[k];
if (fileSize ==
this->CalculateFileSize(true,
precision,
blanking,
dimension,
false, // always
nGrids,
gridDims2))
{
this->Settings.MultiGrid = 1;
this->Settings.Precision = precision;
this->Settings.IBlanking = blanking;
this->Settings.NumberOfDimensions = dimension;
delete[] gridDims2;
return 1;
}
}
}
}
delete[] gridDims2;
return 0;
}
size_t vtkMultiBlockPLOT3DReaderInternals::CalculateFileSize(int mgrid,
int precision, // in bytes
int blanking,
int ndims,
int hasByteCount,
int nGrids,
int* gridDims)
{
size_t size = 0; // the header portion, 3 ints
// N grids
if (mgrid)
{
size += 4; // int for nblocks
if (hasByteCount)
{
size += 2*4; // byte counts for nblocks
}
}
// Header
size += nGrids*ndims*4;
if (hasByteCount)
{
size += 2*4; // byte counts for grid dims
}
for (int i=0; i<nGrids; i++)
{
size += this->CalculateFileSizeForBlock(
precision, blanking, ndims, hasByteCount, gridDims + ndims*i);
}
return size;
}
size_t vtkMultiBlockPLOT3DReaderInternals::CalculateFileSizeForBlock(int precision, // in bytes
int blanking,
int ndims,
int hasByteCount,
int* gridDims)
{
size_t size = 0;
// x, y, (z)
size_t npts = 1;
for (int i=0; i<ndims; i++)
{
npts *= gridDims[i];
}
size += npts*ndims*precision;
if (blanking)
{
size += npts*4;
}
if (hasByteCount)
{
size += 2*4;
}
return size;
}
//-----------------------------------------------------------------------------
bool vtkMultiBlockPLOT3DReaderRecord::Initialize(
FILE* fp, vtkTypeUInt64 offset,
const vtkMultiBlockPLOT3DReaderInternals::InternalSettings& settings,
vtkMultiProcessController* controller)
{
this->SubRecords.clear();
if (!settings.BinaryFile || !settings.HasByteCount)
{
return true;
}
const int rank = controller? controller->GetLocalProcessId() : 0;
int error = 0;
if (rank == 0)
{
vtkTypeUInt64 pos = vtk_ftell(fp);
unsigned int leadingLengthField;
int signBit = 0;
try
{
do
{
vtkSubRecord subrecord;
subrecord.HeaderOffset = offset;
vtkTypeUInt64 dataOffset = subrecord.HeaderOffset + sizeof(int);
vtk_fseek(fp, offset, SEEK_SET);
if (fread(&leadingLengthField, sizeof(unsigned int), 1, fp) != 1)
{
throw Plot3DException();
}
signBit = (0x80000000 & leadingLengthField)? 1 : 0;
if (signBit)
{
unsigned int length = 0xffffffff ^ (leadingLengthField - 1);
subrecord.FooterOffset = dataOffset + length;
}
else
{
subrecord.FooterOffset = dataOffset + leadingLengthField;
}
this->SubRecords.push_back(subrecord);
offset = subrecord.FooterOffset + sizeof(int);
}
while (signBit == 1);
}
catch (Plot3DException&)
{
error = 1;
}
vtk_fseek(fp, pos, SEEK_SET);
}
if (controller == NULL)
{
if (error)
{
this->SubRecords.clear();
}
return error == 0;
}
// Communicate record information to all ranks.
controller->Broadcast(&error, 1, 0);
if (error)
{
this->SubRecords.clear();
return false;
}
if (rank == 0)
{
int count = static_cast<int>(this->SubRecords.size());
controller->Broadcast(&count, 1, 0);
if (count > 0)
{
controller->Broadcast(
reinterpret_cast<vtkTypeUInt64*>(&this->SubRecords[0]), count * 2, 0);
}
}
else
{
int count;
controller->Broadcast(&count, 1, 0);
this->SubRecords.resize(count);
if (count > 0)
{
controller->Broadcast(
reinterpret_cast<vtkTypeUInt64*>(&this->SubRecords[0]), count * 2, 0);
}
}
return true;
}
//-----------------------------------------------------------------------------
vtkMultiBlockPLOT3DReaderRecord::SubRecordSeparators
vtkMultiBlockPLOT3DReaderRecord::GetSubRecordSeparators(
vtkTypeUInt64 startOffset, vtkTypeUInt64 length) const
{
vtkMultiBlockPLOT3DReaderRecord::SubRecordSeparators markers;
if (this->SubRecords.size() <= 1)
{
return markers;
}
// locate the sub-record in which startOffset begins.
VectorOfSubRecords::const_iterator sr_start = this->SubRecords.begin();
while (sr_start != this->SubRecords.end()
&& sr_start->FooterOffset < startOffset)
{
sr_start++;
}
assert(sr_start != this->SubRecords.end());
vtkTypeUInt64 endOffset = startOffset + length;
VectorOfSubRecords::const_iterator sr_end = sr_start;
while (sr_end != this->SubRecords.end()
&& sr_end->FooterOffset < endOffset)
{
markers.push_back(sr_end->FooterOffset);
sr_end++;
assert(sr_end != this->SubRecords.end());
endOffset += vtkMultiBlockPLOT3DReaderRecord::SubRecordSeparatorWidth;
}
assert(sr_end != this->SubRecords.end());
return markers;
}
//-----------------------------------------------------------------------------
std::vector<std::pair<vtkTypeUInt64, vtkTypeUInt64> >
vtkMultiBlockPLOT3DReaderRecord::GetChunksToRead(
vtkTypeUInt64 start, vtkTypeUInt64 length, const std::vector<vtkTypeUInt64> &markers)
{
std::vector<std::pair<vtkTypeUInt64, vtkTypeUInt64> > chunks;
for (size_t cc=0; cc < markers.size(); ++cc)
{
if (start < markers[cc])
{
vtkTypeUInt64 chunksize = (markers[cc] - start);
chunks.push_back(std::pair<vtkTypeUInt64, vtkTypeUInt64>(start, chunksize));
length -= chunksize;
}
start = markers[cc] + vtkMultiBlockPLOT3DReaderRecord::SubRecordSeparatorWidth;
}
if (length > 0)
{
chunks.push_back(std::pair<vtkTypeUInt64, vtkTypeUInt64>(start, length));
}
return chunks;
}
//-----------------------------------------------------------------------------
vtkTypeUInt64 vtkMultiBlockPLOT3DReaderRecord::GetLengthWithSeparators(
vtkTypeUInt64 start, vtkTypeUInt64 length) const
{
return this->GetSubRecordSeparators(start, length).size()
* vtkMultiBlockPLOT3DReaderRecord::SubRecordSeparatorWidth
+ length;
}
|