NEMoSys  0.63.0
A modular, extensible resource with robust automated mesh generation, mesh quality analysis, adaptive mesh refinement, and data transfer between arbitrary meshes.
vtkMesh.H File Reference

Go to the source code of this file.

Classes

class  vtkMesh
 

Functions

template<class TReader >
vtkDataSet * ReadAnXMLOrSTLFile (const std::string &fileName)
 
vtkSmartPointer< vtkUnstructuredGrid > ReadALegacyVTKFile (const std::string &fileName)
 
vtkSmartPointer< vtkUnstructuredGrid > ReadDegenerateVTKFile (const std::string &fileName)
 
bool readLegacyVTKHeader (const std::string &line)
 
bool readLegacyVTKFieldData (const std::istream &meshStream, const std::string &line, vtkSmartPointer< vtkUnstructuredGrid > dataSet_tmp)
 
bool readLegacyVTKPoints (const std::istream &meshStream, const std::string &line, nemId_t &numPoints, vtkSmartPointer< vtkPoints > points, vtkSmartPointer< vtkUnstructuredGrid > dataSet_tmp)
 
bool readLegacyVTKCells (const std::istream &meshStream, const std::string &line, nemId_t &numCells, const std::vector< vtkSmartPointer< vtkIdList >> &vtkCellIds, vtkSmartPointer< vtkUnstructuredGrid > dataSet_tmp)
 
bool readLegacyVTKData (const std::ifstream &meshStream, const std::string &line, nemId_t numTuple, bool pointOrCell, bool &hasPointOrCell, vtkSmartPointer< vtkUnstructuredGrid > dataSet_tmp)
 
void addLegacyVTKData (vtkDataArray *arr, const std::string &type, bool pointOrCell, vtkSmartPointer< vtkUnstructuredGrid > dataSet_tmp)
 
template<class TWriter >
void writeVTFile (const std::string &fname, vtkSmartPointer< vtkDataSet > dataSet)
 

Function Documentation

◆ addLegacyVTKData()

void addLegacyVTKData ( vtkDataArray *  arr,
const std::string &  type,
bool  pointOrCell,
vtkSmartPointer< vtkUnstructuredGrid >  dataSet_tmp 
)

Referenced by ReadAnXMLOrSTLFile().

◆ ReadALegacyVTKFile()

vtkSmartPointer<vtkUnstructuredGrid> ReadALegacyVTKFile ( const std::string &  fileName)

Definition at line 502 of file vtkMesh.C.

References NEM::MSH::New(), meshBase::numCells, meshBase::numPoints, points, readLegacyVTKCells(), readLegacyVTKData(), readLegacyVTKFieldData(), readLegacyVTKHeader(), and readLegacyVTKPoints().

Referenced by ReadAnXMLOrSTLFile(), and vtkMesh::vtkMesh().

503  {
504  std::ifstream meshStream;
505  meshStream.open(fileName, std::ifstream::in);
506  if (!meshStream.good()) {
507  std::cerr << "Could not open file " << fileName << std::endl;
508  exit(1);
509  }
510 
511  // declare points to be pushed into dataSet_tmp
512  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
513  // declare vector of cell ids to be pushed into dataSet_tmp
514  std::vector<vtkSmartPointer<vtkIdList>> vtkCellIds;
515  // declare dataSet_tmp which will be associated to output vtkMesh
516  vtkSmartPointer<vtkUnstructuredGrid> dataSet_tmp =
518  nemId_t numPoints(0), numCells(0);
519  bool readHeader(false), readPoints(false), readCells(false),
520  readFieldData(false), readCellData(false), readPointData(false),
521  cellDataFirst(false), pointDataFirst(false), hasPointData(false),
522  hasCellData(false);
523  std::string line;
524 
525  while (getline(meshStream, line)) {
526  if (!readHeader)
527  if ((readHeader = readLegacyVTKHeader(line)))
528  std::cout << "read header" << std::endl;
529 
530  if (!readFieldData)
531  if ((readFieldData =
532  readLegacyVTKFieldData(meshStream, line, dataSet_tmp)))
533  std::cout << "read field data" << std::endl;
534 
535  if (!readPoints)
536  if ((readPoints = readLegacyVTKPoints(meshStream, line, numPoints, points,
537  dataSet_tmp)))
538  std::cout << "read points" << std::endl;
539 
540  if (!readCells)
541  if ((readCells = readLegacyVTKCells(meshStream, line, numCells,
542  vtkCellIds, dataSet_tmp)))
543  std::cout << "read cells" << std::endl;
544 
545  // call functions based on which data appears first in file
546  if (!readCellData && !readPointData) {
547  cellDataFirst =
548  line.find("CELL_DATA") != std::string::npos && !pointDataFirst;
549  pointDataFirst =
550  line.find("POINT_DATA") != std::string::npos && !cellDataFirst;
551  }
552 
553  // read cell data then point data if it exists
554  if (cellDataFirst) {
555  if (!readCellData) // boolean telling us if there is point data
556  if ((readCellData = readLegacyVTKData(meshStream, line, numCells, false,
557  hasPointData, dataSet_tmp)))
558  std::cout << "read cell data" << std::endl;
559 
560  if (!readPointData && hasPointData)
561  if ((readPointData = readLegacyVTKData(meshStream, line, numPoints,
562  true, hasCellData, dataSet_tmp)))
563  std::cout << "read point data" << std::endl;
564  }
565  // read point data and then cell data if it exists
566  else if (pointDataFirst) {
567  if (!readPointData)
568  if ((readPointData = readLegacyVTKData(meshStream, line, numPoints,
569  true, hasCellData, dataSet_tmp)))
570  std::cout << "read point data" << std::endl;
571 
572  if (!readCellData && hasCellData)
573  if ((readCellData = readLegacyVTKData(meshStream, line, numCells, false,
574  hasPointData, dataSet_tmp)))
575  std::cout << "read cell data" << std::endl;
576  }
577  }
578  return dataSet_tmp;
579 }
bool readLegacyVTKCells(std::istream &meshStream, std::string &line, nemId_t &numCells, std::vector< vtkSmartPointer< vtkIdList >> &vtkCellIds, vtkSmartPointer< vtkUnstructuredGrid > dataSet_tmp)
Definition: vtkMesh.C:358
bool readLegacyVTKPoints(std::istream &meshStream, std::string &line, nemId_t &numPoints, vtkSmartPointer< vtkPoints > points, vtkSmartPointer< vtkUnstructuredGrid > dataSet_tmp)
Definition: vtkMesh.C:334
std::size_t nemId_t
Definition: meshBase.H:51
geoMeshBase * New(MeshType meshType)
Create a new mesh object.
bool readLegacyVTKHeader(const std::string &line)
Definition: vtkMesh.C:294
std::vector< vtkIdType > points
points given by id in .inp file
Definition: inpGeoMesh.C:133
bool readLegacyVTKFieldData(std::istream &meshStream, std::string &line, vtkSmartPointer< vtkUnstructuredGrid > dataSet_tmp)
Definition: vtkMesh.C:305
bool readLegacyVTKData(std::ifstream &meshStream, std::string &line, const nemId_t numTuple, const bool pointOrCell, bool &hasPointOrCell, vtkSmartPointer< vtkUnstructuredGrid > dataSet_tmp)
Definition: vtkMesh.C:406

