当前位置:   article > 正文

VTK-数据集vtkUnstructuredGrid

vtkunstructuredgrid

前言:本博文主要介绍vtkUnstructuredGrid的特点、结构组成,vtkUnstructuredGrid的创建方法,及其vtkUnstructuredGrid相关的接口及示例。

目录

特点

创建方法

vtkUnstructuredGrid->vtkPolyData

vtkPolyData->vtkUnstructuredGrid

vtkUnstructuredGrid相关示例


特点

非结构化网格数据,是最常见的数据集类型,它的拓扑结构和几何结构都是非结构化的,所有单元类型都可以任意组合,所有单元的拓扑结构从零维延伸至三维。如上图f所示。在VTK中,任一类型的数据集都可用非结构化网格来表达,但其储存需要大量的空间,计算时需要消耗大量的资源,除非迫不得已,一般较少使用此种类型的数据集。主要用于有限元分析,计算几何和图形表示等领域。

创建方法

  1. #include <vtkActor.h>
  2. #include <vtkCamera.h>
  3. #include <vtkCellType.h>
  4. #include <vtkDataSetMapper.h>
  5. #include <vtkNamedColors.h>
  6. #include <vtkPoints.h>
  7. #include <vtkProperty.h>
  8. #include <vtkRenderWindow.h>
  9. #include <vtkRenderWindowInteractor.h>
  10. #include <vtkRenderer.h>
  11. #include <vtkUnstructuredGrid.h>
  12. int main( int, char *[] )
  13. {
  14. int i;
  15. static double x[27][3]={{0,0,0}, {1,0,0}, {2,0,0}, {0,1,0}, {1,1,0}, {2,1,0},
  16. {0,0,1}, {1,0,1}, {2,0,1}, {0,1,1}, {1,1,1}, {2,1,1},
  17. {0,1,2}, {1,1,2}, {2,1,2}, {0,1,3}, {1,1,3}, {2,1,3},
  18. {0,1,4}, {1,1,4}, {2,1,4}, {0,1,5}, {1,1,5}, {2,1,5},
  19. {0,1,6}, {1,1,6}, {2,1,6}};
  20. static vtkIdType pts[12][8]={{0, 1, 4, 3, 6, 7, 10, 9},
  21. {1, 2, 5, 4, 7, 8, 11, 10},
  22. {6, 10, 9, 12, 0, 0, 0, 0},
  23. {8, 11, 10, 14, 0, 0, 0, 0},
  24. {16, 17, 14, 13, 12, 15, 0, 0},
  25. {18, 15, 19, 16, 20, 17, 0, 0},
  26. {22, 23, 20, 19, 0, 0, 0, 0},
  27. {21, 22, 18, 0, 0, 0, 0, 0},
  28. {22, 19, 18, 0, 0, 0, 0, 0},
  29. {23, 26, 0, 0, 0, 0, 0, 0},
  30. {21, 24, 0, 0, 0, 0, 0, 0},
  31. {25, 0, 0, 0, 0, 0, 0, 0}};
  32. vtkSmartPointer<vtkNamedColors> colors =
  33. vtkSmartPointer<vtkNamedColors>::New();
  34. vtkSmartPointer<vtkRenderer> renderer =
  35. vtkSmartPointer<vtkRenderer>::New();
  36. vtkSmartPointer<vtkRenderWindow> renWin =
  37. vtkSmartPointer<vtkRenderWindow>::New();
  38. renWin->AddRenderer(renderer);
  39. vtkSmartPointer<vtkRenderWindowInteractor> iren =
  40. vtkSmartPointer<vtkRenderWindowInteractor>::New();
  41. iren->SetRenderWindow(renWin);
  42. vtkSmartPointer<vtkPoints> points =
  43. vtkSmartPointer<vtkPoints>::New();
  44. for (i=0; i<27; i++) points->InsertPoint(i,x[i]);
  45. vtkSmartPointer<vtkUnstructuredGrid> ugrid =
  46. vtkSmartPointer<vtkUnstructuredGrid>::New();
  47. ugrid->Allocate(100);
  48. ugrid->InsertNextCell(VTK_HEXAHEDRON, 8, pts[0]);
  49. ugrid->InsertNextCell(VTK_HEXAHEDRON, 8, pts[1]);
  50. ugrid->InsertNextCell(VTK_TETRA, 4, pts[2]);
  51. ugrid->InsertNextCell(VTK_TETRA, 4, pts[3]);
  52. ugrid->InsertNextCell(VTK_POLYGON, 6, pts[4]);
  53. ugrid->InsertNextCell(VTK_TRIANGLE_STRIP, 6, pts[5]);
  54. ugrid->InsertNextCell(VTK_QUAD, 4, pts[6]);
  55. ugrid->InsertNextCell(VTK_TRIANGLE, 3, pts[7]);
  56. ugrid->InsertNextCell(VTK_TRIANGLE, 3, pts[8]);
  57. ugrid->InsertNextCell(VTK_LINE, 2, pts[9]);
  58. ugrid->InsertNextCell(VTK_LINE, 2, pts[10]);
  59. ugrid->InsertNextCell(VTK_VERTEX, 1, pts[11]);
  60. ugrid->SetPoints(points);
  61. vtkSmartPointer<vtkDataSetMapper> ugridMapper =
  62. vtkSmartPointer<vtkDataSetMapper>::New();
  63. ugridMapper->SetInputData(ugrid);
  64. vtkSmartPointer<vtkActor> ugridActor =
  65. vtkSmartPointer<vtkActor>::New();
  66. ugridActor->SetMapper(ugridMapper);
  67. ugridActor->GetProperty()->SetColor(colors->GetColor3d("Peacock").GetData());
  68. ugridActor->GetProperty()->EdgeVisibilityOn();
  69. renderer->AddActor(ugridActor);
  70. renderer->SetBackground(colors->GetColor3d("Beige").GetData());
  71. renderer->ResetCamera();
  72. renderer->GetActiveCamera()->Elevation(60.0);
  73. renderer->GetActiveCamera()->Azimuth(30.0);
  74. renderer->GetActiveCamera()->Dolly(1.2);
  75. renWin->SetSize(640, 480);
  76. // interact with data
  77. renWin->Render();
  78. iren->Start();
  79. return EXIT_SUCCESS;
  80. }

