当前位置:   article > 正文

VTK图形处理之颜色映射 ,vtkpolydata 数据合并_vtkcleanpolydata

vtkcleanpolydata

颜色可以直接作为一种标量属性数据,设置到相应的点或者单元数据中,这是最直接的一种图形着色方式。

颜色映射

  颜色映射的操作对象是数据集中的标量属性。它是一种常用的 标量算法。它会根据数据集中各个部分不同的标量值,对各个部分上不同的颜色。与此相关的另一种上色方法是控制演员的颜色属性,但这样整个图形只有单一的颜色,这显然没有颜色映射方法灵活。
  创建多边形数据集程序运行 结果显示的彩色立方体就用到了颜色映射。由于对 立方体的6个顶点设置了不同的标量值(点id),所以这6个点的颜色不同。至于立方体其他部分的颜色,也是通过标量值映射来的,不过这些标量值并不是手动设置,而是根据邻近的顶点的标量值,运用某种内插算法计算出来的。

需求:

将两个数据合并,并两组vtkPoint 数据显示不同色;

  1. #include <vtkSmartPointer.h>
  2. #include <vtkPolyData.h>
  3. #include <vtkFloatArray.h>
  4. #include <vtkCellData.h>
  5. #include <vtkLookupTable.h>
  6. #include <vtkPolyDataMapper.h>
  7. #include <vtkActor.h>
  8. #include <vtkProperty.h>
  9. #include <vtkRenderer.h>
  10. #include <vtkRenderWindow.h>
  11. #include <vtkRenderWindowInteractor.h>
  12. int main(){
  13. vtkSmartPointer<vtkPoints> pts = vtkSmartPointer<vtkPoints>::New();
  14. pts->InsertNextPoint(0.0, 0.0, 0.0);
  15. pts->InsertNextPoint(1.0, 0.0, 0.0);
  16. pts->InsertNextPoint(1.0, 1.0, 0.0);
  17. pts->InsertNextPoint(0.0, 1.0, 0.0);
  18. pts->InsertNextPoint(2.0, 0.0, 0.0);
  19. vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();
  20. for (int i = 0; i < 5; i++) {
  21. vtkIdType pt[1] = { i };
  22. vertices->InsertNextCell(1, pt);
  23. }
  24. vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
  25. polyData->SetPoints(pts);
  26. polyData->SetVerts(vertices);
  27. //标量属性
  28. vtkSmartPointer<vtkFloatArray> cellScalars = vtkSmartPointer<vtkFloatArray>::New();
  29. for (int i = 0; i < 9; i++) {
  30. cellScalars->InsertNextValue(i + 1); //九个索引
  31. }
  32. polyData->GetCellData()->SetScalars(cellScalars);
  33. vtkSmartPointer<vtkLookupTable> lut = vtkSmartPointer<vtkLookupTable>::New();
  34. lut->SetNumberOfTableValues(10);
  35. lut->Build();
  36. lut->SetTableValue(0, 0, 0, 0, 1);
  37. for (int i = 1; i < 6; i++)
  38. {
  39. lut->SetTableValue(i, 0.0000, 0.0, 0, 1);
  40. }
  41. vtkSmartPointer<vtkPolyDataMapper> mapper =vtkSmartPointer<vtkPolyDataMapper>::New();
  42. mapper->SetInputData(polyData);
  43. mapper->SetScalarRange(0, 5);
  44. mapper->SetLookupTable(lut);
  45. vtkSmartPointer<vtkActor> actor =vtkSmartPointer<vtkActor>::New();
  46. actor->SetMapper(mapper);
  47. actor->GetProperty()->SetPointSize(3);
  48. vtkSmartPointer<vtkRenderer> render =vtkSmartPointer<vtkRenderer>::New();
  49. render->AddActor(actor);
  50. render->SetBackground(0.0, 0.0, 0.0);
  51. vtkSmartPointer<vtkRenderWindow> rw =vtkSmartPointer<vtkRenderWindow>::New();
  52. rw->AddRenderer(render);
  53. rw->SetSize(320, 320);
  54. vtkSmartPointer<vtkRenderWindowInteractor> rwi = vtkSmartPointer<vtkRenderWindowInteractor>::New();
  55. rwi->SetRenderWindow(rw);
  56. rwi->Start();
  57. return 0;
  58. }

参考:

https://www.cnblogs.com/ybqjymy/p/13984752.html

https://blog.csdn.net/qq_34600424/article/details/106662611

 

 VTK  形状有时可能是 polydata ,当需要将两个或者多个数据进行合并显示。

可以使用类是 vtkAppendPolyData,输入多个数据,直接 update() 就可以得到想要的结果。

