Error writing binary VTK files

元气小坏坏 提交于 2020-01-02 14:53:21

问题


I am trying to write a basic binary VTK file to display some data using ParaView but I have some errors and I don't understand why. Here is my test code in C++:

#include <iostream>
#include <fstream>

double myarray[72] = {
0,0,0,1,0,0,2,0,0,3,0,0,4,0,0,
5,0,0,0,1,0,1,1,0,2,1,0,3,1,0,
4,1,0,5,1,0,0,2,0,1,2,0,2,2,0,
3,2,0,4,2,0,5,2,0,0,3,0,1,3,0,
2,3,0,3,3,0,4,3,0,5,3,0};
int main()
{
    std::ofstream vtkstream("test01.vtk", std::ios::out | std::ios::trunc);
    bool ascii = false;
    if (vtkstream) {
        vtkstream<<"# vtk DataFile Version 2.0"<<"\n";
        vtkstream<<"Exemple"<<"\n";
        if (ascii) {
            vtkstream<<"ASCII"<<"\n";
            vtkstream.close();
            vtkstream.clear();
            vtkstream.open("test01.vtk", std::ios::out | std::ios::app);
            vtkstream<<"DATASET STRUCTURED_GRID"<<std::endl;
            vtkstream<<"DIMENSIONS 6 4 1"<<std::endl;
            vtkstream<<"POINTS 24 double"<<std::endl;
            for (unsigned int i = 0; i < 72; ++i) {
                vtkstream<<myarray[i]<<" ";
            }
        } else {
            vtkstream<<"BINARY"<<"\n";
            vtkstream.close();
            vtkstream.clear();
            vtkstream.open("test01.vtk", std::ios::out | std::ios::app | std::ios::binary);
            vtkstream<<"DATASET STRUCTURED_GRID"<<std::endl;
            vtkstream<<"DIMENSIONS 6 4 1"<<std::endl;
            vtkstream<<"POINTS 24 double"<<std::endl;
            for (unsigned int i = 0; i < 72; ++i) {
                vtkstream<<myarray[i];
            }
        }
        vtkstream.close();
    } else {
        std::cout<<"ERROR"<<std::endl;
    }
    return 0;
}

The ASCII file format works perfectly but the binary version produces the following error in ParaView:

Generic Warning: In ........\src\VTK\IO\vtkDataReader.cxx, line 1363 Error reading binary data!

Where is my mistake in the VTK format?


回答1:


It seems that VTK assumes that binary files are written as big endian, whereas most PCs use little endian storage (see the bottom of page 2 of the VTK file formats document). Can you try swapping the byte order when writing binary data and see if this solves your problem?

See also this VTK users post, which is similar to this question.




回答2:


My 50 cents. Here is the code that finally worked for me. Using byte swap, and using the function write to skip the formatting of the << operator

#include <iostream>
#include <fstream>

// Thanks to https://stackoverflow.com/questions/105252
template <typename T>
void SwapEnd(T& var)
{
  char* varArray = reinterpret_cast<char*>(&var);
  for(long i = 0; i < static_cast<long>(sizeof(var)/2); i++)
    std::swap(varArray[sizeof(var) - 1 - i],varArray[i]);
}

double myarray[72] = {
  0.001,0.002,0,1,0,0,2,0,0,3,0,0,4,0,0,
  5,0,0,0,1,0,1,1,0,2,1,0,3,1,0,
  4,1,0,5,1,0,0,2,0,1,2,0,2,2,0,
  3,2,0,4,2,0,5,2,0,0,3,0,1,3,0,
  2,3,0,3,3,0,4,3,0,5,3,0};

int main()
{
  std::ofstream vtkstream;
  vtkstream.open("test.vtk", std::ios::out | std::ios::app | std::ios::binary);
  if (vtkstream) {
    vtkstream<<"# vtk DataFile Version 2.0"<<"\n";
    vtkstream<<"Exemple"<<"\n";
    vtkstream<<"BINARY"<<"\n";
    vtkstream<<"DATASET STRUCTURED_GRID"<<std::endl;
    vtkstream<<"DIMENSIONS 6 4 1"<<std::endl;
    vtkstream<<"POINTS 24 double"<<std::endl;
    for (unsigned int i = 0; i < 72; ++i) {
      SwapEnd(myarray[i]);
      vtkstream.write((char*)&myarray[i], sizeof(double));
    }
    vtkstream.close();
  } else {
    std::cout<<"ERROR"<<std::endl;
  }
  return 0;
}


来源:https://stackoverflow.com/questions/10913666/error-writing-binary-vtk-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!