[C++][vtk][转载]利用vtk绘制文本

Posted FL1623863129

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[C++][vtk][转载]利用vtk绘制文本相关的知识,希望对你有一定的参考价值。

测试环境:

vs2019专业版

vtk9.1

代码:

#include <vtkSmartPointer.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkTextActor.h>
#include <vtkTextProperty.h>
#include <vtkVectorText.h>
#include <vtkFollower.h>
#include <vtkInteractorStyle.h>
#include <vtkAutoInit.h> 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
const char* const HELLO_TEXT = "Hello world";

int main(int argc, char* argv[])


    auto sphereSource =
        vtkSmartPointer<vtkSphereSource>::New();
    sphereSource->SetRadius(3.0);
    sphereSource->SetCenter(0.0, 0.0, 0.0);

    auto sphereMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
    auto sphereActor =
        vtkSmartPointer<vtkActor>::New();
    sphereActor->SetMapper(sphereMapper);
    sphereActor->GetProperty()->SetOpacity(0.3);

    auto renderer =
        vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(sphereActor);
    auto renWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
    renWindow->SetSize(400, 400);
    renWindow->AddRenderer(renderer);
    auto interactor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    interactor->SetRenderWindow(renWindow);

    int dtype = 2;
    if (dtype == 1)
    
        auto textActor =
            vtkSmartPointer<vtkTextActor>::New();
        textActor->SetInput(HELLO_TEXT);
        textActor->SetPosition2(20, 40);
        textActor->GetTextProperty()->SetColor(1.0, 0.0, 0.0);
        textActor->GetTextProperty()->SetFontSize(24);
        renderer->AddActor2D(textActor);
    
    else if (dtype == 2 || dtype == 3)
    
        auto textSource =
            vtkSmartPointer<vtkVectorText>::New();
        textSource->SetText(HELLO_TEXT);
        auto textMapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();
        textMapper->SetInputConnection(textSource->GetOutputPort());
        if (dtype == 2)
        
            auto textActor =
                vtkSmartPointer<vtkActor>::New();
            textActor->SetMapper(textMapper);
            textActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
            renderer->AddActor(textActor);
        
        else if (dtype == 3)
        
            auto textFollower =
                vtkSmartPointer<vtkFollower>::New();
            textFollower->SetMapper(textMapper);
            textFollower->GetProperty()->SetColor(1.0, 0.0, 0.0);
            textFollower->SetCamera(renderer->GetActiveCamera());
            renderer->AddActor(textFollower);
        
    

    renWindow->Render();
    renderer->ResetCamera();
    renWindow->Render();
    interactor->Start();

    return EXIT_SUCCESS;

结果:

 

以上是关于[C++][vtk][转载]利用vtk绘制文本的主要内容,如果未能解决你的问题,请参考以下文章

[C++][vtk][转载]vtk-8.2.0安装后测试代码示例2.1_RenderCylinde.cpp代码

VTK面绘制(SR)与体绘制(VR)

VTK初学一,线段的绘制c_Line

VTK初学一,线段的CellArray绘制c_Line_CellArray

VTK绘制Bounding Box

VTK切割功能汇总