We have a vtkTextActor object in the render window now. How to put it at the right-top corner or the left-bottom corner in the render window according to the render and text rectangle’s size?
The following code snippet shows the different scenes described above.
#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkTextActor.h>
#include <vtkTextProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <QString>
#define vtkSPtr vtkSmartPointer
#define vtkSPtrNew(Var, Type) vtkSPtr<Type> Var = vtkSPtr<Type>::New();
using namespace std;
int main()
{
vtkSPtrNew( infoActor, vtkTextActor );
QString str = "\"Banana\": long curved fruit\n";
str += "\"Apple\": a round fruit\n";
infoActor->SetInput( str.toLocal8Bit().data() );
infoActor->GetTextProperty()->SetFontSize(18);
infoActor->GetTextProperty()->SetJustificationToLeft();
infoActor->GetTextProperty()->SetVerticalJustificationToBottom();
infoActor->GetTextProperty()->BoldOn();
infoActor->GetTextProperty()->SetColor(1, 1, 1);
//infoActor->GetTextProperty()->SetFontFamilyToArial();
vtkSPtrNew( renderer, vtkRenderer );
renderer->AddActor( infoActor );
renderer->SetBackground( 0, 0, 0 );
vtkSPtrNew( renderWindow, vtkRenderWindow );
renderWindow->AddRenderer( renderer );
renderWindow->SetSize( 800, 400 );
vtkSPtrNew( renderWindowInteractor, vtkRenderWindowInteractor );
renderWindowInteractor->SetRenderWindow( renderWindow );
renderer->ResetCamera();
renderWindow->Render();
int *winSize = renderWindow->GetSize();
double size[2] = { 0 };
infoActor->GetSize( renderer, size );
infoActor->SetPosition( winSize[0] - size[0] - 60, winSize[1] - size[1] - 20 );
renderWindowInteractor->Start();
return 0;
}
The right-top position:
If we want to put the text in the left-bottom corner, there are a few options that need configuring.
double size[2] = { 0 };
infoActor->GetSize( renderer, size );
infoActor->SetPosition( 60, 20 );
If we remove offsets in x and y directions for text actor position setting, then we have the following result.
It indicates the text actor has extra space between the left-bottom of text and its boundary.
We can add an image and show it by actor2D, then the space is obvious.
vtkSPtrNew( reader, vtkPNGReader );
reader->SetFileName( "/Users/weiyang/Desktop/person.png" );
reader->Update();
vtkSPtrNew( imageMapper, vtkImageMapper );
imageMapper->SetInputData( reader->GetOutput() );
imageMapper->Update();
imageMapper->SetDebug( true );
int *extend = reader->GetOutput()->GetExtent();
printf( "extend: (%d, %d, %d, %d , %d, %d)\n", extend[0], extend[1], extend[2], extend[3], extend[4], extend[5] );
imageMapper->GetInputAlgorithm()->UpdateInformation();
double *range = imageMapper->GetInput()->GetScalarRange();
imageMapper->SetColorWindow( range[1] - range[0] );
imageMapper->SetColorLevel(0.5 * (range[1] + range[0]));
vtkSPtrNew( actor2D, vtkActor2D );
actor2D->SetMapper( imageMapper );
actor2D->SetPosition( 0, 0 );
renderer->AddActor2D( actor2D );