I'm trying to overlay text on an image in vtk when a certain button is clicked. I use a vtkTextActor
, vtkTextRepresentation
, and vtkTextWidget
to achieve this.
Here is my code:
// set up actor
textActor->SetInput(label.toStdString().c_str());
textActor->GetTextProperty()->SetColor(1.0, 0.0, 0.0);
// set location of widget
textRepresentation->GetPositionCoordinate()->SetCoordinateSystemToWorld();
textRepresentation->GetPositionCoordinate()->SetValue(200,200);
// set up widget
textWidget->SetInteractor(imageInteractor);
textWidget->SetRepresentation(textRepresentation);
textWidget->SetDefaultRenderer(rend);
textWidget->SetTextActor(textActor);
textWidget->SelectableOff();
// render widget
rend->GetRenderWindow()->Render();
textWidget->On();
textWidget->GetTextActor()->SetWidth(vc[0]);
textWidget->GetTextActor()->SetHeight(vc[1]);
rend->GetRenderWindow()->Render(); // crash
If I comment out the SetWidth
and SetHeight
function calls, the program works fine and the text widget pops up rather small. However when I try to adjust the width and height of textActor
it crashes during the render call. Note that vc[0]
and vc[1]
are relative to the viewport coordinate system, I calculate them in a different function.
I can adjust the width and height of textActor
BEFORE the textWidget->On()
call, and the widget will still pop up, but it ignores my width and height changes. I cannot seem to change the box dimensions programatically. Am I doing something wrong or is this a vtk bug?
Here is an example. I'd like the text to appear bigger (changing the font size wont work, I think its because the actor itself is too small).
UPDATE
I have not figured out why its crashing or how to fix it. As a workaround, I've been adjusting the Position2Coordinate
value in my textRepresentation
. The rendering call seems to be readjusting my set values. Can anyone explain why or how to fix this?
I'm not sure what's causing the crash, but I found a workaround. By using textRepresentation->SetPosition(x1,x2)
and textRepresentation->SetPosition2(width,height)
, I'm able to control the location and size of the widget. Its important to note that SetPosition2()
is relative to SetPosition()
meaning instead of a coordinate, the second position should be thought of a width and height. All of my values are in the Normalized Viewport coordinate system.