在使用vtk编写一些算法时候,我们需要可视化程序的执行过程,或者能够在图形上看到当前的执行情况,比如,在连接多边形构造矿体的时候,需要检查算法连接的顶点是否正确,这个时候就需要显示点的ID。另外还可能需要显示cell的ID,下面的例子就给出在vtk中如何显示点的ID和cell的ID.
这里使用vtkConeSource来演示,程序的最终目的显示Cone的顶点id 和每个面的id。
vtkConeSource cone = new vtkConeSource(); cone.SetHeight(3.0); cone.SetRadius(1.0); cone.SetResolution(10); vtkPolyDataMapper coneMapper = new vtkPolyDataMapper(); coneMapper.SetInputConnection(cone.GetOutputPort()); vtkActor coneActor = new vtkActor(); coneActor.SetMapper(coneMapper); renderer.AddActor(coneActor);
然后,使用vtkIdFilter来给vtkConeSource生成field data并使用vtkLabeledDataMapper来显示:
vtkIdFilter ids = new vtkIdFilter(); ids.SetInputConnection(cone.GetOutputPort()); ids.PointIdsOn(); ids.CellIdsOn(); ids.FieldDataOn(); vtkLabeledDataMapper ldm = new vtkLabeledDataMapper(); ldm.SetInputConnection(ids.GetOutputPort()); ldm.SetLabelModeToLabelFieldData(); vtkActor2D pointLabels = new vtkActor2D(); pointLabels.SetMapper(ldm); renderer.AddActor(pointLabels);
要显示cell的id需要使用vtkCellCenters:
vtkCellCenters cc = new vtkCellCenters(); cc.SetInputConnection(ids.GetOutputPort()); vtkLabeledDataMapper cellMapper = new vtkLabeledDataMapper(); cellMapper.SetInputConnection(cc.GetOutputPort()); cellMapper.SetLabelModeToLabelFieldData(); cellMapper.GetLabelTextProperty().SetColor(0, 1, 0); vtkActor2D cellLabels = new vtkActor2D(); cellLabels.SetMapper(cellMapper); renderer.AddActor(cellLabels);
需要注意的是,vtkIdFilter 和 vtkCellCenters的输入, 其中vtkCellCenters的输入是vtkIdFilter 的输出。
最终的执行效果如下:
vtkLabeledDataMapper 的一些参数设置方法,比如
SetLabelFormat("%g");可以设置标签的格式
GetLabelTextProperty().SetFontFamilyToCourier();设置字体
GetLabelTextProperty().SetFontSize(10);可以设置大小
SetLabelModeToLabelScalars()方法表示使用vtkPolyData的scalar值作为标签
vtkIdFilter中的几个方法:
CellIdsOn()以cells的id来生成标签数据
FieldDataOn()以vtkPolyData的属性值来生成标签数据
PointIdsOn()这个就是用点的ID来作为标签
使用vtkLabeledDataMapper 显示少了的标签是可以的,但是如果标签的数量达到几百以上了,速度就变得很慢,严重影响操作体验,不管怎么优化,改变都很小。但是在网上查资料,说在QT下面使用vtkLabeledDataMapper显示几千个标签速度很快,貌似是两个平台下面的渲染机制不同,不知道vtk什么时候解决这个问题。