简体   繁体   English

写入二进制VTK文件时出错

[英]Error writing binary VTK files

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. 我正在尝试编写一个基本的二进制VTK文件,以使用ParaView显示一些数据,但出现一些错误,我不明白为什么。 Here is my test code in C++: 这是我在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: ASCII文件格式可以完美运行,但是二进制版本在ParaView中产生以下错误:

Generic Warning: In ........\\src\\VTK\\IO\\vtkDataReader.cxx, line 1363 Error reading binary data! 一般警告:在........ \\ src \\ VTK \\ IO \\ vtkDataReader.cxx中,行1363读取二进制数据时出错!

Where is my mistake in the VTK format? VTK格式的错误在哪里?

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 ). 似乎VTK假定二进制文件是按大端字节序写入的,而大多数PC都使用很少的字节序存储(请参阅VTK文件格式文档第2页的底部)。 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. 另请参见此VTK用户帖子 ,与此问题类似。

My 50 cents. 我的50美分。 Here is the code that finally worked for me. 这是最终对我有用的代码。 Using byte swap, and using the function write to skip the formatting of the << operator 使用字节交换,并使用函数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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM