Put a cone and a string in the renderer window, the cone will be smaller or bigger if we zoom in/out. The string keeps the same size as we change the camera’s status because it’s a 2D actor.
I will show how to scale the string to follow the 3D model cone in the renderer by the following example.
Create class customIteractorStyle which inherited vtkInteractorStyleTrackballCamera and define our function void KeepTextActorSize();. Take a length of the vector from the world coordinate system and convert it to a new length of a vector in the display coordinate system, use the value to set the height of the text. We must take the same vector from the renderer as we update the size of the text.

main.cpp

#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkHull.h>
#include <vtkProperty.h>
#include <vtkCamera.h>
#include <vtkPlane.h>
#include <vtkPNGReader.h>
#include <vtkSTLReader.h>
#include <vtkImageMapper.h>
#include <vtkActor2D.h>
#include <vtkAlgorithm.h>
#include <vtkTextActor.h>
#include <vtkTextProperty.h>
#include <vtkCamera.h>

#include "customIteractorStyle.h"
#include "point.hpp"

#define vtkSPtr vtkSmartPointer
#define vtkSPtrNew(Var, Type) vtkSPtr<Type> Var = vtkSPtr<Type>::New();


using namespace std;


vtkSmartPointer<vtkRenderer> renderer;
vtkSmartPointer<vtkCamera> camera;


int main()
{
    setbuf( stdout, nullptr );

    vtkSPtrNew( cone, vtkConeSource );
    vtkSPtrNew( mapper, vtkPolyDataMapper );
    mapper->SetInputConnection( cone->GetOutputPort() );

    vtkSPtrNew( coneActor, vtkActor );
    coneActor->SetMapper( mapper );

    vtkSPtrNew( textActor, vtkTextActor );
    textActor->GetTextProperty()->SetFontSize( 30 );
    textActor->GetTextProperty()->BoldOn();
    textActor->SetTextScaleModeToProp();
    textActor->SetMinimumSize( 1, 1 );
    textActor->SetInput( "hello world!" );

    renderer = vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor2D( textActor );
    renderer->AddActor( coneActor );
    renderer->SetBackground( 0, 0, 0 );


    vtkSPtrNew( renderWindow, vtkRenderWindow );
    renderWindow->AddRenderer( renderer );
    renderWindow->SetSize( 600, 400 );


    vtkSPtrNew( renderWindowInteractor, vtkRenderWindowInteractor );
    renderWindowInteractor->SetRenderWindow( renderWindow );

    vtkSPtrNew( iStyle, customIteractorStyle );
    iStyle->Setm_TextActor( textActor );
    iStyle->Setm_ConeActor( coneActor );
    iStyle->Setm_Render( renderer );
    iStyle->Setm_Interactor( renderWindowInteractor );
    renderWindowInteractor->SetInteractorStyle( iStyle );

    renderer->ResetCamera();
    renderWindow->Render();
    iStyle->KeepTextActorSize();


    renderWindowInteractor->Start();
    return 0;
}

customIteractorStyle.h

#pragma once

#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkTextActor.h>
#include <vtkTransform.h>

#define CPP_SET_MACRO(name,type) \
  void Set##name(type _arg) \
  { \
    if (this->name != _arg) \
    { \
    this->name = _arg; \
    } \
  }

#define vtkSPtr vtkSmartPointer
#define vtkSPtrNew(Var, Type) vtkSPtr<Type> Var = vtkSPtr<Type>::New();

class customIteractorStyle: public vtkInteractorStyleTrackballCamera
{
public:
    static customIteractorStyle *New(){ return new customIteractorStyle(); }
    void OnMouseWheelForward() override;
    void OnMouseWheelBackward() override;
    void OnLeftButtonDown() override;
    void Rotate() override;
    void KeepTextActorSize();

    CPP_SET_MACRO( m_TextActor, vtkTextActor *);
    CPP_SET_MACRO( m_ConeActor, vtkActor * );
    CPP_SET_MACRO( m_Render, vtkSmartPointer<vtkRenderer> );
    CPP_SET_MACRO( m_Interactor, vtkRenderWindowInteractor *);
protected:    
    customIteractorStyle();
    ~customIteractorStyle() override;

    vtkTextActor *m_TextActor;
    vtkActor *m_ConeActor;
    vtkSmartPointer<vtkRenderer> m_Render;
    vtkRenderWindowInteractor *m_Interactor;
};

customIteractorStyle.cpp

#include "customIteractorStyle.h"
#include "point.hpp"

#include <vtkCamera.h>

void customIteractorStyle::OnMouseWheelForward()
{
    vtkInteractorStyleTrackballCamera::OnMouseWheelForward();
    KeepTextActorSize();
}

void customIteractorStyle::OnMouseWheelBackward()
{
    vtkInteractorStyleTrackballCamera::OnMouseWheelBackward();
    KeepTextActorSize();
}

void customIteractorStyle::OnLeftButtonDown()
{
    vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}

void customIteractorStyle::Rotate()
{
    vtkInteractorStyleTrackballCamera::Rotate();
}

customIteractorStyle::customIteractorStyle()
{
    m_ConeActor = nullptr;
    m_TextActor = nullptr;
    m_Interactor = nullptr;
}

customIteractorStyle::~customIteractorStyle()
{
    m_ConeActor = nullptr;
    m_TextActor = nullptr;
}

void customIteractorStyle::KeepTextActorSize()
{    
    Point tmpPt1( m_ConeActor->GetCenter() );
    double floatArray[4] = { tmpPt1[0], tmpPt1[1], tmpPt1[2], 0 };
    m_Render->SetWorldPoint( floatArray );
    m_Render->WorldToDisplay();
    Point disPos( m_Render->GetDisplayPoint() );
    Point viewUp( m_Render->GetActiveCamera()->GetViewUp() );
    viewUp.Unit();
    Point tmpPt2 = tmpPt1 + viewUp;
    double tmpW1[4] = { tmpPt1[0], tmpPt1[1], tmpPt1[2], 0 };
    m_Render->SetWorldPoint( tmpW1 );
    m_Render->WorldToDisplay();
    tmpPt1 = Point( m_Render->GetDisplayPoint() );
    double tmpW2[4] = { tmpPt2[0], tmpPt2[1], tmpPt2[2], 0 };
    m_Render->SetWorldPoint( tmpW2 );
    m_Render->WorldToDisplay();
    tmpPt2 = Point( m_Render->GetDisplayPoint() );
    Point len = (tmpPt2 - tmpPt1);
    m_TextActor->SetHeight( len.Length()*0.0005 );
    m_TextActor->SetDisplayPosition( disPos[0], disPos[1] );
}

Output:

scale text
Categories: VTK

0 0 votes
Article Rating
Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
KennyG
KennyG
1 year ago

hello could you pls leave code for point.hpp?

Tex To PDF
: convert the Latex file which suffix is tex to a PDF file

X
2
0
Would love your thoughts, please comment.x
()
x