想要对原始数据进行位置操作,一定要搞清楚VtkTransform 和 SetPosition的区别啊!!!SetPosition这是改变Actor的显示位置,原始数据的位置是不变的,而VtkTransform是对原始数据进行平移,旋转的,是改变原始数据位置的,切记别入坑。
1 #include <vtkSmartPointer.h>
2 #include <vtkSphereSource.h>
3 #include <vtkPolyData.h>
4 #include <vtkTransform.h>
5 #include <vtkTransformPolyDataFilter.h>
6 #include <vtkPolyDataMapper.h>
7 #include <vtkActor.h>
8 #include <vtkProperty.h>
9 #include <vtkRenderWindow.h>
10 #include <vtkRenderer.h>
11 #include <vtkRenderWindowInteractor.h>
12
13 //如果出现错误请加上下面的声明
14 #include "vtkAutoInit.h"
15 VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
16 VTK_MODULE_INIT(vtkInteractionStyle);
17
18 int main(int, char *[])
19 {
20 // Create the polydata geometry
21
22 vtkSmartPointer<vtkSphereSource> sphereSource =
23 vtkSmartPointer<vtkSphereSource>::New();
24 sphereSource->Update();
25
26 // Set up the actor to display the untransformed polydata
27
28 vtkSmartPointer<vtkPolyDataMapper> originalMapper =
29 vtkSmartPointer<vtkPolyDataMapper>::New();
30 originalMapper->SetInputConnection(sphereSource->GetOutputPort());
31
32 vtkSmartPointer<vtkActor> originalActor =
33 vtkSmartPointer<vtkActor>::New();
34 originalActor->SetMapper(originalMapper);
35 originalActor->GetProperty()->SetColor(1, 0, 0);
36
37 // Set up the transform filter
38 vtkSmartPointer<vtkTransform> translation = vtkSmartPointer<vtkTransform>::New();
39 translation->Translate(1.0, 2.0, 3.0); //tran
40 vtkSmartPointer<vtkTransformPolyDataFilter> transformFilter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
41 transformFilter->SetInputConnection(sphereSource->GetOutputPort());
42 transformFilter->SetTransform(translation);
43 transformFilter->Update();
44 // Set up the actor to display the transformed polydata
45
46 vtkSmartPointer<vtkPolyDataMapper> transformedMapper =
47 vtkSmartPointer<vtkPolyDataMapper>::New();
48 transformedMapper->SetInputConnection(transformFilter->GetOutputPort());
49 vtkSmartPointer<vtkActor> transformedActor =
50 vtkSmartPointer<vtkActor>::New();
51 transformedActor->SetMapper(transformedMapper); transformedActor->GetProperty()->SetColor(0, 1, 0);
52
53 // Set up the rest of the visualization pipeline
54
55 vtkSmartPointer<vtkRenderer> renderer =
56 vtkSmartPointer<vtkRenderer>::New();
57 renderer->AddActor(originalActor);
58 renderer->AddActor(transformedActor); renderer->SetBackground(.3, .6, .3); // Set renderer's background color to green
59
60 vtkSmartPointer<vtkRenderWindow> renderWindow =
61 vtkSmartPointer<vtkRenderWindow>::New();
62 renderWindow->AddRenderer(renderer);
63
64 vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
65 vtkSmartPointer<vtkRenderWindowInteractor>::New();
66 renderWindowInteractor->SetRenderWindow(renderWindow)`````````````
67 ``
68 renderWindowInteractor->Start();
69
70 return EXIT_SUCCESS;
71 }