VTK: IO/CGNS/cgio_helpers.h Source File

| VTK 9.3.1

cgio_helpers.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-FileCopyrightText: Copyright 2013-2014 Mickael Philit
3 // SPDX-License-Identifier: BSD-3-Clause
9 #ifndef cgio_helpers_h
10 #define cgio_helpers_h
11
12 #include "vtkCGNSReaderInternal.h"
13
14 #include <string.h> // for inline strcmp
15 #include <string>
16 #include <vector>
17
18 namespace CGNSRead
19 {
20 VTK_ABI_NAMESPACE_BEGIN
21
25 template <typename T>
26 inline int readNodeData(int cgioNum, double nodeId, std::vector<T>& data)
27 {
28 int n;
29 cgsize_t size = 1;
30 cgsize_t dimVals[12];
31 int ndim;
32 constexpr const char* dtName = CGNSRead::detail::cgns_type_name<T>();
33
34 if (cgio_get_dimensions(cgioNum, nodeId, &ndim, dimVals) != CG_OK)
35 {
36 cgio_error_exit("cgio_get_dimensions");
37 return 1;
38 }
39
40 // allocate data
41 for (n = 0; n < ndim; n++)
42 {
43 size *= dimVals[n];
44 }
45 if (size <= 0)
46 {
47 return 1;
48 }
49 data.resize(size);
50
51 // read data
52 if (cgio_read_all_data_type(cgioNum, nodeId, dtName, data.data()) != CG_OK)
53 {
54 return 1;
55 }
56
57 return 0;
58 }
59
60 /*
61 * Converts data read from the file using native type to the type specified
62 * as the template argument. Just uses static_cast to do type conversion.
63 */
64 template <typename T>
65 inline int readNodeDataAs(int cgioNum, double nodeId, std::vector<T>& data)
66 {
67 // Retrieve data type from node
68 char dtype[CGIO_MAX_DATATYPE_LENGTH + 1];
69 if (cgio_get_data_type(cgioNum, nodeId, dtype) != CG_OK)
70 {
71 cgio_error_exit("cgio_get_data_type");
72 return CG_ERROR;
73 }
74
75 if (strcmp(dtype, "I4") == 0)
76 {
77 std::vector<vtkTypeInt32> i32vector;
78 readNodeData<vtkTypeInt32>(cgioNum, nodeId, i32vector);
79 data.resize(i32vector.size());
80 std::copy(i32vector.begin(), i32vector.end(), data.begin());
81 }
82 else if (strcmp(dtype, "I8") == 0)
83 {
84 std::vector<vtkTypeInt64> i64vector;
85 readNodeData<vtkTypeInt64>(cgioNum, nodeId, i64vector);
86 data.resize(i64vector.size());
87 std::copy(i64vector.begin(), i64vector.end(), data.begin());
88 }
89 else if (strcmp(dtype, "R4") == 0)
90 {
91 std::vector<float> fvector;
92 readNodeData<float>(cgioNum, nodeId, fvector);
93 data.resize(fvector.size());
94 std::copy(fvector.begin(), fvector.end(), data.begin());
95 }
96 else if (strcmp(dtype, "R8") == 0)
97 {
98 std::vector<double> dvector;
99 readNodeData<double>(cgioNum, nodeId, dvector);
100 data.resize(dvector.size());
101 std::copy(dvector.begin(), dvector.end(), data.begin());
102 }
103 else
104 {
105 return CG_ERROR;
106 }
107
108 return CG_OK;
109 }
110
111 /*
112 * Read data of char type from the given node.
113 * Specialization of readNodeData<>().
114 */
115 template <>
116 int readNodeData<char>(int cgioNum, double nodeId, std::vector<char>& data);
117
121 int readNodeStringData(int cgioNum, double nodeId, std::string& data);
122
126 int getNodeChildrenId(int cgioNum, double fatherId, std::vector<double>& childrenIds);
127
131 int readBaseIds(int cgioNum, double rootId, std::vector<double>& baseIds);
132
136 int readBaseCoreInfo(int cgioNum, double baseId, CGNSRead::BaseInformation& baseInfo);
137
141 int readBaseIteration(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
142
146 int readZoneIterInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
147
151 int readSolInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
152
156 int readBaseFamily(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo,
157 const std::string& parentPath = "");
158
162 int readBaseReferenceState(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
163
167 int readZoneInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
168
172 int readZoneInfo(int cgioNum, double zoneId, CGNSRead::ZoneInformation& zoneInfo);
173
177 void releaseIds(int cgioNum, const std::vector<double>& ids);
178
179 VTK_ABI_NAMESPACE_END
180 }
181 #endif // cgio_helpers_h
CGNSRead::BaseInformation
Definition: vtkCGNSReaderInternal.h:257
CGNSRead::ZoneInformation
Definition: vtkCGNSReaderInternal.h:235
CGNSRead
This file defines functions used by vtkCGNSReader and vtkCGNSReaderInternal.
Definition: cgio_helpers.h:19
CGNSRead::readBaseReferenceState
int readBaseReferenceState(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)
Read reference state information in the given ReferenceState_t node.
CGNSRead::readNodeStringData
int readNodeStringData(int cgioNum, double nodeId, std::string &data)
Read string data from the given node.
CGNSRead::getNodeChildrenId
int getNodeChildrenId(int cgioNum, double fatherId, std::vector< double > &childrenIds)
Read IDs of all children for the node with the given ID.
CGNSRead::readBaseCoreInfo
int readBaseCoreInfo(int cgioNum, double baseId, CGNSRead::BaseInformation &baseInfo)
Read name, cell and physical dimensions for the given CGNSBase_t node.
CGNSRead::readNodeDataAs
int readNodeDataAs(int cgioNum, double nodeId, std::vector< T > &data)
Definition: cgio_helpers.h:65
CGNSRead::readBaseFamily
int readBaseFamily(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo, const std::string &parentPath="")
Read base family information in the given Family_t node.
CGNSRead::releaseIds
void releaseIds(int cgioNum, const std::vector< double > &ids)
Release all IDs in the vector.
CGNSRead::readSolInfo
int readSolInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)
Read data arrays information in the given FlowSolution_t node.
CGNSRead::readNodeData< char >
int readNodeData< char >(int cgioNum, double nodeId, std::vector< char > &data)
CGNSRead::readNodeData
int readNodeData(int cgioNum, double nodeId, std::vector< T > &data)
Read data of the specified type from the given node.
Definition: cgio_helpers.h:26
CGNSRead::readBaseIteration
int readBaseIteration(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)
Read timesteps information in the given BaseIterativeData_t node.
CGNSRead::readBaseIds
int readBaseIds(int cgioNum, double rootId, std::vector< double > &baseIds)
Search for bases under the node with the given ID and read their IDs.
CGNSRead::readZoneIterInfo
int readZoneIterInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)
Read which type of pointers are used for temporal data in the given ZoneIterativeData_t node.
CGNSRead::readZoneInfo
int readZoneInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)
Read general data array information in the given Zone_t node.
vtkX3D::size
@ size
Definition: vtkX3D.h:253
vtkX3D::data
@ data
Definition: vtkX3D.h:315
vtkX3D::string
@ string
Definition: vtkX3D.h:490
vtkCGNSReaderInternal.h