3#include <vtkAbstractArray.h>
4#include <vtkCellArray.h>
5#include <vtkDoubleArray.h>
6#include <vtkFieldData.h>
7#include <vtkIdTypeArray.h>
10#include <vtkPolyData.h>
11#include <vtkSmartPointer.h>
12#include <vtkStringArray.h>
13#include <vtkUnstructuredGrid.h>
15#ifdef TTK_ENABLE_MPI_TIME
19 vtkFieldData *fieldData,
21 std::string &errorMsg) {
22 std::string varName = iString;
24 bool varIndexDefined =
false;
27 size_t const indexDelimiter0 = iString.find(
'[');
28 size_t const indexDelimiter1 = iString.find(
']');
29 if(indexDelimiter0 != std::string::npos
30 && indexDelimiter1 != std::string::npos) {
31 if(indexDelimiter0 > indexDelimiter1
32 || iString.find(
'[', indexDelimiter0 + 1) != std::string::npos
33 || iString.find(
'}', indexDelimiter1 + 1) != std::string::npos) {
34 errorMsg =
"Invalid Syntax:\n" + iString;
38 varName = iString.substr(0, indexDelimiter0);
39 varIndex = stoi(iString.substr(
40 indexDelimiter0 + 1, indexDelimiter1 - indexDelimiter0 - 1));
41 varIndexDefined =
true;
45 auto column = fieldData->GetAbstractArray(varName.data());
46 if(column ==
nullptr) {
47 errorMsg =
"FieldData does not contain array '" + varName +
"'";
51 size_t const n = column->GetNumberOfTuples();
52 size_t const m = column->GetNumberOfComponents();
55 if(!varIndexDefined) {
57 oString = column->GetVariantValue(0).ToString();
58 for(
int i = 1; i < s; i++)
59 oString +=
"," + column->GetVariantValue(i).ToString();
62 if(varIndex < 0 || varIndex >= s) {
63 errorMsg =
"Index " + std::to_string(varIndex) +
"/" + std::to_string(s)
64 +
" for FieldData Array '" + varName +
"' out of range";
67 oString = column->GetVariantValue(varIndex).ToString();
74 vtkFieldData *fieldData,
76 std::string &errorMsg) {
79 while(oString.find(
'{') != std::string::npos
80 && oString.find(
'}') != std::string::npos) {
81 size_t o = oString.find(
'{');
82 size_t const c = oString.find(
'}');
87 size_t oNext = oString.find(
'{', o + 1);
88 while(oNext != std::string::npos && oNext < c) {
90 oNext = oString.find(
'{', o + 1);
96 std::string
const var = oString.substr(o + 1, c - 1 - o);
102 oString = oString.substr(0, o).append(rVar).append(
103 oString.substr(c + 1, oString.length() - c - 1));
106 if(oString.find(
'{') != std::string::npos
107 || oString.find(
'}') != std::string::npos) {
108 errorMsg =
"Invalid Syntax:\n" + iString;
116 std::vector<std::string> &v) {
121 size_t j = iString.find(
',');
122 while(j != std::string::npos) {
123 v.push_back(iString.substr(i, j - i));
125 j = iString.find(
',', i);
127 if(iString.length() > i)
128 v.push_back(iString.substr(i, iString.length() - i));
134 std::vector<double> &v) {
135 std::vector<std::string> stringVector;
139 size_t const n = stringVector.size();
142 for(
size_t i = 0; i < n; i++)
143 v[i] = stod(stringVector[i]);
153 size_t const firstComma = line.find(
',', 0);
155 if(firstComma == std::string::npos)
158 std::string
const arrayName = line.substr(0, firstComma);
160 std::vector<std::string> valuesAsString;
162 line.substr(firstComma + 1, std::string::npos), valuesAsString);
163 size_t const nValues = valuesAsString.size();
168 bool isNumeric =
true;
170 std::vector<double> valuesAsDouble(nValues);
172 for(
size_t i = 0; i < nValues; i++)
173 valuesAsDouble[i] = std::stod(valuesAsString[i]);
174 }
catch(
const std::invalid_argument &) {
176 }
catch(
const std::out_of_range &) {
182 array->SetName(arrayName.data());
183 array->SetNumberOfValues(nValues);
184 for(
size_t i = 0; i < nValues; i++)
185 array->SetValue(i, valuesAsDouble[i]);
189 array->SetName(arrayName.data());
190 array->SetNumberOfValues(nValues);
191 for(
size_t i = 0; i < nValues; i++)
192 array->SetValue(i, valuesAsString[i]);
199 size_t const firstComma = line.find(
',', 0);
201 if(firstComma == std::string::npos)
204 std::string
const arrayName = line.substr(0, firstComma);
205 std::string
const valuesAsString
206 = line.substr(firstComma + 1, std::string::npos);
208 std::vector<double> values;
210 size_t const n = values.size();
213 array->SetName(arrayName.data());
214 array->SetNumberOfComponents(1);
215 array->SetNumberOfTuples(n);
216 auto arrayData =
reinterpret_cast<double *
>(
GetVoidPointer(array));
217 for(
size_t i = 0; i < n; i++)
218 arrayData[i] = values[i];
227 void *outPtr =
nullptr;
231 switch(array->GetDataType()) {
233 auto *aosArray = vtkAOSDataArrayTemplate<VTK_TT>::FastDownCast(array);
234 if(aosArray) { outPtr = aosArray->GetVoidPointer(start); });
247 slicedArray->SetName(array->GetName());
248 slicedArray->SetNumberOfComponents(array->GetNumberOfComponents());
249 slicedArray->SetNumberOfTuples(1);
250 slicedArray->SetTuple(0, idx, array);
256 vtkIdType numValues) {
257 void *outPtr =
nullptr;
258 switch(array->GetDataType()) {
259 vtkTemplateMacro(
auto *aosArray
260 = vtkAOSDataArrayTemplate<VTK_TT>::FastDownCast(array);
262 outPtr = aosArray->WriteVoidPointer(valueIdx, numValues);
270 vtkIdType numValues) {
271 void *outPtr =
nullptr;
272 switch(array->GetDataType()) {
274 auto *aosArray = vtkAOSDataArrayTemplate<VTK_TT>::FastDownCast(array);
275 if(aosArray) { outPtr = aosArray->WritePointer(valueIdx, numValues); });
284 switch(array->GetDataType()) {
286 auto *aosArray = vtkAOSDataArrayTemplate<VTK_TT>::FastDownCast(array);
287 if(aosArray) { aosArray->SetVoidArray(data, size, save); }
else {
288 std::cerr <<
"SetVoidArray on incompatible vtkDataArray:" << endl;
289 array->Print(std::cerr);
296 vtkCellArray *cellArray) {
298 vtkNew<vtkIdList> verts;
299 for(vtkIdType cid = 0; cid < ncells; cid++) {
300 const vtkIdType nbVerts = cells[curPos];
301 verts->SetNumberOfIds(nbVerts);
303 for(vtkIdType v = 0; v < nbVerts; v++) {
304 verts->SetId(v, cells[curPos]);
307 cellArray->InsertNextCell(verts);
312 vtkIdType
const *cells_off,
314 vtkCellArray *cellArray) {
316 vtkNew<vtkIdList> verts;
317 for(vtkIdType cid = 0; cid < ncells; cid++) {
318 const vtkIdType nbVerts = cells_off[cid + 1] - cells_off[cid];
319 verts->SetNumberOfIds(nbVerts);
320 for(vtkIdType v = 0; v < nbVerts; v++) {
321 verts->SetId(v, cells_co[curPos]);
324 cellArray->InsertNextCell(verts);
329 vtkPoints *
const points) {
331 if(dataSet ==
nullptr || points ==
nullptr) {
335 if(!dataSet->IsA(
"vtkUnstructuredGrid") && !dataSet->IsA(
"vtkPolyData")) {
339 const size_t nPoints = points->GetNumberOfPoints();
344 vtkNew<vtkIdTypeArray> offsets{};
345 offsets->SetNumberOfTuples(nPoints + 1);
346 auto offsetsData = ttkUtils::GetPointer<vtkIdType>(offsets);
347 for(
size_t i = 0; i <= nPoints; i++)
350 vtkNew<vtkIdTypeArray> connectivity{};
351 connectivity->SetNumberOfTuples(nPoints);
352 auto connectivityData = ttkUtils::GetPointer<vtkIdType>(connectivity);
353 for(
size_t i = 0; i < nPoints; i++)
354 connectivityData[i] = i;
356 vtkNew<vtkCellArray> cells{};
357 cells->SetData(offsets, connectivity);
359 if(dataSet->IsA(
"vtkUnstructuredGrid")) {
360 const auto vtu{vtkUnstructuredGrid::SafeDownCast(dataSet)};
362 vtu->SetPoints(points);
363 vtu->SetCells(VTK_VERTEX, cells);
365 }
else if(dataSet->IsA(
"vtkPolyData")) {
366 const auto vtp{vtkPolyData::SafeDownCast(dataSet)};
368 vtp->SetPoints(points);
369 vtp->SetVerts(cells);
static int replaceVariables(const std::string &iString, vtkFieldData *fieldData, std::string &oString, std::string &errorMsg)
static void * GetVoidPointer(vtkDataArray *array, vtkIdType start=0)
static int stringListToDoubleVector(const std::string &iString, std::vector< double > &v)
static vtkSmartPointer< vtkAbstractArray > SliceArray(vtkAbstractArray *array, vtkIdType idx)
static void FillCellArrayFromSingle(vtkIdType const *cells, vtkIdType ncells, vtkCellArray *cellArray)
static void * WritePointer(vtkDataArray *array, vtkIdType start, vtkIdType numValues)
static int replaceVariable(const std::string &iString, vtkFieldData *fieldData, std::string &oString, std::string &errorMsg)
static int CellVertexFromPoints(vtkDataSet *const dataSet, vtkPoints *const points)
static vtkSmartPointer< vtkAbstractArray > csvToVtkArray(const std::string &line)
static vtkSmartPointer< vtkDoubleArray > csvToDoubleArray(const std::string &line)
static void FillCellArrayFromDual(vtkIdType const *cells_co, vtkIdType const *cells_off, vtkIdType ncells, vtkCellArray *cellArray)
static void SetVoidArray(vtkDataArray *array, void *data, vtkIdType size, int save)
static void * WriteVoidPointer(vtkDataArray *array, vtkIdType start, vtkIdType numValues)
static int stringListToVector(const std::string &iString, std::vector< std::string > &v)