MCPcopy Create free account
hub / github.com/Kitware/VTK / ReadSliceFile

Function ReadSliceFile

IO/FDS/vtkFDSReader.cxx:373–442  ·  view source on GitHub ↗

------------------------------------------------------------------------------ Since the file format does not describe the endianness, we won't be dealing with that

Source from the content-addressed store, hash-verified

371//------------------------------------------------------------------------------
372// Since the file format does not describe the endianness, we won't be dealing with that
373vtkSmartPointer<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(

Callers 1

VisitMethod · 0.85

Calls 12

ReadUntilMethod · 0.80
NewFunction · 0.50
FastDownCastFunction · 0.50
emptyMethod · 0.45
OpenMethod · 0.45
c_strMethod · 0.45
ResetMethod · 0.45
SetStreamMethod · 0.45
ReadMethod · 0.45
SetNumberOfComponentsMethod · 0.45
SetNumberOfTuplesMethod · 0.45
GetPointerMethod · 0.45

Tested by

no test coverage detected