一杯清酒邀明月
天下本无事,庸人扰之而烦耳。

 1 #include <vtkAutoInit.h>
 2 VTK_MODULE_INIT(vtkRenderingOpenGL2);
 3 VTK_MODULE_INIT(vtkRenderingFreeType);
 4 VTK_MODULE_INIT(vtkInteractionStyle);
 5 
 6 #include <vtkPolyData.h>
 7 #include <vtkSphereSource.h>
 8 #include <vtkDecimatePro.h>
 9 #include <vtkQuadricDecimation.h>
10 #include <vtkQuadricClustering.h>
11 #include <vtkSmartPointer.h>
12 #include <vtkPolyDataMapper.h>
13 #include <vtkProperty.h>
14 #include <vtkRenderWindow.h>
15 #include <vtkRenderWindowInteractor.h>
16 #include <vtkRenderer.h>
17 #include <vtkPolydataReader.h>
18 #include <vtkCamera.h>
19 
20 //测试文件:../data/fran_cut.vtk
21 int main(int argc, char * argv[])
22 {
23     vtkSmartPointer<vtkPolyDataReader> reader = 
24         vtkSmartPointer<vtkPolyDataReader>::New();
25     reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第六章_图像处理\\data\\fran_cut.vtk");
26     reader->Update();
27     vtkSmartPointer<vtkPolyData> original  =  reader->GetOutput();
28 
29     std::cout << "抽取前:" << std::endl << "------------" << std::endl;
30     std::cout << "模型点数为: " << original->GetNumberOfPoints() << std::endl;
31     std::cout << "模型面数为: " << original->GetNumberOfPolys() << std::endl;
32 
33     vtkSmartPointer<vtkDecimatePro> decimate =
34         vtkSmartPointer<vtkDecimatePro>::New();
35     decimate->SetInputData(original);
36     decimate->SetTargetReduction(.80); //80%的三角面片被移除
37     decimate->Update();
38 
39     vtkSmartPointer<vtkPolyData> decimated = decimate->GetOutput();
40     std::cout << "抽取后" << std::endl << "------------" << std::endl;
41     std::cout << "模型点数为:" << decimated->GetNumberOfPoints()<< std::endl;
42     std::cout << "模型面数为:" << decimated->GetNumberOfPolys()<< std::endl;
43 
44     vtkSmartPointer<vtkPolyDataMapper> origianlMapper =
45         vtkSmartPointer<vtkPolyDataMapper>::New();
46     origianlMapper->SetInputData(original);
47 
48     vtkSmartPointer<vtkActor> origianlActor =
49         vtkSmartPointer<vtkActor>::New();
50     origianlActor->SetMapper(origianlMapper);
51 
52     vtkSmartPointer<vtkPolyDataMapper> decimatedMapper =
53         vtkSmartPointer<vtkPolyDataMapper>::New();
54     decimatedMapper->SetInputData(decimated);
55 
56     vtkSmartPointer<vtkActor> decimatedActor =
57         vtkSmartPointer<vtkActor>::New();
58     decimatedActor->SetMapper(decimatedMapper);
59 
60     double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
61     double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
62 
63     vtkSmartPointer<vtkRenderer> leftRenderer =
64         vtkSmartPointer<vtkRenderer>::New();
65     leftRenderer->SetViewport(leftViewport);
66     leftRenderer->AddActor(origianlActor);
67     leftRenderer->SetBackground(1.0, 1.0, 1.0);
68 
69     vtkSmartPointer<vtkRenderer> rightRenderer =
70         vtkSmartPointer<vtkRenderer>::New();
71     rightRenderer->SetViewport(rightViewport);
72     rightRenderer->AddActor(decimatedActor);
73     rightRenderer->SetBackground(1.0, 1.0, 1.0);
74 
75     leftRenderer->GetActiveCamera()->SetPosition(0, -1, 0);
76     leftRenderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
77     leftRenderer->GetActiveCamera()->SetViewUp(0, 0, 1);
78     leftRenderer->GetActiveCamera()->Azimuth(30);
79     leftRenderer->GetActiveCamera()->Elevation(30);
80     leftRenderer->ResetCamera();
81     rightRenderer->SetActiveCamera(leftRenderer->GetActiveCamera());
82 
83     vtkSmartPointer<vtkRenderWindow> renderWindow =
84         vtkSmartPointer<vtkRenderWindow>::New();
85     renderWindow->AddRenderer(leftRenderer);
86     renderWindow->AddRenderer(rightRenderer);
87     renderWindow->SetSize(640, 320);
88     renderWindow->Render();
89     renderWindow->SetWindowName("PolyDataDecimation");
90 
91     vtkSmartPointer<vtkRenderWindowInteractor> interactor =
92         vtkSmartPointer<vtkRenderWindowInteractor>::New();
93     interactor->SetRenderWindow(renderWindow);
94 
95     renderWindow->Render();
96     interactor->Start();
97 
98     return EXIT_SUCCESS;
99 }

posted on 2023-08-16 08:37  一杯清酒邀明月  阅读(125)  评论(0编辑  收藏  举报