《第一部分VTK程序一般流程》由会员分享,可在线阅读,更多相关《第一部分VTK程序一般流程(24页珍藏版)》请在金锄头文库上搜索。
1、报告人:邹水中报告人:邹水中日日 期:期:2010.4.22010.4.2VTK使用总结使用总结内容简介内容简介一、一、VTK程序一般流程程序一般流程二、二、VTK的数据对象接口的数据对象接口三、三、 VTK的面绘制、体绘制、多平面显示的面绘制、体绘制、多平面显示四、四、视点变换与相机控制视点变换与相机控制五、五、 VTK事件交互、事件交互、VTK与与MFC混合编程混合编程六、六、VTK视窗控制与文视窗控制与文本本标注标注七、七、VTK在测量中应用与在测量中应用与VTK数据切割数据切割第一部分 VTK程序一般流程Visualization Toolkit 的机制Visualization To
2、olkit 是一个用于可视化应用程序构造与运行的支撑环境,它是在三维函数库OpenGL 的基础上采用面向对象的设计方法发展起来的。它将在可视化开发过程中会经常遇到的细节屏蔽起来,并将一些常用的算法封装起来。比如Visualization Toolkit 将在表面重建中比较常见的Marching Cubes 算法封装起来,以类的形式给以支持,这样在对三维规则点阵数据进行表面重建时就不必再重复编写Marching Cubes 算法的代码而直接使用Visualization Toolkit 中已经提供的vtkMarchingCubes 类。Visualization Toolkit Visualiz
3、ation Toolkit 的框架结构的框架结构图形流水线:将几何数据转换为图像;可视化流水线:将信息转换为几何数据.另一种看法是:可视化流水线:负责构建几何表示;图形流水线:负责绘制. vtk使用一种lazy评价机制.对于lazy评价机制,通常无需人工地调用Update(),因为filters连接着可视化流水线,当actor接收到绘制本身的要求会将方法回馈到他的mapper,然后Update方法自动发送至可视化流水线。Render方法通常开始数据要求,数据被通过流水线向下投递。依赖于流水线的哪个端口过时,流水线中的filters可能重新执行,将数据送至更新的流水线末端,通过actor绘制.可
4、视化模型可视化模型VTK 图形模型由以下核心对象组成vtkActor,vtkActor2D,vtkVolume vtkProp 和/或vtpProp3D 的子类vtkLightvtkCameravtkProperty,vtkProperty2DvtkMaper,vtkMapper2D vtkAbstractMapper 的子类vtkTransformvtkLookupTable,vtkColorTransferFunction- vtkScalarsToColors 的子类/for objects that convert scalars to colors vtkRendervtkRende
5、rWindowvtkRenderWindowInteractor图形模型图形模型vtkLight用来展示和操控场景中的灯光,只在3D中用到.VtkCamera控制3D几何投影到2D。定位:定点、方向,还控制视角投影和立体视图.vtkMapper与查找表vtkLookupTable联合使用,用于转换和绘制几何体。Mapper提供可视化流水线和图形模型.vtkLookupTable是vtkScalarsToColors和vtkColorTransferFunction的子类.vtkRenderer和vtkRenderWindow用来管理图形机与电脑窗口系统的接口.可以生成多个render wind
6、ows,每个render windows中有多个renderer去绘制.一个render window中可以有多个区域(视窗).vtkRenderWindowInteractor用于与场景交互,操纵camera,选择对象.vtkWin32OpenGLRenderWindow和vtkWin32RenderWindowInteractorvtkWin32OpenGLRenderWindow is a concrete implementation of the abstract class vtkRenderWindow. vtkWin32OpenGLRenderer interfaces to
7、the standard OpenGL graphics library in the Windows/NT environment.implements Win32 specific functions required by vtkRenderWindowInteractor. vtkActor的子类vtkAssembly,vtkFollower,vtkLODActor等.vtkAssembly允许actors分层,在层级平移、旋转、缩放时合理转换.vtkLODActor在改变几何表示时保持交互帧速率示例代码1演示VTK一般流程及常用类的使用vtkVolume16Reader *v16 =
8、 vtkVolume16Reader:New();v16-SetDataDimensions(64,64);v16-SetDataByteOrderToLittleEndian();v16-SetFilePrefix (D:/VTK 5.4/VTKData/Data/headsq/quarter);v16-SetImageRange(1, 93);v16-SetDataSpacing (3.2, 3.2, 1.5);vtkContourFilter *skinExtractor = vtkContourFilter:New();skinExtractor-SetInputConnection(
9、 v16-GetOutputPort();skinExtractor-SetValue(0, 500);vtkPolyDataNormals *skinNormals = vtkPolyDataNormals:New();skinNormals-SetInputConnection(skinExtractor-GetOutputPort();skinNormals-SetFeatureAngle(60.0);/if the surface normal between two adjacent triangles is = FeatureAngle, an edge exists vtkStr
10、ipper *skinStripper = vtkStripper:New();skinStripper-SetInputConnection(skinNormals-GetOutputPort();vtkPolyDataMapper *skinMapper = vtkPolyDataMapper:New();skinMapper-SetInputConnection(skinStripper-GetOutputPort();skinMapper-ScalarVisibilityOff();示例代码1vtkActor *skin = vtkActor:New();skin-SetMapper(
11、skinMapper);skin-GetProperty()-SetDiffuseColor(1, .49, .25);skin-GetProperty()-SetSpecular(.3);skin-GetProperty()-SetSpecularPower(20);vtkLookupTable *bwLut = vtkLookupTable:New();bwLut-SetTableRange (0, 2000);bwLut-SetSaturationRange(0, 1);bwLut-SetHueRange (0, 1);bwLut-SetValueRange (0.5, 1);bwLut
12、-Build(); /effective built/ The result is an image of type VTK_UNSIGNED_CHAR vtkImageMapToColors *saggitalColors = vtkImageMapToColors:New();saggitalColors-SetInputConnection(v16-GetOutputPort();saggitalColors-SetLookupTable(bwLut);vtkImageActor *saggital = vtkImageActor:New();saggital-SetInput(sagg
13、italColors-GetOutput();saggital-SetDisplayExtent(32,32, 0,63, 0,92);vtkRenderer *aRenderer = vtkRenderer:New();vtkRenderWindow *renWin = vtkRenderWindow:New();renWin-AddRenderer(aRenderer);vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor:New();iren-SetRenderWindow(renWin);vtkCamera *aCame
14、ra = vtkCamera:New();aCamera-SetViewUp (0, 0, -1);aCamera-SetPosition (0, 1, 0);aCamera-SetFocalPoint (0, 0, 0);aCamera-ComputeViewPlaneNormal();aRenderer-AddActor(saggital);skin-GetProperty()-SetOpacity(0.5);aRenderer-SetActiveCamera(aCamera);aRenderer-Render();aRenderer-ResetCamera ();aCamera-Doll
15、y(1.5);aRenderer-SetBackground(1,1,1);aRenderer-ResetCameraClippingRange ();renWin-SetSize(640, 480);iren-Initialize();iren-Start(); 示例代码3演示视窗显示控制、相机控制class vtkWidgetWindowLevelCallback : public vtkCommandpublic: static vtkWidgetWindowLevelCallback *New() return new vtkWidgetWindowLevelCallback; voi
16、d Execute( vtkObject *caller, unsigned long vtkNotUsed( event ), void *callData ) vtkImagePlaneWidget* self = reinterpret_cast( caller ); if(!self) return; double* wl = static_cast( callData ); if ( self = this-WidgetX ) this-WidgetY-SetWindowLevel(wl0,wl1); this-WidgetZ-SetWindowLevel(wl0,wl1); els
17、e if( self = this-WidgetY ) this-WidgetX-SetWindowLevel(wl0,wl1); this-WidgetZ-SetWindowLevel(wl0,wl1); else if (self = this-WidgetZ) this-WidgetX-SetWindowLevel(wl0,wl1); this-WidgetY-SetWindowLevel(wl0,wl1); vtkWidgetWindowLevelCallback():WidgetX( 0 ), WidgetY( 0 ), WidgetZ ( 0 ) vtkImagePlaneWidg
18、et* WidgetX; vtkImagePlaneWidget* WidgetY; vtkImagePlaneWidget* WidgetZ;int main( int argc, char *argv ) vtkRenderer* ren1 = vtkRenderer:New(); vtkRenderer* ren2 = vtkRenderer:New(); vtkRenderer* ren3 = vtkRenderer:New(); vtkRenderer* ren = vtkRenderer:New(); vtkRenderWindow* renWin = vtkRenderWindo
19、w:New(); vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor:New(); iren-SetRenderWindow(renWin); renWin-AddRenderer(ren1); renWin-AddRenderer(ren2); renWin-AddRenderer(ren3); renWin-AddRenderer(ren);vtkDICOMImageReader *v16 = vtkDICOMImageReader:New();v16-SetDataByteOrderToLittleEndian();v1
20、6-SetDirectoryName(E:/MedImageData/336);v16-Update();vtkOutlineFilter* outline = vtkOutlineFilter:New();outline-SetInputConnection(v16-GetOutputPort();vtkPolyDataMapper* outlineMapper = vtkPolyDataMapper:New();outlineMapper-SetInputConnection(outline-GetOutputPort();vtkActor* outlineActor = vtkActor
21、:New();outlineActor-SetMapper( outlineMapper); vtkWidgetWindowLevelCallback* cbk = vtkWidgetWindowLevelCallback:New();cbk-WidgetX = planeWidgetX;cbk-WidgetY = planeWidgetY;cbk-WidgetZ = planeWidgetZ;/ planeWidgetX-AddObserver( vtkCommand:EndWindowLevelEvent, cbk );/planeWidgetY-AddObserver( vtkComma
22、nd:EndWindowLevelEvent, cbk );/planeWidgetZ-AddObserver( vtkCommand:EndWindowLevelEvent, cbk );cbk-Delete();vtkImageMapToColors* colorMap1 = vtkImageMapToColors:New();colorMap1-PassAlphaToOutputOff();colorMap1-SetActiveComponent(0);colorMap1-SetOutputFormatToLuminance();colorMap1-SetInput(planeWidge
23、tX-GetResliceOutput();colorMap1-SetLookupTable(planeWidgetX-GetLookupTable();vtkImageActor* imageActor1 = vtkImageActor:New();imageActor1-PickableOff();imageActor1-SetInput(colorMap1-GetOutput(); ren-AddActor( outlineActor); ren1-AddActor( imageActor1); ren2-AddActor( imageActor2); ren3-AddActor( im
24、ageActor3); ren-SetBackground( 0.1, 0.1, 0.2); ren1-SetBackground( 0.1, 0.2, 0.1); ren2-SetBackground( 0.2, 0.1, 0.2); ren3-SetBackground( 0.2, 0.2, 0.2); renWin-SetSize( 600, 400); ren-SetViewport(0,0.5,0.5,1); ren1-SetViewport(0.5,0.5,1,1); ren2-SetViewport(0,0,0.5,0.5); ren3-SetViewport(0.5,0,1,0
25、.5); renWin-Render(); ren-GetActiveCamera()-Elevation(110); ren-GetActiveCamera()-SetViewUp(0, 0, -1); ren-GetActiveCamera()-Azimuth(45); ren-GetActiveCamera()-Dolly(1.15); ren-ResetCameraClippingRange(); iren-Initialize(); iren-Start(); renWin-Render(); iren-SetKeyCode(z); iren-InvokeEvent(vtkComma
26、nd:CharEvent,NULL); iren-SetKeyCode(z); iren-InvokeEvent(vtkCommand:CharEvent,NULL); iren-Start();vtkCellPicker* picker = vtkCellPicker:New();picker-SetTolerance(0.005);vtkProperty* ipwProp = vtkProperty:New();/assign default props to the ipws texture plane actorvtkImagePlaneWidget* planeWidgetX = v
27、tkImagePlaneWidget:New();planeWidgetX-SetInteractor( iren);planeWidgetX-SetKeyPressActivationValue(x);planeWidgetX-SetPicker(picker);planeWidgetX-RestrictPlaneToVolumeOn();planeWidgetX-GetPlaneProperty()-SetColor(1,0,0);planeWidgetX-SetTexturePlaneProperty(ipwProp);planeWidgetX-TextureInterpolateOff();planeWidgetX-SetResliceInterpolateToNearestNeighbour();planeWidgetX-SetInput(v16-GetOutput();planeWidgetX-SetPlaneOrientationToXAxes();planeWidgetX-SetSliceIndex(32);planeWidgetX-GetTexturePlaneProperty()-SetOpacity(1);planeWidgetX-DisplayTextOn();planeWidgetX-On();planeWidgetX-InteractionOn();