写二进制VTK文件时出错

时间:2012-06-06 11:51:50

标签: c++ binaryfiles vtk paraview

我正在尝试编写一个基本的二进制VTK文件来使用ParaView显示一些数据,但我有一些错误,我不明白为什么。这是我在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;
}

ASCII文件格式工作正常,但二进制版本在ParaView中产生以下错误:

  

通用警告:在........ \ src \ VTK \ IO \ vtkDataReader.cxx中,行   1363读取二进制数据时出错!

我在VTK格式中的错误在哪里?

2 个答案:

答案 0 :(得分:2)

似乎VTK假定二进制文件是以大端编写的,而大多数PC使用小端存储(参见VTK file formats document的第2页底部)。你可以尝试在编写二进制数据时交换字节顺序,看看这是否解决了你的问题?

另请参阅this VTK users post,这与此问题类似。

答案 1 :(得分:2)

我的50美分。这是最终为我工作的代码。使用字节交换,并使用函数write跳过<<运算符

的格式
#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;
}