VTK  9.4.20241117
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
13
14#include <string.h> // for inline strcmp
15#include <string>
16#include <vector>
17
18namespace CGNSRead
19{
20VTK_ABI_NAMESPACE_BEGIN
21
25template <typename T>
26inline 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 */
64template <typename T>
65inline 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 */
115template <>
116int readNodeData<char>(int cgioNum, double nodeId, std::vector<char>& data);
117
121int readNodeStringData(int cgioNum, double nodeId, std::string& data);
122
126int getNodeChildrenId(int cgioNum, double fatherId, std::vector<double>& childrenIds);
127
131int readBaseIds(int cgioNum, double rootId, std::vector<double>& baseIds);
132
136int readBaseCoreInfo(int cgioNum, double baseId, CGNSRead::BaseInformation& baseInfo);
137
141int readBaseIteration(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
142
146int readZoneIterInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
147
151int readSolInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
152
156int readBaseFamily(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo,
157 const std::string& parentPath = "");
158
162int readBaseReferenceState(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
163
167int readZoneInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
168
172int readZoneInfo(int cgioNum, double zoneId, CGNSRead::ZoneInformation& zoneInfo);
173
177void releaseIds(int cgioNum, const std::vector<double>& ids);
178
179VTK_ABI_NAMESPACE_END
180}
181#endif // cgio_helpers_h
This file defines functions used by vtkCGNSReader and vtkCGNSReaderInternal.
int readBaseReferenceState(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)
Read reference state information in the given ReferenceState_t node.
int readNodeStringData(int cgioNum, double nodeId, std::string &data)
Read string data from the given node.
int getNodeChildrenId(int cgioNum, double fatherId, std::vector< double > &childrenIds)
Read IDs of all children for the node with the given ID.
int readBaseCoreInfo(int cgioNum, double baseId, CGNSRead::BaseInformation &baseInfo)
Read name, cell and physical dimensions for the given CGNSBase_t node.
int readNodeDataAs(int cgioNum, double nodeId, std::vector< T > &data)
int readBaseFamily(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo, const std::string &parentPath="")
Read base family information in the given Family_t node.
void releaseIds(int cgioNum, const std::vector< double > &ids)
Release all IDs in the vector.
int readSolInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)
Read data arrays information in the given FlowSolution_t node.
int readNodeData< char >(int cgioNum, double nodeId, std::vector< char > &data)
int readNodeData(int cgioNum, double nodeId, std::vector< T > &data)
Read data of the specified type from the given node.
int readBaseIteration(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)
Read timesteps information in the given BaseIterativeData_t node.
int readBaseIds(int cgioNum, double rootId, std::vector< double > &baseIds)
Search for bases under the node with the given ID and read their IDs.
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.
int readZoneInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)
Read general data array information in the given Zone_t node.