1 #include <vtkAutoInit.h> 2 VTK_MODULE_INIT(vtkRenderingOpenGL2); 3 VTK_MODULE_INIT(vtkRenderingFreeType); 4 VTK_MODULE_INIT(vtkInteractionStyle); 5 6 #include <vtkSmartPointer.h> 7 #include <vtkPolyDataReader.h> 8 #include <vtkPolyData.h> 9 #include <vtkLinearSubdivisionFilter.h> 10 #include <vtkLoopSubdivisionFilter.h> 11 #include <vtkButterflySubdivisionFilter.h> 12 #include <vtkPolyDataMapper.h> 13 #include <vtkActor.h> 14 #include <vtkRenderer.h> 15 #include <vtkRenderWindow.h> 16 #include <vtkCamera.h> 17 #include <vtkRenderWindowInteractor.h> 18 19 int main() 20 { 21 //读数据 22 vtkSmartPointer<vtkPolyDataReader> reader = 23 vtkSmartPointer<vtkPolyDataReader>::New(); 24 reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第六章_图像处理\\data\\fran_cut.vtk"); 25 reader->Update(); 26 27 vtkSmartPointer<vtkPolyData> orig = reader->GetOutput(); 28 std::cout << "original" << "-----------------------" << std::endl; 29 std::cout << "模型点数为: " << orig->GetNumberOfPoints() << std::endl; 30 std::cout << "模型面数为: " << orig->GetNumberOfPolys() << std::endl; 31 //线性网格细分滤波器 32 vtkSmartPointer<vtkLinearSubdivisionFilter> linear = 33 vtkSmartPointer<vtkLinearSubdivisionFilter>::New(); 34 linear->SetInputData(orig); 35 linear->SetNumberOfSubdivisions(4); 36 linear->Update(); 37 38 vtkSmartPointer<vtkPolyData> linearInfo = linear->GetOutput(); 39 std::cout << "linear" << "-----------------------" << std::endl; 40 std::cout << "模型点数为: " << linearInfo->GetNumberOfPoints() << std::endl; 41 std::cout << "模型面数为: " << linearInfo->GetNumberOfPolys() << std::endl; 42 43 //Loop网格细分滤波器 44 vtkSmartPointer<vtkLoopSubdivisionFilter> loop = 45 vtkSmartPointer<vtkLoopSubdivisionFilter>::New(); 46 loop->SetInputData(orig); 47 loop->SetNumberOfSubdivisions(4); 48 loop->Update(); 49 50 vtkSmartPointer<vtkPolyData> loopInfo = loop->GetOutput(); 51 std::cout << "loop" << "-----------------------" << std::endl; 52 std::cout << "模型点数为: " << loopInfo->GetNumberOfPoints() << std::endl; 53 std::cout << "模型面数为: " << loopInfo->GetNumberOfPolys() << std::endl; 54 55 //butterfly网格细分滤波器 56 vtkSmartPointer<vtkButterflySubdivisionFilter> butterfly = 57 vtkSmartPointer<vtkButterflySubdivisionFilter>::New(); 58 butterfly->SetInputData(orig); 59 butterfly->SetNumberOfSubdivisions(4); 60 butterfly->Update(); 61 62 vtkSmartPointer<vtkPolyData> butterflyInfo = butterfly->GetOutput(); 63 std::cout << "butterfly" << "-----------------------" << std::endl; 64 std::cout << "模型点数为: " << butterflyInfo->GetNumberOfPoints() << std::endl; 65 std::cout << "模型面数为: " << butterflyInfo->GetNumberOfPolys() << std::endl; 66 67 vtkSmartPointer<vtkPolyDataMapper> origMapper = 68 vtkSmartPointer<vtkPolyDataMapper>::New(); 69 origMapper->SetInputData(orig); 70 vtkSmartPointer<vtkActor> origActor = 71 vtkSmartPointer<vtkActor>::New(); 72 origActor->SetMapper(origMapper); 73 74 vtkSmartPointer<vtkPolyDataMapper> linearMapper = 75 vtkSmartPointer<vtkPolyDataMapper>::New(); 76 linearMapper->SetInputData(linear->GetOutput()); 77 vtkSmartPointer<vtkActor> linearActor = 78 vtkSmartPointer<vtkActor>::New(); 79 linearActor->SetMapper(linearMapper); 80 81 vtkSmartPointer<vtkPolyDataMapper> loopMapper = 82 vtkSmartPointer<vtkPolyDataMapper>::New(); 83 loopMapper->SetInputData(loop->GetOutput()); 84 vtkSmartPointer<vtkActor> loopActor = 85 vtkSmartPointer<vtkActor>::New(); 86 loopActor->SetMapper(loopMapper); 87 88 vtkSmartPointer<vtkPolyDataMapper> butterflyMapper = 89 vtkSmartPointer<vtkPolyDataMapper>::New(); 90 butterflyMapper->SetInputData(butterfly->GetOutput()); 91 vtkSmartPointer<vtkActor> butterflyActor = 92 vtkSmartPointer<vtkActor>::New(); 93 butterflyActor->SetMapper(butterflyMapper); 94 // 95 double ltView[4] = { 0, 0, 0.5, 0.5 }; 96 double rtView[4] = { 0.5, 0, 1, 0.5 }; 97 double lbView[4] = { 0, 0.5, 0.5, 1 }; 98 double rbView[4] = { 0.5, 0.5, 1, 1 }; 99 100 vtkSmartPointer<vtkRenderer> origRender = 101 vtkSmartPointer<vtkRenderer>::New(); 102 origRender->SetViewport(ltView); 103 origRender->AddActor(origActor); 104 origRender->SetBackground(1, 0, 0); 105 106 vtkSmartPointer<vtkRenderer> linearRender = 107 vtkSmartPointer<vtkRenderer>::New(); 108 linearRender->SetViewport(rtView); 109 linearRender->AddActor(linearActor); 110 linearRender->SetBackground(0, 1, 0); 111 112 vtkSmartPointer<vtkRenderer> loopRender = 113 vtkSmartPointer<vtkRenderer>::New(); 114 loopRender->SetViewport(lbView); 115 loopRender->AddActor(loopActor); 116 loopRender->SetBackground(0, 0, 1); 117 118 vtkSmartPointer<vtkRenderer> butterflyRender = 119 vtkSmartPointer<vtkRenderer>::New(); 120 butterflyRender->SetViewport(rbView); 121 butterflyRender->AddActor(butterflyActor); 122 butterflyRender->SetBackground(0, 0, 0); 123 // 124 vtkSmartPointer<vtkRenderWindow> rw = 125 vtkSmartPointer<vtkRenderWindow>::New(); 126 rw->AddRenderer(origRender); 127 rw->AddRenderer(linearRender); 128 rw->AddRenderer(loopRender); 129 rw->AddRenderer(butterflyRender); 130 rw->SetSize(640, 640); 131 rw->SetWindowName("PolyData Subdivision"); 132 133 origRender->GetActiveCamera()->SetPosition(0, -1, 0); 134 origRender->GetActiveCamera()->SetFocalPoint(0, 0, 0); 135 origRender->GetActiveCamera()->SetViewUp(0, 0, 1); 136 origRender->GetActiveCamera()->Azimuth(30); 137 origRender->GetActiveCamera()->Elevation(30); 138 origRender->ResetCamera();//刷新照相机 139 linearRender->SetActiveCamera(origRender->GetActiveCamera()); 140 loopRender->SetActiveCamera(origRender->GetActiveCamera()); 141 butterflyRender->SetActiveCamera(origRender->GetActiveCamera()); 142 143 vtkSmartPointer<vtkRenderWindowInteractor> rwi = 144 vtkSmartPointer<vtkRenderWindowInteractor>::New(); 145 rwi->SetRenderWindow(rw); 146 rwi->Start(); 147 rw->Render(); 148 149 return 0; 150 }
放大细节
红色视窗为原始三角网格模型,绿色视窗采用了线性细分算法;蓝色视窗采用Loop细分算法;黑色视窗采用了Butterfly细分算法。可见Loop细分、Butterfly细分能够得到较为光滑的效果;效果优于线性细分算法。
标签:GetActiveCamera,rw,57,网格,vtkSmartPointer,origRender,New,include,VTK From: https://www.cnblogs.com/ybqjymy/p/17632970.html