2012-06-06 38 views
1

我想寫一個基本的二進制VTK文件來顯示一些數據使用ParaView,但我有一些錯誤,我不明白爲什麼。這裏是C++我的測試代碼:編寫二進制VTK文件時出錯

#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格式錯誤在哪裏?

+0

的格式化這個問題是相關的:http://stackoverflow.com/q/10608217/623518 – Chris

回答

1

看起來VTK認爲二進制文件被寫爲大端,而大多數PC使用小端存儲(參見VTK file formats document第2頁的底部)。你可以嘗試在編寫二進制數據時交換字節順序,看看這是否能解決你的問題?

另請參閱this VTK users post,這與此問題類似。

1

我的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; 
} 
+0

是偉大的。但是,最好在這裏使用「float」而不是「double」,因爲ParaView不支持「double」(請參閱​​https://public.kitware.com/pipermail/paraview/2015-April/033896.html)(糾正我,如果paraview/opengl修復這個問題)。另外,在cpp中,float需要4個字節,而double使用8個字節。 –