【问题标题】:Write vtk file in binary format以二进制格式写入 vtk 文件
【发布时间】:2019-04-25 17:34:41
【问题描述】:

我正在尝试从我的 C++ 代码中编写一个 vtk 文件。我有两个版本,一个是 ASCII,另一个是二进制。 ASCII 版本运行良好。

我想做类似这篇文章的事情:Error writing binary VTK files

这是我的代码:

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

我哪里做得不好?

【问题讨论】:

  • 这是你应该自己解决的问题。获取一组简单的数据和一个已知的工作二进制编写器(您甚至链接了一个)。使用您的二进制编写器输出简单数据,也可以使用已知的编写器输出简单数据。通过二进制(十六进制?)查看器中的 2 个输出,找出它们的分歧点。这应该是足够的信息来解决您的问题。如果您已经这样做了但仍然不知道,请在此处显示差异作为对您问题的编辑。

标签: c++ binary vtk


【解决方案1】:

好的,问题是我编写二进制文件时的数据大小。下面的代码和平工作。希望可以帮助别人。

 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();

【讨论】:

  • 如果你把所有的代码或者一个仓库的链接贴出来会更好。
猜你喜欢
  • 2017-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-13
  • 2020-12-13
  • 1970-01-01
  • 2014-12-08
相关资源
最近更新 更多