◆ ReadAnXMLOrSTLFile()

template<class TReader >
vtkDataSet* ReadAnXMLOrSTLFile ( const std::string &  fileName)

Definition at line 40 of file vtkMesh.H.

References addLegacyVTKData(), NEM::MSH::New(), points, ReadALegacyVTKFile(), ReadDegenerateVTKFile(), readLegacyVTKCells(), readLegacyVTKData(), readLegacyVTKFieldData(), readLegacyVTKHeader(), and readLegacyVTKPoints().

40  {
41  vtkSmartPointer<TReader> reader = vtkSmartPointer<TReader>::New();
42  reader->SetFileName(fileName.c_str());
43  reader->Update();
44  reader->GetOutput()->Register(reader);
45  return vtkDataSet::SafeDownCast(reader->GetOutput());
46 }
geoMeshBase * New(MeshType meshType)
Create a new mesh object.

◆ ReadDegenerateVTKFile()

vtkSmartPointer<vtkUnstructuredGrid> ReadDegenerateVTKFile ( const std::string &  fileName)

Definition at line 581 of file vtkMesh.C.

References cellType, nemAux::flip_map(), id, NEM::MSH::New(), meshBase::numCells, meshBase::numPoints, and points.

Referenced by ReadAnXMLOrSTLFile(), and vtkMesh::vtkMesh().

