IdentifyHoles
VTKEx/Cxx/Meshes/IdentifyHoles
Description¶
This example fills the holes in a mesh and then extracts the filled holes as seprate regions.
The example proceeds as follow:
- Read the polydata.
- Fill the holes with vtkFillHolesFilter.
- Create a new polydata that contains the filled holes. To do this we rely on the fact that the fill holes filter stores the original cells first and then adds the new cells that fill the holes. Using vtkCellIterator, we skip the original cells and then continue iterating to obtain the new cells.
- Use vtkConnectivityFilter on the filled polydata to identify the individual holes.
Note
We have to use vtkConnectivtyFilter and not vtkPolyDataConnectivityFilter since the later does not create RegionIds cell data.
Question
If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum
Code¶
IdentifyHoles.cxx
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCellData.h>
#include <vtkCellIterator.h>
#include <vtkConnectivityFilter.h>
#include <vtkDataSetMapper.h>
#include <vtkFillHolesFilter.h>
#include <vtkGenericCell.h>
#include <vtkNamedColors.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolyDataNormals.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkUnstructuredGrid.h>
#include <vtkXMLPolyDataReader.h>
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "Usgae: " << argv[0] << " file.vtp e.g. Torso.vtp"
<< std::endl;
return EXIT_FAILURE;
}
auto colors = vtkSmartPointer<vtkNamedColors>::New();
auto reader = vtkSmartPointer<vtkXMLPolyDataReader>::New();
reader->SetFileName(argv[1]);
reader->Update();
// Fill the holes
auto fillHoles = vtkSmartPointer<vtkFillHolesFilter>::New();
fillHoles->SetInputConnection(reader->GetOutputPort());
fillHoles->SetHoleSize(1000.0);
// Make the triangle winding order consistent
auto normals = vtkSmartPointer<vtkPolyDataNormals>::New();
normals->SetInputConnection(fillHoles->GetOutputPort());
normals->ConsistencyOn();
normals->SplittingOff();
normals->Update();
normals->GetOutput()->GetPointData()->SetNormals(
reader->GetOutput()->GetPointData()->GetNormals());
// How many added cells
vtkIdType numOriginalCells = reader->GetOutput()->GetNumberOfCells();
vtkIdType numNewCells = normals->GetOutput()->GetNumberOfCells();
// Iterate over the original cells
auto it = normals->GetOutput()->NewCellIterator();
vtkIdType numCells = 0;
for (it->InitTraversal();
!it->IsDoneWithTraversal() && numCells < numOriginalCells;
it->GoToNextCell(), ++numCells)
{
}
std::cout << "Num original: " << numOriginalCells
<< ", Num new: " << numNewCells
<< ", Num added: " << numNewCells - numOriginalCells << std::endl;
auto holePolyData = vtkSmartPointer<vtkPolyData>::New();
holePolyData->Allocate(normals->GetOutput(), numNewCells - numOriginalCells);
holePolyData->SetPoints(normals->GetOutput()->GetPoints());
auto cell = vtkSmartPointer<vtkGenericCell>::New();
// The remaining cells are the new ones from the hole filler
for (; !it->IsDoneWithTraversal(); it->GoToNextCell())
{
it->GetCell(cell);
holePolyData->InsertNextCell(it->GetCellType(), cell->GetPointIds());
}
it->Delete();
// We have to use ConnectivtyFilter and not
// PolyDataConnectivityFilter since the later does not create
// RegionIds cell data.
auto connectivity = vtkSmartPointer<vtkConnectivityFilter>::New();
connectivity->SetInputData(holePolyData);
connectivity->SetExtractionModeToAllRegions();
connectivity->ColorRegionsOn();
connectivity->Update();
std::cout << "Found " << connectivity->GetNumberOfExtractedRegions()
<< " holes" << std::endl;
// Visualize
// Create a mapper and actor for the fill polydata
auto filledMapper = vtkSmartPointer<vtkDataSetMapper>::New();
filledMapper->SetInputConnection(connectivity->GetOutputPort());
filledMapper->SetScalarModeToUseCellData();
filledMapper->SetScalarRange(connectivity->GetOutput()
->GetCellData()
->GetArray("RegionId")
->GetRange());
auto filledActor = vtkSmartPointer<vtkActor>::New();
filledActor->SetMapper(filledMapper);
filledActor->GetProperty()->SetDiffuseColor(
colors->GetColor3d("Peacock").GetData());
// Create a mapper and actor for the original polydata
auto originalMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
originalMapper->SetInputConnection(reader->GetOutputPort());
auto backfaceProp = vtkSmartPointer<vtkProperty>::New();
backfaceProp->SetDiffuseColor(colors->GetColor3d("Banana").GetData());
auto originalActor = vtkSmartPointer<vtkActor>::New();
originalActor->SetMapper(originalMapper);
originalActor->SetBackfaceProperty(backfaceProp);
originalActor->GetProperty()->SetDiffuseColor(
colors->GetColor3d("Flesh").GetData());
originalActor->GetProperty()->SetRepresentationToWireframe();
// Create a renderer, render window, and interactor
auto renderer = vtkSmartPointer<vtkRenderer>::New();
auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(512, 512);
renderWindow->AddRenderer(renderer);
auto renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// Add the actor to the scene
renderer->AddActor(originalActor);
renderer->AddActor(filledActor);
renderer->SetBackground(colors->GetColor3d("Burlywood").GetData());
renderer->GetActiveCamera()->SetPosition(0, -1, 0);
renderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
renderer->GetActiveCamera()->SetViewUp(0, 0, 1);
renderer->GetActiveCamera()->Azimuth(60);
renderer->GetActiveCamera()->Elevation(30);
renderer->ResetCamera();
// Render and interact
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(IdentifyHoles)
find_package(VTK COMPONENTS
vtkvtkCommonColor
vtkvtkCommonCore
vtkvtkCommonDataModel
vtkvtkFiltersCore
vtkvtkFiltersModeling
vtkvtkIOXML
vtkvtkInteractionStyle
vtkvtkRenderingContextOpenGL2
vtkvtkRenderingCore
vtkvtkRenderingFreeType
vtkvtkRenderingGL2PSOpenGL2
vtkvtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
message("Skipping IdentifyHoles: ${VTK_NOT_FOUND_MESSAGE}")
return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
# old system
include(${VTK_USE_FILE})
add_executable(IdentifyHoles MACOSX_BUNDLE IdentifyHoles.cxx )
target_link_libraries(IdentifyHoles PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
add_executable(IdentifyHoles MACOSX_BUNDLE IdentifyHoles.cxx )
target_link_libraries(IdentifyHoles PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS IdentifyHoles
MODULES ${VTK_LIBRARIES}
)
endif ()
Download and Build IdentifyHoles¶
Click here to download IdentifyHoles and its CMakeLists.txt file. Once the tarball IdentifyHoles.tar has been downloaded and extracted,
cd IdentifyHoles/build
If VTK is installed:
cmake ..
If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:
cmake -DVTK_DIR:PATH=/home/me/vtk_build ..
Build the project:
make
and run it:
./IdentifyHoles
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.