2

我正在尝试编写一个基本的二进制 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 格式中的错误在哪里?

4

2 回答 2

2

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

另请参阅此 VTK 用户帖子,与此问题类似。

于 2012-06-06T13:55:52.550 回答
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;
}
于 2018-01-19T21:21:28.623 回答