582  {
583  std::ifstream meshStream(fileName);
584  if (!meshStream.good()) {
585  std::cerr << "Error opening file " << fileName << std::endl;
586  exit(1);
587  }
588 
589  std::string line;
590  std::map<std::vector<double>, int> point_map;
591  std::map<int, int> duplicate_point_map;
592  std::pair<std::map<std::vector<double>, int>::iterator, bool> point_ret;
593  int numPoints;
594  int numCells;
595  int cellListSize;
596  std::vector<vtkSmartPointer<vtkIdList>> vtkCellIds;
597  vtkSmartPointer<vtkUnstructuredGrid> dataSet_tmp =
599  while (getline(meshStream, line)) {
600  if (line.find("POINTS") != std::string::npos) {
601  std::istringstream ss(line);
602  std::string tmp;
603  ss >> tmp >> numPoints;
604  std::vector<double> point(3);
605  int pntId = 0;
606  int realPntId = 0;
607  while (getline(meshStream, line) && pntId < numPoints) {
608  if (!line.empty()) {
609  std::istringstream ss(line);
610  ss >> point[0] >> point[1] >> point[2];
611  point_ret = point_map.insert(
612  std::pair<std::vector<double>, int>(point, realPntId));
613  if (point_ret.second) ++realPntId;
614  duplicate_point_map.insert(
615  std::pair<int, int>(pntId, point_ret.first->second));
616  ++pntId;
617  }
618  }
619  }
620 
621  if (line.find("CELLS") != std::string::npos) {
622  std::istringstream ss(line);
623  std::string tmp;
624  ss >> tmp >> numCells >> cellListSize;
625  int cellId = 0;
626  // int realCellId = 0;
627  vtkCellIds.resize(numCells);
628  while (cellId < numCells && getline(meshStream, line)) {
629  if (!line.empty()) {
630  std::istringstream ss(line);
631  int numId;
632  int id;
633  ss >> numId;
634  vtkCellIds[cellId] = vtkSmartPointer<vtkIdList>::New();
635  vtkCellIds[cellId]->SetNumberOfIds(numId);
636  // cells[cellId].resize(numId);
637  for (int j = 0; j < numId; ++j) {
638  ss >> id;
639  vtkCellIds[cellId]->SetId(j, duplicate_point_map[id]);
640  // cells[cellId][j] = duplicate_point_map[id];
641  }
642  ++cellId;
643  }
644  }
645  // get cell types
646  while (getline(meshStream, line)) {
647  if (line.find("CELL_TYPES") != std::string::npos) {
648  dataSet_tmp->Allocate(numCells);
649  cellId = 0;
650  // cellTypes.resize(numCells);
651  while (cellId < numCells && getline(meshStream, line)) {
652  if (!line.empty()) {
653  std::istringstream ss(line);
654  int cellType;
655  ss >> cellType;
656  // cellTypes[cellId] = cellType;
657  dataSet_tmp->InsertNextCell(cellType, vtkCellIds[cellId]);
658  ++cellId;
659  }
660  }
661  break;
662  }
663  }
664  }
665  }
666 
667  std::multimap<int, std::vector<double>> flipped = nemAux::flip_map(point_map);
668  auto flip_it = flipped.begin();
669 
670  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
671  points->SetNumberOfPoints(point_map.size());
672  int pnt = 0;
673  while (flip_it != flipped.end()) {
674  points->SetPoint(pnt, flip_it->second.data());
675  ++flip_it;
676  ++pnt;
677  }
678  dataSet_tmp->SetPoints(points);
679  return dataSet_tmp;
680 }
std::multimap< B, A > flip_map(const std::map< A, B > &src)
geoMeshBase * New(MeshType meshType)
Create a new mesh object.
vtkIdType id
id in .inp file
Definition: inpGeoMesh.C:128
VTKCellType cellType
Definition: inpGeoMesh.C:129
std::vector< vtkIdType > points
points given by id in .inp file
Definition: inpGeoMesh.C:133

◆ readLegacyVTKCells()

bool readLegacyVTKCells ( const std::istream &  meshStream,
const std::string &  line,
nemId_t numCells,
const std::vector< vtkSmartPointer< vtkIdList >> &  vtkCellIds,
vtkSmartPointer< vtkUnstructuredGrid >  dataSet_tmp 
)

Referenced by ReadAnXMLOrSTLFile().

◆ readLegacyVTKData()

bool readLegacyVTKData ( const std::ifstream &  meshStream,
const std::string &  line,
nemId_t  numTuple,
bool  pointOrCell,
bool &  hasPointOrCell,
vtkSmartPointer< vtkUnstructuredGrid >  dataSet_tmp 
)

Referenced by ReadAnXMLOrSTLFile().

◆ readLegacyVTKFieldData()

bool readLegacyVTKFieldData ( const std::istream &  meshStream,
const std::string &  line,
vtkSmartPointer< vtkUnstructuredGrid >  dataSet_tmp 
)

Referenced by ReadAnXMLOrSTLFile().

◆ readLegacyVTKHeader()

bool readLegacyVTKHeader ( const std::string &  line)

Definition at line 294 of file vtkMesh.C.

Referenced by ReadALegacyVTKFile(), and ReadAnXMLOrSTLFile().

294  {
295  if (line.find("DATASET") != -1) {
296  if (line.find("UNSTRUCTURED_GRID") == std::string::npos) {
297  std::cerr << "Reading a " << line << " is not supported" << std::endl;
298  exit(1);
299  }
300  return true;
301  }
302  return false;
303 }

◆ readLegacyVTKPoints()

bool readLegacyVTKPoints ( const std::istream &  meshStream,
const std::string &  line,
nemId_t numPoints,
vtkSmartPointer< vtkPoints >  points,
vtkSmartPointer< vtkUnstructuredGrid >  dataSet_tmp 
)

Referenced by ReadAnXMLOrSTLFile().

◆ writeVTFile()

template<class TWriter >
void writeVTFile ( const std::string &  fname,
vtkSmartPointer< vtkDataSet >  dataSet 
)

Definition at line 87 of file vtkMesh.H.

References NEM::MSH::New().

88  {
89  vtkSmartPointer<TWriter> Writer = vtkSmartPointer<TWriter>::New();
90  Writer->SetFileName(fname.c_str());
91  Writer->SetInputData(dataSet);
92  // WARNING: ASCII does not work for STL and legacy VTK files
93  // WARNING: large files will be generated
94  // Writer->SetDataModeToAscii();
95  Writer->Update();
96  Writer->Write();
97 }
geoMeshBase * New(MeshType meshType)
Create a new mesh object.