1 #include "vtkAutoInit.h"
2 VTK_MODULE_INIT(vtkRenderingOpenGL2);
3 VTK_MODULE_INIT(vtkInteractionStyle);
4
5 #include <vtkSmartPointer.h>
6 #include <vtkPolyDataReader.h>
7 #include <vtkPolyDataNormals.h> //计算法向量
8 #include <vtkMaskPoints.h>
9 #include <vtkArrowSource.h>
10 #include <vtkGlyph3D.h>
11 #include <vtkPointData.h>
12 #include <vtkProperty.h>
13 //
14 #include <vtkPolyDataMapper.h>
15 #include <vtkActor.h>
16 #include <vtkRenderer.h>
17 #include <vtkRenderWindow.h>
18 #include <vtkRenderWindowInteractor.h>
19
20 int main()
21 {
22 vtkSmartPointer<vtkPolyDataReader> plyReader =
23 vtkSmartPointer<vtkPolyDataReader>::New();
24 plyReader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第六章_图像处理\\data\\fran_cut.vtk");
25 plyReader->Update();
26
27 vtkSmartPointer<vtkPolyDataNormals> normFilter =
28 vtkSmartPointer<vtkPolyDataNormals>::New();
29 normFilter->SetInputData(plyReader->GetOutput());
30 normFilter->SetComputePointNormals(1);//开启点法向量计算
31 normFilter->SetComputeCellNormals(0); //关闭单元法向量计算
32 normFilter->SetAutoOrientNormals(1);
33 normFilter->SetSplitting(0);
34 normFilter->Update();
35
36 vtkSmartPointer<vtkMaskPoints> mask =
37 vtkSmartPointer<vtkMaskPoints>::New();
38 mask->SetInputData(normFilter->GetOutput());
39 mask->SetMaximumNumberOfPoints(300);
40 mask->RandomModeOn();
41 mask->Update();
42
43 vtkSmartPointer<vtkArrowSource> arrow =
44 vtkSmartPointer<vtkArrowSource>::New();
45 arrow->Update(); //一定要更新 否则数据没有添加进来,程序会报错
46
47 vtkSmartPointer<vtkGlyph3D> glyph =
48 vtkSmartPointer<vtkGlyph3D>::New();
49 glyph->SetInputData(mask->GetOutput());
50 glyph->SetSourceData(arrow->GetOutput());//每一点用箭头代替
51 glyph->SetVectorModeToUseNormal();//设置向量显示模式和法向量一致
52 glyph->SetScaleFactor(0.01); //设置伸缩比例
53 glyph->Update();
54
55 vtkSmartPointer<vtkPolyDataMapper> mapper =
56 vtkSmartPointer<vtkPolyDataMapper>::New();
57 mapper->SetInputData(plyReader->GetOutput());
58 vtkSmartPointer<vtkPolyDataMapper> normMapper =
59 vtkSmartPointer<vtkPolyDataMapper>::New();
60 normMapper->SetInputData(normFilter->GetOutput());
61 vtkSmartPointer<vtkPolyDataMapper> glyphMapper =
62 vtkSmartPointer<vtkPolyDataMapper>::New();
63 glyphMapper->SetInputData(glyph->GetOutput());
64
65 vtkSmartPointer<vtkActor> actor =
66 vtkSmartPointer<vtkActor>::New();
67 actor->SetMapper(mapper);
68 vtkSmartPointer<vtkActor> normActor =
69 vtkSmartPointer<vtkActor>::New();
70 normActor->SetMapper(normMapper);
71 vtkSmartPointer<vtkActor> glyphActor =
72 vtkSmartPointer<vtkActor>::New();
73 glyphActor->SetMapper(glyphMapper);
74 glyphActor->GetProperty()->SetColor(1, 0, 0);
75
76 double origView[4] = { 0, 0, 0.33, 1 };
77 double normView[4] = { 0.33, 0, 0.66, 1 };
78 double glyphView[4] = { 0.66, 0, 1, 1 };
79 vtkSmartPointer<vtkRenderer> origRender =
80 vtkSmartPointer<vtkRenderer>::New();
81 origRender->SetViewport(origView);
82 origRender->AddActor(actor);
83 origRender->SetBackground(1, 0, 0);
84 vtkSmartPointer<vtkRenderer> normRender =
85 vtkSmartPointer<vtkRenderer>::New();
86 normRender->SetViewport(normView);
87 normRender->AddActor(normActor);
88 normRender->SetBackground(0, 1, 0);
89 vtkSmartPointer<vtkRenderer> glyphRender =
90 vtkSmartPointer<vtkRenderer>::New();
91 glyphRender->SetViewport(glyphView);
92 glyphRender->AddActor(glyphActor);
93 glyphRender->AddActor(normActor);
94 glyphRender->SetBackground(0, 0, 1);
95
96 vtkSmartPointer<vtkRenderWindow> rw =
97 vtkSmartPointer<vtkRenderWindow>::New();
98 rw->AddRenderer(origRender);
99 rw->AddRenderer(normRender);
100 rw->AddRenderer(glyphRender);
101 rw->SetWindowName("Calculating Point Norm & Cell Norm");
102 rw->SetSize(960, 320);
103 rw->Render();
104
105 vtkSmartPointer<vtkRenderWindowInteractor> rwi =
106 vtkSmartPointer<vtkRenderWindowInteractor>::New();
107 rwi->SetRenderWindow(rw);
108 rwi->Initialize();
109 rwi->Start();
110
111 return 0;
112 }