当前位置:   article > 正文

VTK基础教程(1)- vtkPolyData 介绍

vtk

目录

VTK的编译与环境搭建

VTK一般渲染流程

VTK数据类型-vtkPolyData

使用vtk自带的多边形数据

 使用自己创建的数据

添加顶点颜色

添加面颜色


VTK的编译与环境搭建

这部分目前已经有了大佬做了成熟的傻瓜式配置,大家可以参考这篇文章:VTK+VS2019(或其他版本均可) 傻瓜式一站式 配置_Nancy_fairy的博客-CSDN博客_vs2019 vtk

VTK一般渲染流程

这部分的介绍也很多,大家可以参考这篇文章:VTK笔记-了解VTK_黑山老妖的博客-CSDN博客_vtk应用领域最近,重新学习了VTK相关内容,网上资料质量参差不齐,自己从多处搜集资料,整理记录之;VTK简介Vtk,(visualization toolkit)是一个开源的免费软件系统,主要用于三维计算机图形学、图像处理和可视化。Vtk是在面向对象原理的基础上设计和实现的,它的内核是用C++构建的,包含有大约250,000行代码,2000多个类,还包含有几个转换界面,因此也可以自由的通过Java,Tcl/Tk和Python各种语言使用vtk。视觉化工具函式库(VTK,Visualization Toolkit)https://blog.csdn.net/liushao1031177/article/details/115503446

VTK数据类型-vtkPolyData

多边形数据集主要由顶点、多顶点、线、折线和三角形地带等单元组成。
顶点、线和多边形构成了用来表达0、1和2维几何图形的基本要素的最小集合。特别是三角形带,表达N个三角形仅需N+2个点,且大多数图形渲染三角形带速度极快。


使用vtk自带的多边形数据

vtk自带很多的成熟的多边形数据,包括圆柱,球体,锥体等。我们仅需设置几个属性就可以使用现成的数据创建模型并显示。以下为创建显示锥体的代码

  1. void SimpleVtk()
  2. {
  3. vtkConeSource* source = vtkConeSource::New();
  4. source->SetHeight(13);
  5. source->SetRadius(5);
  6. vtkPolyDataMapper* coneMapper = vtkPolyDataMapper::New();
  7. coneMapper->SetInputConnection(source->GetOutputPort());
  8. vtkActor* coneActor = vtkActor::New();
  9. coneActor->SetMapper(coneMapper);
  10. vtkRenderer* render = vtkRenderer::New();
  11. vtkRenderWindow* renwin = vtkRenderWindow::New();
  12. vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
  13. render->AddActor(coneActor);
  14. render->SetBackground(0.8, 0.4, 0.4);
  15. renwin->AddRenderer(render);
  16. iren->SetRenderWindow(renwin);
  17. iren->Initialize();
  18. iren->Start();
  19. source->Delete();
  20. coneMapper->Delete();
  21. coneActor->Delete();
  22. render->Delete();
  23. renwin->Delete();
  24. iren->Delete();
  25. }

首先创建锥体资源,并设置其属性,紧接着将其传递给映射器,映射器将结果传递给渲染组件进行渲染显示。以下为运行的结果:

 使用自己创建的数据

当然有时候默认的模型会不够用,我们想要自己创建模型。以下为创建一个四面体的代码:

  1. void makePolyData()
  2. {
  3. vtkPoints* points = vtkPoints::New();
  4. points->InsertNextPoint(0.0, 0.0, 0.0);
  5. points->InsertNextPoint(1.0, 0.0, 0.0);
  6. points->InsertNextPoint(0.0, 1.0, 0.0);
  7. points->InsertNextPoint(0.0, 0.0, 1.0);
  8. vtkTriangle* tran1 = vtkTriangle::New();
  9. tran1->GetPointIds()->SetId(0, 0);
  10. tran1->GetPointIds()->SetId(1, 1);
  11. tran1->GetPointIds()->SetId(2, 2);
  12. vtkTriangle* tran2 = vtkTriangle::New();
  13. tran2->GetPointIds()->SetId(0, 0);
  14. tran2->GetPointIds()->SetId(1, 1);
  15. tran2->GetPointIds()->SetId(2, 3);
  16. vtkTriangle* tran3 = vtkTriangle::New();
  17. tran3->GetPointIds()->SetId(0, 0);
  18. tran3->GetPointIds()->SetId(1, 2);
  19. tran3->GetPointIds()->SetId(2, 3);
  20. vtkTriangle* tran4 = vtkTriangle::New();
  21. tran4->GetPointIds()->SetId(0, 1);
  22. tran4->GetPointIds()->SetId(1, 2);
  23. tran4->GetPointIds()->SetId(2, 3);
  24. vtkCellArray* cells = vtkCellArray::New();
  25. cells->InsertNextCell(tran1);
  26. cells->InsertNextCell(tran2);
  27. cells->InsertNextCell(tran3);
  28. cells->InsertNextCell(tran4);
  29. vtkPolyData* source = vtkPolyData::New();
  30. source->SetPoints(points);
  31. source->SetPolys(cells);
  32. vtkPolyDataMapper* coneMapper = vtkPolyDataMapper::New();
  33. coneMapper->SetInputData(source);
  34. vtkActor* coneActor = vtkActor::New();
  35. coneActor->SetMapper(coneMapper);
  36. vtkRenderer* render = vtkRenderer::New();
  37. vtkRenderWindow* renwin = vtkRenderWindow::New();
  38. vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
  39. render->AddActor(coneActor);
  40. render->SetBackground(0.8, 0.4, 0.4);
  41. renwin->AddRenderer(render);
  42. iren->SetRenderWindow(renwin);
  43. iren->Initialize();
  44. iren->Start();
  45. points->Delete();
  46. tran1->Delete();
  47. tran2->Delete();
  48. tran3->Delete();
  49. tran4->Delete();
  50. cells->Delete();
  51. source->Delete();
  52. coneMapper->Delete();
  53. coneActor->Delete();
  54. render->Delete();
  55. renwin->Delete();
  56. iren->Delete();
  57. }

