pFlowToVTK finalized and tested

- geometry conversion now can handle separate vtk files.
This commit is contained in:
Hamidreza Norouzi
2024-03-30 08:35:16 -07:00
parent 815b134e1e
commit ef0e752929
13 changed files with 460 additions and 382 deletions

View File

@ -24,19 +24,51 @@ Licence:
#include <regex>
#include "vtkFile.hpp"
#include "pointFieldToVTK.hpp"
#include "triSurface.hpp"
#include "multiTriSurface.hpp"
#include "triSurfaceFields.hpp"
#include "IOobject.hpp"
namespace pFlow::TSFtoVTK
{
bool regexCheck(word TYPENAME, word fieldType)
bool convertTimeFolderTriSurfaceFields(
systemControl& control,
const fileSystem& destPath,
const word& bName,
bool separate);
bool triSurfaceToVTK(iOstream &os,
const realx3 *points,
const uint32x3 *vertices,
const subSurface &subSurf);
bool triSurfaceToVTK(iOstream &os,
const realx3* points,
const uint32x3* vertices,
uint32 numPoints,
uint32 numTris);
bool convertTimeFolderTriSurfaceFieldsSingle(
multiTriSurface& surface,
const fileSystem& destPath,
real time,
const word& bName);
bool convertTimeFolderTriSurfaceFieldsSeparate(
multiTriSurface& surface,
const fileSystem& destPath,
real time,
const word& bName);
inline
bool regexCheck(const word& TYPENAME, const word& fieldType)
{
std::regex match("triSurfaceField\\<([A-Za-z1-9_]*)\\,([A-Za-z1-9_]*)\\>");
std::smatch search1, search2;
std::smatch search1;
std::smatch search2;
if(!std::regex_match(fieldType, search1, match))return false;
if(!std::regex_match(TYPENAME, search2, match))return false;
if(search1.size()!=3)return false;
@ -45,201 +77,27 @@ bool regexCheck(word TYPENAME, word fieldType)
}
template<typename Type>
bool checkFieldType(word objectType)
inline
bool checkTriFieldType(word objectType)
{
//if( pointField<VectorSingle,Type>::TYPENAME() == objectType )return true;
//if( pointField<VectorSingle,Type, HostSpace>::TYPENAME() == objectType ) return true;
//if( pointField<VectorDual, Type>::TYPENAME() == objectType )return true;
return regexCheck(triSurfaceField<VectorSingle,Type>::TYPENAME(), objectType);
}
template<typename Type>
bool triDataToVTK(iOstream& os, const Type& dataEntity)
{
fatalErrorInFunction<<
"not implemented function!";
fatalExit;
return false;
}
template<>
bool triDataToVTK(iOstream& os, const triSurface& surface )
{
auto nP = surface.numPoints();
auto hPoints = surface.points().hostVector();
os << "DATASET POLYDATA" << endl;
os << "POINTS " << nP << " float" << endl;
for ( auto i=0; i<nP; i++ )
{
os << hPoints[i].x() << " " << hPoints[i].y() << " " << hPoints[i].z() << endl;
}
auto nV = surface.numTriangles();
auto hVertices = surface.vertices().hostVector();
os << "POLYGONS " << nV << " " << 4*nV << endl;
for(auto i=0; i<nV; i++)
{
os<< 3 <<" "<< hVertices[i].x() << " " << hVertices[i].y() <<" "<<hVertices[i].z()<<endl;
}
return true;
}
template<>
bool triDataToVTK(iOstream& os, const multiTriSurface& surface )
{
auto nP = surface.numPoints();
auto hPoints = surface.points().hostVector();
os << "DATASET UNSTRUCTURED_GRID" << endl;
os << "POINTS " << nP << " float" << endl;
for ( auto i=0; i<nP; i++ )
{
os << hPoints[i].x() << " " << hPoints[i].y() << " " << hPoints[i].z() << endl;
}
auto nV = surface.numTriangles();
auto hVertices = surface.vertices().hostVector();
os<<"CELLS "<< nV<<' '<< 4*nV<<'\n';
//os << "POLYGONS " << nV << " " << 4*nV << endl;
for(auto i=0; i<nV; i++)
{
os<< 3 <<" "<< hVertices[i].x() << " " << hVertices[i].y() <<" "<<hVertices[i].z()<<endl;
}
os<<"CELL_TYPES "<< nV<<'\n';
for(int32 i=0; i<nV; i++)
{
os<< 5 <<'\n';
}
os << "CELL_DATA " << nV << endl;
return true;
}
bool addRealx3TriSurfaceField(
iOstream& os,
word fieldName,
int32 size,
realx3* field )
{
if(size==0) return true;
os << "FIELD FieldData 1\n"<<
fieldName << " 3 " << size << " float\n";
for(int32 i=0; i<size; ++i)
{
os<< field[i].x()<<' '<< field[i].y()<<' '<<field[i].z()<<'\n';
}
return true;
return regexCheck(triSurfaceField<Type>::TYPENAME(), objectType);
}
bool convertRealx3TypetriSurfaceField(
iOstream& os,
const IOfileHeader& header,
const multiTriSurface& tSurface)
{
word objectType = header.objectType();
multiTriSurface& tSurface);
if(!checkFieldType<realx3>(objectType))return false;
auto objField = IOobject::make<realx3TriSurfaceField_H>
(
header,
tSurface,
static_cast<real>(0)
);
auto& Field = objField().getObject<realx3TriSurfaceField_H>();
realx3* data = Field.hostVectorAll().data();
REPORT(2)<<"writing "<< greenColor <<header.objectName()<<defaultColor<<" field to vtk."<<endREPORT;
return addRealx3TriSurfaceField(
os,
header.objectName(),
tSurface.size(),
data );
}
bool convertTimeFolderTriSurfaceFields(
fileSystem timeFolder,
real time,
fileSystem destPath,
word bName)
{
// check if pointStructure exist in this folder
IOfileHeader triSurfaeHeader(
objectFile(
triSurfaceFile__,
timeFolder,
objectFile::READ_ALWAYS,
objectFile::WRITE_ALWAYS)
);
if( !triSurfaeHeader.headerOk(true) )
{
output<<yellowText("Time folder "<< timeFolder <<
" does not contain any triSurface data file. Skipping this folder . . ."
)<<nl;
return true;
}
vtkFile vtk(destPath, bName, time);
if(!vtk) return false;
auto triSurfaceObjPtr = IOobject::make<multiTriSurface>(triSurfaeHeader);
auto& tSurface = triSurfaceObjPtr().getObject<multiTriSurface>();
// get a list of files in this timeFolder;
REPORT(1)<<"Wrting triSurface mesh/Geometry to vtk file."<<endREPORT;
if(!triDataToVTK(vtk(), tSurface))
{
fatalErrorInFunction<<
"error in writing triSurface data to vtk file "<< vtk.fileName()<<endl;
return false;
}
bool convertRealx3TypetriSurfaceFieldSeparate(
const fileSystem& destPath,
const IOfileHeader& header,
multiTriSurface& tSurface,
const word& bName,
real time);
auto fileList = containingFiles(timeFolder);
for(auto& file:fileList)
{
IOfileHeader fieldHeader(
objectFile(
file.wordPath(),
"",
objectFile::READ_ALWAYS,
objectFile::WRITE_ALWAYS) );
if( fieldHeader.headerOk(true) )
{
//output<<"object file type is "<<fieldHeader.objectType()<<endl;
convertRealx3TypetriSurfaceField(vtk(), fieldHeader, tSurface);
}
}
return true;
}
}
#endif