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.C File Reference

Go to the source code of this file.

Functions

bool readLegacyVTKHeader (const std::string &line)
 
bool readLegacyVTKFieldData (std::istream &meshStream, std::string &line, vtkSmartPointer< vtkUnstructuredGrid > dataSet_tmp)
 
bool readLegacyVTKPoints (std::istream &meshStream, std::string &line, nemId_t &numPoints, vtkSmartPointer< vtkPoints > points, vtkSmartPointer< vtkUnstructuredGrid > dataSet_tmp)
 
bool readLegacyVTKCells (std::istream &meshStream, std::string &line, nemId_t &numCells, std::vector< vtkSmartPointer< vtkIdList >> &vtkCellIds, vtkSmartPointer< vtkUnstructuredGrid > dataSet_tmp)
 
bool readLegacyVTKData (std::ifstream &meshStream, std::string &line, const nemId_t numTuple, const bool pointOrCell, bool &hasPointOrCell, vtkSmartPointer< vtkUnstructuredGrid > dataSet_tmp)
 
vtkSmartPointer< vtkUnstructuredGrid > ReadALegacyVTKFile (const std::string &fileName)
 
vtkSmartPointer< vtkUnstructuredGrid > ReadDegenerateVTKFile (const std::string &fileName)
 

Function Documentation

◆ 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

◆ 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 ( std::istream &  meshStream,
std::string &  line,
nemId_t numCells,
std::vector< vtkSmartPointer< vtkIdList >> &  vtkCellIds,
vtkSmartPointer< vtkUnstructuredGrid >  dataSet_tmp 
)

Definition at line 358 of file vtkMesh.C.

References cellType, id, NEM::MSH::New(), and meshBase::numCells.

Referenced by ReadALegacyVTKFile().

361  {
362  if (line.find("CELLS") != std::string::npos) {
363  std::istringstream ss(line);
364  std::string tmp;
365  ss >> tmp >> numCells;
366  vtkCellIds.resize(numCells);
367  // get cells
368  nemId_t i = 0;
369  while (i < numCells && getline(meshStream, line)) {
370  if (!line.empty()) {
371  std::istringstream ss(line);
372  nemId_t numId;
373  nemId_t id;
374  ss >> numId;
375  vtkCellIds[i] = vtkSmartPointer<vtkIdList>::New();
376  vtkCellIds[i]->SetNumberOfIds(numId);
377  for (nemId_t j = 0; j < numId; ++j) {
378  ss >> id;
379  vtkCellIds[i]->SetId(j, id);
380  }
381  ++i;
382  }
383  }
384  // get cell types
385  while (getline(meshStream, line)) {
386  if (line.find("CELL_TYPES") != std::string::npos) {
387  dataSet_tmp->Allocate(numCells);
388  i = 0;
389  while (i < numCells && getline(meshStream, line)) {
390  if (!line.empty()) {
391  std::istringstream ss(line);
392  int cellType;
393  ss >> cellType;
394  dataSet_tmp->InsertNextCell(cellType, vtkCellIds[i]);
395  ++i;
396  }
397  }
398  break;
399  }
400  }
401  return true;
402  }
403  return false;
404 }
std::size_t nemId_t
Definition: meshBase.H:51
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

◆ readLegacyVTKData()

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

Definition at line 406 of file vtkMesh.C.

References NEM::MSH::New().

Referenced by ReadALegacyVTKFile().

