vtk代码生成网格文件
为了研究vtk文件的格式需要代码生成vtk文件,生成文件的c++代码如下:
1 int writeVtkFile() { 2 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New(); 3 points->InsertNextPoint(0.0, 0.0, 0.0); 4 points->InsertNextPoint(1.0, 0.0, 0.0); 5 points->InsertNextPoint(1.0, 1.0, 0.0); 6 points->InsertNextPoint(0.0, 1.0, 0.0); 7 8 points->InsertNextPoint(0.0, 0.0, 1.0); 9 points->InsertNextPoint(1.0, 0.0, 1.0); 10 points->InsertNextPoint(1.0, 1.0, 1.0); 11 points->InsertNextPoint(0.0, 1.0, 1.0); 12 13 14 vtkSmartPointer<vtkPolygon> polygon1 = vtkSmartPointer<vtkPolygon>::New(); 15 polygon1->GetPointIds()->SetNumberOfIds(4); 16 polygon1->GetPointIds()->SetId(0, 0); 17 polygon1->GetPointIds()->SetId(1, 1); 18 polygon1->GetPointIds()->SetId(2, 2); 19 polygon1->GetPointIds()->SetId(3, 3); 20 21 vtkSmartPointer<vtkPolygon> polygon2 = vtkSmartPointer<vtkPolygon>::New(); 22 polygon2->GetPointIds()->SetNumberOfIds(4); 23 polygon2->GetPointIds()->SetId(0, 4); 24 polygon2->GetPointIds()->SetId(1, 5); 25 polygon2->GetPointIds()->SetId(2, 6); 26 polygon2->GetPointIds()->SetId(3, 7); 27 28 vtkSmartPointer<vtkPolygon> polygon3 = vtkSmartPointer<vtkPolygon>::New(); 29 polygon3->GetPointIds()->SetNumberOfIds(4); 30 polygon3->GetPointIds()->SetId(0, 0); 31 polygon3->GetPointIds()->SetId(1, 1); 32 polygon3->GetPointIds()->SetId(2, 5); 33 polygon3->GetPointIds()->SetId(3, 4); 34 35 vtkSmartPointer<vtkPolygon> polygon4 = vtkSmartPointer<vtkPolygon>::New(); 36 polygon4->GetPointIds()->SetNumberOfIds(4); 37 polygon4->GetPointIds()->SetId(0, 1); 38 polygon4->GetPointIds()->SetId(1, 2); 39 polygon4->GetPointIds()->SetId(2, 6); 40 polygon4->GetPointIds()->SetId(3, 5); 41 42 vtkSmartPointer<vtkPolygon> polygon5 = vtkSmartPointer<vtkPolygon>::New(); 43 polygon5->GetPointIds()->SetNumberOfIds(4); 44 polygon5->GetPointIds()->SetId(0, 2); 45 polygon5->GetPointIds()->SetId(1, 3); 46 polygon5->GetPointIds()->SetId(2, 7); 47 polygon5->GetPointIds()->SetId(3, 6); 48 49 vtkSmartPointer<vtkPolygon> polygon6 = vtkSmartPointer<vtkPolygon>::New(); 50 polygon6->GetPointIds()->SetNumberOfIds(4); 51 polygon6->GetPointIds()->SetId(0, 3); 52 polygon6->GetPointIds()->SetId(1, 0); 53 polygon6->GetPointIds()->SetId(2, 4); 54 polygon6->GetPointIds()->SetId(3, 7); 55 56 57 vtkSmartPointer<vtkCellArray> polycells = vtkSmartPointer<vtkCellArray>::New(); 58 polycells->InsertNextCell(polygon1); 59 polycells->InsertNextCell(polygon2); 60 polycells->InsertNextCell(polygon3); 61 polycells->InsertNextCell(polygon4); 62 polycells->InsertNextCell(polygon5); 63 polycells->InsertNextCell(polygon6); 64 65 vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New(); 66 polyData->SetPoints(points); 67 polyData->SetPolys(polycells); 68 69 70 vtkSmartPointer<vtkDoubleArray> pressure = vtkSmartPointer<vtkDoubleArray>::New(); 71 pressure->SetNumberOfValues(8); 72 pressure->SetValue(0, 0); 73 pressure->SetValue(1, 1); 74 pressure->SetValue(2, 2); 75 pressure->SetValue(3, 3); 76 pressure->SetValue(4, 4); 77 pressure->SetValue(5, 3); 78 pressure->SetValue(6, 2); 79 pressure->SetValue(7, 1); 80 pressure->SetName("pressure"); 81 82 vtkSmartPointer<vtkDoubleArray> temperature = vtkSmartPointer<vtkDoubleArray>::New(); 83 temperature->SetNumberOfValues(6); 84 temperature->SetValue(0, 5); 85 temperature->SetValue(1, 6); 86 temperature->SetValue(2, 7); 87 temperature->SetValue(3, 8); 88 temperature->SetValue(4, 9); 89 temperature->SetValue(5, 8); 90 temperature->SetName("temperature"); 91 92 polyData->GetPointData()->AddArray(pressure); 93 polyData->GetCellData()->AddArray(temperature); 94 95 96 vtkSmartPointer<vtkPolyDataWriter> writer = vtkSmartPointer<vtkPolyDataWriter>::New(); 97 writer->SetFileName("test.vtk"); 98 writer->SetInputData(polyData); 99 writer->Write(); 100 101 return 0; 102 }
生成的vtk文件内容如下:
1 # vtk DataFile Version 5.1 2 vtk output 3 ASCII 4 DATASET POLYDATA 5 POINTS 8 float 6 0 0 0 1 0 0 1 1 0 7 0 1 0 0 0 1 1 0 1 8 1 1 1 0 1 1 9 POLYGONS 7 24 10 OFFSETS vtktypeint64 11 0 4 8 12 16 20 24 12 CONNECTIVITY vtktypeint64 13 0 1 2 3 4 5 6 7 0 14 1 5 4 1 2 6 5 2 3 15 7 6 3 0 4 7 16 CELL_DATA 6 17 FIELD FieldData 1 18 temperature 1 6 double 19 5 6 7 8 9 8 20 POINT_DATA 8 21 FIELD FieldData 1 22 pressure 1 8 double 23 0 1 2 3 4 3 2 1
备注:
CONNECTIVITY:就是上述所有 cell 的点 ID 顺序地放到一个列表里。但仅仅这样是不行的,因为不能辨认出那些点组合形成了什么cell,这就是 offsets 的功能了。
OFFSETS:每个 cell 起始点对应在 connectivity 列表中索引。如上,offsets 还多出了一个数。最后一个数,是 connectivity 的长度值。
# vtk DataFile Version 5.1
FAXINY-Step-1-2
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 5 double
0.0 0.0 0.0
1.0 0.0 0.0
1.0 1.0 0.0
0.0 1.0 0.0
0.5 0.2 1.0
CELLS 2 5
OFFSETS vtktypeint64
0 5 10
CONNECTIVITY vtktypeint64
0 1 2 3 4
CELL_TYPES 1
14
paraview软件查看节点上的属性pressure,效果如下:
paraview软件查看单元上的属性temperature,效果如下: