赞
踩
颜色可以直接作为一种标量属性数据,设置到相应的点或者单元数据中,这是最直接的一种图形着色方式。
颜色映射的操作对象是数据集中的标量属性。它是一种常用的 标量算法。它会根据数据集中各个部分不同的标量值,对各个部分上不同的颜色。与此相关的另一种上色方法是控制演员的颜色属性,但这样整个图形只有单一的颜色,这显然没有颜色映射方法灵活。
创建多边形数据集程序运行 结果显示的彩色立方体就用到了颜色映射。由于对 立方体的6个顶点设置了不同的标量值(点id),所以这6个点的颜色不同。至于立方体其他部分的颜色,也是通过标量值映射来的,不过这些标量值并不是手动设置,而是根据邻近的顶点的标量值,运用某种内插算法计算出来的。
需求:
将两个数据合并,并两组vtkPoint 数据显示不同色;
- #include <vtkSmartPointer.h>
- #include <vtkPolyData.h>
- #include <vtkFloatArray.h>
- #include <vtkCellData.h>
- #include <vtkLookupTable.h>
- #include <vtkPolyDataMapper.h>
- #include <vtkActor.h>
- #include <vtkProperty.h>
- #include <vtkRenderer.h>
- #include <vtkRenderWindow.h>
- #include <vtkRenderWindowInteractor.h>
-
- int main(){
-
- vtkSmartPointer<vtkPoints> pts = vtkSmartPointer<vtkPoints>::New();
-
- pts->InsertNextPoint(0.0, 0.0, 0.0);
- pts->InsertNextPoint(1.0, 0.0, 0.0);
- pts->InsertNextPoint(1.0, 1.0, 0.0);
- pts->InsertNextPoint(0.0, 1.0, 0.0);
- pts->InsertNextPoint(2.0, 0.0, 0.0);
-
- vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();
-
- for (int i = 0; i < 5; i++) {
- vtkIdType pt[1] = { i };
- vertices->InsertNextCell(1, pt);
- }
-
- vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
- polyData->SetPoints(pts);
- polyData->SetVerts(vertices);
-
- //标量属性
- vtkSmartPointer<vtkFloatArray> cellScalars = vtkSmartPointer<vtkFloatArray>::New();
-
- for (int i = 0; i < 9; i++) {
- cellScalars->InsertNextValue(i + 1); //九个索引
- }
-
- polyData->GetCellData()->SetScalars(cellScalars);
-
- vtkSmartPointer<vtkLookupTable> lut = vtkSmartPointer<vtkLookupTable>::New();
- lut->SetNumberOfTableValues(10);
- lut->Build();
- lut->SetTableValue(0, 0, 0, 0, 1);
-
- for (int i = 1; i < 6; i++)
- {
- lut->SetTableValue(i, 0.0000, 0.0, 0, 1);
- }
-
- vtkSmartPointer<vtkPolyDataMapper> mapper =vtkSmartPointer<vtkPolyDataMapper>::New();
- mapper->SetInputData(polyData);
- mapper->SetScalarRange(0, 5);
- mapper->SetLookupTable(lut);
-
- vtkSmartPointer<vtkActor> actor =vtkSmartPointer<vtkActor>::New();
- actor->SetMapper(mapper);
- actor->GetProperty()->SetPointSize(3);
-
- vtkSmartPointer<vtkRenderer> render =vtkSmartPointer<vtkRenderer>::New();
- render->AddActor(actor);
- render->SetBackground(0.0, 0.0, 0.0);
-
- vtkSmartPointer<vtkRenderWindow> rw =vtkSmartPointer<vtkRenderWindow>::New();
- rw->AddRenderer(render);
- rw->SetSize(320, 320);
-
- vtkSmartPointer<vtkRenderWindowInteractor> rwi = vtkSmartPointer<vtkRenderWindowInteractor>::New();
- rwi->SetRenderWindow(rw);
- rwi->Start();
-
- return 0;
- }
-
参考:
https://www.cnblogs.com/ybqjymy/p/13984752.html
https://blog.csdn.net/qq_34600424/article/details/106662611
VTK 形状有时可能是 polydata ,当需要将两个或者多个数据进行合并显示。
可以使用类是 vtkAppendPolyData,输入多个数据,直接 update() 就可以得到想要的结果。
如果去掉重复点可以使用 vtkCleanPolyData 的对象进行基本的数据处理;
- #include <vtkSmartPointer.h>
- #include <vtkPolyData.h>
- #include <vtkFloatArray.h>
- #include <vtkCellData.h>
- #include <vtkLookupTable.h>
- #include <vtkPolyDataMapper.h>
- #include <vtkActor.h>
- #include <vtkProperty.h>
- #include <vtkRenderer.h>
- #include <vtkRenderWindow.h>
- #include <vtkRenderWindowInteractor.h>
-
- #include <vtkSmartPointer.h>
- #include <vtkLineSource.h>
- #include <vtkTubeFilter.h>
- #include <vtkConeSource.h>
- #include <vtkAppendPolyData.h>
- #include <vtkCleanPolyData.h>
- #include <vtkPolyDataMapper.h>
- #include <vtkActor.h>
- #include <vtkRenderer.h>
- #include <vtkRenderWindow.h>
- #include <vtkRenderWindowInteractor.h>
- #include <vtkPointData.h>
- #include <vtkLine.h>
-
- int main(){
-
- //Create a cone
- vtkSmartPointer<vtkConeSource> coneSource =
- vtkSmartPointer<vtkConeSource>::New();
- coneSource->SetCenter(0, 0, 0);
- coneSource->SetRadius(2);
- coneSource->SetHeight(10);
- coneSource->SetResolution(50);
- coneSource->Update();
-
- vtkIntArray *scalars = vtkIntArray::New();
- vtkPointData *conePointDataPointer = coneSource->GetOutput()->GetPointData();
- vtkPolyData *conePolyDataPointer = coneSource->GetOutput();
- scalars->SetNumberOfTuples(conePolyDataPointer->GetNumberOfPoints());
- scalars->SetNumberOfComponents(1);
- for (int i = 0; i < conePolyDataPointer->GetNumberOfPoints(); i++)
- {
- scalars->SetTuple1(i, 1);
- }
- conePointDataPointer->SetScalars(scalars);
- // second
- vtkSmartPointer<vtkLineSource> line = vtkSmartPointer<vtkLineSource>::New();
- line->SetPoint1(1.0, 0, 0);
- line->SetPoint2(0, 1.0, 20);
-
- vtkSmartPointer<vtkTubeFilter> tube = vtkSmartPointer<vtkTubeFilter>::New();
- tube->SetInputConnection(line->GetOutputPort());
- tube->SetRadius(2);
- tube->SetNumberOfSides(50);
- tube->SetCapping(1);
- tube->Update();
- vtkIntArray *scalarstube = vtkIntArray::New();
- vtkPointData *tubePointDataPointer = tube->GetOutput()->GetPointData();
- vtkPolyData *tubePolyDataPointer = tube->GetOutput();
- scalarstube->SetNumberOfTuples(tubePolyDataPointer->GetNumberOfPoints());
- scalarstube->SetNumberOfComponents(1);
- for (int i = 0; i < tubePolyDataPointer->GetNumberOfPoints(); i++)
- {
- scalarstube->SetTuple1(i, 0);
- }
- tubePointDataPointer->SetScalars(scalarstube);
-
- // combine two poly data
- vtkSmartPointer<vtkAppendPolyData> appendFilter =
- vtkSmartPointer<vtkAppendPolyData>::New();
- appendFilter->AddInputData(tube->GetOutput());
- appendFilter->AddInputData(coneSource->GetOutput());
- appendFilter->Update();
- // Remove any duplicate points.
- //vtkSmartPointer<vtkCleanPolyData> cleanFilter =
- // vtkSmartPointer<vtkCleanPolyData>::New();
- //cleanFilter->SetInputConnection(appendFilter->GetOutputPort());
- //cleanFilter->Update();
-
- //Create a mapper and actor
- vtkSmartPointer<vtkPolyDataMapper> appendmapper =
- vtkSmartPointer<vtkPolyDataMapper>::New();
- appendmapper->SetInputConnection(appendFilter->GetOutputPort());
-
- vtkSmartPointer<vtkActor> appendactor =
- vtkSmartPointer<vtkActor>::New();
- appendactor->SetMapper(appendmapper);
-
-
-
- //Create a renderer, render window, and interactor
- vtkSmartPointer<vtkRenderer> renderer =
- vtkSmartPointer<vtkRenderer>::New();
- vtkSmartPointer<vtkRenderWindow> renderWindow =
- vtkSmartPointer<vtkRenderWindow>::New();
- renderWindow->AddRenderer(renderer);
- vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
- vtkSmartPointer<vtkRenderWindowInteractor>::New();
- renderWindowInteractor->SetRenderWindow(renderWindow);
-
- //Add the actors to the scene
- renderer->AddActor(appendactor);
- renderer->SetBackground(.3, .2, .1); // Background color dark red
-
- //Render and interact
- renderWindow->Render();
- renderWindowInteractor->Start();
-
-
-
- return 0;
- }
-
源:
https://blog.csdn.net/Jane_yuhui/article/details/83893218
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。