在加載stl文件的時候有些文件的mesh太多致使加載速度太慢須要進行下采樣,以便提升加載速度,可使用vtk提供的vtkDecimatePro進行下采樣.app
#include <vtkPolyData.h> #include <vtkSTLReader.h> #include <vtkSmartPointer.h> #include <vtkPolyDataMapper.h> #include <vtkActor.h> #include <vtkRenderWindow.h> #include <vtkRenderer.h> #include <vtkRenderWindowInteractor.h> #include <vtk-8.1/vtkCleanPolyData.h> #include <vtk-8.1/vtkDecimatePro.h> #include <vtk-8.1/vtkSTLWriter.h> #include "logger/Logger.h" int main(int argc, char *argv[]) { std::string inputFilename = "large.stl"; vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New(); reader->SetFileName(inputFilename.c_str()); reader->Update(); vtkSmartPointer<vtkPolyData> stlData = reader->GetOutput(); LOG_INFO << stlData->GetNumberOfPoints(); //只保留2000個點 double d = 1-2000.0/stlData->GetNumberOfPoints(); vtkSmartPointer<vtkDecimatePro> decimate = vtkSmartPointer<vtkDecimatePro>::New(); decimate->SetInputData(stlData); decimate->PreserveTopologyOff(); decimate->SplittingOn(); decimate->BoundaryVertexDeletionOn(); decimate->SetTargetReduction(d); decimate->Update(); vtkSmartPointer<vtkPolyData> decimated = vtkSmartPointer<vtkPolyData>::New(); decimated->ShallowCopy(decimate->GetOutput()); LOG_INFO << decimated->GetNumberOfPoints(); std::string filename = "small.stl"; vtkSmartPointer<vtkSTLWriter> stlWriter = vtkSmartPointer<vtkSTLWriter>::New(); stlWriter->SetFileName(filename.c_str()); stlWriter->SetInputData(decimated); stlWriter->Write(); return 0; }
decimate->PreserveTopologyOff();ci
decimate->SplittingOn();get
decimate->BoundaryVertexDeletionOn();input
這幾行設置是確保下采樣比例可以到指定比例,不然可能沒法到達你設置的採樣比.string