409  {
410  // determines whether more than one point or cell array in dataSet
411  bool inProgress(true);
412  // while there is a cell or point array to be read
413  while (inProgress) {
414  int numComponent(0);
415  std::string line, attribute, name, type;
416  vtkSmartPointer<vtkDoubleArray> arr =
418  while (getline(meshStream, line)) {
419  if (!line.empty()) {
420  if (line.find("LOOKUP_TABLE default") != std::string::npos) break;
421 
422  size_t hasScalar = line.find("SCALARS");
423  size_t hasVector = line.find("VECTORS");
424  size_t hasNormal = line.find("NORMALS");
425  if (hasScalar == std::string::npos && hasVector == std::string::npos &&
426  hasNormal == std::string::npos) {
427  std::cerr << "attribute type in " << line << " not supported"
428  << std::endl;
429  exit(1);
430  } else {
431  std::istringstream ss(line);
432  ss >> attribute >> name >> type >> numComponent;
433  if (hasScalar != std::string::npos)
434  numComponent =
435  numComponent > 4 || numComponent < 1 ? 1 : numComponent;
436  else if (hasVector != std::string::npos ||
437  hasNormal != std::string::npos)
438  numComponent = 3;
439  arr->SetName(name.c_str());
440  arr->SetNumberOfComponents(numComponent);
441  arr->SetNumberOfTuples(numTuple);
442  // std::cout << arr->GetName() << " " << arr->GetNumberOfComponents()
443  // << " " << arr->GetNumberOfTuples() << std::endl;
444  break;
445  }
446  }
447  }
448 
449  nemId_t i = 0;
450  while (i < numTuple && getline(meshStream, line)) {
451  if (!line.empty() && line.find("LOOKUP") == std::string::npos) {
452  std::istringstream ss(line);
453  // double vals[numComponent];
454  std::vector<double> vals;
455  vals.resize(numComponent, 0.);
456 
457  for (int j = 0; j < numComponent; ++j) ss >> vals[j];
458  arr->InsertTuple(i, vals.data());
459  ++i;
460  }
461  }
462 
463  if (pointOrCell)
464  dataSet_tmp->GetPointData()->AddArray(arr);
465  else
466  dataSet_tmp->GetCellData()->AddArray(arr);
467 
468  bool moreData(false);
469  std::streampos curr = meshStream.tellg();
470  while (getline(meshStream, line)) {
471  // if point data encountered while looking for more cell data, we know we
472  // read all cell data
473  if (line.find("POINT_DATA") != std::string::npos && !pointOrCell) {
474  curr = meshStream.tellg();
475  hasPointOrCell = true;
476  break;
477  }
478  // if cell data encountered while looking for more point data, we know we
479  // read all point data
480  if (line.find("CELL_DATA") != std::string::npos && pointOrCell) {
481  hasPointOrCell = true;
482  curr = meshStream.tellg();
483  break;
484  }
485  // if the previous two conditions are not met and this one is,
486  // we know we have more than one point/cell data array
487  if (line.find("SCALARS") != std::string::npos ||
488  line.find("VECTORS") != std::string::npos ||
489  line.find("NORMALS") != std::string::npos) {
490  moreData = true;
491  break;
492  }
493  }
494 
495  meshStream.seekg(curr);
496 
497  if (!moreData) inProgress = false;
498  }
499  return true;
500 }
std::size_t nemId_t
Definition: meshBase.H:51
geoMeshBase * New(MeshType meshType)
Create a new mesh object.

◆ readLegacyVTKFieldData()

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

Definition at line 305 of file vtkMesh.C.

References NEM::MSH::New().

Referenced by ReadALegacyVTKFile().

306  {
307  if (line.find("FIELD") != std::string::npos) {
308  if (getline(meshStream, line)) {
309  std::string arrname, type;
310  int numComponent;
311  nemId_t numTuple;
312  std::stringstream ss;
313  ss.str(line);
314  ss >> arrname >> numComponent >> numTuple >> type;
315  vtkSmartPointer<vtkDoubleArray> arr =
317  arr->SetName(arrname.c_str());
318  arr->SetNumberOfComponents(numComponent);
319  arr->SetNumberOfTuples(numTuple);
320  getline(meshStream, line);
321  ss.str(line);
322  double val;
323  for (int i = 0; i < numTuple; ++i) {
324  ss >> val;
325  arr->InsertTuple(i, &val);
326  }
327  dataSet_tmp->GetFieldData()->AddArray(arr);
328  return true;
329  }
330  }
331  return false;
332 }
std::size_t nemId_t
Definition: meshBase.H:51
geoMeshBase * New(MeshType meshType)
Create a new mesh object.

◆ 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 ( std::istream &  meshStream,
std::string &  line,
nemId_t numPoints,
vtkSmartPointer< vtkPoints >  points,
vtkSmartPointer< vtkUnstructuredGrid >  dataSet_tmp 
)

Definition at line 334 of file vtkMesh.C.

References meshBase::numPoints.

Referenced by ReadALegacyVTKFile().

336  {
337  if (line.find("POINTS") != std::string::npos) {
338  std::istringstream ss(line);
339  std::string tmp;
340  ss >> tmp >> numPoints;
341  points->SetNumberOfPoints(numPoints);
342  double point[3];
343  nemId_t i = 0;
344  while (getline(meshStream, line) && i < numPoints) {
345  if (!line.empty()) {
346  std::istringstream ss(line);
347  ss >> point[0] >> point[1] >> point[2];
348  points->SetPoint(i, point);
349  ++i;
350  }
351  }
352  dataSet_tmp->SetPoints(points);
353  return true;
354  }
355  return false;
356 }
std::size_t nemId_t
Definition: meshBase.H:51
std::vector< vtkIdType > points
points given by id in .inp file
Definition: inpGeoMesh.C:133