------------------------------------------------------------------------------ Since the file format does not describe the endianness, we won't be dealing with that
| 371 | //------------------------------------------------------------------------------ |
| 372 | // Since the file format does not describe the endianness, we won't be dealing with that |
| 373 | vtkSmartPointer<vtkDataArray> ReadSliceFile(const std::string& fileName, |
| 374 | vtkIdType requestedTimeStep, vtkIdType nTuples, vtkIdType nComponents) |
| 375 | { |
| 376 | // two lines per time step + one time value line + 4 header lines |
| 377 | vtkIdType targetLineNumber = requestedTimeStep * 2 + 1 + 4; |
| 378 | |
| 379 | vtkNew<vtkFileResourceStream> fileStream; |
| 380 | if (fileName.empty() || !fileStream->Open(fileName.c_str())) |
| 381 | { |
| 382 | vtkErrorWithObjectMacro(nullptr, |
| 383 | << "Failed to open file: " << (fileName.empty() ? fileName : "No file name for slice given")); |
| 384 | return nullptr; |
| 385 | } |
| 386 | |
| 387 | vtkNew<vtkResourceParser> parser; |
| 388 | parser->Reset(); |
| 389 | parser->SetStream(fileStream); |
| 390 | parser->StopOnNewLineOff(); |
| 391 | |
| 392 | unsigned int size = 0; |
| 393 | for (vtkIdType iL = 0; iL < targetLineNumber; ++iL) |
| 394 | { |
| 395 | parser->Read(reinterpret_cast<char*>(&size), 4); |
| 396 | parser->ReadUntil(vtkResourceParser::DiscardNone, ReadNothing, size + 4); |
| 397 | } |
| 398 | |
| 399 | parser->Read(reinterpret_cast<char*>(&size), 4); |
| 400 | std::size_t nBytesFloat = nComponents * nTuples * sizeof(float); |
| 401 | std::size_t nBytesDouble = nComponents * nTuples * sizeof(double); |
| 402 | |
| 403 | if (size != nBytesFloat && size != nBytesDouble) |
| 404 | { |
| 405 | vtkErrorWithObjectMacro(nullptr, |
| 406 | "Line length seems to be " << size << " bytes when expected " << nBytesFloat |
| 407 | << " for floats and " << nBytesDouble << " for doubles"); |
| 408 | return nullptr; |
| 409 | } |
| 410 | |
| 411 | vtkSmartPointer<vtkDataArray> result; |
| 412 | if (size == nBytesFloat) |
| 413 | { |
| 414 | result = vtkSmartPointer<vtkFloatArray>::New(); |
| 415 | } |
| 416 | else |
| 417 | { |
| 418 | result = vtkSmartPointer<vtkDoubleArray>::New(); |
| 419 | } |
| 420 | result->SetNumberOfComponents(nComponents); |
| 421 | result->SetNumberOfTuples(nTuples); |
| 422 | std::size_t readBytes; |
| 423 | if (size == nBytesFloat) |
| 424 | { |
| 425 | readBytes = parser->Read( |
| 426 | reinterpret_cast<char*>(vtkFloatArray::FastDownCast(result)->GetPointer(0)), size); |
| 427 | } |
| 428 | else |
| 429 | { |
| 430 | readBytes = parser->Read( |
no test coverage detected