WG: [Paraview] VTU File Format wih raw Data [includeing] attachement
Sören Freudiger
muffmolch at gmx.de
Fri Oct 19 07:32:18 EDT 2007
Hi
We really tried to do it. But it still doesn't work. And we cannot find the problem.
It looks quite easy to me. But something with the offsets must be tricky.
Our ascii-vtu file works fine (without appended data).
But the appended version with "raw" data couldn't be opened.
Attached there's a small program to show the problem.
It's very simple and justs buts one or two octants with some node data into a vtu-file.
If we only have one cell it seems to work. It can be opened with paraview 2.6.x and 3.x.
But the two cell version always ends with the error message:
" ERROR: In ..\..\..\ParaView3\VTK\IO\vtkXMLUnstructuredDataReader.cxx, line 646
vtkXMLUnstructuredGridReader (051948A0): Cannot read cell connectivity from Cells in piece 0 because the "connectivity" array is not long enough. "
But I cannot find an error at the connectivities (I just wrote it to ASCII and the connectivity for each Oct was 8 and the datalength was #cells*4(byte) ).
The type sizes are correct. Float32=4bytes, Int32=4 bytes...
Every data-block is something like that:
[data-size in byte][data]
[data-size in byte] = Int32
[data] = #data * specified type
The blocks are stored continuously after the "_"
The offsets seems to be correct
offset[0] = 0
offset[1] = offset[0] + 4 + bytes of dataset[0]
offset[2] = offset[1] + 4 + bytes of dataset[1]
offset[3] = offset[2] + 4 + bytes of dataset[3]
...
I don't get it. Has anybody a clue?
Gruß,
SirAnn
--
Tel.: +49 531/391-7595,
Mobil: +49 176/210 17 444
email1: muffmolch at gmx.de
email2: muffmolch at web.de
email3: SirAnnTheMan at googlemail.com
-----------
main.cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
std::ofstream out("./file.vtu");
if(!out) exit(1);
int nofCells = 1; //you can chose 1 or 2
int nofNodes = nofCells*8; //octs!!!
int bytesPerByteVal = 4; //==sizeof(int)
int bytesPoints = 3 /*x1/x2/x3 */ * nofNodes * sizeof(float);
int bytesCellConnectivty = 8 /*nodes per oct */ * nofCells * sizeof(int );
int bytesCellOffsets = 1 /*offset per oct*/ * nofCells * sizeof(int );
int bytesCellTypes = 1 /*type of oct */ * nofCells * sizeof(int );
int bytesScalarData = 1 /*scalar */ * nofNodes * sizeof(float);
int offset = 0;
//VTK FILE
out<<"<?xml version=\"1.0\"?>\n";
out<<"<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\" >"<<"\n";
out<<" <UnstructuredGrid>"<<"\n";
out<<" <Piece NumberOfPoints=\""<<nofNodes<<"\" NumberOfCells=\""<<nofCells<<"\">\n";
//POINTS SECTION
out<<" <Points>\n";
out<<" <DataArray type=\"Float32\" NumberOfComponents=\"3\" format=\"appended\" offset=\""<< offset <<"\" />\n";
out<<" </Points>\n";
offset += (bytesPerByteVal + bytesPoints);
//CELLS SECTION
out<<" <Cells>\n";
out<<" <DataArray type=\"Int32\" Name=\"connectivity\" format=\"appended\" offset=\""<< offset <<"\" />\n";
offset += (bytesPerByteVal + bytesCellConnectivty);
out<<" <DataArray type=\"Int32\" Name=\"offsets\" format=\"appended\" offset=\""<< offset <<"\" />\n";
offset += (bytesPerByteVal + bytesCellOffsets);
out<<" <DataArray type=\"Int32\" Name=\"types\" format=\"appended\" offset=\""<< offset <<"\" />\n ";
offset += (bytesPerByteVal + bytesCellTypes);
out<<" </Cells>\n";
//DATA SECTION
out<<" <PointData>\n";
for(size_t s=0; s<datanames.size(); ++s)
{
out<< " <DataArray type=\"Float32\" Name=\""<< datanames[s] <<"\" format=\"appended\" offset=\""<< offset <<"\" /> \n";
offset += (bytesPerByteVal + bytesScalarData);
}
out<<" </PointData>\n";
out<<" </Piece>\n";
out<<" </UnstructuredGrid>\n";
int itmp;
float ftmp;
// AppendedData SECTION
out<<" <AppendedData encoding=\"raw\">\n";
out<<"_";
//POINTS SECTION
//out<<bytesPoints<<":";
out.write((char*)&bytesPoints,bytesPerByteVal);
//point1
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
//point2
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
//point3
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
//point4
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
//point5
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
//point6
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
//point7
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
//point8
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
if(nofCells==2){
//point9
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 3.0; out.write((char*)&ftmp,sizeof(float));
//point10
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 3.0; out.write((char*)&ftmp,sizeof(float));
//point11
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 3.0; out.write((char*)&ftmp,sizeof(float));
//point12
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 3.0; out.write((char*)&ftmp,sizeof(float));
//point13
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 4.0; out.write((char*)&ftmp,sizeof(float));
//point14
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 4.0; out.write((char*)&ftmp,sizeof(float));
//point15
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 4.0; out.write((char*)&ftmp,sizeof(float));
//point16
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 4.0; out.write((char*)&ftmp,sizeof(float));
}
//CELLS SECTION
//cellConnectivity
out.write( (char*)&bytesCellConnectivty, bytesPerByteVal );
//cell1
itmp = 0; out.write( (char*)&itmp, sizeof(int) );
itmp = 1; out.write( (char*)&itmp, sizeof(int) );
itmp = 3; out.write( (char*)&itmp, sizeof(int) );
itmp = 2; out.write( (char*)&itmp, sizeof(int) );
itmp = 4; out.write( (char*)&itmp, sizeof(int) );
itmp = 5; out.write( (char*)&itmp, sizeof(int) );
itmp = 7; out.write( (char*)&itmp, sizeof(int) );
itmp = 6; out.write( (char*)&itmp, sizeof(int) );
if(nofCells==2)
{
//cell2
itmp = 8; out.write( (char*)&itmp, sizeof(int) );
itmp = 9; out.write( (char*)&itmp, sizeof(int) );
itmp = 11; out.write( (char*)&itmp, sizeof(int) );
itmp = 10; out.write( (char*)&itmp, sizeof(int) );
itmp = 12; out.write( (char*)&itmp, sizeof(int) );
itmp = 13; out.write( (char*)&itmp, sizeof(int) );
itmp = 15; out.write( (char*)&itmp, sizeof(int) );
itmp = 14; out.write( (char*)&itmp, sizeof(int) );
}
//cellOffsets
out.write( (char*)&bytesCellOffsets, bytesPerByteVal );
itmp = 8; out.write( (char*)&itmp, sizeof(int) );
if(nofCells==2) { itmp = 16; out.write( (char*)&itmp, sizeof(int) ); }
//cellTypes
out.write( (char*)&bytesCellTypes, bytesPerByteVal );
int vtkCellType = 11;
out.write( (char*)&vtkCellType, sizeof(int) );
if(nofCells==2) { out.write( (char*)&vtkCellType, sizeof(int) ); }
//DATA SECTION
out.write((char*)&bytesScalarData,bytesPerByteVal);
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 2.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 3.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 4.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 5.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 6.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 7.0; out.write((char*)&ftmp,sizeof(float));
if(nofCells==2)
{
ftmp = 8.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 9.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 10.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 11.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 12.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 13.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 14.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 15.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 16.0; out.write((char*)&ftmp,sizeof(float));
}
out<<"\n</AppendedData>\n";
out<<"</VTKFile>";
out<<endl;
out<<flush;
out.close();
cout<<"done"<<endl;
return 0;
}
-------------------------------------
-----Ursprüngliche Nachricht-----
Von: Brad King [mailto:brad.king at kitware.com]
Gesendet: Donnerstag, 18. Oktober 2007 15:13
An: Sören Freudiger
Cc: paraview at paraview.org
Betreff: Re: [Paraview] VTU File Format wih raw Data
Sören Freudiger wrote:
> Is it possible to use “raw” data inside the VTU files?
Yes.
> Something like:
>
> <VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian">
> <UnstructuredGrid>
> <Piece NumberOfPoints="6" NumberOfCells="1">
> <Points>
> <DataArray type = " Float32" NumberOfComponents="3" format="appended" offset=”0” />
> </Points>
> <Cells>
> <DataArray type="Int32" Name="connectivity" format="appended" offset=”123” />
> <DataArray type="Int32" Name="offsets" format="appended" offset=”234” />
> <DataArray type="UInt8" Name="types" format="appended" offset=”345” />
> </Cells>
> <PointData>
> <DataArray type="float" Name="stress" format="appended" offset=”567” />
> </PointData>
> </Piece>
> </UnstructuredGrid>
> <AppendedData encoding="raw">
> _...
> </AppendedData>
> </VTKFile>
>
> I cannot find a concrete example.
> Is is right that I the raw data is organized somehow like that
>
> _(size=#points*3*sizeof(Float32()(data of array)(size of array1)(size=8(oct)*#cells*sizeof(Int32()(connectivity array)…
>
> If so, has the length to be stored in same format as Data (Int32,Float32, etc.).
> Is it possible to mix data types or do they all have to be in same format (e.g. Float32)
The format is
_NNNN<data>NNNN<data>NNNN<data>
^ ^ ^
1 2 3
where each "NNNN" is an unsigned 32-bit integer, and <data> consists of
a number of bytes equal to the preceding NNNN value. The corresponding
DataArray elements must have format="appended" and offset attributes
equal to the following:
1.) offset="0"
2.) offset="(4+NNNN1)"
3.) offset="(4+NNNN1+4+NNNN2)"
These blocks may be in any order as long as the corresponding offset
elements point to the right spot. Each array can have its own type
specified in its DataArray element. The byte order of all values (both
in the NNNN header and in the data) is that specified by the byte_order
attribute at the top of the file.
-Brad
-------------- next part --------------
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
std::ofstream out("./file.vtu");
if(!out) exit(1);
int nofCells = 1; //you can chose 1 or 2
int nofNodes = nofCells*8; //octs!!!
int bytesPerByteVal = 4; //==sizeof(int)
int bytesPoints = 3 /*x1/x2/x3 */ * nofNodes * sizeof(float);
int bytesCellConnectivty = 8 /*nodes per oct */ * nofCells * sizeof(int );
int bytesCellOffsets = 1 /*offset per oct*/ * nofCells * sizeof(int );
int bytesCellTypes = 1 /*type of oct */ * nofCells * sizeof(int );
int bytesScalarData = 1 /*scalar */ * nofNodes * sizeof(float);
int offset = 0;
//VTK FILE
out<<"<?xml version=\"1.0\"?>\n";
out<<"<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\" >"<<"\n";
out<<" <UnstructuredGrid>"<<"\n";
out<<" <Piece NumberOfPoints=\""<<nofNodes<<"\" NumberOfCells=\""<<nofCells<<"\">\n";
//POINTS SECTION
out<<" <Points>\n";
out<<" <DataArray type=\"Float32\" NumberOfComponents=\"3\" format=\"appended\" offset=\""<< offset <<"\" />\n";
out<<" </Points>\n";
offset += (bytesPerByteVal + bytesPoints);
//CELLS SECTION
out<<" <Cells>\n";
out<<" <DataArray type=\"Int32\" Name=\"connectivity\" format=\"appended\" offset=\""<< offset <<"\" />\n";
offset += (bytesPerByteVal + bytesCellConnectivty);
out<<" <DataArray type=\"Int32\" Name=\"offsets\" format=\"appended\" offset=\""<< offset <<"\" />\n";
offset += (bytesPerByteVal + bytesCellOffsets);
out<<" <DataArray type=\"Int32\" Name=\"types\" format=\"appended\" offset=\""<< offset <<"\" />\n ";
offset += (bytesPerByteVal + bytesCellTypes);
out<<" </Cells>\n";
//DATA SECTION
out<<" <PointData>\n";
for(size_t s=0; s<datanames.size(); ++s)
{
out<< " <DataArray type=\"Float32\" Name=\""<< datanames[s] <<"\" format=\"appended\" offset=\""<< offset <<"\" /> \n";
offset += (bytesPerByteVal + bytesScalarData);
}
out<<" </PointData>\n";
out<<" </Piece>\n";
out<<" </UnstructuredGrid>\n";
int itmp;
float ftmp;
// AppendedData SECTION
out<<" <AppendedData encoding=\"raw\">\n";
out<<"_";
//POINTS SECTION
//out<<bytesPoints<<":";
out.write((char*)&bytesPoints,bytesPerByteVal);
//point1
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
//point2
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
//point3
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
//point4
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
//point5
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
//point6
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
//point7
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
//point8
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
if(nofCells==2){
//point9
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 3.0; out.write((char*)&ftmp,sizeof(float));
//point10
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 3.0; out.write((char*)&ftmp,sizeof(float));
//point11
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 3.0; out.write((char*)&ftmp,sizeof(float));
//point12
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 3.0; out.write((char*)&ftmp,sizeof(float));
//point13
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 4.0; out.write((char*)&ftmp,sizeof(float));
//point14
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 4.0; out.write((char*)&ftmp,sizeof(float));
//point15
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 4.0; out.write((char*)&ftmp,sizeof(float));
//point16
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 4.0; out.write((char*)&ftmp,sizeof(float));
}
//CELLS SECTION
//cellConnectivity
out.write( (char*)&bytesCellConnectivty, bytesPerByteVal );
//cell1
itmp = 0; out.write( (char*)&itmp, sizeof(int) );
itmp = 1; out.write( (char*)&itmp, sizeof(int) );
itmp = 3; out.write( (char*)&itmp, sizeof(int) );
itmp = 2; out.write( (char*)&itmp, sizeof(int) );
itmp = 4; out.write( (char*)&itmp, sizeof(int) );
itmp = 5; out.write( (char*)&itmp, sizeof(int) );
itmp = 7; out.write( (char*)&itmp, sizeof(int) );
itmp = 6; out.write( (char*)&itmp, sizeof(int) );
if(nofCells==2)
{
//cell2
itmp = 8; out.write( (char*)&itmp, sizeof(int) );
itmp = 9; out.write( (char*)&itmp, sizeof(int) );
itmp = 11; out.write( (char*)&itmp, sizeof(int) );
itmp = 10; out.write( (char*)&itmp, sizeof(int) );
itmp = 12; out.write( (char*)&itmp, sizeof(int) );
itmp = 13; out.write( (char*)&itmp, sizeof(int) );
itmp = 15; out.write( (char*)&itmp, sizeof(int) );
itmp = 14; out.write( (char*)&itmp, sizeof(int) );
}
//cellOffsets
out.write( (char*)&bytesCellOffsets, bytesPerByteVal );
itmp = 8; out.write( (char*)&itmp, sizeof(int) );
if(nofCells==2) { itmp = 16; out.write( (char*)&itmp, sizeof(int) ); }
//cellTypes
out.write( (char*)&bytesCellTypes, bytesPerByteVal );
int vtkCellType = 11;
out.write( (char*)&vtkCellType, sizeof(int) );
if(nofCells==2) { out.write( (char*)&vtkCellType, sizeof(int) ); }
//DATA SECTION
out.write((char*)&bytesScalarData,bytesPerByteVal);
ftmp = 0.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 1.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 2.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 3.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 4.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 5.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 6.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 7.0; out.write((char*)&ftmp,sizeof(float));
if(nofCells==2)
{
ftmp = 8.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 9.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 10.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 11.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 12.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 13.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 14.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 15.0; out.write((char*)&ftmp,sizeof(float));
ftmp = 16.0; out.write((char*)&ftmp,sizeof(float));
}
out<<"\n</AppendedData>\n";
out<<"</VTKFile>";
out<<endl;
out<<flush;
out.close();
cout<<"done"<<endl;
return 0;
}
More information about the ParaView
mailing list