Hi everyone, I want to generate binary vtk file.
I write a code that can first generate correct ascii vtk file, then do some modification on that, let it can generate correct binary vtk file.
I face the problem when convert first acsii to binary, see the if switch in follow code.
#include <fstream>
#include <iostream>
#include "base64.h"
int main()
{
std::ofstream fout;
bool is_binary = true;
fout.open("new.vtu", std::ios::binary);
fout << "<?xml version=\"1.0\"?>\n";
fout << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n";
fout << "<UnstructuredGrid>\n";
fout << "<Piece NumberOfPoints=\"4\" NumberOfCells=\"2\">\n";
fout << "<PointData Scalars=\"\" Vectors=\"\">\n";
if (is_binary)
{
fout << "<DataArray type=\"Float64\" Name=\"solution\" NumberOfComponents=\"3\" format=\"binary\">\n";
double x[] = {56.25, 6.25, 0, 100, 6.25, 0, 56.25, 0, 0, 100, 0, 0};
int result_size;
char *encoding = base64((char *)x, sizeof(double) * 12, &result_size);
std::cout << result_size << std::endl;
fout.write(encoding, result_size);
free(encoding);
}
else
{
fout << "<DataArray type=\"Float64\" Name=\"solution\" NumberOfComponents=\"3\" format=\"ascii\">\n";
fout << "56.25 6.25 0\n";
fout << "100 6.25 0\n";
fout << "56.25 0 0\n";
fout << "100 0 0\n";
}
fout << "</DataArray>\n";
fout << "</PointData>\n";
fout << "<Points>\n";
fout << "<DataArray type=\"Float64\" NumberOfComponents=\"3\" format=\"ascii\">\n";
fout << "7.5 2.5 0\n";
fout << "10 2.5 0\n";
fout << "7.5 0 0\n";
fout << "10 0 0\n";
fout << "</DataArray>\n";
fout << "</Points>\n";
fout << "<Cells>\n";
fout << "<DataArray type=\"Int32\" Name = \"connectivity\" format=\"ascii\">\n";
fout << "0 2 1\n";
fout << "1 2 3\n";
fout << "</DataArray>\n";
fout << "<DataArray type=\"Int32\" Name = \"offsets\" format=\"ascii\">\n";
fout << "3\n";
fout << "6\n";
fout << "</DataArray>\n";
fout << "<DataArray type=\"Int8\" Name = \"types\" format=\"ascii\">\n";
fout << "5\n";
fout << "5\n";
fout << "5\n";
fout << "5\n";
fout << "</DataArray>\n";
fout << "</Cells>\n";
fout << "</Piece>\n";
fout << "</UnstructuredGrid>\n";
fout << "</VTKFile>\n";
fout.close();
return 0;
}
if is_binary = false
, the code can generate correct vtk file, but if is_binary = true
, the code can not generate correct vtk file.
I already notice that the base64 encoding, the base64.h
is copied from https://github.com/superwills/NibbleAndAHalf/blob/master/NibbleAndAHalf/base64.h
If I open the vtk file with paraview
, it complains that:
Can not read point data array "solution" for PointData in piece 0. The data array in the element may be too short
the code is compiled with g++ main.cpp
, and my machine is little endian.
I have already see many topic
- https://stackoverflow.com/questions/10913666/error-writing-binary-vtk-files
- https://stackoverflow.com/questions/55829282/write-vtk-file-in-binary-format
- https://stackoverflow.com/questions/10608217/how-to-write-ascii-and-binary-data-to-the-same-file-at-the-same-time
But I still can not figure out what I am doing wrong.
Can anyone help me? Thanks for your time.