vtkUnstructuredGrid->vtkPolyData

  1. vtkUnstructuredGrid* unSGrid = ...
  2. vtkDataSetSurfaceFilter * filter = vtkDataSetSurfaceFilter::New();
  3. filter->SetInputData(unSGrid);
  4. filter->Update();
  5. vtkPolyData* pd = filter->GetOutput();

        使用vtkGeometryFilter方式生成,参考样例:PolyData中的GeometryFilter.

  1. vtkSmartPointer<vtkUnstructuredGrid> unstructuredGrid =
  2. vtkSmartPointer<vtkUnstructuredGrid>::New();
  3. vtkSmartPointer<vtkGeometryFilter> geometryFilter =
  4. vtkSmartPointer<vtkGeometryFilter>::New();
  5. geometryFilter->SetInputData(unstructuredGrid);
  6. geometryFilter->Update();
  7. vtkPolyData* polydata = geometryFilter->GetOutput();
  8. std::cout << "Output has " << polydata->GetNumberOfPoints() << " points." << std::endl;

 

vtkPolyData->vtkUnstructuredGrid

  1. vtkUnstructuredGrid* zxCommonMethod::polyDataToUnstructuredGrid(vtkPolyData* pd)
  2. {
  3. if (pd == nullptr) return nullptr;
  4. vtkPoints* inPts = pd->GetPoints();
  5. vtkIdType numPts = pd->GetNumberOfPoints();
  6. vtkCellArray* vertexs = pd->GetVerts();
  7. vtkCellArray* lines = pd->GetLines();
  8. vtkCellArray* polys = pd->GetPolys();
  9. vtkCellArray* strips = pd->GetStrips();
  10. vtkIdType numVertexs = pd->GetNumberOfVerts();
  11. vtkIdType numLines = pd->GetNumberOfLines();
  12. vtkIdType numPolys = pd->GetNumberOfPolys();
  13. vtkIdType numStrips = pd->GetNumberOfStrips();
  14. vtkIdType numCells = pd->GetNumberOfCells();
  15. if (numPts < 1 || (numVertexs + numLines + numPolys + numStrips) < 1) return nullptr;
  16. vtkUnstructuredGrid* uGrid = vtkUnstructuredGrid::New();
  17. uGrid->SetPoints(inPts);
  18. const vtkIdType* pts = nullptr;
  19. vtkIdType npts;
  20. for (vtkIdType id = 0; id < numCells; id++)
  21. {
  22. pd->GetCellPoints(id, npts, pts);
  23. uGrid->InsertNextCell(pd->GetCellType(id), npts, pts);
  24. }
  25. return uGrid;
  26. }

