写入二进制VTK文件时出错 [英] Error writing binary VTK files

查看:134
本文介绍了写入二进制VTK文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个基本的二进制VTK文件,以使用ParaView显示一些数据,但是出现一些错误,我不明白为什么.这是我在C ++中的测试代码:

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;
}

ASCII文件格式可以完美运行,但是二进制版本在ParaView中产生以下错误:

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

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

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

我的VTK格式在哪里出错?

Where is my mistake in the VTK format?

推荐答案

VTK似乎假定二进制文件是以大端字节序写入的,而大多数PC都使用小端字节序存储(请参见 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). Can you try swapping the byte order when writing binary data and see if this solves your problem?

另请参见此VTK用户发布的 ,与此问题类似.

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

这篇关于写入二进制VTK文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