天天看點

通過VTK顯示STL模型

void showStlModel(const std::string& modelPath){
    vtkSmartPointer<vtkSTLReader> stlReader = vtkSmartPointer<vtkSTLReader>::New();
    stlReader->SetFileName(modelPath.c_str());//modelPath是stl模型在電腦裡面的存儲路徑
    stlReader->Update();

    if (!_cellLocator) {
        _cellLocator = vtkSmartPointer<vtkCellLocator>::New();
    }
    _cellLocator->SetDataSet(stlReader->GetOutput());
    _cellLocator->BuildLocator();

    if (!_stlModel) {
        _stlModel = vtkSmartPointer<vtkPolyData>::New();
    }
    _stlModel = stlReader->GetOutput();
    auto normals = vtkSmartPointer<vtkTriangleMeshPointNormals>::New();
    normals->SetInputData(_stlModel);
    normals->Update();
    _stlModel = normals->GetOutput();
    auto modelMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    _modelActor->SetMapper(modelMapper);
    modelMapper->SetInputConnection(stlReader->GetOutputPort());
    _qvtkWidget->GetRenderWindow()->Render();
}
           
VTK