vtkUnstructuredGrid相关示例

实例1:ClipUnstructuredGridWithPlane

该实例使用vtkTableBasedClipDataSet对vtkUnstructuredGrid数据集进行Clip。与其它Clip相关的Filter不同,vtkTableBasedClipDataSet在未Clip的部分保留原来的Cells。

输入数据:treemesh.vtk

  1. #include <vtkSmartPointer.h>
  2. #include <vtkTableBasedClipDataSet.h>
  3. #include <vtkUnstructuredGridReader.h>
  4. #include <vtkUnstructuredGrid.h>
  5. #include <vtkPlane.h>
  6. #include <vtkTransform.h>
  7. #include <vtkActor.h>
  8. #include <vtkCamera.h>
  9. #include <vtkCellTypes.h>
  10. #include <vtkDataSetMapper.h>
  11. #include <vtkLookupTable.h>
  12. #include <vtkProperty.h>
  13. #include <vtkRenderWindow.h>
  14. #include <vtkRenderWindowInteractor.h>
  15. #include <vtkRenderer.h>
  16. #include <vtkNamedColors.h>
  17. int main(int argc, char *argv[])
  18. {
  19. if (argc < 2)
  20. {
  21. std::cout << "Usage: " << argv[0] << " filename.vtk" << std::endl;
  22. return EXIT_FAILURE;
  23. }
  24. // Create the reader for the data.
  25. std::string filename = argv[1];
  26. std::cout << "Loading " << filename.c_str() << std::endl;
  27. auto reader =
  28. vtkSmartPointer<vtkUnstructuredGridReader>::New();
  29. reader->SetFileName(filename.c_str());
  30. reader->Update();
  31. double bounds[6];
  32. reader->GetOutput()->GetBounds(bounds);
  33. double center[3];
  34. reader->GetOutput()->GetCenter(center);
  35. auto colors =
  36. vtkSmartPointer<vtkNamedColors>::New();
  37. auto renderer = vtkSmartPointer<vtkRenderer>::New();
  38. renderer->SetBackground(colors->GetColor3d("Wheat").GetData());
  39. renderer->UseHiddenLineRemovalOn();
  40. auto renderWindow =
  41. vtkSmartPointer<vtkRenderWindow>::New();
  42. renderWindow->AddRenderer(renderer);
  43. renderWindow->SetSize(640, 480);
  44. auto interactor =
  45. vtkSmartPointer<vtkRenderWindowInteractor>::New();
  46. interactor->SetRenderWindow(renderWindow);
  47. double xnorm[3] = {-1.0, -1.0, 1.0};
  48. auto clipPlane = vtkSmartPointer<vtkPlane>::New();
  49. clipPlane->SetOrigin(reader->GetOutput()->GetCenter());
  50. clipPlane->SetNormal(xnorm);
  51. auto clipper =
  52. vtkSmartPointer<vtkTableBasedClipDataSet>::New();
  53. clipper->SetClipFunction(clipPlane);
  54. clipper->SetInputData(reader->GetOutput());
  55. clipper->SetValue(0.0);
  56. clipper->GenerateClippedOutputOn();
  57. clipper->Update();
  58. auto insideMapper =
  59. vtkSmartPointer<vtkDataSetMapper>::New();
  60. insideMapper->SetInputData(clipper->GetOutput());
  61. insideMapper->ScalarVisibilityOff();
  62. auto insideActor =
  63. vtkSmartPointer<vtkActor>::New();
  64. insideActor->SetMapper(insideMapper);
  65. insideActor->GetProperty()->SetDiffuseColor(colors->GetColor3d("banana").GetData());
  66. insideActor->GetProperty()->SetAmbient(.3);
  67. insideActor->GetProperty()->EdgeVisibilityOn();
  68. auto clippedMapper =
  69. vtkSmartPointer<vtkDataSetMapper>::New();
  70. clippedMapper->SetInputData(clipper->GetClippedOutput());
  71. clippedMapper->ScalarVisibilityOff();
  72. auto clippedActor =
  73. vtkSmartPointer<vtkActor>::New();
  74. clippedActor->SetMapper(clippedMapper);
  75. clippedActor->GetProperty()->SetDiffuseColor(colors->GetColor3d("tomato").GetData());
  76. insideActor->GetProperty()->SetAmbient(.3);
  77. clippedActor->GetProperty()->EdgeVisibilityOn();
  78. // Create transforms to make a better visualization
  79. auto insideTransform = vtkSmartPointer<vtkTransform>::New();
  80. insideTransform->Translate(-(bounds[1] - bounds[0]) * .75, 0, 0);
  81. insideTransform->Translate(center[0], center[1], center[2]);
  82. insideTransform->RotateY(-120.0);
  83. insideTransform->Translate(-center[0], -center[1], -center[2]);
  84. insideActor->SetUserTransform(insideTransform);
  85. auto clippedTransform = vtkSmartPointer<vtkTransform>::New();
  86. clippedTransform->Translate((bounds[1] - bounds[0]) * .75, 0, 0);
  87. clippedTransform->Translate(center[0], center[1], center[2]);
  88. clippedTransform->RotateY(60.0);
  89. clippedTransform->Translate(-center[0], -center[1], -center[2]);
  90. clippedActor->SetUserTransform(clippedTransform);
  91. renderer->AddViewProp(clippedActor);
  92. renderer->AddViewProp(insideActor);
  93. renderer->ResetCamera();
  94. renderer->GetActiveCamera()->Dolly(1.4);
  95. renderer->ResetCameraClippingRange();
  96. renderWindow->Render();
  97. renderWindow->SetWindowName("ClipUnstructuredGridWithPlane");
  98. renderWindow->Render();
  99. interactor->Start();
  100. // Generate a report
  101. vtkIdType numberOfCells = clipper->GetOutput()->GetNumberOfCells();
  102. std::cout << "------------------------" << std::endl;
  103. std::cout << "The inside dataset contains a " << std::endl
  104. << clipper->GetOutput()->GetClassName()
  105. << " that has " << numberOfCells << " cells" << std::endl;
  106. typedef std::map<int,int> CellContainer;
  107. CellContainer cellMap;
  108. for (vtkIdType i = 0; i < numberOfCells; i++)
  109. {
  110. cellMap[clipper->GetOutput()->GetCellType(i)]++;
  111. }
  112. for (auto c : cellMap)
  113. {
  114. std::cout << "\tCell type "
  115. << vtkCellTypes::GetClassNameFromTypeId(c.first)
  116. << " occurs " << c.second << " times." << std::endl;
  117. }
  118. numberOfCells = clipper->GetClippedOutput()->GetNumberOfCells();
  119. std::cout << "------------------------" << std::endl;
  120. std::cout << "The clipped dataset contains a " << std::endl
  121. << clipper->GetClippedOutput()->GetClassName()
  122. << " that has " << numberOfCells << " cells" << std::endl;
  123. typedef std::map<int,int> OutsideCellContainer;
  124. CellContainer outsideCellMap;
  125. for (vtkIdType i = 0; i < numberOfCells; i++)
  126. {
  127. outsideCellMap[clipper->GetClippedOutput()->GetCellType(i)]++;
  128. }
  129. for (auto c : outsideCellMap)
  130. {
  131. std::cout << "\tCell type "
  132. << vtkCellTypes::GetClassNameFromTypeId(c.first)
  133. << " occurs " << c.second << " times." << std::endl;
  134. }
  135. return EXIT_SUCCESS;
  136. }

