vtk 加载 stl

vtk 加载 stl

viewSTL.cpp

#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkSmartPointer.h>
#include <vtkVectorText.h>
#include <vtkAxesActor.h>
#include <vtkPlane.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPlaneSource.h>
#include <vtkProperty.h>
#include <vtkCaptionActor2D.h>
#include <vtkSTLReader.h>
#include <vtkInteractorStyleTrackballCamera.h>

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

void loadFileWhitSTL(vtkSmartPointer<vtkRenderer> renderer)
{
    const char *path = "/home/tiger/project/YDMS/YdMedicalSystem/NailSTL/NailType01/45mm/M4.5x45_11.65.STL";

    vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();
    reader->SetFileName(path);
    reader->Update();

    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(reader->GetOutputPort());

    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
    renderer->AddActor(actor);
}

void createText(vtkSmartPointer<vtkRenderer> renderer)
{
    vtkSmartPointer<vtkVectorText> text = vtkSmartPointer<vtkVectorText>::New();
    text->SetText("21530311");
    text->Update();

    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(text->GetOutputPort());

    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
    actor->SetPosition(5.0, 5.0, 30.0);
    actor->SetScale(10.0);
    actor->GetProperty()->SetPointSize(48);
    actor->GetProperty()->SetColor(1.0, 0.0, 0.0);
    renderer->AddActor(actor);
}

void createAxes(vtkSmartPointer<vtkRenderer> renderer)
{
    //轴
    vtkSmartPointer<vtkAxesActor> axes = vtkSmartPointer<vtkAxesActor>::New();
    axes->SetTotalLength(150.0, 100.0, 50.0);
    axes->SetShaftType(vtkAxesActor::CYLINDER_SHAFT);//设置轴类型圆柱形状
    axes->SetCylinderRadius(0.01);
    axes->SetConeRadius(0.2);
    axes->GetXAxisCaptionActor2D()->SetWidth(0.02);
    axes->GetYAxisCaptionActor2D()->SetWidth(0.04);
    axes->GetZAxisCaptionActor2D()->SetWidth(0.08);

    renderer->AddActor(axes);
}

//底板
void createGround(vtkSmartPointer<vtkRenderer> renderer)
{
    vtkSmartPointer<vtkPlaneSource> plane = vtkSmartPointer<vtkPlaneSource>::New();
    plane->SetXResolution(50);
    plane->SetYResolution(50);
    plane->SetCenter(0, 0, 0);
    plane->SetNormal(0, 0, 1);

    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(plane->GetOutputPort());
    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
    actor->SetScale(300.0, 300.0, 1.0);
    actor->GetProperty()->SetRepresentationToWireframe();

    renderer->AddActor(actor);
}

int main(int, char *[])
{
    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);
    vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
    renderWindowInteractor->SetInteractorStyle(style);

    loadFileWhitSTL(renderer);
    createText(renderer);
    createAxes(renderer);
    createGround(renderer);

    renderer->SetBackground(0.2, 0.2, 0.2);
    renderer->ResetCameraClippingRange();

    renderWindow->Render();
    renderWindowInteractor->Initialize();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}


CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

project(learnVTK)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

# viewSTL
add_executable(viewSTL viewSTL.cpp)
target_link_libraries(viewSTL  ${VTK_LIBRARIES})

效果图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值