3D annotation

效果如图:




TCL代码如下

package require vtk

vtkConeSource cone

vtkPolyDataMapper coneMapper
coneMapper SetInputConnection [cone GetOutputPort]

vtkActor coneActor
coneActor SetMapper coneMapper

# ------ start to add 3D text ------
vtkVectorText text3D
    text3D SetText "This is a cone."

vtkTransform text3DTransform
    text3DTransform SetInput [coneActor GetUserTransform]
    text3DTransform Translate -0.5 1 0
    text3DTransform Scale 0.1 0.1 0.1

# vtkTransformPolyDataFilter fetch transform and vtkAlgorithmOutput.
vtkTransformPolyDataFilter text3DDataFilter
    text3DDataFilter SetTransform text3DTransform
    text3DDataFilter SetInputConnection [text3D GetOutputPort]

vtkPolyDataMapper text3DMapper
    text3DMapper SetInputConnection [text3DDataFilter GetOutputPort]

vtkActor text3DActor
    text3DActor SetMapper text3DMapper
# ------ adding 3D text end -----

vtkRenderer ren1
ren1 AddActor coneActor
ren1 AddActor text3DActor
ren1 SetBackground 0 0 0

vtkRenderWindow renWin
renWin AddRenderer ren1

vtkRenderWindowInteractor iren
iren SetRenderWindow renWin

# Withdraw the default tk window
wm withdraw .

iren Start

(0,0,0)是cone的中心,在不移动3D annotation的情况下,第一个字符是占据着cone的中心位置。
Just like this:



2D annotation

将2D lable放到cone下方不动

# ------ start to add 2D text ------
vtkTextSource text2D
text2D SetText "This is a cone."
vtkPolyDataMapper2D text2DMapper
text2DMapper SetInputConnection [text2D GetOutputPort]

vtkActor2D text2DActor
text2DActor SetMapper text2DMapper

# Why can't convert world to display coordinate by tcl?
#set PPoint [coneActor GetPosition]
# set PPoint0 [lindex $PPoint 0]
# set PPoint1 [lindex $PPoint 1]
# set PPoint2 [lindex $PPoint 2]
# set PPoint0 [expr $PPoint0 - 0.5]
# set PPoint1 [expr $PPoint1 - 0.5]
#
#ren1 SetWorldPoint $PPoint0 $PPoint1 $PPoint2 1.0
#ren1 WorldToDisplay
#set Pos [ren1 GetDisplayPoint]
# set Pos0 [lindex $Pos 0]
# set Pos1 [lindex $Pos 1]
#
#puts $PPoint0 ;# -0.5
#puts $PPoint1 ;# -0.5
#puts $PPoint2 ;#
#puts $Pos ;# 0.0 0.0 0.99800998
#puts $Pos0 ;# 0
#puts $Pos1 ;# 0

text2DActor SetPosition 62.628721 62.628721 ;# the value is calculated by cpp
ren1 AddActor2D text2DActor
# ------ add 2D text end -------

中间的坐标转换使用TC一直没有成功,下面是对应的CPP代码:

// ----------------- Position text2DActor -------------------
double *pos = actor->GetPosition();
double newPos[3] = { pos[0], pos[1], pos[2] };
newPos[0] = newPos[0] - 0.5;
newPos[1] = newPos[1] - 0.5;
renderer->SetWorldPoint( newPos );
renderer->WorldToDisplay();
printf( "GetPosition: %lf, %lf, %lf\n", actor->GetPosition()[0], actor->GetPosition()[1], actor->GetPosition()[2] );
pos = renderer->GetDisplayPoint();
printf( "pos: %lf, %lf, %lf\n", pos[0], pos[1], pos[2] );
text2DActor->SetPosition( pos[0], pos[1] );
// ----------------- Position text2DActor end -------------------



将2D label就放到cone的下方(三维空间)

# ------ start to add 2D text ------
vtkTextSource text2D
text2D SetText "This is a cone."

vtkTransform text2DTransform
text2DTransform SetInput [coneActor GetUserTransform]
text2DTransform Translate -0.5 -1 0
text2DTransform Scale 0.01 0.01 0.01

vtkTransformPolyDataFilter text2DDataFilter
text2DDataFilter SetTransform text2DTransform
text2DDataFilter SetInputConnection [text2D GetOutputPort]

vtkCoordinate coords
coords SetCoordinateSystemToWorld

vtkPolyDataMapper2D text2DMapper
text2DMapper SetInputConnection [text2DDataFilter GetOutputPort]
text2DMapper SetTransformCoordinate coords

vtkActor2D text2DActor
text2DActor SetMapper text2DMapper
# ------ add 2D text end -------



分类: C plus plusTCL

发表评论

电子邮件地址不会被公开。 必填项已用*标注