VTK  9.1.0
cgio_helpers.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: cgio_helpers.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14 =========================================================================*/
15// Copyright 2013-2014 Mickael Philit.
16
17// .NAME cgio_helpers -- function used by vtkCGNSReader
18// and vtkCGNSReaderInternal
19// .SECTION Description
20// provide function to simplify "CGNS" reading through cgio
21//
22// .SECTION Caveats
23//
24//
25// .SECTION Thanks
26// Thanks to .
27
28#ifndef cgio_helpers_h
29#define cgio_helpers_h
30
31#include <map>
32#include <string.h> // for inline strcmp
33#include <string>
34#include <vector>
35
37
38namespace CGNSRead
39{
40
41//------------------------------------------------------------------------------
42template <typename T>
43inline int readNodeData(int cgioNum, double nodeId, std::vector<T>& data)
44{
45 int n;
46 cgsize_t size = 1;
47 cgsize_t dimVals[12];
48 int ndim;
49 constexpr const char* dtName = CGNSRead::detail::cgns_type_name<T>();
50
51 if (cgio_get_dimensions(cgioNum, nodeId, &ndim, dimVals) != CG_OK)
52 {
53 cgio_error_exit("cgio_get_dimensions");
54 return 1;
55 }
56
57 // allocate data
58 for (n = 0; n < ndim; n++)
59 {
60 size *= dimVals[n];
61 }
62 if (size <= 0)
63 {
64 return 1;
65 }
66 data.resize(size);
67
68 // read data
69 if (cgio_read_all_data_type(cgioNum, nodeId, dtName, &data[0]) != CG_OK)
70 {
71 return 1;
72 }
73
74 return 0;
75}
76
77//------------------------------------------------------------------------------
78/*
79 * Converts data read from the file using native type to the type specified
80 * as the template argument. Just uses static_cast to do type conversion.
81 */
82template <typename T>
83inline int readNodeDataAs(int cgioNum, double nodeId, std::vector<T>& data)
84{
85 // let's get type in file.
86 char dtype[CGIO_MAX_DATATYPE_LENGTH + 1];
87 if (cgio_get_data_type(cgioNum, nodeId, dtype) != CG_OK)
88 {
89 cgio_error_exit("cgio_get_data_type");
90 return 1;
91 }
92
93 if (strcmp(dtype, "I4") == 0)
94 {
95 std::vector<vtkTypeInt32> i32vector;
96 readNodeData<vtkTypeInt32>(cgioNum, nodeId, i32vector);
97 data.resize(i32vector.size());
98 std::copy(i32vector.begin(), i32vector.end(), data.begin());
99 }
100 else if (strcmp(dtype, "I8") == 0)
101 {
102 std::vector<vtkTypeInt64> i64vector;
103 readNodeData<vtkTypeInt64>(cgioNum, nodeId, i64vector);
104 data.resize(i64vector.size());
105 std::copy(i64vector.begin(), i64vector.end(), data.begin());
106 }
107 else if (strcmp(dtype, "R4") == 0)
108 {
109 std::vector<float> fvector;
110 readNodeData<float>(cgioNum, nodeId, fvector);
111 data.resize(fvector.size());
112 std::copy(fvector.begin(), fvector.end(), data.begin());
113 }
114 else if (strcmp(dtype, "R8") == 0)
115 {
116 std::vector<double> dvector;
117 readNodeData<double>(cgioNum, nodeId, dvector);
118 data.resize(dvector.size());
119 std::copy(dvector.begin(), dvector.end(), data.begin());
120 }
121 else
122 {
123 return 1;
124 }
125 return CG_OK;
126}
127
128//------------------------------------------------------------------------------
129// Specialize char array
130template <>
131int readNodeData<char>(int cgioNum, double nodeId, std::vector<char>& data);
132
133//------------------------------------------------------------------------------
134int readNodeStringData(int cgioNum, double nodeId, std::string& data);
135
136//------------------------------------------------------------------------------
137int getNodeChildrenId(int cgioNum, double fatherId, std::vector<double>& childrenIds);
138
139//------------------------------------------------------------------------------
140int readBaseIds(int cgioNum, double rootId, std::vector<double>& baseIds);
141
142//------------------------------------------------------------------------------
143int readBaseCoreInfo(int cgioNum, double baseId, CGNSRead::BaseInformation& baseInfo);
144
145//------------------------------------------------------------------------------
146int readBaseIteration(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
147
148//------------------------------------------------------------------------------
149int readZoneIterInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
150
151//------------------------------------------------------------------------------
152int readSolInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
153
154//------------------------------------------------------------------------------
155int readBaseFamily(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo,
156 const std::string& parentPath = "");
157
158//------------------------------------------------------------------------------
159int readBaseReferenceState(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
160
161//------------------------------------------------------------------------------
162int readZoneInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
163
164//------------------------------------------------------------------------------
168int readZoneInfo(int cgioNum, double zoneId, CGNSRead::ZoneInformation& zoneInfo);
169//------------------------------------------------------------------------------
173void releaseIds(int cgioNum, const std::vector<double>& ids);
174}
175#endif // cgio_helpers_h
int readBaseReferenceState(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)
int readNodeStringData(int cgioNum, double nodeId, std::string &data)
int getNodeChildrenId(int cgioNum, double fatherId, std::vector< double > &childrenIds)
int readBaseCoreInfo(int cgioNum, double baseId, CGNSRead::BaseInformation &baseInfo)
int readNodeDataAs(int cgioNum, double nodeId, std::vector< T > &data)
Definition: cgio_helpers.h:83
int readBaseFamily(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo, const std::string &parentPath="")
void releaseIds(int cgioNum, const std::vector< double > &ids)
release all ids in the vector.
int readSolInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)
int readNodeData< char >(int cgioNum, double nodeId, std::vector< char > &data)
int readNodeData(int cgioNum, double nodeId, std::vector< T > &data)
Definition: cgio_helpers.h:43
int readBaseIteration(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)
int readBaseIds(int cgioNum, double rootId, std::vector< double > &baseIds)
int readZoneIterInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)
int readZoneInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)