1 #include <vtkAutoInit.h>
2 VTK_MODULE_INIT(vtkRenderingOpenGL);
3 VTK_MODULE_INIT(vtkRenderingFreeType);
4 VTK_MODULE_INIT(vtkInteractionStyle);
5
6 #include <vtkSmartPointer.h>
7 #include <vtkPolyDataReader.h>
8 #include <vtkDecimatePro.h>
9 #include <vtkPolyDataMapper.h>
10 #include <vtkActor.h>
11 #include <vtkRenderer.h>
12 #include <vtkCamera.h>
13 #include <vtkRenderWindow.h>
14 #include <vtkRenderWindowInteractor.h>
15
16 int main()
17 {
18 vtkSmartPointer<vtkPolyDataReader> reader =
19 vtkSmartPointer<vtkPolyDataReader>::New();
20 reader->SetFileName("fran_cut.vtk");
21 reader->Update();
22
23 vtkSmartPointer<vtkPolyData> original = reader->GetOutput();
24 std::cout << "抽取前"<< "-----------------------" << std::endl;
25 std::cout << "模型点数为: " << original->GetNumberOfPoints() << std::endl;
26 std::cout << "模型面数为: " << original->GetNumberOfPolys() << std::endl;
27
28 vtkSmartPointer<vtkDecimatePro> decimation =
29 vtkSmartPointer<vtkDecimatePro>::New();
30 decimation->SetInputData(reader->GetOutput());
31 decimation->SetTargetReduction(0.6);
32 decimation->Update();
33
34 vtkSmartPointer<vtkPolyData> decimated = decimation->GetOutput();
35 std::cout << "抽取后"<< "-----------------------" << std::endl;
36 std::cout << "模型点数为:" << decimated->GetNumberOfPoints() << std::endl;
37 std::cout << "模型面数为:" << decimated->GetNumberOfPolys() << std::endl;
38 /
39 vtkSmartPointer<vtkPolyDataMapper> origMapper =
40 vtkSmartPointer<vtkPolyDataMapper>::New();
41 origMapper->SetInputData(reader->GetOutput());
42 vtkSmartPointer<vtkActor> origActor =
43 vtkSmartPointer<vtkActor>::New();
44 origActor->SetMapper(origMapper);
45
46 vtkSmartPointer<vtkPolyDataMapper> deciMapper =
47 vtkSmartPointer<vtkPolyDataMapper>::New();
48 deciMapper->SetInputData(decimation->GetOutput());
49 vtkSmartPointer<vtkActor> deciActor =
50 vtkSmartPointer<vtkActor>::New();
51 deciActor->SetMapper(deciMapper);
52 ///
53 double leftViewport[4] = { 0.0, 0.0, 0.5, 1.0 };
54 double rightViewport[4] = { 0.5, 0.0, 1.0, 1.0 };
55
56 vtkSmartPointer<vtkRenderer> leftRenderer =
57 vtkSmartPointer<vtkRenderer>::New();
58 leftRenderer->SetViewport(leftViewport);
59 leftRenderer->AddActor(origActor);
60 leftRenderer->SetBackground(1.0, 0, 0);
61
62 vtkSmartPointer<vtkRenderer> rightRenderer =
63 vtkSmartPointer<vtkRenderer>::New();
64 rightRenderer->SetViewport(rightViewport);
65 rightRenderer->AddActor(deciActor);
66 rightRenderer->SetBackground(0, 0, 0);
67
68 leftRenderer->GetActiveCamera()->SetPosition(0, -1, 0);
69 leftRenderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
70 leftRenderer->GetActiveCamera()->SetViewUp(0, 0, 1);
71 leftRenderer->GetActiveCamera()->Azimuth(30);
72 leftRenderer->GetActiveCamera()->Elevation(30);
73 leftRenderer->ResetCamera();//刷新照相机
74 rightRenderer->SetActiveCamera(leftRenderer->GetActiveCamera());//同步显示
75 ///
76 vtkSmartPointer<vtkRenderWindow> rw =
77 vtkSmartPointer<vtkRenderWindow>::New();
78 rw->AddRenderer(leftRenderer);
79 rw->AddRenderer(rightRenderer);
80 rw->SetSize(640, 320);
81 rw->SetWindowName("PolyData Decimation");
82
83 vtkSmartPointer<vtkRenderWindowInteractor> rwi =
84 vtkSmartPointer<vtkRenderWindowInteractor>::New();
85 rwi->SetRenderWindow(rw);
86 rwi->Start();
87
88 return 0;
89 }