/*! * Writes entire VTR but the data. */ void VTKRectilinearGrid::writeMetaData( ){ std::fstream str; str.open( fh.getPath( ), std::ios::out ) ; //Writing XML header str << "<?xml version=\"1.0\"?>" << std::endl; //Writing Piece Information str << "<VTKFile type=\"RectilinearGrid\" version=\"0.1\" byte_order=\"LittleEndian\" header_type=\"" << HeaderType << "\">" << std::endl; str << " <RectilinearGrid WholeExtent= \"" << global_index[0][0] << " " << global_index[0][1]<< " " << global_index[1][0] << " " << global_index[1][1]<< " " << global_index[2][0] << " " << global_index[2][1]<< " " << "\" >" << std::endl; str << " <Piece Extent= \" " << local_index[0][0] << " " << local_index[0][1]<< " " << local_index[1][0] << " " << local_index[1][1]<< " " << local_index[2][0] << " " << local_index[2][1]<< " " << "\" >" << std::endl; //Header for Data writeDataHeader( str, false ) ; //Wring Geometry Information str << " <Coordinates>" << std::endl; writeDataArray( str, *geometry[0] ) ; writeDataArray( str, *geometry[1] ) ; writeDataArray( str, *geometry[2] ) ; str << " </Coordinates>" << std::endl; //Closing Piece str << " </Piece>" << std::endl; str << " </RectilinearGrid>" << std::endl; //Write Appended Section str << " <AppendedData encoding=\"raw\">" << std::endl; str << "_" ; str << std::endl ; //Closing XML str << "</VTKFile>" << std::endl; str.close() ; return ; };
/*! * Writes entire VTU but the data. */ void VTKUnstructuredGrid::writeMetaData( ){ std::fstream str ; std::string line ; str.open( fh.getName( ), std::ios::out ) ; //Writing XML header str << "<?xml version=\"1.0\"?>" << std::endl; //Writing Piece Information str << "<VTKFile type=\"UnstructuredGrid\" version=\"1.0\" byte_order=\"LittleEndian\" header_type=\"" << HeaderType << "\">" << std::endl; str << " <UnstructuredGrid>" << std::endl;; str << " <Piece NumberOfPoints=\"" << nr_points << "\" NumberOfCells=\"" << nr_cells << "\">" << std::endl; //Header for Data writeDataHeader( str, false ); //Wring Geometry Information str << " <Points>" << std::endl ;; writeDataArray( str, geometry[0] ) ; str << " </Points>" << std::endl; str << " <Cells>" << std::endl ;; writeDataArray( str, geometry[1] ) ; writeDataArray( str, geometry[2] ) ; writeDataArray( str, geometry[3] ) ; str << " </Cells>" << std::endl; //Closing Piece str << " </Piece>" << std::endl; str << " </UnstructuredGrid>" << std::endl; //Appended Section str << " <AppendedData encoding=\"raw\">" << std::endl; str << "_" ; str << std::endl ; str << "</VTKFile>" << std::endl; str.close() ; return ; };
/*! * Writes data headers in strean * @param[in] str output stream * @param[in] parallel flag for parallel data headers for collection files [true/false] */ void VTK::writeDataHeader( std::fstream &str, bool parallel ){ std::string line ; VTKLocation location ; std::stringstream scalars, vectors ; for( int j=0; j<2; j++){ if( j==0 ) location = VTKLocation::POINT ; if( j==1 ) location = VTKLocation::CELL ; // scalars.clear(); // vectors.clear(); scalars.str(""); vectors.str(""); //Creating Scalar and Vector Lists scalars << "\"" ; vectors << "\"" ; for( auto &field : data ){ if( field->getLocation() == location){ if( field->getFieldType() == VTKFieldType::SCALAR ) scalars << field->getName() << " " ; else if( field->getFieldType() == VTKFieldType::VECTOR ) vectors << field->getName() << " " ; }; }; scalars << "\"" ; vectors << "\"" ; if( location == VTKLocation::POINT) { str << " <" ; if( parallel ) str << "P" ; str << "PointData " ; } else if( location == VTKLocation::CELL ) { str << " <" ; if( parallel ) str << "P" ; str << "CellData " ; }; str << " Scalars=" << scalars.str() << " Vectors=" << vectors.str() << ">" << std::endl; //Writing DataArray for( auto &field : data ){ if( field->getLocation() == location && !parallel) writeDataArray( str, *field ) ; if( field->getLocation() == location && parallel) writePDataArray( str, *field ); }; str << " </" ; if( parallel ) str << "P" ; if( location == VTKLocation::POINT) str << "PointData> " << std::endl; if( location == VTKLocation::CELL) str << "CellData> " << std::endl; }; return ; };
void VTKExporter::writeVTKXML() { std::string filename = vtkFilename.getFullPath(); std::ostringstream oss; oss << nbFiles; if ( filename.size() > 3 && filename.substr(filename.size()-4)==".vtu") { if (!overwrite.getValue()) filename = filename.substr(0,filename.size()-4) + oss.str() + ".vtu"; } else { if (!overwrite.getValue()) filename += oss.str(); filename += ".vtu"; } outfile = new std::ofstream(filename.c_str()); if( !outfile->is_open() ) { msg_error() << "Error creating file "<<filename; delete outfile; outfile = NULL; return; } const helper::vector<std::string>& pointsData = dPointsDataFields.getValue(); const helper::vector<std::string>& cellsData = dCellsDataFields.getValue(); helper::ReadAccessor<Data<defaulttype::Vec3Types::VecCoord> > pointsPos = position; const int nbp = (!pointsPos.empty()) ? pointsPos.size() : topology->getNbPoints(); unsigned int numberOfCells; numberOfCells = ( (writeEdges.getValue()) ? topology->getNbEdges() : 0 ) +( (writeTriangles.getValue()) ? topology->getNbTriangles() : 0 ) +( (writeQuads.getValue()) ? topology->getNbQuads() : 0 ) +( (writeTetras.getValue()) ? topology->getNbTetras() : 0 ) +( (writeHexas.getValue()) ? topology->getNbHexas() : 0 ); msg_info() << "### VTKExporter[" << this->getName() << "] ###" << msgendl << "Nb points: " << nbp << msgendl << "Nb edges: " << ( (writeEdges.getValue()) ? topology->getNbEdges() : 0 ) << msgendl << "Nb triangles: " << ( (writeTriangles.getValue()) ? topology->getNbTriangles() : 0 ) << msgendl << "Nb quads: " << ( (writeQuads.getValue()) ? topology->getNbQuads() : 0 ) << msgendl << "Nb tetras: " << ( (writeTetras.getValue()) ? topology->getNbTetras() : 0 ) << msgendl << "Nb hexas: " << ( (writeHexas.getValue()) ? topology->getNbHexas() : 0 ) << msgendl << "### ###" << msgendl << "Total nb cells: " << numberOfCells << msgendl; //write header *outfile << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"BigEndian\">" << std::endl; *outfile << " <UnstructuredGrid>" << std::endl; //write piece *outfile << " <Piece NumberOfPoints=\"" << nbp << "\" NumberOfCells=\""<< numberOfCells << "\">" << std::endl; //write point data if (!pointsData.empty()) { *outfile << " <PointData>" << std::endl; writeDataArray(pointsDataObject, pointsDataField, pointsDataName); *outfile << " </PointData>" << std::endl; } //write cell data if (!cellsData.empty()) { *outfile << " <CellData>" << std::endl; writeDataArray(cellsDataObject, cellsDataField, cellsDataName); *outfile << " </CellData>" << std::endl; } //write points *outfile << " <Points>" << std::endl; *outfile << " <DataArray type=\"Float32\" NumberOfComponents=\"3\" format=\"ascii\">" << std::endl; if (!pointsPos.empty()) { for (int i = 0 ; i < nbp; i++) { *outfile << "\t" << pointsPos[i] << std::endl; } } else if (mstate && mstate->getSize() == (size_t)nbp) { for (size_t i = 0; i < mstate->getSize(); i++) *outfile << " " << mstate->getPX(i) << " " << mstate->getPY(i) << " " << mstate->getPZ(i) << std::endl; } else { for (int i = 0; i < nbp; i++) *outfile << " " << topology->getPX(i) << " " << topology->getPY(i) << " " << topology->getPZ(i) << std::endl; } *outfile << " </DataArray>" << std::endl; *outfile << " </Points>" << std::endl; //write cells *outfile << " <Cells>" << std::endl; //write connectivity *outfile << " <DataArray type=\"Int32\" Name=\"connectivity\" format=\"ascii\">" << std::endl; if (writeEdges.getValue()) { for (int i=0 ; i<topology->getNbEdges() ; i++) *outfile << " " << topology->getEdge(i) << std::endl; } if (writeTriangles.getValue()) { for (int i=0 ; i<topology->getNbTriangles() ; i++) *outfile << " " << topology->getTriangle(i) << std::endl; } if (writeQuads.getValue()) { for (int i=0 ; i<topology->getNbQuads() ; i++) *outfile << " " << topology->getQuad(i) << std::endl; } if (writeTetras.getValue()) { for (int i=0 ; i<topology->getNbTetras() ; i++) *outfile << " " << topology->getTetra(i) << std::endl; } if (writeHexas.getValue()) { for (int i=0 ; i<topology->getNbHexas() ; i++) *outfile << " " << topology->getHexa(i) << std::endl; } *outfile << " </DataArray>" << std::endl; //write offsets int num = 0; *outfile << " <DataArray type=\"Int32\" Name=\"offsets\" format=\"ascii\">" << std::endl; *outfile << " "; if (writeEdges.getValue()) { for (int i=0 ; i<topology->getNbEdges() ; i++) { num += 2; *outfile << num << " "; } } if (writeTriangles.getValue()) { for (int i=0 ; i<topology->getNbTriangles() ; i++) { num += 3; *outfile << num << " "; } } if (writeQuads.getValue()) { for (int i=0 ; i<topology->getNbQuads() ; i++) { num += 4; *outfile << num << " "; } } if (writeTetras.getValue()) { for (int i=0 ; i<topology->getNbTetras() ; i++) { num += 4; *outfile << num << " "; } } if (writeHexas.getValue()) { for (int i=0 ; i<topology->getNbHexas() ; i++) { num += 8; *outfile << num << " "; } } *outfile << std::endl; *outfile << " </DataArray>" << std::endl; //write types *outfile << " <DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">" << std::endl; *outfile << " "; if (writeEdges.getValue()) { for (int i=0 ; i<topology->getNbEdges() ; i++) *outfile << 3 << " "; } if (writeTriangles.getValue()) { for (int i=0 ; i<topology->getNbTriangles() ; i++) *outfile << 5 << " "; } if (writeQuads.getValue()) { for (int i=0 ; i<topology->getNbQuads() ; i++) *outfile << 9 << " "; } if (writeTetras.getValue()) { for (int i=0 ; i<topology->getNbTetras() ; i++) *outfile << 10 << " "; } if (writeHexas.getValue()) { for (int i=0 ; i<topology->getNbHexas() ; i++) *outfile << 12 << " "; } *outfile << std::endl; *outfile << " </DataArray>" << std::endl; *outfile << " </Cells>" << std::endl; //write end *outfile << " </Piece>" << std::endl; *outfile << " </UnstructuredGrid>" << std::endl; *outfile << "</VTKFile>" << std::endl; outfile->close(); ++nbFiles; msg_info() << "Export VTK XML in file " << filename << " done."; }