如何使用 VTK 加载 STL 文件显示 3D 模型?
void loadFileWhitSTL(vtkSmartPointer<vtkRenderer> renderer);
void createText(vtkSmartPointer<vtkRenderer> renderer);
void createAxes(vtkSmartPointer<vtkRenderer> renderer);
void createGround(vtkSmartPointer<vtkRenderer> renderer);
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(.2, .2, .2);
renderer->ResetCameraClippingRange();
renderWindow->Render();
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
void loadFileWhitSTL(vtkSmartPointer<vtkRenderer> renderer)
{
const char *path = R"(D:\Qt\Qmake\VTK\stl\5e1bd9bc2958e.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("20200311");
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->GetXAxisCaptionActor2D()->SetWidth(0.03);
axes->GetYAxisCaptionActor2D()->SetWidth(0.03);
axes->GetZAxisCaptionActor2D()->SetWidth(0.03);
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);
}
转载请注明出处并保持作品的完整性,谢谢