首先创建点资源,并通过InsertNextPoint方法添加坐标点,其次创建四面体的四个三角形,通过GetPointIds()->SetId()方法设置点的索引,然后再将四个三角形通过InsertNextCell方法添加进多边形单元中。再然后将点资源和多边形单元添加到vtkPolyData中,将Data置入渲染流程即可显示这个四面体。以下为输出的结果:

 很显然这种生成方式略显繁琐,vtk可以添加三角形带进入多边形单元中。一般情况下的3D模型都是由三角形带组成的,且三角形带非常节约资源且渲染速度快。以下为三角形带的代码:

  1. void makePolyData2()
  2. {
  3. vtkPoints* points = vtkPoints::New();
  4. points->InsertNextPoint(0.0, 0.0, 0.0);
  5. points->InsertNextPoint(1.0, 0.0, 0.0);
  6. points->InsertNextPoint(0.0, 1.0, 0.0);
  7. points->InsertNextPoint(0.0, 0.0, 1.0);
  8. vtkCellArray* array = vtkCellArray::New();
  9. array->InsertNextCell(6);
  10. array->InsertCellPoint(1);
  11. array->InsertCellPoint(3);
  12. array->InsertCellPoint(0);
  13. array->InsertCellPoint(2);
  14. array->InsertCellPoint(1);
  15. array->InsertCellPoint(3);
  16. vtkPolyData* source = vtkPolyData::New();
  17. source->SetPoints(points);
  18. source->SetStrips(array);
  19. vtkPolyDataMapper* coneMapper = vtkPolyDataMapper::New();
  20. coneMapper->SetInputData(source);
  21. vtkActor* coneActor = vtkActor::New();
  22. coneActor->SetMapper(coneMapper);
  23. vtkRenderer* render = vtkRenderer::New();
  24. vtkRenderWindow* renwin = vtkRenderWindow::New();
  25. vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
  26. render->AddActor(coneActor);
  27. render->SetBackground(0.8, 0.4, 0.4);
  28. renwin->AddRenderer(render);
  29. iren->SetRenderWindow(renwin);
  30. iren->Initialize();
  31. iren->Start();
  32. points->Delete();
  33. array->Delete();
  34. source->Delete();
  35. coneMapper->Delete();
  36. coneActor->Delete();
  37. render->Delete();
  38. renwin->Delete();
  39. iren->Delete();
  40. }

通过InsertCellPoint方法将点的索引添加进三角带中,在资源中使用SetStrips添加三角带。以下为渲染结果:

添加顶点颜色

 既然我们已经成功的生成了四面体,但他是灰色的,显然不是很好看。以下为添加颜色显示的代码,首先是添加顶点颜色:

  1. void makePolyDataAndSetColor()
  2. {
  3. unsigned char red[3] = { 255, 0, 0 };
  4. unsigned char green[3] = { 0, 255, 0 };
  5. unsigned char blue[3] = { 0, 0, 255 };
  6. vtkPoints* points = vtkPoints::New();
  7. points->InsertNextPoint(0.0, 0.0, 0.0);
  8. points->InsertNextPoint(1.0, 0.0, 0.0);
  9. points->InsertNextPoint(0.0, 1.0, 0.0);
  10. points->InsertNextPoint(0.0, 0.0, 1.0);
  11. vtkCellArray* array = vtkCellArray::New();
  12. array->InsertNextCell(6);
  13. array->InsertCellPoint(1);
  14. array->InsertCellPoint(3);
  15. array->InsertCellPoint(0);
  16. array->InsertCellPoint(2);
  17. array->InsertCellPoint(1);
  18. array->InsertCellPoint(3);
  19. vtkUnsignedCharArray* pointsColors = vtkUnsignedCharArray::New();
  20. pointsColors->SetNumberOfComponents(3);
  21. pointsColors->InsertNextTypedTuple(red);
  22. pointsColors->InsertNextTypedTuple(green);
  23. pointsColors->InsertNextTypedTuple(blue);
  24. pointsColors->InsertNextTypedTuple(red);
  25. vtkPolyData* source = vtkPolyData::New();
  26. source->SetPoints(points);
  27. source->SetStrips(array);
  28. source->GetPointData()->SetScalars(pointsColors);
  29. vtkPolyDataMapper* coneMapper = vtkPolyDataMapper::New();
  30. coneMapper->SetInputData(source);
  31. vtkActor* coneActor = vtkActor::New();
  32. coneActor->SetMapper(coneMapper);
  33. vtkRenderer* render = vtkRenderer::New();
  34. vtkRenderWindow* renwin = vtkRenderWindow::New();
  35. vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
  36. render->AddActor(coneActor);
  37. render->SetBackground(0.8, 0.4, 0.4);
  38. renwin->AddRenderer(render);
  39. iren->SetRenderWindow(renwin);
  40. iren->Initialize();
  41. iren->Start();
  42. points->Delete();
  43. array->Delete();
  44. pointsColors->Delete();
  45. source->Delete();
  46. coneMapper->Delete();
  47. coneActor->Delete();
  48. render->Delete();
  49. renwin->Delete();
  50. iren->Delete();
  51. }