实例2:ClipUnstructuredGridWithPlane2

该实例使用vtkClipDataSet对vtkUnstructuredGrid数据集进行Clip。vtkClipDataSet在未Clip的部位不会保留原有的Cells。与实例1比较,该实例在未Clip的区域将原有的vtkHexahedron转换成了vtkTetra。该实例的结果vtkUnstructuredGrid数据集的Cells数量比之前增加了四倍。

输入:treemesh.vtk

  1. #include <vtkSmartPointer.h>
  2. #include <vtkClipDataSet.h>
  3. #include <vtkUnstructuredGridReader.h>
  4. #include <vtkUnstructuredGrid.h>
  5. #include <vtkPlane.h>
  6. #include <vtkTransform.h>
  7. #include <vtkActor.h>
  8. #include <vtkCamera.h>
  9. #include <vtkCellTypes.h>
  10. #include <vtkDataSetMapper.h>
  11. #include <vtkLookupTable.h>
  12. #include <vtkProperty.h>
  13. #include <vtkRenderWindow.h>
  14. #include <vtkRenderWindowInteractor.h>
  15. #include <vtkRenderer.h>
  16. #include <vtkNamedColors.h>
  17. int main(int argc, char *argv[])
  18. {
  19. if (argc < 2)
  20. {
  21. std::cout << "Usage: " << argv[0] << " filename.vtk" << std::endl;
  22. return EXIT_FAILURE;
  23. }
  24. // Create the reader for the data.
  25. std::string filename = argv[1];
  26. std::cout << "Loading " << filename.c_str() << std::endl;
  27. auto reader =
  28. vtkSmartPointer<vtkUnstructuredGridReader>::New();
  29. reader->SetFileName(filename.c_str());
  30. reader->Update();
  31. double bounds[6];
  32. reader->GetOutput()->GetBounds(bounds);
  33. double center[3];
  34. reader->GetOutput()->GetCenter(center);
  35. auto colors =
  36. vtkSmartPointer<vtkNamedColors>::New();
  37. auto renderer = vtkSmartPointer<vtkRenderer>::New();
  38. renderer->SetBackground(colors->GetColor3d("Wheat").GetData());
  39. renderer->UseHiddenLineRemovalOn();
  40. auto renderWindow =
  41. vtkSmartPointer<vtkRenderWindow>::New();
  42. renderWindow->AddRenderer(renderer);
  43. renderWindow->SetSize(640, 480);
  44. auto interactor =
  45. vtkSmartPointer<vtkRenderWindowInteractor>::New();
  46. interactor->SetRenderWindow(renderWindow);
  47. double xnorm[3] = {-1.0, -1.0, 1.0};
  48. auto clipPlane = vtkSmartPointer<vtkPlane>::New();
  49. clipPlane->SetOrigin(reader->GetOutput()->GetCenter());
  50. clipPlane->SetNormal(xnorm);
  51. auto clipper =
  52. vtkSmartPointer<vtkClipDataSet>::New();
  53. clipper->SetClipFunction(clipPlane);
  54. clipper->SetInputData(reader->GetOutput());
  55. clipper->SetValue(0.0);
  56. clipper->GenerateClippedOutputOn();
  57. clipper->Update();
  58. auto insideMapper =
  59. vtkSmartPointer<vtkDataSetMapper>::New();
  60. insideMapper->SetInputData(clipper->GetOutput());
  61. insideMapper->ScalarVisibilityOff();
  62. auto insideActor =
  63. vtkSmartPointer<vtkActor>::New();
  64. insideActor->SetMapper(insideMapper);
  65. insideActor->GetProperty()->SetDiffuseColor(colors->GetColor3d("banana").GetData());
  66. insideActor->GetProperty()->SetAmbient(.3);
  67. insideActor->GetProperty()->EdgeVisibilityOn();
  68. auto clippedMapper =
  69. vtkSmartPointer<vtkDataSetMapper>::New();
  70. clippedMapper->SetInputData(clipper->GetClippedOutput());
  71. clippedMapper->ScalarVisibilityOff();
  72. auto clippedActor =
  73. vtkSmartPointer<vtkActor>::New();
  74. clippedActor->SetMapper(clippedMapper);
  75. clippedActor->GetProperty()->SetDiffuseColor(colors->GetColor3d("tomato").GetData());
  76. insideActor->GetProperty()->SetAmbient(.3);
  77. clippedActor->GetProperty()->EdgeVisibilityOn();
  78. // Create transforms to make a better visualization
  79. auto insideTransform = vtkSmartPointer<vtkTransform>::New();
  80. insideTransform->Translate(-(bounds[1] - bounds[0]) * .75, 0, 0);
  81. insideTransform->Translate(center[0], center[1], center[2]);
  82. insideTransform->RotateY(-120.0);
  83. insideTransform->Translate(-center[0], -center[1], -center[2]);
  84. insideActor->SetUserTransform(insideTransform);
  85. auto clippedTransform = vtkSmartPointer<vtkTransform>::New();
  86. clippedTransform->Translate((bounds[1] - bounds[0]) * .75, 0, 0);
  87. clippedTransform->Translate(center[0], center[1], center[2]);
  88. clippedTransform->RotateY(60.0);
  89. clippedTransform->Translate(-center[0], -center[1], -center[2]);
  90. clippedActor->SetUserTransform(clippedTransform);
  91. renderer->AddViewProp(clippedActor);
  92. renderer->AddViewProp(insideActor);
  93. renderer->ResetCamera();
  94. renderer->GetActiveCamera()->Dolly(1.4);
  95. renderer->ResetCameraClippingRange();
  96. renderWindow->Render();
  97. renderWindow->SetWindowName("ClipUnstructuredGridWithPlane2");
  98. renderWindow->Render();
  99. interactor->Start();
  100. // Generate a report
  101. vtkIdType numberOfCells = clipper->GetOutput()->GetNumberOfCells();
  102. std::cout << "------------------------" << std::endl;
  103. std::cout << "The inside dataset contains a " << std::endl
  104. << clipper->GetOutput()->GetClassName()
  105. << " that has " << numberOfCells << " cells" << std::endl;
  106. typedef std::map<int,int> CellContainer;
  107. CellContainer cellMap;
  108. for (vtkIdType i = 0; i < numberOfCells; i++)
  109. {
  110. cellMap[clipper->GetOutput()->GetCellType(i)]++;
  111. }
  112. for (auto c : cellMap)
  113. {
  114. std::cout << "\tCell type "
  115. << vtkCellTypes::GetClassNameFromTypeId(c.first)
  116. << " occurs " << c.second << " times." << std::endl;
  117. }
  118. numberOfCells = clipper->GetClippedOutput()->GetNumberOfCells();
  119. std::cout << "------------------------" << std::endl;
  120. std::cout << "The clipped dataset contains a " << std::endl
  121. << clipper->GetClippedOutput()->GetClassName()
  122. << " that has " << numberOfCells << " cells" << std::endl;
  123. typedef std::map<int,int> OutsideCellContainer;
  124. CellContainer outsideCellMap;
  125. for (vtkIdType i = 0; i < numberOfCells; i++)
  126. {
  127. outsideCellMap[clipper->GetClippedOutput()->GetCellType(i)]++;
  128. }
  129. for (auto c : outsideCellMap)
  130. {
  131. std::cout << "\tCell type "
  132. << vtkCellTypes::GetClassNameFromTypeId(c.first)
  133. << " occurs " << c.second << " times." << std::endl;
  134. }
  135. return EXIT_SUCCESS;
  136. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/996079
推荐阅读
相关标签
  

闽ICP备14008679号