| 1 | {{{#!c++ |
| 2 | // VTK Structured Grid Sample |
| 3 | // |
| 4 | // Created on Sun Mar 23 11:17:34 2014 |
| 5 | // |
| 6 | // |
| 7 | #include <iostream> |
| 8 | #include <fstream> |
| 9 | #include <vtkXMLStructuredGridWriter.h> |
| 10 | #include <vtkStructuredGrid.h> |
| 11 | #include <vtkDataSet.h> |
| 12 | #include <vtkPointData.h> |
| 13 | #include <vtkCellData.h> |
| 14 | #include <vtkCellArray.h> |
| 15 | #include <vtkDoubleArray.h> |
| 16 | |
| 17 | int main(int argc, char **argv) { |
| 18 | // Declare "ifsteam" class object |
| 19 | ifstream ifile; |
| 20 | |
| 21 | // Open a file with read-mode (default) "open" is a member function of "ifstream" |
| 22 | ifile.open("officeFlow.txt"); |
| 23 | // Check if file exists |
| 24 | if (ifile.fail()) { |
| 25 | std::cout << "file not found" << endl; |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | // Reading data from the file. ">>" is a operator of "ifstream" |
| 30 | int nx, ny, nz; |
| 31 | ifile >> nx >> ny >> nz; |
| 32 | |
| 33 | // setup VTK |
| 34 | vtkXMLStructuredGridWriter *writer = vtkXMLStructuredGridWriter::New(); |
| 35 | vtkStructuredGrid *structured_grid = vtkStructuredGrid::New(); |
| 36 | structured_grid->SetDimensions(nx, ny, nz); |
| 37 | vtkPoints *nodes = vtkPoints::New(); |
| 38 | nodes->Allocate(nx * ny * nz); |
| 39 | vtkDoubleArray* prsArray = vtkDoubleArray::New(); |
| 40 | prsArray->SetNumberOfComponents(1); |
| 41 | prsArray->SetName("Pressure"); |
| 42 | vtkDoubleArray* velArray = vtkDoubleArray::New(); |
| 43 | velArray->SetNumberOfComponents(3); |
| 44 | velArray->SetName("Velocity"); |
| 45 | |
| 46 | for (int k = 0; k < nz; k++) { |
| 47 | for (int j = 0; j < ny; j++) { |
| 48 | for (int i = 0; i < nx; i++) { |
| 49 | double x, y, z, u, v, w, p; |
| 50 | ifile >> x >> y >> z >> u >> v >> w >> p; |
| 51 | |
| 52 | nodes->InsertNextPoint(x, y, z); |
| 53 | prsArray->InsertNextTuple(&p); |
| 54 | velArray->InsertNextTuple3(u, v, w); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | // close file |
| 59 | ifile.close(); |
| 60 | |
| 61 | structured_grid->SetPoints(nodes); |
| 62 | structured_grid->GetPointData()->AddArray(velArray); |
| 63 | structured_grid->GetPointData()->AddArray(prsArray); |
| 64 | |
| 65 | #if (VTK_MAJOR_VERSION >=6) |
| 66 | writer->SetInputData(structured_grid); |
| 67 | #else |
| 68 | writer->SetInput(structured_grid); |
| 69 | #endif |
| 70 | |
| 71 | writer->SetFileName("officeFlow.vts"); |
| 72 | writer->SetDataModeToAscii(); |
| 73 | writer->Write(); |
| 74 | |
| 75 | structured_grid->Delete(); |
| 76 | writer->Delete(); |
| 77 | nodes->Delete(); |
| 78 | velArray->Delete(); |
| 79 | prsArray->Delete(); |
| 80 | } |
| 81 | }}} |