- 直接使用vtkConnectivityFilter提取连通区域,渲染上没问题,但是打印出polydata中的点数,发现跟原始数据是一致的。
for (int i = 0; i < numRegions; ++i){vtkSmartPointer<vtkConnectivityFilter> connectivityFilter = vtkSmartPointer<vtkConnectivityFilter>::New();connectivityFilter->SetInputData(polyData);connectivityFilter->SetExtractionModeToSpecifiedRegions();connectivityFilter->InitializeSpecifiedRegionList();connectivityFilter->AddSpecifiedRegion(i);connectivityFilter->Update();cout << connectivityFilter->GetOutput()->GetNumberOfPoints() << endl;}
2. 正确的做法是后街一个vtkCleanPolyData过滤器
for (int i = 0; i < numRegions; ++i){vtkSmartPointer<vtkConnectivityFilter> connectivityFilter = vtkSmartPointer<vtkConnectivityFilter>::New();connectivityFilter->SetInputData(polyData);connectivityFilter->SetExtractionModeToSpecifiedRegions();connectivityFilter->InitializeSpecifiedRegionList();connectivityFilter->AddSpecifiedRegion(i);connectivityFilter->Update();// 使用vtkCleanPolyData删除孤立点vtkSmartPointer<vtkCleanPolyData> cleanPolyData = vtkSmartPointer<vtkCleanPolyData>::New();cleanPolyData->SetInputData(connectivityFilter->GetOutput());cleanPolyData->Update();cout << cleanPolyData->GetOutput()->GetNumberOfPoints() << endl;}