以二进制格式写入 vtk 文件
Write vtk file in binary format
我正在尝试从我的C++代码编写vtk文件。我有两个版本,一个是ASCII版本,另一个是二进制版本。ASCII 版本运行良好。
我想做一些类似于这篇文章的事情:写入二进制 VTK 文件时出错
这是我的代码:
std::ofstream file;
if(is_binary)
file.open("test.vtk", std::ios::out | std::ios::binary);
else
file.open("test.vtk");
file << "# vtk DataFile Version 2.0" << std::endl
<< "Comment if needed" << std::endl;
if(is_binary)
file << "BINARY"<< std::endl << std::endl;
else
file << "ASCII"<< std::endl << std::endl;
file << "DATASET POLYDATA" << std::endl << "POINTS " << nb_particles << " float" << std::endl;
for(size_t cell_i=0;cell_i<n_cells;cell_i++)
for(size_t i =0; i<cells[cell_i].size();++i)
{
if(is_binary)
{
double rx = cells[cell_i][field::rx][i];
double ry = cells[cell_i][field::ry][i];
double rz = cells[cell_i][field::rz][i];
SwapEnd(rx);
SwapEnd(ry);
SwapEnd(rz);
file.write(reinterpret_cast<char*>(&rx), sizeof(double));
file.write(reinterpret_cast<char*>(&ry), sizeof(double));
file.write(reinterpret_cast<char*>(&rz), sizeof(double));
}
else
{
Vec3d tmp = grid.particle_position(cell_i,i);
file << tmp.x << " " << tmp.y << " " << tmp.z << std::endl;
}
}
if(is_binary)
file << std::endl;
file << "POINT_DATA " << nb_particles << std::endl;
if(has_id_field)
{
file<< "SCALARS index int 1" << std::endl<< "LOOKUP_TABLE default" << std::endl;
for(size_t cell_i=0;cell_i<n_cells;cell_i++)
for(size_t i =0; i<cells[cell_i].size();++i)
{
if(is_binary)
{
uint64_t id = cells[cell_i][field::id][i];
SwapEnd(id);
file.write(reinterpret_cast<char*>(&id), sizeof(uint64_t));
}
else
file << cells[cell_i][field::id][i] << std::endl;
}
if(is_binary)
file << std::endl;
}
if(has_type_field)
{
file<< "SCALARS type int 1" << std::endl<< "LOOKUP_TABLE default" << std::endl;
for(size_t cell_i=0;cell_i<n_cells;cell_i++)
for(size_t i =0; i<cells[cell_i].size();++i)
{
if(is_binary)
{
uint8_t type;
SwapEnd(type);
file.write(reinterpret_cast<char*>(&type), sizeof(uint8_t));
}
else
file << static_cast<int>(cells[cell_i][field::type][i]) << std::endl;
}
if(is_binary)
file << std::endl;
}
file.close();
使用函数:
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]);
}
我的系统是小端序:
lscpu | grep "Byte Order"
Byte Order: Little Endian
Paraview在读取输出文件时出错:
vtkPolyDataReader (0x3863800): Unrecognized keyword: @aic�9h��8
我做得不好的地方是什么?
好的,问题是我编写二进制文件时的数据大小。以下代码的和平有效。希望可以帮助某人。
std::ofstream file;
if(is_binary)
file.open("test.vtk", std::ios::out | std::ios::binary);
else
file.open("test.vtk");
file << "# vtk DataFile Version 2.0" << std::endl
<< "Comment if needed" << std::endl;
if(is_binary)
file << "BINARY"<< std::endl << std::endl;
else
file << "ASCII"<< std::endl << std::endl;
file << "DATASET POLYDATA" << std::endl << "POINTS " << nb_particles << " double" << std::endl;
for(size_t cell_i=0;cell_i<n_cells;cell_i++)
for(size_t i =0; i<cells[cell_i].size();++i)
{
if(is_binary)
{
double rx = cells[cell_i][field::rx][i];
double ry = cells[cell_i][field::ry][i];
double rz = cells[cell_i][field::rz][i];
SwapEnd(rx);
SwapEnd(ry);
SwapEnd(rz);
file.write(reinterpret_cast<char*>(&rx), sizeof(double));
file.write(reinterpret_cast<char*>(&ry), sizeof(double));
file.write(reinterpret_cast<char*>(&rz), sizeof(double));
}
else
{
Vec3d tmp = grid.particle_position(cell_i,i);
file << tmp.x << " " << tmp.y << " " << tmp.z << std::endl;
}
}
if(is_binary)
file << std::endl;
file << "POINT_DATA " << nb_particles << std::endl;
if(has_id_field)
{
file<< "SCALARS index int 1" << std::endl<< "LOOKUP_TABLE default" << std::endl;
for(size_t cell_i=0;cell_i<n_cells;cell_i++)
for(size_t i =0; i<cells[cell_i].size();++i)
{
if(is_binary)
{
uint64_t id = cells[cell_i][field::id][i];
int id_i = static_cast<int>(id);
SwapEnd(id_i);
file.write(reinterpret_cast<char*>(&id_i), sizeof(int));
}
else
file << cells[cell_i][field::id][i] << std::endl;
}
if(is_binary)
file << std::endl;
}
if(has_type_field)
{
file<< "SCALARS type int 1" << std::endl<< "LOOKUP_TABLE default" << std::endl;
for(size_t cell_i=0;cell_i<n_cells;cell_i++)
for(size_t i =0; i<cells[cell_i].size();++i)
{
if(is_binary)
{
uint8_t type= cells[cell_i][field::type][i];
int type_i = static_cast<int>(type);
SwapEnd(type_i);
file.write(reinterpret_cast<char*>(&type_i), sizeof(int));
}
else
file << static_cast<int>(cells[cell_i][field::type][i]) << std::endl;
}
if(is_binary)
file << std::endl;
}
file.close();
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- Qt VTK交互风格的信号到小部件
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在 VTK 下保存网格文件
- 以二进制格式写入 vtk 文件
- C++VTK从多个文件中读取和保存数据
- 如何在C++中编写带有 VTK 的标量文件
- 将 Obj 文件导入 vtk
- 如何将 vtkSphere 保存到 VTK 文件
- 正在读取.vtk文件
- Cmake编译目录中具有VTK依赖项的所有c++文件
- 写入二进制 VTK 文件时出错
- 无法启动使用 vtk 的可执行文件
- 什么软件可以可视化VTK文件格式
- Vtk读取dicom文件
- 使用vtkGenericDataObjectReader读取.vtk二进制文件