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})
效果图