[VTK] vtkTextActor position setting problem

Keywords: Mobile github

Previous articles [vtk] get the length and width of vtkTextActor We know how to get the length and width of text.
This paper discusses the position of vtkTextActor when its size becomes wider.
In vtkTextActor, SetPosition method is provided. From the annotation, it can be seen that its parameter corresponds to the lower left corner coordinate of the 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(); \
}

Let's look at an experiment: https://github.com/theArcticOcean/CLib/tree/master/VTKLearn/textActor
This project shows cone and text, red text in the lower left corner of the window.
Then write the following function in the click function of PushButton:

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();
}

In the button click event, add two more lines of text.
Then, we retrieve the size data, and we find that the textActor is indeed widened. But its position has not changed. The text area is extending upward.

Note the refresh method: do not use render - > render(), but UI - > qvtkWidget - > getrenderwindow() - > render(); (qvtkWidget here is QVTKOpenGLWidget object)
What is the default coordinate system of textActor?
Unfortunately, we can't use the 3D actor method to get the information of the default coordinate system:

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

But vtkActor2D has a similar approach:

  /**
   * 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; }

Then pass

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

We get: getcoordinatesystesasstring: viewport
That is, vtkActor2D's default coordinate system is viewport.
But textactor - > setposition2 function annotation, we know that the coordinate of the upper right corner is the normalized viewport coordinate, which is worth noting.

  /**
   * 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);

Posted by rblais666 on Tue, 10 Dec 2019 07:08:49 -0800