50 { vtkDataType::VTK_INT,
"int" },
51 { vtkDataType::VTK_UINT,
"unsigned_int" },
52 { vtkDataType::VTK_LONG,
"long" },
53 { vtkDataType::VTK_ULONG,
"unsigned_long" },
54 { vtkDataType::VTK_FLOAT,
"float" },
55 { vtkDataType::VTK_DOUBLE,
"double" },
56 { vtkDataType::VTK_STRING,
"string" },
57 { vtkDataType::VTK_ID,
"vtkIdType" },
67 { vtkDataSetType::VTK_FIELD,
"FIELD" },
68 { vtkDataSetType::VTK_SCALARS,
"SCALARS" },
69 { vtkDataSetType::VTK_VECTORS,
"VECTORS" },
79 { parseMode::NOMODE,
"NOMODE" },
80 { parseMode::UNSTRUCTURED_GRID,
"UNSTRUCTURED_GRID" },
81 { parseMode::POLYDATA,
"POLYDATA" },
82 { parseMode::CELL_DATA,
"CELL_DATA" },
89 void Foam::vtkUnstructuredReader::warnUnhandledType
96 if (warningGiven.insert(
type))
99 <<
"Skipping unknown cell type " <<
type <<
nl;
104 void Foam::vtkUnstructuredReader::extractCells
121 label celli = cells_.size();
123 cellMap_.setSize(cells_.size(), -1);
125 label facei = faces_.size();
127 faceMap_.setSize(faces_.size(), -1);
129 label lineI = lines_.size();
131 lineMap_.setSize(lines_.size(), -1);
145 warnUnhandledType(inFile,
cellTypes[i], warningGiven);
146 label nRead = cellVertData[dataIndex++];
150 <<
"Expected size 1 for VTK_VERTEX but found "
159 warnUnhandledType(inFile,
cellTypes[i], warningGiven);
160 label nRead = cellVertData[dataIndex++];
168 label nRead = cellVertData[dataIndex++];
172 <<
"Expected size 2 for VTK_LINE but found "
178 segment[0] = cellVertData[dataIndex++];
179 segment[1] = cellVertData[dataIndex++];
186 label nRead = cellVertData[dataIndex++];
192 pointi = cellVertData[dataIndex++];
200 face&
f = faces_[facei++];
202 label nRead = cellVertData[dataIndex++];
206 <<
"Expected size 3 for VTK_TRIANGLE but found "
209 f[0] = cellVertData[dataIndex++];
210 f[1] = cellVertData[dataIndex++];
211 f[2] = cellVertData[dataIndex++];
218 face&
f = faces_[facei++];
220 label nRead = cellVertData[dataIndex++];
224 <<
"Expected size 4 for VTK_QUAD but found "
227 f[0] = cellVertData[dataIndex++];
228 f[1] = cellVertData[dataIndex++];
229 f[2] = cellVertData[dataIndex++];
230 f[3] = cellVertData[dataIndex++];
237 face&
f = faces_[facei++];
238 label nRead = cellVertData[dataIndex++];
240 for (label& pointi :
f)
242 pointi = cellVertData[dataIndex++];
249 label nRead = cellVertData[dataIndex++];
253 <<
"Expected size 4 for VTK_TETRA but found "
256 tetPoints[0] = cellVertData[dataIndex++];
257 tetPoints[1] = cellVertData[dataIndex++];
258 tetPoints[2] = cellVertData[dataIndex++];
259 tetPoints[3] = cellVertData[dataIndex++];
261 cells_[celli++] = cellShape(tet, tetPoints,
true);
267 label nRead = cellVertData[dataIndex++];
271 <<
"Expected size 5 for VTK_PYRAMID but found "
274 pyrPoints[0] = cellVertData[dataIndex++];
275 pyrPoints[1] = cellVertData[dataIndex++];
276 pyrPoints[2] = cellVertData[dataIndex++];
277 pyrPoints[3] = cellVertData[dataIndex++];
278 pyrPoints[4] = cellVertData[dataIndex++];
280 cells_[celli++] = cellShape(pyr, pyrPoints,
true);
286 label nRead = cellVertData[dataIndex++];
290 <<
"Expected size 6 for VTK_WEDGE but found "
294 prismPoints[0] = cellVertData[dataIndex++];
295 prismPoints[2] = cellVertData[dataIndex++];
296 prismPoints[1] = cellVertData[dataIndex++];
297 prismPoints[3] = cellVertData[dataIndex++];
298 prismPoints[5] = cellVertData[dataIndex++];
299 prismPoints[4] = cellVertData[dataIndex++];
301 cells_[celli++] = cellShape(prism, prismPoints,
true);
307 label nRead = cellVertData[dataIndex++];
311 <<
"Expected size 8 for VTK_HEXAHEDRON but found "
314 hexPoints[0] = cellVertData[dataIndex++];
315 hexPoints[1] = cellVertData[dataIndex++];
316 hexPoints[2] = cellVertData[dataIndex++];
317 hexPoints[3] = cellVertData[dataIndex++];
318 hexPoints[4] = cellVertData[dataIndex++];
319 hexPoints[5] = cellVertData[dataIndex++];
320 hexPoints[6] = cellVertData[dataIndex++];
321 hexPoints[7] = cellVertData[dataIndex++];
323 cells_[celli++] = cellShape(
hex, hexPoints,
true);
328 warnUnhandledType(inFile,
cellTypes[i], warningGiven);
329 label nRead = cellVertData[dataIndex++];
335 <<
"Read " << celli <<
" cells;" << facei <<
" faces." <<
nl;
337 cells_.setSize(celli);
338 cellMap_.setSize(celli);
339 faces_.setSize(facei);
340 faceMap_.setSize(facei);
341 lines_.setSize(lineI);
342 lineMap_.setSize(lineI);
346 void Foam::vtkUnstructuredReader::readField
350 const word& arrayName,
351 const word& dataType,
355 if (vtkDataTypeNames.found(dataType))
357 switch (vtkDataTypeNames[dataType])
367 IOobject(arrayName,
"", obj),
370 readBlock(inFile, fieldVals().size(), fieldVals());
380 IOobject(arrayName,
"", obj),
383 readBlock(inFile, fieldVals().size(), fieldVals());
391 <<
"Reading strings:" << size <<
nl;
395 IOobject(arrayName,
"", obj),
399 inFile.getLine(fieldVals()[0]);
402 for (
string&
s : fieldVals())
413 <<
"Unhandled type " << dataType <<
nl
414 <<
"Skipping " << size
417 readBlock(inFile, size, fieldVals);
425 <<
"Unhandled type " << dataType <<
nl
426 <<
"Skipping " << size
429 readBlock(inFile, size, fieldVals);
438 const label wantedSize
443 word dataName(inFile);
446 <<
"dataName:" << dataName <<
nl;
451 Pout<<
"numArrays:" << numArrays <<
nl;
453 for (label i = 0; i < numArrays; i++)
455 word arrayName(inFile);
458 word dataType(inFile);
461 <<
"Reading field " << arrayName
462 <<
" of " << numTuples <<
" tuples of rank " << numComp <<
nl;
464 if (wantedSize != -1 && numTuples != wantedSize)
467 <<
"Expected " << wantedSize <<
" tuples but only have "
487 const parseMode readMode
490 if (readMode == CELL_DATA)
494 else if (readMode == POINT_DATA)
511 cellData_(
IOobject(
"cellData", obr)),
512 pointData_(
IOobject(
"pointData", obr)),
513 otherData_(
IOobject(
"otherData", obr))
519 void Foam::vtkUnstructuredReader::read(
ISstream& inFile)
530 if (dataType_ ==
"BINARY")
533 <<
"Binary reading not supported"
537 parseMode readMode = NOMODE;
538 label wantedSize = -1;
546 while (inFile.
read(tok).
good() && tok.isWord())
548 const word tag = tok.wordToken();
552 <<
" tag:" << tag <<
nl;
554 if (tag ==
"DATASET")
556 word geomType(inFile);
559 readMode = parseModeNames[geomType];
562 else if (tag ==
"POINTS")
568 <<
"Reading " <<
nPoints <<
" numbers representing "
569 << points_.size() <<
" coordinates." <<
nl;
571 word primitiveTag(inFile);
572 if (primitiveTag !=
"float" && primitiveTag !=
"double")
575 <<
"Expected 'float' entry but found "
581 inFile >>
p.x() >>
p.y() >>
p.z();
584 else if (tag ==
"CELLS")
589 <<
"Reading " << nCells <<
" cells or faces." <<
nl;
591 readBlock(inFile, nNumbers, cellVerts);
593 else if (tag ==
"CELL_TYPES")
598 readBlock(inFile, nCellTypes,
cellTypes);
600 if (
cellTypes.size() > 0 && cellVerts.size() == 0)
604 <<
" cellTypes but no cells."
608 extractCells(inFile,
cellTypes, cellVerts);
611 else if (tag ==
"LINES")
616 <<
"Reading " << nLines <<
" lines." <<
nl;
619 readBlock(inFile, nNumbers, lineVerts);
621 label lineI = lines_.size();
622 lines_.setSize(lineI+nLines);
623 lineMap_.setSize(lines_.size());
626 for (label i = 0; i < nLines; i++)
628 lineMap_[lineI] = lineI;
631 for (label& pointi :
f)
633 pointi = lineVerts[elemI++];
638 else if (tag ==
"POLYGONS")
645 <<
"Reading " << nFaces <<
" faces." <<
nl;
648 readBlock(inFile, nNumbers, faceVerts);
650 label facei = faces_.size();
651 faces_.setSize(facei+nFaces);
652 faceMap_.setSize(faces_.size());
655 for (label i = 0; i < nFaces; i++)
657 faceMap_[facei] = facei;
658 face&
f = faces_[facei];
660 for (label& pointi :
f)
662 pointi = faceVerts[elemI++];
667 else if (tag ==
"POINT_DATA")
671 wantedSize = points_.size();
677 <<
"Reading POINT_DATA : expected " << wantedSize
681 else if (tag ==
"CELL_DATA")
684 wantedSize = cells_.size()+faces_.size()+lines_.size();
687 if (nCells != wantedSize)
690 <<
"Reading CELL_DATA : expected "
695 else if (tag ==
"FIELD")
698 readFieldArray(inFile, selectRegistry(readMode), wantedSize);
700 else if (tag ==
"SCALARS")
704 IStringStream is(line);
710 <<
"Reading scalar " << dataName
711 <<
" of type " << dataType
712 <<
" from lookup table" <<
nl;
714 word lookupTableTag(inFile);
715 if (lookupTableTag !=
"LOOKUP_TABLE")
718 <<
"Expected tag LOOKUP_TABLE but read "
723 word lookupTableName(inFile);
728 selectRegistry(readMode),
734 else if (tag ==
"VECTORS" || tag ==
"NORMALS")
739 IStringStream is(line);
743 <<
"Reading vector " << dataName
744 <<
" of type " << dataType <<
nl;
746 objectRegistry& reg = selectRegistry(readMode);
759 vtkDataTypeNames[dataType] == VTK_FLOAT
760 || vtkDataTypeNames[dataType] == VTK_DOUBLE
763 objectRegistry::iterator iter = reg.find(dataName);
768 IOobject(dataName,
"", reg),
773 for (
vector& val : fieldVals())
775 val.x() =
s[elemI++];
776 val.y() =
s[elemI++];
777 val.z() =
s[elemI++];
782 else if (tag ==
"TEXTURE_COORDINATES")
787 IStringStream is(line);
793 <<
"Reading texture coords " << dataName
794 <<
" dimension " << dim
795 <<
" of type " << dataType <<
nl;
798 readBlock(inFile, coords.size(), coords);
800 else if (tag ==
"TRIANGLE_STRIPS")
805 <<
"Reading " << nStrips <<
" triangle strips." <<
nl;
808 readBlock(inFile, nNumbers, faceVerts);
813 for (label i = 0; i < nStrips; i++)
815 label nVerts = faceVerts[elemI++];
822 label facei = faces_.size();
823 faces_.setSize(facei+nTris);
824 faceMap_.setSize(faces_.size());
826 for (label i = 0; i < nStrips; i++)
828 label nVerts = faceVerts[elemI++];
829 label nTris = nVerts-2;
832 faceMap_[facei] = facei;
833 face&
f = faces_[facei++];
835 f[0] = faceVerts[elemI++];
836 f[1] = faceVerts[elemI++];
837 f[2] = faceVerts[elemI++];
838 for (label triI = 1; triI < nTris; triI++)
840 faceMap_[facei] = facei;
841 face&
f = faces_[facei++];
843 f[0] = faceVerts[elemI-1];
844 f[1] = faceVerts[elemI-2];
845 f[2] = faceVerts[elemI++];
849 else if (tag ==
"METADATA")
851 word infoTag(inFile);
852 if (infoTag !=
"INFORMATION")
855 <<
"Unsupported tag "
860 <<
"Consuming " << nInfo <<
" metadata information." <<
nl;
865 for (label i = 0; i < 2*nInfo; i++)
873 <<
"Unsupported tag "
886 for (cellShape& shape : cells_)
888 if (shape.model() == prism)
903 const point bottomCc(bottom.centre());
904 const vector bottomNormal(bottom.areaNormal());
905 const point topCc(top.centre());
907 if (((topCc - bottomCc) & bottomNormal) < 0)
910 Swap(shape[0], shape[3]);
911 Swap(shape[1], shape[4]);
912 Swap(shape[2], shape[5]);
926 Info<<
"Read points:" << points_.size()
927 <<
" cellShapes:" << cells_.size()
928 <<
" faces:" << faces_.size()
929 <<
" lines:" << lines_.size()
932 Info<<
"Cell fields:" <<
nl;
933 printFieldStats<vectorIOField>(cellData_);
934 printFieldStats<scalarIOField>(cellData_);
935 printFieldStats<labelIOField>(cellData_);
936 printFieldStats<stringIOList>(cellData_);
939 Info<<
"Point fields:" <<
nl;
940 printFieldStats<vectorIOField>(pointData_);
941 printFieldStats<scalarIOField>(pointData_);
942 printFieldStats<labelIOField>(pointData_);
943 printFieldStats<stringIOList>(pointData_);
946 Info<<
"Other fields:" <<
nl;
947 printFieldStats<vectorIOField>(otherData_);
948 printFieldStats<scalarIOField>(otherData_);
949 printFieldStats<labelIOField>(otherData_);
950 printFieldStats<stringIOList>(otherData_);