【VTK】vtkTextActor位置设置问题

在之前的文章【vtk】获取vtkTextActor的长和宽 中我们知道了如何获取text的长和宽。
本文讨论vtkTextActor的size在变宽后,它的位置问题。
在vtkTextActor中,有提供SetPosition方法,从注释可以看出,它的参数对应着actor的左下角坐标。

/**
* Get the PositionCoordinate instance of vtkCoordinate.
* This is used for for complicated or relative positioning.
* The position variable controls the lower left corner of the Actor2D
*/
vtkViewportCoordinateMacro(Position);


#define vtkViewportCoordinateMacro(name) \
virtual vtkCoordinate *Get##name##Coordinate () \
{ \
    vtkDebugMacro(<< this->GetClassName() << " (" << this << "): returning " #name "Coordinate address " << this->name##Coordinate ); \
    return this->name##Coordinate; \
} \
virtual void Set##name(double x[2]) {this->Set##name(x[0],x[1]);} \
virtual void Set##name(double x, double y) \
{ \
    this->name##Coordinate->SetValue(x,y); \
} \
virtual double *Get##name() \
{ \
    return this->name##Coordinate->GetValue(); \
}

来看一个实验:https://github.com/theArcticOcean/CLib/tree/master/VTKLearn/textActor
这个工程显示了cone和text,红色的text在window的左下角。
接着在PushButton的click函数中写入这样的功能:

void Widget::on_pushButton_clicked()
{
    textActor->SetInput( "hello\nworld\nmac" );

    double size[2];
    vtkRendererCollection *rendererCollection = ui->qvtkWidget->GetRenderWindow()->GetRenderers();
    vtkRenderer *renderer = rendererCollection->GetFirstRenderer();
    textActor->GetSize( renderer, size );
    printf( "size: %lf, %lf\n", size[0], size[1] );
    //renderer->Render();
    ui->qvtkWidget->GetRenderWindow()->Render();
}

在button click事件中,再增添两行文字。
然后,重新获取size数据,我们发现,textActor的确变宽了。但是它的位置并没有变化,文字区域在向上伸展。

【VTK】vtkTextActor位置设置问题_第1张图片

注意刷新方式:不要使用 renderer->Render(),而要 ui->qvtkWidget->GetRenderWindow()->Render(); (这里的qvtkWidget是QVTKOpenGLWidget对象)
textActor默认的坐标系是什么?
获取方案1

vtkCoordinate *cd = textActor->GetMapper()->GetTransformCoordinate(); 
assert( nullptr != cd ); 
printf( "GetCoordinateSystemAsString: %s\n", cd->GetCoordinateSystemAsString() );

获取方案2

  /**
   * Return the actual vtkCoordinate reference that the mapper should use
   * to position the actor. This is used internally by the mappers and should
   * be overridden in specialized subclasses and otherwise ignored.
   */
  virtual vtkCoordinate *GetActualPositionCoordinate(void)
    { return this->PositionCoordinate; }

然后通过

vtkCoordinate *cd = textActor->GetActualPositionCoordinate();
assert( nullptr != cd );
printf( "GetCoordinateSystemAsString: %s\n", cd->GetCoordinateSystemAsString() );

我们得到:GetCoordinateSystemAsString: Viewport
即, vtkActor2D默认的坐标系统是viewport
但textActor->SetPosition2函数注释,我们得知右上角点的坐标是归一化viewport坐标,这一点值得注意。

  /**
   * Access the Position2 instance variable. This variable controls
   * the upper right corner of the Actor2D. It is by default
   * relative to Position and in normalized viewport coordinates.
   * Some 2D actor subclasses ignore the position2 variable
   */
  vtkViewportCoordinateMacro(Position2);

但是,如果textActor有设置对齐,比如竖直居中,从左到右显示等,那么SetPosition的作用点就 不是整片文字区域的左下角了
比如:

textActor->GetTextProperty()->SetJustificationToLeft();
textActor->GetTextProperty()->SetVerticalJustificationToCentered();

三行文字的显示:

【VTK】vtkTextActor位置设置问题_第2张图片

你可能感兴趣的:(C,C++学习笔记,VTK)