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 
36 #include "vtkCGNSReaderInternal.h"
37 
38 namespace CGNSRead
39 {
40 
41 //------------------------------------------------------------------------------
42 template <typename T>
43 inline 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  */
82 template <typename T>
83 inline 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
130 template <>
131 int readNodeData<char>(int cgioNum, double nodeId, std::vector<char>& data);
132 
133 //------------------------------------------------------------------------------
134 int readNodeStringData(int cgioNum, double nodeId, std::string& data);
135 
136 //------------------------------------------------------------------------------
137 int getNodeChildrenId(int cgioNum, double fatherId, std::vector<double>& childrenIds);
138 
139 //------------------------------------------------------------------------------
140 int readBaseIds(int cgioNum, double rootId, std::vector<double>& baseIds);
141 
142 //------------------------------------------------------------------------------
143 int readBaseCoreInfo(int cgioNum, double baseId, CGNSRead::BaseInformation& baseInfo);
144 
145 //------------------------------------------------------------------------------
146 int readBaseIteration(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
147 
148 //------------------------------------------------------------------------------
149 int readZoneIterInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
150 
151 //------------------------------------------------------------------------------
152 int readSolInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
153 
154 //------------------------------------------------------------------------------
155 int readBaseFamily(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo,
156  const std::string& parentPath = "");
157 
158 //------------------------------------------------------------------------------
159 int readBaseReferenceState(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
160 
161 //------------------------------------------------------------------------------
162 int readZoneInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation& baseInfo);
163 
164 //------------------------------------------------------------------------------
168 int readZoneInfo(int cgioNum, double zoneId, CGNSRead::ZoneInformation& zoneInfo);
169 //------------------------------------------------------------------------------
173 void releaseIds(int cgioNum, const std::vector<double>& ids);
174 }
175 #endif // cgio_helpers_h
data
int readBaseIteration(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)
int getNodeChildrenId(int cgioNum, double fatherId, std::vector< double > &childrenIds)
int readNodeDataAs(int cgioNum, double nodeId, std::vector< T > &data)
Definition: cgio_helpers.h:83
int readBaseCoreInfo(int cgioNum, double baseId, CGNSRead::BaseInformation &baseInfo)
void releaseIds(int cgioNum, const std::vector< double > &ids)
release all ids in the vector.
int readBaseReferenceState(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)
CG_LONG_T cgsize_t
Definition: cgnstypes.h:89
int readNodeData< char >(int cgioNum, double nodeId, std::vector< char > &data)
int readBaseIds(int cgioNum, double rootId, std::vector< double > &baseIds)
int readZoneIterInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)
size
int readNodeData(int cgioNum, double nodeId, std::vector< T > &data)
Definition: cgio_helpers.h:43
int readZoneInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)
int readSolInfo(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo)
int readBaseFamily(int cgioNum, double nodeId, CGNSRead::BaseInformation &baseInfo, const std::string &parentPath="")
int readNodeStringData(int cgioNum, double nodeId, std::string &data)