如果去掉重复点可以使用 vtkCleanPolyData 的对象进行基本的数据处理;

  1. #include <vtkSmartPointer.h>
  2. #include <vtkPolyData.h>
  3. #include <vtkFloatArray.h>
  4. #include <vtkCellData.h>
  5. #include <vtkLookupTable.h>
  6. #include <vtkPolyDataMapper.h>
  7. #include <vtkActor.h>
  8. #include <vtkProperty.h>
  9. #include <vtkRenderer.h>
  10. #include <vtkRenderWindow.h>
  11. #include <vtkRenderWindowInteractor.h>
  12. #include <vtkSmartPointer.h>
  13. #include <vtkLineSource.h>
  14. #include <vtkTubeFilter.h>
  15. #include <vtkConeSource.h>
  16. #include <vtkAppendPolyData.h>
  17. #include <vtkCleanPolyData.h>
  18. #include <vtkPolyDataMapper.h>
  19. #include <vtkActor.h>
  20. #include <vtkRenderer.h>
  21. #include <vtkRenderWindow.h>
  22. #include <vtkRenderWindowInteractor.h>
  23. #include <vtkPointData.h>
  24. #include <vtkLine.h>
  25. int main(){
  26. //Create a cone
  27. vtkSmartPointer<vtkConeSource> coneSource =
  28. vtkSmartPointer<vtkConeSource>::New();
  29. coneSource->SetCenter(0, 0, 0);
  30. coneSource->SetRadius(2);
  31. coneSource->SetHeight(10);
  32. coneSource->SetResolution(50);
  33. coneSource->Update();
  34. vtkIntArray *scalars = vtkIntArray::New();
  35. vtkPointData *conePointDataPointer = coneSource->GetOutput()->GetPointData();
  36. vtkPolyData *conePolyDataPointer = coneSource->GetOutput();
  37. scalars->SetNumberOfTuples(conePolyDataPointer->GetNumberOfPoints());
  38. scalars->SetNumberOfComponents(1);
  39. for (int i = 0; i < conePolyDataPointer->GetNumberOfPoints(); i++)
  40. {
  41. scalars->SetTuple1(i, 1);
  42. }
  43. conePointDataPointer->SetScalars(scalars);
  44. // second
  45. vtkSmartPointer<vtkLineSource> line = vtkSmartPointer<vtkLineSource>::New();
  46. line->SetPoint1(1.0, 0, 0);
  47. line->SetPoint2(0, 1.0, 20);
  48. vtkSmartPointer<vtkTubeFilter> tube = vtkSmartPointer<vtkTubeFilter>::New();
  49. tube->SetInputConnection(line->GetOutputPort());
  50. tube->SetRadius(2);
  51. tube->SetNumberOfSides(50);
  52. tube->SetCapping(1);
  53. tube->Update();
  54. vtkIntArray *scalarstube = vtkIntArray::New();
  55. vtkPointData *tubePointDataPointer = tube->GetOutput()->GetPointData();
  56. vtkPolyData *tubePolyDataPointer = tube->GetOutput();
  57. scalarstube->SetNumberOfTuples(tubePolyDataPointer->GetNumberOfPoints());
  58. scalarstube->SetNumberOfComponents(1);
  59. for (int i = 0; i < tubePolyDataPointer->GetNumberOfPoints(); i++)
  60. {
  61. scalarstube->SetTuple1(i, 0);
  62. }
  63. tubePointDataPointer->SetScalars(scalarstube);
  64. // combine two poly data
  65. vtkSmartPointer<vtkAppendPolyData> appendFilter =
  66. vtkSmartPointer<vtkAppendPolyData>::New();
  67. appendFilter->AddInputData(tube->GetOutput());
  68. appendFilter->AddInputData(coneSource->GetOutput());
  69. appendFilter->Update();
  70. // Remove any duplicate points.
  71. //vtkSmartPointer<vtkCleanPolyData> cleanFilter =
  72. // vtkSmartPointer<vtkCleanPolyData>::New();
  73. //cleanFilter->SetInputConnection(appendFilter->GetOutputPort());
  74. //cleanFilter->Update();
  75. //Create a mapper and actor
  76. vtkSmartPointer<vtkPolyDataMapper> appendmapper =
  77. vtkSmartPointer<vtkPolyDataMapper>::New();
  78. appendmapper->SetInputConnection(appendFilter->GetOutputPort());
  79. vtkSmartPointer<vtkActor> appendactor =
  80. vtkSmartPointer<vtkActor>::New();
  81. appendactor->SetMapper(appendmapper);
  82. //Create a renderer, render window, and interactor
  83. vtkSmartPointer<vtkRenderer> renderer =
  84. vtkSmartPointer<vtkRenderer>::New();
  85. vtkSmartPointer<vtkRenderWindow> renderWindow =
  86. vtkSmartPointer<vtkRenderWindow>::New();
  87. renderWindow->AddRenderer(renderer);
  88. vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
  89. vtkSmartPointer<vtkRenderWindowInteractor>::New();
  90. renderWindowInteractor->SetRenderWindow(renderWindow);
  91. //Add the actors to the scene
  92. renderer->AddActor(appendactor);
  93. renderer->SetBackground(.3, .2, .1); // Background color dark red
  94. //Render and interact
  95. renderWindow->Render();
  96. renderWindowInteractor->Start();
  97. return 0;
  98. }

 源:

https://blog.csdn.net/Jane_yuhui/article/details/83893218

 

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

闽ICP备14008679号