10 #include <vtkCellData.h>
11 #include <vtkUnstructuredGrid.h>
17 #define USE_POINT_INDICES_TO_INSERT
19 #define INVALID_MAT_ID -INT_MAX
24 #define STRNCASECMP _strnicmp
28 #define STRNCASECMP strncasecmp
33 static float Getf(
const char *s) {
49 bool haveSeenNumChars =
false;
50 while (!haveSeenNumChars || (*p !=
'-' && *p !=
'+' && *p !=
'\0')) {
51 if (
'0' <= *p && *p <=
'9' || *p ==
'.' || *p ==
'+' || *p ==
'-')
52 haveSeenNumChars =
true;
56 if (haveSeenNumChars && (*p ==
'-' || *p ==
'+')) {
58 while (*p !=
'\0') *q++ = *p++;
61 val = strtod(tmps, &ends);
64 val = strtod(s, &ends);
68 qDebug() <<
"Error occured at " << __FUNCTION__;
87 static int Geti(
const char *s) {
91 long val = strtol(s, &ends, 10);
94 qDebug() <<
"Error occured at " << __FUNCTION__;
159 std::ifstream ifile(
fileName().toLocal8Bit());
160 if (ifile.fail())
return;
168 #if !defined(MDSERVER)
170 vtkPoints *pts = vtkPoints::New();
171 pts->Allocate(nPoints);
172 #ifdef USE_POINT_INDICES_TO_INSERT
173 pts->SetNumberOfPoints(nPoints);
176 vtkUnstructuredGrid *ugrid = vtkUnstructuredGrid::New();
177 ugrid->SetPoints(pts);
178 ugrid->Allocate(nCells);
180 pts = ugrid->GetPoints();
187 bool recognized =
false;
188 bool titleRead =
false;
189 for (
int lineIndex = 0; !ifile.eof(); ++lineIndex) {
193 ifile.getline(line, 1024);
196 if (line[0] ==
'$' && !recognized) {
197 if (!titleRead)
m_title += line;
198 }
else if (strncmp(line,
"GRID*", 5) == 0) {
203 ifile.getline(line + 72, 1024 - 72);
205 #define LONG_FIELD_WIDTH 16
206 char *valstart = line + 81 - 1;
207 pt[2] =
Getf(valstart);
210 char *valend = line + 72;
212 pt[1] =
Getf(valstart);
217 pt[0] =
Getf(valstart);
219 #ifdef USE_POINT_INDICES_TO_INSERT
223 int psi =
Geti(valstart) - 1;
225 #if !defined(MDSERVER)
228 pts->SetPoint(psi, pt);
230 int newSize = int(
float(nPoints) * 1.1f);
231 if (newSize < psi) newSize = int(
float(psi) * 1.1f);
232 if (newSize <= nPoints) newSize = nPoints + 1;
234 qDebug() <<
"Resizing point array from " << nPoints
235 <<
" points to " << newSize
236 <<
" points because we need to insert point index "
239 pts->GetData()->Resize(nPoints);
240 pts->SetNumberOfPoints(nPoints);
241 pts->SetPoint(psi, pt);
247 #if !defined(MDSERVER)
248 pts->InsertNextPoint(pt);
252 }
else if (strncmp(line,
"GRID", 4) == 0) {
255 #define SHORT_FIELD_WIDTH 8
257 char *valend = line + 48;
260 pt[2] =
Getf(valstart);
265 pt[1] =
Getf(valstart);
270 pt[0] =
Getf(valstart);
272 #ifdef USE_POINT_INDICES_TO_INSERT
276 int psi =
Geti(valstart) - 1;
278 #if !defined(MDSERVER)
281 pts->SetPoint(psi, pt);
283 int newSize = int(
float(nPoints) * 1.1f);
284 if (newSize < psi) newSize = int(
float(psi) * 1.1f);
285 if (newSize <= nPoints) newSize = nPoints + 1;
287 qDebug() <<
"Resizing point array from " << nPoints
288 <<
" points to " << newSize
289 <<
" points because we need to insert point index "
292 pts->GetData()->Resize(nPoints);
293 pts->SetNumberOfPoints(nPoints);
294 pts->SetPoint(psi, pt);
301 #if !defined(MDSERVER)
302 pts->InsertNextPoint(pt);
307 }
else if (strncmp(line,
"CHEXA", 5) == 0) {
308 #define INDEX_FIELD_WIDTH 8
310 ifile.getline(line + 72, 1024 - 72);
312 char *valstart = line + 88;
313 char *valend = valstart;
314 verts[7] =
Geti(valstart) - 1;
318 verts[6] =
Geti(valstart) - 1;
327 verts[5] =
Geti(valstart) - 1;
332 verts[4] =
Geti(valstart) - 1;
337 verts[3] =
Geti(valstart) - 1;
342 verts[2] =
Geti(valstart) - 1;
347 verts[1] =
Geti(valstart) - 1;
352 verts[0] =
Geti(valstart) - 1;
358 matid =
Geti(valstart);
361 #if !defined(MDSERVER)
362 ugrid->InsertNextCell(VTK_HEXAHEDRON, 8, verts);
363 if (matCountOpt)
m_matList.push_back(matid);
365 }
else if (strncmp(line,
"CTETRA", 6) == 0) {
366 char *valstart = line + 48;
367 char *valend = valstart;
368 verts[3] =
Geti(valstart) - 1;
372 verts[2] =
Geti(valstart) - 1;
377 verts[1] =
Geti(valstart) - 1;
382 verts[0] =
Geti(valstart) - 1;
388 matid =
Geti(valstart);
391 #if !defined(MDSERVER)
392 ugrid->InsertNextCell(VTK_TETRA, 4, verts);
393 if (matCountOpt)
m_matList.push_back(matid);
395 }
else if (strncmp(line,
"CPYRAM", 6) == 0) {
396 char *valstart = line + 56;
397 char *valend = valstart;
398 verts[4] =
Geti(valstart) - 1;
402 verts[3] =
Geti(valstart) - 1;
407 verts[2] =
Geti(valstart) - 1;
412 verts[1] =
Geti(valstart) - 1;
417 verts[0] =
Geti(valstart) - 1;
423 matid =
Geti(valstart);
426 #if !defined(MDSERVER)
427 ugrid->InsertNextCell(VTK_PYRAMID, 5, verts);
428 if (matCountOpt)
m_matList.push_back(matid);
430 }
else if (strncmp(line,
"CPENTA", 6) == 0) {
431 char *valstart = line + 64;
432 char *valend = valstart;
433 verts[5] =
Geti(valstart) - 1;
437 verts[4] =
Geti(valstart) - 1;
442 verts[3] =
Geti(valstart) - 1;
447 verts[2] =
Geti(valstart) - 1;
452 verts[1] =
Geti(valstart) - 1;
457 verts[0] =
Geti(valstart) - 1;
463 matid =
Geti(valstart);
470 #if !defined(MDSERVER)
471 if (verts[4] == verts[5])
472 ugrid->InsertNextCell(VTK_PYRAMID, 5, verts);
474 ugrid->InsertNextCell(VTK_WEDGE, 6, verts);
475 if (matCountOpt)
m_matList.push_back(matid);
477 }
else if (strncmp(line,
"CQUAD4", 6) == 0) {
478 char *valstart = line + 48;
479 char *valend = valstart;
480 verts[3] =
Geti(valstart) - 1;
484 verts[2] =
Geti(valstart) - 1;
489 verts[1] =
Geti(valstart) - 1;
494 verts[0] =
Geti(valstart) - 1;
500 matid =
Geti(valstart);
503 #if !defined(MDSERVER)
504 ugrid->InsertNextCell(VTK_QUAD, 4, verts);
505 if (matCountOpt)
m_matList.push_back(matid);
507 }
else if (strncmp(line,
"CTRIA2", 6) == 0 ||
508 strncmp(line,
"CTRIA3", 6) == 0) {
509 char *valstart = line + 40;
510 char *valend = valstart;
511 verts[2] =
Geti(valstart) - 1;
515 verts[1] =
Geti(valstart) - 1;
520 verts[0] =
Geti(valstart) - 1;
526 matid =
Geti(valstart);
529 #if !defined(MDSERVER)
530 ugrid->InsertNextCell(VTK_TRIANGLE, 3, verts);
531 if (matCountOpt)
m_matList.push_back(matid);
533 }
else if (strncmp(line,
"CBAR", 4) == 0) {
534 char *valstart = line + 32;
535 char *valend = valstart;
536 verts[1] =
Geti(valstart) - 1;
540 verts[0] =
Geti(valstart) - 1;
546 matid =
Geti(valstart);
549 #if !defined(MDSERVER)
550 ugrid->InsertNextCell(VTK_LINE, 2, verts);
551 if (matCountOpt)
m_matList.push_back(matid);
553 }
else if (strncmp(line,
"TITLE", 5) == 0) {
556 if ((ptr = strstr(line,
"=")) != 0)
560 }
else if (strncmp(line,
"CEND", 4) == 0 ||
561 strncmp(line,
"BEGIN BULK", 10) == 0 ||
562 strncmp(line,
"NASTRAN", 7) == 0) {
572 rpf->SetInputData(ugrid);
593 vtkUnstructuredGrid *grid = vtkUnstructuredGrid::SafeDownCast(
594 m_reader->GetOutput()->GetBlock(0));
595 int num =
m_reader->GetNumberOfCellArrays();
596 for (
int i = 0; i < num; ++i) {
610 std::ifstream ifile(
fileName().toLocal8Bit());
611 if (ifile.fail())
return;
617 vtkPoints *pts = vtkPoints::New();
618 pts->Allocate(nPoints);
620 vtkUnstructuredGrid *ugrid = vtkUnstructuredGrid::New();
621 ugrid->SetPoints(pts);
622 ugrid->Allocate(nCells);
625 #define MAX_ANSYS_LINE 200
629 bool recognized =
false;
630 bool fatalError =
false;
631 bool readingCoordinates =
false;
632 bool readingConnectivity =
false;
633 int expectedLineLength = 0;
635 int firstFieldWidth = 8;
639 for (
int lineIndex = 0; !ifile.eof(); ++lineIndex) {
643 int linelen =
static_cast<int>(strlen(line));
645 int linelen = strlen(line) - 1;
648 if (expectedLineLength > 0 && linelen < expectedLineLength) {
649 memset(line + linelen + 1, 0,
652 debug5 <<
"Padding line with NULLs" <<
endl;
653 debug5 << line <<
endl;
658 if (readingCoordinates) {
660 for (
int i = 0; i < firstFieldWidth && valid; ++i)
661 valid &= (line[i] ==
' ' || (line[i] >=
'0' && line[i] <=
'9'));
664 expectedLineLength = 0;
665 readingCoordinates =
false;
670 if (readingCoordinates) {
671 char *valstart = line + fieldStart;
672 char *valend = valstart + fieldWidth;
673 pt[2] = atof(valstart);
675 valstart -= fieldWidth;
676 valend -= fieldWidth;
678 pt[1] = atof(valstart);
680 valstart -= fieldWidth;
681 valend -= fieldWidth;
683 pt[0] = atof(valstart);
686 debug4 << pt[0] <<
", " << pt[1] <<
", " << pt[2] <<
endl;
688 pts->InsertNextPoint(pt);
689 }
else if (readingConnectivity) {
691 line[fieldWidth] =
'\0';
692 bool realCell = atoi(line) > 0;
694 expectedLineLength = 0;
695 readingConnectivity =
false;
700 static const int ncellsColumn = 9;
701 line[ncellsColumn * fieldWidth] =
'\0';
702 int nverts = atoi(line + (ncellsColumn - 1) * fieldWidth);
705 char *valstart = line + fieldStart;
706 char *valend = valstart + fieldWidth;
707 for (
int i = 0; i < 8; ++i) {
708 int ivalue = atoi(valstart);
709 verts[7 - i] = (ivalue > 0) ? (ivalue - 1) : ivalue;
710 valstart -= fieldWidth;
711 valend -= fieldWidth;
716 for(
int j = 0; j < 8; ++j)
717 debug4 <<
", " << verts[j];
720 ugrid->InsertNextCell(VTK_HEXAHEDRON, 8, verts);
722 qDebug() <<
"The file " <<
fileName()
723 <<
" contained cells "
724 "that are not hexes.";
730 char *comma = strstr(line,
",");
732 char *comma2 = strstr(comma + 1,
",");
735 char *cols = comma + 1;
736 qDebug() <<
"Coordinate data stored in " << atoi(cols)
744 ifile.getline(line, 1024);
745 if (line[0] ==
'(') {
747 fieldWidth, expectedLineLength);
748 qDebug() <<
"firstFieldWidth=" << firstFieldWidth
749 <<
", fieldStart=" << fieldStart
750 <<
", fieldWidth=" << fieldWidth
751 <<
", expectedLineLength=" << expectedLineLength;
752 readingCoordinates =
true;
754 qDebug() <<
"Malformed format string: " << line;
758 char *comma = strstr(line,
",");
760 char *comma2 = strstr(comma + 1,
",");
763 char *cols = comma + 1;
764 qDebug() <<
"Connectivity data stored in " << atoi(cols)
772 ifile.getline(line, 1024);
773 if (line[0] ==
'(') {
775 fieldWidth, expectedLineLength);
776 qDebug() <<
"firstFieldWidth=" << firstFieldWidth
777 <<
", fieldStart=" << fieldStart
778 <<
", fieldWidth=" << fieldWidth
779 <<
", expectedLineLength=" << expectedLineLength;
780 readingConnectivity =
true;
782 qDebug() <<
"Malformed format string: " << line;
786 if (
m_title.toStdString() ==
"")
798 expectedLineLength = 0;
799 readingCoordinates =
false;
800 readingConnectivity =
false;
804 if (recognized && !fatalError)
815 int &expectedLineLength)
const {
816 char *
fmt = line + 1;
819 expectedLineLength = 0;
820 bool keepGoing =
true;
824 if ((ptr = strstr(
fmt,
",")) != 0) {
827 expectedLineLength += linelen;
829 }
else if ((ptr = strstr(
fmt,
")")) != 0) {
832 expectedLineLength += linelen;
838 fieldStart = expectedLineLength - fieldWidth;
842 int &firstFieldWidth,
845 int &expectedLineLength)
const {
846 char *
fmt = line + 1;
849 expectedLineLength = 0;
850 bool keepGoing =
true;
855 if ((ptr = strstr(
fmt,
",")) != 0) {
860 firstFieldWidth = fieldWidth;
862 expectedLineLength += linelen;
864 }
else if ((ptr = strstr(
fmt,
")")) != 0) {
869 firstFieldWidth = fieldWidth;
871 expectedLineLength += linelen;
877 fieldStart = expectedLineLength - fieldWidth;
882 int &linelen)
const {
885 qDebug() <<
"avtANSYSFileFormat::Interpret: " <<
fmt;
888 if (sscanf(
fmt,
"%de%d.%d", &i0, &i1, &i2) == 3) {
889 linelen = i0 * i1 / 2;
893 else if (sscanf(
fmt,
"%di%d", &i0, &i1) == 2) {
897 qDebug() <<
"Invalid format string: " <<
fmt;
void setFileName(const QString &file)
AbstractFileReader(QObject *parent=nullptr)
void setTitle(const QString &title)
void interpretFormatString(char *line, int &fieldStart, int &fieldWidth, int &expectedLineLength) const
void interpret(const char *fmt, int &fieldWidth, int &linelen) const
AnsysFileReader(QObject *parent=nullptr)
void interpretFormatStringEx(char *line, int &firstFieldWidth, int &fieldStart, int &fieldWidth, int &expectedLineLength) const
DynaFileReader(QObject *parent=nullptr)
QMap< QString, vtkDataArray * > m_dataMap
FluentFileReader(QObject *parent=nullptr)
std::vector< int > m_matList
NastranFileReader(QObject *parent=nullptr)
std::map< int, int > m_uniqMatIds
ObjFileReader(QObject *parent=nullptr)
PdbFileReader(QObject *parent=nullptr)
PlyFileReader(QObject *parent=nullptr)
vtkDataObject * m_dataObject
vtkGenericDataObjectReader * m_reader
StlFileReader(QObject *parent=nullptr)
VtkFileReader(QObject *parent=nullptr)
static vtkUnstructuredGridRelevantPointsFilter * New()
#define SHORT_FIELD_WIDTH
#define INDEX_FIELD_WIDTH
QTextStream & endl(QTextStream &stream)
static float Getf(const char *s)
void vtkInitOnce(T **obj)
static int Geti(const char *s)