新建一个unsignedchar数组作为颜色资源,将颜色通过InsertNextTypedTuple()方法加入到颜色资源中。然后将顶点颜色资源通过GetPointData()->SetScalars()方法加入到polyData中。以下为渲染结果:

添加面颜色

当然我们也可以给每个面添加纯色:

  1. void makePolyDataAndSetColor2()
  2. {
  3. unsigned char red[3] = { 255, 0, 0 };
  4. unsigned char green[3] = { 0, 255, 0 };
  5. unsigned char blue[3] = { 0, 0, 255 };
  6. vtkPoints* points = vtkPoints::New();
  7. points->InsertNextPoint(0.0, 0.0, 0.0);
  8. points->InsertNextPoint(1.0, 0.0, 0.0);
  9. points->InsertNextPoint(0.0, 1.0, 0.0);
  10. points->InsertNextPoint(0.0, 0.0, 1.0);
  11. vtkTriangle* tran1 = vtkTriangle::New();
  12. tran1->GetPointIds()->SetId(0, 0);
  13. tran1->GetPointIds()->SetId(1, 1);
  14. tran1->GetPointIds()->SetId(2, 2);
  15. vtkTriangle* tran2 = vtkTriangle::New();
  16. tran2->GetPointIds()->SetId(0, 0);
  17. tran2->GetPointIds()->SetId(1, 1);
  18. tran2->GetPointIds()->SetId(2, 3);
  19. vtkTriangle* tran3 = vtkTriangle::New();
  20. tran3->GetPointIds()->SetId(0, 0);
  21. tran3->GetPointIds()->SetId(1, 2);
  22. tran3->GetPointIds()->SetId(2, 3);
  23. vtkTriangle* tran4 = vtkTriangle::New();
  24. tran4->GetPointIds()->SetId(0, 1);
  25. tran4->GetPointIds()->SetId(1, 2);
  26. tran4->GetPointIds()->SetId(2, 3);
  27. vtkCellArray* cells = vtkCellArray::New();
  28. cells->InsertNextCell(tran1);
  29. cells->InsertNextCell(tran2);
  30. cells->InsertNextCell(tran3);
  31. cells->InsertNextCell(tran4);
  32. vtkUnsignedCharArray* cellColors = vtkUnsignedCharArray::New();
  33. cellColors->SetNumberOfComponents(3);
  34. cellColors->InsertNextTypedTuple(red);
  35. cellColors->InsertNextTypedTuple(green);
  36. cellColors->InsertNextTypedTuple(blue);
  37. cellColors->InsertNextTypedTuple(red);
  38. vtkPolyData* source = vtkPolyData::New();
  39. source->SetPoints(points);
  40. source->SetPolys(cells);
  41. source->GetCellData()->SetScalars(cellColors);
  42. vtkPolyDataMapper* coneMapper = vtkPolyDataMapper::New();
  43. coneMapper->SetInputData(source);
  44. vtkActor* coneActor = vtkActor::New();
  45. coneActor->SetMapper(coneMapper);
  46. vtkRenderer* render = vtkRenderer::New();
  47. vtkRenderWindow* renwin = vtkRenderWindow::New();
  48. vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
  49. render->AddActor(coneActor);
  50. render->SetBackground(0.8, 0.4, 0.4);
  51. renwin->AddRenderer(render);
  52. iren->SetRenderWindow(renwin);
  53. iren->Initialize();
  54. iren->Start();
  55. points->Delete();
  56. cellColors->Delete();
  57. tran1->Delete();
  58. tran2->Delete();
  59. tran3->Delete();
  60. tran4->Delete();
  61. cells->Delete();
  62. source->Delete();
  63. coneMapper->Delete();
  64. coneActor->Delete();
  65. render->Delete();
  66. renwin->Delete();
  67. iren->Delete();
  68. }

 这次用的是GetCellData()->SetScalars()方法将面颜色传递给各个面。渲染结果如下:

 大家也可以试试同时添加顶点颜色和面颜色是什么效果。

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

闽ICP备14008679号