Loading...
Searching...
No Matches
VTKUtils.cpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25#include "VTK.hpp"
26
27namespace bitpit{
28
36
37 switch (type){
38
39 case VTKElementType::VERTEX:
40 return 1 ;
41
42 case VTKElementType::LINE:
43 return 2 ;
44
45 case VTKElementType::TRIANGLE:
46 case VTKElementType::QUADRATIC_EDGE:
47 return 3 ;
48
49 case VTKElementType::PIXEL:
50 case VTKElementType::QUAD:
51 case VTKElementType::TETRA:
52 return 4 ;
53
54 case VTKElementType::VOXEL:
55 case VTKElementType::HEXAHEDRON:
56 case VTKElementType::QUADRATIC_QUAD:
57 case VTKElementType::QUADRATIC_TETRA:
58 return 8 ;
59
60 case VTKElementType::WEDGE:
61 case VTKElementType::QUADRATIC_TRIANGLE:
62 return 6 ;
63
64 case VTKElementType::PYRAMID:
65 return 5 ;
66
67 case VTKElementType::QUADRATIC_HEXAHEDRON:
68 return 20 ;
69
70 default:
71 return 0 ;
72
73 }
74
75
76}
77
84bool vtk::convertStringToDataArray( const std::string &line, VTKField &field ){
85
86 std::string typ, name, code, com, offs ;
87 int components(1), offset ;
88
89 VTKFormat codex ;
90 VTKDataType type ;
91 VTKFieldType comp(VTKFieldType::SCALAR) ;
92
93 bool success(true) ;
94
95
96 if( bitpit::utils::string::keywordInString( line, "<DataArray ") ){
97 success = success && bitpit::utils::string::getAfterKeyword( line, "type=", '\"', typ) ;
98 success = success && bitpit::utils::string::getAfterKeyword( line, "Name=", '\"', name) ;
99 success = success && bitpit::utils::string::getAfterKeyword( line, "format=", '\"', code) ;
100
101 if( bitpit::utils::string::getAfterKeyword( line, "NumberOfComponents=", '\"', com) ){
102 bitpit::utils::string::convertString( com, components ) ;
103 }
104
105 if(components==3)
106 comp=VTKFieldType::VECTOR ;
107 else if(components==9)
108 comp=VTKFieldType::TENSOR ;
109
110 vtk::convertStringToEnum( typ, type) ;
111 vtk::convertStringToEnum( code, codex) ;
112
113 field.setDataType(type) ;
114 field.setName(name) ;
115 field.setCodification(codex) ;
116 if(name != "connectivity")
117 field.setFieldType(comp) ;
118
119 if(code=="appended") {
120 if( bitpit::utils::string::getAfterKeyword( line, "offset=", '\"', offs) ){
122 field.setOffset(offset) ;
123 }
124 else{
125 success = false ;
126 }
127 }
128
129 return success ;
130 }
131
132 else{
133 return false ;
134 }
135
136
137}
138
144std::string vtk::convertDataArrayToString( const VTKField &field ){
145
146 std::stringstream os("") ;
147 unsigned comp = field.getComponentCount() ;
148
149 os << " <DataArray "
150 << "type=\"" << vtk::convertEnumToString( field.getDataType() ) << "\" "
151 << "Name=\"" << field.getName() << "\" "
152 << "NumberOfComponents=\""<< comp << "\" "
153 << "format=\"" << vtk::convertEnumToString(field.getCodification()) << "\" ";
154
155 if( field.getCodification() == VTKFormat::APPENDED ){
156 os << "offset=\"" << field.getOffset() << "\" " ;
157 }
158
159 os << ">" ;
160
161 return( os.str() ) ;
162
163
164}
165
171std::string vtk::convertPDataArrayToString( const VTKField &field ){
172
173 std::stringstream os("") ;
174 unsigned comp = field.getComponentCount() ;
175
176 os << " <PDataArray "
177 << "type=\"" << vtk::convertEnumToString(field.getDataType()) << "\" "
178 << "Name=\"" << field.getName() << "\" "
179 << "NumberOfComponents=\""<< comp << "\" "
180 << ">" ;
181
182 return( os.str() ) ;
183
184}
185
192
193 switch(loc){
194 case VTKLocation::CELL :
195 return("Cell");
196 case VTKLocation::POINT :
197 return("Point");
198 case VTKLocation::UNDEFINED :
199 return("Undefined") ;
200 default:
201 return("Undefined") ;
202 }
203}
204
211
212 switch(cod){
213 case VTKFormat::ASCII :
214 return("ascii");
215 case VTKFormat::APPENDED :
216 return("appended");
217 case VTKFormat::UNDEFINED :
218 return("Undefined") ;
219 default:
220 return("Undefined") ;
221 }
222}
223
230
231 switch(type){
232 case VTKDataType::Int8 :
233 return("Int8");
234 case VTKDataType::Int16 :
235 return("Int16");
236 case VTKDataType::Int32 :
237 return("Int32");
238 case VTKDataType::Int64 :
239 return("Int64");
240 case VTKDataType::UInt8 :
241 return("UInt8");
242 case VTKDataType::UInt16 :
243 return("UInt16");
244 case VTKDataType::UInt32 :
245 return("UInt32");
246 case VTKDataType::UInt64 :
247 return("UInt64");
248 case VTKDataType::Float32 :
249 return("Float32");
250 case VTKDataType::Float64 :
251 return("Float64");
252 case VTKDataType::UNDEFINED :
253 return("Undefined");
254 default:
255 return("Undefined") ;
256 }
257}
258
265bool vtk::convertStringToEnum( const std::string &str, VTKLocation &loc ){
266
267
268 if( str == "Cell"){
269 loc = VTKLocation::CELL;
270 return(true);
271
272 } else if ( str == "Point" ){
273 loc = VTKLocation::POINT;
274 return(true);
275
276 } else {
277 loc = VTKLocation::UNDEFINED ;
278 return(false);
279 }
280
281}
282
289bool vtk::convertStringToEnum( const std::string &str, VTKFormat &cod ){
290
291
292 if( str == "ascii"){
293 cod = VTKFormat::ASCII ;
294 return(true);
295
296 } else if ( str == "appended" ){
297 cod = VTKFormat::APPENDED ;
298 return(true);
299
300 } else {
301 cod = VTKFormat::UNDEFINED ;
302 return(false);
303 }
304
305}
306
313bool vtk::convertStringToEnum( const std::string &str, VTKDataType &type ){
314
315 if ( str =="Int8" ){
316 type = VTKDataType::Int8;
317 return(true);
318 }
319
320 else if ( str =="Int16" ){
321 type = VTKDataType::Int16;
322 return(true);
323 }
324
325 else if ( str =="Int32" ){
326 type = VTKDataType::Int32;
327 return(true);
328 }
329
330 else if ( str =="Int64" ){
331 type = VTKDataType::Int64;
332 return(true);
333 }
334
335 else if ( str =="UInt8" ){
336 type = VTKDataType::UInt8;
337 return(true);
338 }
339
340 else if ( str =="UInt16" ){
341 type = VTKDataType::UInt16;
342 return(true);
343 }
344
345 else if ( str =="UInt32" ){
346 type = VTKDataType::UInt32;
347 return(true);
348 }
349
350 else if ( str =="UInt64" ){
351 type = VTKDataType::UInt64;
352 return(true);
353 }
354
355 else if ( str =="Float32" ){
356 type = VTKDataType::Float32;
357 return(true);
358 }
359
360 else if ( str =="Float64" ){
361 type = VTKDataType::Float64;
362 return(true);
363 }
364
365 else {
366 type = VTKDataType::UNDEFINED;
367 return(false);
368 }
369
370}
371
372
373}
VTKField handles geometry and data field information for the VTK format.
Definition VTK.hpp:247
const std::string & getName() const
Definition VTKField.cpp:167
VTKFormat getCodification() const
Definition VTKField.cpp:208
uint64_t getOffset() const
Definition VTKField.cpp:216
void setDataType(VTKDataType)
Definition VTKField.cpp:97
void setCodification(VTKFormat)
Definition VTKField.cpp:113
void setFieldType(VTKFieldType)
Definition VTKField.cpp:121
static unsigned getComponentCount(VTKFieldType fieldType)
Definition VTKField.cpp:40
VTKDataType getDataType() const
Definition VTKField.cpp:192
void setOffset(uint64_t)
Definition VTKField.cpp:137
void setName(const std::string &)
Definition VTKField.cpp:89
VTKFieldType
Definition VTK.hpp:63
VTKFormat
Definition VTK.hpp:92
VTKElementType
Definition VTK.hpp:112
VTKDataType
Definition VTK.hpp:74
VTKLocation
Definition VTK.hpp:102
bool keywordInString(const std::string &line, const std::string &key)
void convertString(std::string input, T &output)
bool getAfterKeyword(const std::string &line, const std::string &key, char del, std::string &result)
std::string convertDataArrayToString(const VTKField &)
Definition VTKUtils.cpp:144
std::string convertEnumToString(VTKLocation)
Definition VTKUtils.cpp:191
bool convertStringToEnum(const std::string &, VTKLocation &)
Definition VTKUtils.cpp:265
std::string convertPDataArrayToString(const VTKField &)
Definition VTKUtils.cpp:171
bool convertStringToDataArray(const std::string &, VTKField &)
Definition VTKUtils.cpp:84
uint8_t getElementNodeCount(VTKElementType)
Definition VTKUtils.cpp:35
--- layout: doxygen_footer ---