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

1.需求

根据vtkLookupTable为点设置相对应的颜色,在这里我们需要首先为点设置标量属性值。

2.代码

 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 
13 int main(){
14 
15     vtkSmartPointer<vtkPoints> pts = vtkSmartPointer<vtkPoints>::New();
16 
17     pts->InsertNextPoint(0.0, 0.0, 0.0);
18     pts->InsertNextPoint(1.0, 0.0, 0.0);
19     pts->InsertNextPoint(1.0, 1.0, 0.0);
20     pts->InsertNextPoint(0.0, 1.0, 0.0);
21     pts->InsertNextPoint(2.0, 0.0, 0.0);
22 
23     vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();
24 
25     for (int i = 0; i < 5; i++) {
26         vtkIdType pt[1] = { i };
27         vertices->InsertNextCell(1, pt);
28     }
29 
30     vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
31     polyData->SetPoints(pts);
32     polyData->SetVerts(vertices);
33 
34     //标量属性
35     vtkSmartPointer<vtkFloatArray> cellScalars = vtkSmartPointer<vtkFloatArray>::New();
36 
37     for (int i = 0; i < 9; i++)    {
38         cellScalars->InsertNextValue(i + 1); //九个索引
39     }
40 
41     polyData->GetCellData()->SetScalars(cellScalars);
42 
43     vtkSmartPointer<vtkLookupTable> lut = vtkSmartPointer<vtkLookupTable>::New();
44     lut->SetNumberOfTableValues(10);
45     lut->Build();
46     lut->SetTableValue(0, 0, 0, 0, 1);
47     lut->SetTableValue(1, 0.8900, 0.8100, 0.3400, 1);
48     lut->SetTableValue(2, 1.0000, 0.3882, 0.2784, 1);
49     lut->SetTableValue(3, 0.9608, 0.8706, 0.7020, 1);
50     lut->SetTableValue(4, 0.9020, 0.9020, 0.9804, 1);
51     lut->SetTableValue(5, 1.0000, 0.4900, 0.2500, 1);
52 
53     vtkSmartPointer<vtkPolyDataMapper> mapper =vtkSmartPointer<vtkPolyDataMapper>::New();
54     mapper->SetInputData(polyData);
55     mapper->SetScalarRange(0, 5);
56     mapper->SetLookupTable(lut);
57 
58     vtkSmartPointer<vtkActor> actor =vtkSmartPointer<vtkActor>::New();
59     actor->SetMapper(mapper);
60     actor->GetProperty()->SetPointSize(3);
61 
62     vtkSmartPointer<vtkRenderer> render =vtkSmartPointer<vtkRenderer>::New();
63     render->AddActor(actor);
64     render->SetBackground(0.0, 0.0, 0.0);
65 
66     vtkSmartPointer<vtkRenderWindow> rw =vtkSmartPointer<vtkRenderWindow>::New();
67     rw->AddRenderer(render);
68     rw->SetSize(320, 320);
69 
70     vtkSmartPointer<vtkRenderWindowInteractor> rwi = vtkSmartPointer<vtkRenderWindowInteractor>::New();
71     rwi->SetRenderWindow(rw);
72     rwi->Start();
73 
74     return 0;
75 }

3.结果
VTK vtkpolydata图形着色_缩放

如果什么也没有显示可以用鼠标滚轮进行缩放一下