triSurfaceFieldToVTK.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 
22 #ifndef __triSurfaceFieldToVTK_hpp__
23 #define __triSurfaceFieldToVTK_hpp__
24 
25 #include <regex>
26 
27 #include "vtkFile.hpp"
28 #include "triSurface.hpp"
29 #include "multiTriSurface.hpp"
30 #include "triSurfaceFields.hpp"
31 #include "IOobject.hpp"
32 
33 namespace pFlow::TSFtoVTK
34 {
35 
36 bool regexCheck(word TYPENAME, word fieldType)
37 {
38  std::regex match("triSurfaceField\\<([A-Za-z1-9_]*)\\,([A-Za-z1-9_]*)\\>");
39  std::smatch search1, search2;
40  if(!std::regex_match(fieldType, search1, match))return false;
41  if(!std::regex_match(TYPENAME, search2, match))return false;
42  if(search1.size()!=3)return false;
43  if(search1.size()!=search2.size())return false;
44  return search1[1] == search2[1];
45 }
46 
47 template<typename Type>
48 bool checkFieldType(word objectType)
49 {
50  //if( pointField<VectorSingle,Type>::TYPENAME() == objectType )return true;
51  //if( pointField<VectorSingle,Type, HostSpace>::TYPENAME() == objectType ) return true;
52  //if( pointField<VectorDual, Type>::TYPENAME() == objectType )return true;
54 
55 }
56 
57 template<typename Type>
58 bool triDataToVTK(iOstream& os, const Type& dataEntity)
59 {
61  "not implemented function!";
62  fatalExit;
63  return false;
64 }
65 
66 template<>
67 bool triDataToVTK(iOstream& os, const triSurface& surface )
68 {
69  auto nP = surface.numPoints();
70  auto hPoints = surface.points().hostVector();
71 
72  os << "DATASET POLYDATA" << endl;
73  os << "POINTS " << nP << " float" << endl;
74 
75 
76  for ( auto i=0; i<nP; i++ )
77  {
78  os << hPoints[i].x() << " " << hPoints[i].y() << " " << hPoints[i].z() << endl;
79  }
80 
81  auto nV = surface.numTriangles();
82  auto hVertices = surface.vertices().hostVector();
83 
84  os << "POLYGONS " << nV << " " << 4*nV << endl;
85 
86  for(auto i=0; i<nV; i++)
87  {
88  os<< 3 <<" "<< hVertices[i].x() << " " << hVertices[i].y() <<" "<<hVertices[i].z()<<endl;
89  }
90 
91  return true;
92 }
93 
94 template<>
95 bool triDataToVTK(iOstream& os, const multiTriSurface& surface )
96 {
97  auto nP = surface.numPoints();
98  auto hPoints = surface.points().hostVector();
99 
100  os << "DATASET UNSTRUCTURED_GRID" << endl;
101  os << "POINTS " << nP << " float" << endl;
102 
103 
104  for ( auto i=0; i<nP; i++ )
105  {
106  os << hPoints[i].x() << " " << hPoints[i].y() << " " << hPoints[i].z() << endl;
107  }
108 
109  auto nV = surface.numTriangles();
110  auto hVertices = surface.vertices().hostVector();
111 
112  os<<"CELLS "<< nV<<' '<< 4*nV<<'\n';
113  //os << "POLYGONS " << nV << " " << 4*nV << endl;
114 
115  for(auto i=0; i<nV; i++)
116  {
117  os<< 3 <<" "<< hVertices[i].x() << " " << hVertices[i].y() <<" "<<hVertices[i].z()<<endl;
118  }
119 
120  os<<"CELL_TYPES "<< nV<<'\n';
121 
122  for(int32 i=0; i<nV; i++)
123  {
124  os<< 5 <<'\n';
125  }
126 
127  os << "CELL_DATA " << nV << endl;
128 
129  return true;
130 }
131 
133  iOstream& os,
134  word fieldName,
135  int32 size,
136  realx3* field )
137 {
138  if(size==0) return true;
139 
140 
141  os << "FIELD FieldData 1\n"<<
142  fieldName << " 3 " << size << " float\n";
143  for(int32 i=0; i<size; ++i)
144  {
145  os<< field[i].x()<<' '<< field[i].y()<<' '<<field[i].z()<<'\n';
146  }
147 
148  return true;
149 }
150 
152  iOstream& os,
153  const IOfileHeader& header,
154  const multiTriSurface& tSurface)
155 {
156  word objectType = header.objectType();
157 
158  if(!checkFieldType<realx3>(objectType))return false;
159 
160  auto objField = IOobject::make<realx3TriSurfaceField_H>
161  (
162  header,
163  tSurface,
164  static_cast<real>(0)
165  );
166 
167  auto& Field = objField().getObject<realx3TriSurfaceField_H>();
168 
169  realx3* data = Field.hostVectorAll().data();
170 
171  REPORT(2)<<"writing "<< greenColor <<header.objectName()<<defaultColor<<" field to vtk."<<endREPORT;
172 
174  os,
175  header.objectName(),
176  tSurface.size(),
177  data );
178 }
179 
182  real time,
183  fileSystem destPath,
184  word bName)
185 {
186 
187  // check if pointStructure exist in this folder
188  IOfileHeader triSurfaeHeader(
189  objectFile(
191  timeFolder,
194  );
195 
196  if( !triSurfaeHeader.headerOk(true) )
197  {
198  output<<yellowText("Time folder "<< timeFolder <<
199  " does not contain any triSurface data file. Skipping this folder . . ."
200  )<<nl;
201  return true;
202  }
203 
204  vtkFile vtk(destPath, bName, time);
205 
206  if(!vtk) return false;
207 
208  auto triSurfaceObjPtr = IOobject::make<multiTriSurface>(triSurfaeHeader);
209  auto& tSurface = triSurfaceObjPtr().getObject<multiTriSurface>();
210 
211  // get a list of files in this timeFolder;
212  REPORT(1)<<"Wrting triSurface mesh/Geometry to vtk file."<<endREPORT;
213  if(!triDataToVTK(vtk(), tSurface))
214  {
216  "error in writing triSurface data to vtk file "<< vtk.fileName()<<endl;
217  return false;
218  }
219 
220 
221  auto fileList = containingFiles(timeFolder);
222 
223 
224  for(auto& file:fileList)
225  {
226  IOfileHeader fieldHeader(
227  objectFile(
228  file.wordPath(),
229  "",
232 
233  if( fieldHeader.headerOk(true) )
234  {
235  //output<<"object file type is "<<fieldHeader.objectType()<<endl;
236  convertRealx3TypetriSurfaceField(vtk(), fieldHeader, tSurface);
237  }
238  }
239 
240  return true;
241 }
242 
243 }
244 
245 #endif
pFlow::VectorSingle::hostVector
const INLINE_FUNCTION_H auto hostVector() const
Definition: VectorSingle.hpp:336
pFlow::vtkFile
Definition: vtkFile.hpp:33
pFlow::IOfileHeader::objectType
const word & objectType() const
Definition: IOfileHeader.hpp:72
endREPORT
#define endREPORT
Definition: streams.hpp:41
triSurfaceFields.hpp
pFlow::triSurfaceFile__
const char * triSurfaceFile__
Definition: vocabs.hpp:43
pFlow::real
float real
Definition: builtinTypes.hpp:46
fatalExit
#define fatalExit
Definition: error.hpp:57
IOobject.hpp
REPORT
#define REPORT(n)
Definition: streams.hpp:40
pFlow::vtkFile::fileName
virtual fileSystem fileName() const
Definition: vtkFile.cpp:70
pFlow::TSFtoVTK::convertTimeFolderTriSurfaceFields
bool convertTimeFolderTriSurfaceFields(fileSystem timeFolder, real time, fileSystem destPath, word bName)
Definition: triSurfaceFieldToVTK.hpp:180
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::containingFiles
fileSystemList containingFiles(const fileSystem &path)
Definition: fileSystem.cpp:335
pFlow::objectFile::WRITE_ALWAYS
@ WRITE_ALWAYS
Definition: objectFile.hpp:46
pFlow::TSFtoVTK::triDataToVTK
bool triDataToVTK(iOstream &os, const Type &dataEntity)
Definition: triSurfaceFieldToVTK.hpp:58
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
pFlow::Field
Definition: Field.hpp:33
pFlow::TSFtoVTK
Definition: triSurfaceFieldToVTK.hpp:33
pFlow::triple::y
INLINE_FUNCTION_HD T & y()
Definition: triple.hpp:141
pFlow::triSurface::points
const realx3Vector_D & points() const
Definition: triSurface.hpp:184
pFlow::multiTriSurface
Definition: multiTriSurface.hpp:33
pFlow::fileSystem
Definition: fileSystem.hpp:63
pFlow::triSurfaceField
Definition: triSurfaceField.hpp:34
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::TSFtoVTK::regexCheck
bool regexCheck(word TYPENAME, word fieldType)
Definition: triSurfaceFieldToVTK.hpp:36
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
multiTriSurface.hpp
defaultColor
const char * defaultColor
Definition: iOstream.hpp:31
pFlow::triSurface::numTriangles
size_t numTriangles() const
Definition: triSurface.hpp:154
pFlow::output
Ostream output
pFlow::objectFile::READ_ALWAYS
@ READ_ALWAYS
Definition: objectFile.hpp:39
pFlow::TSFtoVTK::convertRealx3TypetriSurfaceField
bool convertRealx3TypetriSurfaceField(iOstream &os, const IOfileHeader &header, const multiTriSurface &tSurface)
Definition: triSurfaceFieldToVTK.hpp:151
pFlow::TSFtoVTK::checkFieldType
bool checkFieldType(word objectType)
Definition: triSurfaceFieldToVTK.hpp:48
pFlow::triSurface::numPoints
size_t numPoints() const
Definition: triSurface.hpp:149
pFlow::objectFile
Definition: objectFile.hpp:33
pFlow::triple::z
INLINE_FUNCTION_HD T & z()
Definition: triple.hpp:144
pFlow::TSFtoVTK::addRealx3TriSurfaceField
bool addRealx3TriSurfaceField(iOstream &os, word fieldName, int32 size, realx3 *field)
Definition: triSurfaceFieldToVTK.hpp:132
pFlow::IOfileHeader
Definition: IOfileHeader.hpp:35
pFlow::IOfileHeader::headerOk
bool headerOk(bool silent=false)
Definition: IOfileHeader.cpp:71
pFlow::IOfileHeader::objectName
const word & objectName() const
Definition: IOfileHeader.hpp:66
pFlow::triSurface::vertices
const int32x3Vector_D & vertices() const
Definition: triSurface.hpp:214
pFlow::triSurface
Definition: triSurface.hpp:38
pFlow::triple::x
INLINE_FUNCTION_HD T & x()
Definition: triple.hpp:138
pFlow::triple< real >
yellowText
#define yellowText(text)
Definition: streams.hpp:30
pFlow::nl
constexpr char nl
Definition: iOstream.hpp:409
pFlow::iOstream
Definition: iOstream.hpp:53
greenColor
const char * greenColor
Definition: iOstream.hpp:34
triSurface.hpp
pFlow::triSurface::size
size_t size() const
Definition: triSurface.hpp:159
vtkFile.hpp
pFlow::timeFolder
Definition: timeFolder.hpp:32