The article discuss an issue about set position for vtkTextActor object. The class vtkTextActor provides interface SetPosition, its parameter controls object’s left-bottom coordinate.
/**
* 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 for vtkTextActor, text actor.
The window in it shows a cone and text, the red text shows at the left-bottom corner.
Change strings for text actor in QPushButton’s click function.
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();
}
Add two more strings lines after the click event, and get size of text actor, we can find the text actor become wider but not change actor’s position. The text area is stretching up.
Use ui->qvtkWidget->GetRenderWindow()->Render()
rather than renderer->Render()
.
What’s the default coordinating system of vtkTextActor?
Solution A
vtkCoordinate *cd = textActor->GetMapper()->GetTransformCoordinate();
assert( nullptr != cd );
printf( "GetCoordinateSystemAsString: %s\n", cd->GetCoordinateSystemAsString() );
Solution B
vtkCoordinate *cd = textActor->GetActualPositionCoordinate();
assert( nullptr != cd );
printf( "GetCoordinateSystemAsString: %s\n", cd->GetCoordinateSystemAsString() );
We can get the result GetCoordinateSystemAsString: Viewport
. So Viewport is vtkActor2D’s default coordinate system. But the comment for textActor->SetPosition2
tells us the right-top corner’s coordinate is value in normalized viewport coordinates.
Change aligns horizontally and vertically, for example, center vertically.
textActor->GetTextProperty()->SetJustificationToLeft();
textActor->GetTextProperty()->SetVerticalJustificationToCentered();
New layout: