CadCaeCamPlm专业门户网站 联系HuangSteve@163.com
上图的程序实现为: vtkBYUReader *model_reader=vtkBYUReader::New();
model_reader->SetGeometryFileName(\载入三维图形数据 vtkPolyDataNormals *model_normals=vtkPolyDataNormals::New(); model_normals->SetInput(model_reader->GetOutput());//法线设置 vtkTextureMapToCylinder *tmapper=vtkTextureMapToCylinder::New(); tmapper->SetInput(model_normals->GetOutput()); tmapper->PreventSeamOn(); vtkTransformTextureCoords *transform_texture=vtkTransformTextureCoords::New(); transform_texture->SetInput(tmapper->GetOutput());//纹理处理 vtkDataSetMapper *mapper=vtkDataSetMapper::New(); mapper->SetInput(transform_texture->GetOutput()); vtkJPEGReader *texture_reader=vtkJPEGReader::New();//载入图像文件数据 texture_reader->SetFileName(\ vtkTexture *texture=vtkTexture::New();
texture->SetInputConnection(texture_reader->GetOutputPort()); texture->InterpolateOn(); vtkActor *actor=vtkActor::New();
actor->SetMapper(mapper);//图形数据显示
actor->SetTexture(texture);//将图像映射到图形数据上
1.8用 VTK 进行 CT 片三维重构
VTK 作为一种通用的可视化类库,在科学和工程界有着广泛的应用。其中一种重要而常见的应用就是在医学领域,比如著名的“可视化人”项目,就有 VTK 的应用。实际上,VTK 在医学方面的应用是是 VTK 比较强调的,在它提供的例程应用中,就有专门的医学目录。学习它们,并应用于我们的项目中,是一条合适的途径。
比如,如果要做一个 3D 的医学影像系统,其中重要的一个环节就是三维重构,这也是在全膝置换手术中需要的。所以,用 VTK 进行 CT 片的三维重构,就是一个合适的应用的例子。
利用 VTK 做 CT 片的三维重构,从算法和数据结构来看,就是一个读取数据问题和一个图形显示问题。 对于我们手里的 CT 片,因为它们符合 DICOM 3.0 标准,所以对它们的读取还是相对容易的。VTK 已经实现了这类数据的读取,vtkImageSource类的很多子类都可以完成这种工作,如vtkImageReader 类或者 vtkVolumeReader 等。这样我们编程的时候只要指定必须的参数,就可以实现对单张 CT 片乃至 CT 片序列的读
www.CadCaeCam.com/Forum/index.htm
CadCaeCamPlm专业门户网站收集整理, 版权属于原作者 联系HuangSteve@163.com
第 6 页 共 10 页
CadCaeCamPlm专业门户网站 联系HuangSteve@163.com
取了。根据 VTK 的数据管道,原始数据读取入了以后,就可以使用各种各种过滤器进行数据转换,在这个例子里,即进行表面抽取、影射,然后就可以绘制了。因为是三维重建,所以还应该加入交互器,以便可以在绘制窗口里进行交互。
关于重建算法,对于 CT 片这种三维规则数据场,可以采取成熟、基本的算法来进行三维重建,比如 MarchingCubes 算法。
1.9 Picking
Picking用来选取数据以及演员actors,或者询问隐藏的数据属性.
vtkPicker is used to select instances of vtkProp3D by shooting a ray into a graphics window and intersecting with the actor's bounding box. The ray is defined from a point defined in window (or pixel) coordinates, and a point located from the camera's position.
vtkPicker may return more than one vtkProp3D, since more than one bounding box may be intersected. vtkPicker returns the list of props that were hit, the pick coordinates in world and untransformed mapper space, and the prop (vtkProp3D) and mapper that are \to the camera. The closest prop is the one whose center point (i.e., center of bounding box) projected on the ray is closest to the camera.
1.10 VTK
中的坐标系统
VTK支持多种坐标系统,它用类vtkCoordinate 来控制对不同的坐标系统进行设置.
1.10.1对坐标系统的设置如下:( The coordinate systems in vtk are as follows:)
DISPLAY - x-y pixel values in window NORMALIZED DISPLAY - x-y (0,1) normalized values VIEWPORT - x-y pixel values in viewport
NORMALIZED VIEWPORT - x-y (0,1) normalized value in viewport
VIEW - x-y-z (-1,1) values in camera coordinates. (z is depth) WORLD - x-y-z global coordinate values USERDEFINED - x-y-z in User defined space
1.10.2样例程序如下:
www.CadCaeCam.com/Forum/index.htm
CadCaeCamPlm专业门户网站收集整理, 版权属于原作者 联系HuangSteve@163.com
第 7 页 共 10 页
CadCaeCamPlm专业门户网站 联系HuangSteve@163.com
vtkCoordinate *normCoords= vtkCoordinate::New();
normCoords->SetCoordinateSystemToNormalizedViewport(); vtkPolyDataMapper2D *mapper= vtkPolyDataMapper2D::New(); mapper->SetInput(*&^*^*);
mappe->SetTransformCoordinate(normCoords); 1.10.3对坐标设置的方法如下:
void vtkCoordinate::SetCoordinateSystemToDisplay();
void vtkCoordinate::SetCoordinateSystemToNormalizedDisplay(); void vtkCoordinate::SetCoordinateSystemToViewport();
void vtkCoordinate::SetCoordinateSystemToNormalizedViewport(); void vtkCoordinate::SetCoordinateSystemToView(); void vtkCoordinate::SetCoordinateSystemToWorld(); 1.11 vtkActor2D
vtkActor2D和vtkActor很相似,只不过它是对二维图形的操作.
vtkActor2D is similar to vtkActor, but it is made to be used with two dimensional images and annotation. 使用范例如下:
vtkPolyData *selectRect= vtkPolyData::New(); selectRect->SetPoints (pts); selectRect ->SetLines (rect);
vtkPolyDataMapper2D *rectMapper= vtkPolyDataMapper2D::New(); rectMapper ->SetInput (selectRect);
vtkActor2D *rectActor= vtkActor2D::New(); rectActor ->SetMapper (rectMapper);
1.12 在图形窗口上添加注释
在VTK中有两种方法为所生成的图形添加注释:
(1)2D注释,将文字放在二维的图形窗口上,文字不会因为用户的操作而变化
(2)3D注释,将文字添加在所生成的三维图形上,或者将文字直接以三维图形的方式显示. 1.12.1 2D Annotation(二维注释)
程序范例如下: vtkTextMapper *textActor=vtkTextMapper::New(); textActor->SetInput (\is a sphere\所要显示的注释文字 vtkScaledTextActor *mmm=vtkScaledTextActor::New(); mmm->SetMapper(textActor); mmm->SetDisplayPosition(90, 50 );//设定注释位置 ren1->AddActor2D(mmm);//注释文字的绘制
在vtkTextMapper中,我们可以对文字的字体,大小,内容等属性做出更改,在vtkScaledTextActor中,我们可以控制注释的位置,以及颜色.
www.CadCaeCam.com/Forum/index.htm
CadCaeCamPlm专业门户网站收集整理, 版权属于原作者 联系HuangSteve@163.com
第 8 页 共 10 页
CadCaeCamPlm专业门户网站 联系HuangSteve@163.com
1.12.2 3D Annotation and vtkFollower
三维注释是用vtkVectorText 来创建注释内容文字. vtkFollower是绘制三维注释的一种方式. 程序范例如下: vtkVectorText atext atext SetText \vtkPolyDataMapper textMapper textMapper SetInputConnection [atext GetOutputPort] vtkFollower textActor textActor SetMapper textMapper textActor SetScale 0.2 0.2 0.2 textActor AddPosition 0 -0.1 0 ren1 AddActor textActor 程序运行效果如下:
其字体随着球的运动而运动.
4.13 三维数据的的转动(Transforming Data)
在VTK中,对三维数据的转动可以用vtkTransformFilter这个类来实现.
vtkTransformFilter is a filter to transform point coordinates, and associated point normals and vectors. Other point data is passed through the filter.
www.CadCaeCam.com/Forum/index.htm
CadCaeCamPlm专业门户网站收集整理, 版权属于原作者 联系HuangSteve@163.com
第 9 页 共 10 页
CadCaeCamPlm专业门户网站 联系HuangSteve@163.com
An alternative method of transformation is to use vtkActor's methods to scale, rotate, and translate objects. The difference between the two methods is that vtkActor's transformation simply effects where objects are rendered (via the graphics pipeline), whereas vtkTransformFilter actually modifies point coordinates in the
visualization pipeline. This is necessary for some objects (e.g., vtkProbeFilter) that require point coordinates as input.
另一个对三维图形的转动实现的类为vtkTransformPolyDataFilter.该类主要用于对多边形数据的操作
vtkTransformPolyDataFilter is a filter to transform point coordinates and associated point and cell normals and vectors. Other point and cell data is passed through the filter unchanged. This filter is specialized for polygonal data.
使用范例如下:
vtkTransform *transL1= vtkTransform::New(); transL1->Translate(3.7, 0.0 ,28.37); transL1->Scale( 5 ,5 ,5); transL1->RotateY( 90);
vtkTransformPolyDataFilter *tf= vtkTransformPolyDataFilter::New(); tf->SetInputConnection (line->GetOutputPort()); tf-> SetTransform( transL1);
www.CadCaeCam.com/Forum/index.htm
CadCaeCamPlm专业门户网站收集整理, 版权属于原作者 联系HuangSteve@163.com
第 10 页 共 10 页