VTK Users Guide 中C++例程之rainbow

本人将《The VTK User’s Guide 11th Edition 》中的TCL编写的例程进行了C++的转换,使用的平台是VS2015,VTK版本是8.0.1。之所以贴出来,以求方便大家使用C++来进行VTK的研究。

VTK/Examples/Rendering/Tcl/rainbow.tcl之转成C++

 1 #include "vtkAutoInit.h"
 2 #include "vtkRenderer.h"
 3 #include "vtkRenderWindow.h"
 4 #include "vtkRenderWindowInteractor.h"
 5 #include "vtkSmartPointer.h"
 6 #include "vtkMultiBlockPLOT3DReader.h"
 7 #include "vtkMultiBlockDataSet.h"
 8 #include "vtkStructuredGridGeometryFilter.h"
 9 #include "vtkLookupTable.h"
10 #include "vtkPolyDataMapper.h"
11 #include "vtkActor.h"
12 #include "vtkStructuredGridOutlineFilter.h"
13 #include "vtkCamera.h"
14 
15 int main()
16 {
17     VTK_MODULE_INIT(vtkRenderingOpenGL2);
18     VTK_MODULE_INIT(vtkRenderingFreeType);
19     VTK_MODULE_INIT(vtkInteractionStyle);
20 
21     //# First create pipeline a simple pipeline that reads a structure grid
22     //# and then extracts a plane from the grid.The plane will be colored
23     //# differently by using different lookup tables.
24     //# Note: the Update method is manually invoked because it causes the
25     //# reader to read; later on we use the output of the reader to set
26     //# a range for the scalar values.
27     vtkSmartPointer<vtkMultiBlockPLOT3DReader> pl3d = vtkSmartPointer<vtkMultiBlockPLOT3DReader>::New();
28     pl3d->SetXYZFileName("D://VTK/VTK-8.0.1/.ExternalData/MD5/13338e5fa5a798f5d709b5ef6a9be1a0");
29     pl3d->SetQFileName("D://VTK/VTK-8.0.1/.ExternalData/MD5/ff18fe9c1c68ce2842e0f4875464107d");
30     pl3d->SetScalarFunctionNumber(100);
31     pl3d->SetVectorFunctionNumber(202);
32     pl3d->Update();
33 
34     vtkSmartPointer<vtkStructuredGridGeometryFilter> plane = vtkSmartPointer<vtkStructuredGridGeometryFilter>::New();
35     plane->SetInputData(pl3d->GetOutput()->GetBlock(0));
36     plane->SetExtent(1, 100, 1, 100, 7, 7);
37     vtkSmartPointer<vtkLookupTable> lut = vtkSmartPointer<vtkLookupTable>::New();
38     vtkSmartPointer<vtkPolyDataMapper> planeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
39     planeMapper->SetLookupTable(lut);
40     planeMapper->SetInputConnection(plane->GetOutputPort());
41     planeMapper->SetScalarRange(plane->GetOutput()->GetScalarRange());
42     vtkSmartPointer<vtkActor> planeActor = vtkSmartPointer<vtkActor>::New();
43     planeActor->SetMapper(planeMapper);
44 
45     //# This creates a weird effect. The Build() method causes the lookup table
46     //# to allocate memory and create a table based on the currect hue, saturation,
47     //# value, and alpha(transparency) range.Here we then manually overwrite the
48     //# values generated by the Build() method.
49     lut->SetNumberOfColors(256);
50     lut->Build();
51     for (int i=0;i<16;i++)
52     {
53         lut->SetTableValue(i * 16, 1.0, 0, 0, 1.0);//red
54         lut->SetTableValue(i * 16 + 1, 0, 1.0, 1, 1.0);//green
55         lut->SetTableValue(i * 16 + 2, 0, 0, 1.0, 1.0);//blue
56         lut->SetTableValue(i * 16 + 3, 0, 0, 0, 1.0);//black
57     }
58 
59     //# This creates an outline around the data.
60     vtkSmartPointer<vtkStructuredGridOutlineFilter> outline = vtkSmartPointer<vtkStructuredGridOutlineFilter>::New();
61     outline->SetInputData(pl3d->GetOutput()->GetBlock(0));
62     vtkSmartPointer<vtkPolyDataMapper> outlineMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
63     outlineMapper->SetInputConnection(outline->GetOutputPort());
64     vtkSmartPointer<vtkActor> outlineActor = vtkSmartPointer<vtkActor>::New();
65     outlineActor->SetMapper(outlineMapper);
66 
67 
68 
69     //Create the Renderer, RenderWindow, RenderWindowInteractor
70     vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New();
71     vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
72     renWin->AddRenderer(ren1);
73     vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
74     iren->SetRenderWindow(renWin);
75 
76     //Add the actors to the renderer, set the background and size
77     ren1->AddActor(outlineActor);
78     ren1->AddActor(planeActor);
79     ren1->SetBackground(0.1, 0.2, 0.4);
80     ren1->TwoSidedLightingOff();
81     renWin->SetSize(250, 250);
82     iren->Initialize();
83     ren1->GetActiveCamera()->SetClippingRange(3.95297, 50);
84     ren1->GetActiveCamera()->SetFocalPoint(8.88908, 0.595038, 29.3342);
85     ren1->GetActiveCamera()->SetPosition(-12.3332, 31.7479, 41.2387);
86     ren1->GetActiveCamera()->SetViewUp(0.060772, -0.319905, 0.945498);
87 
88     iren->Start();
89 
90     return 0;
91 }

 

posted on 2017-11-28 17:22  湘水亭长  阅读(1394)  评论(1编辑  收藏  举报