ExtractPolyLinesFromPolyData
vtk-examples/Cxx/PolyData/ExtractPolyLinesFromPolyData
Description¶
This example uses vtkCutter to create contour lines. It processes these lines with vtkStripper to create continuous poly lines. After exiting the example with the "e" key, the lines are printed.
Other languages
See (Python)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ExtractPolyLinesFromPolyData.cxx
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCellArray.h>
#include <vtkColor.h>
#include <vtkCutter.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlane.h>
#include <vtkPoints.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkStripper.h>
#ifdef VTK_CELL_ARRAY_V2
#include <vtkCellArrayIterator.h>
#endif // VTK_CELL_ARRAY_V2
int main(int, char*[])
{
// Define colors for example
vtkNew<vtkNamedColors> colors;
vtkColor3d lineColor = colors->GetColor3d("peacock");
vtkColor3d modelColor = colors->GetColor3d("silver");
vtkColor3d backgroundColor = colors->GetColor3d("wheat");
vtkNew<vtkSphereSource> modelSource;
vtkNew<vtkPlane> plane;
vtkNew<vtkCutter> cutter;
cutter->SetInputConnection(modelSource->GetOutputPort());
cutter->SetCutFunction(plane);
cutter->GenerateValues(10, -0.5, 0.5);
vtkNew<vtkPolyDataMapper> modelMapper;
modelMapper->SetInputConnection(modelSource->GetOutputPort());
vtkNew<vtkActor> model;
model->SetMapper(modelMapper);
model->GetProperty()->SetDiffuseColor(modelColor.GetData());
model->GetProperty()->SetInterpolationToFlat();
vtkNew<vtkStripper> stripper;
stripper->SetInputConnection(cutter->GetOutputPort());
stripper->JoinContiguousSegmentsOn();
vtkNew<vtkPolyDataMapper> linesMapper;
linesMapper->SetInputConnection(stripper->GetOutputPort());
vtkNew<vtkActor> lines;
lines->SetMapper(linesMapper);
lines->GetProperty()->SetDiffuseColor(lineColor.GetData());
lines->GetProperty()->SetLineWidth(3.0);
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(640, 480);
renderWindow->SetWindowName("ExtractPolyLinesFromPolyData");
vtkNew<vtkRenderWindowInteractor> interactor;
interactor->SetRenderWindow(renderWindow);
// Add the actors to the renderer
renderer->AddActor(model);
renderer->AddActor(lines);
renderer->SetBackground(backgroundColor.GetData());
renderer->GetActiveCamera()->Azimuth(-45);
renderer->GetActiveCamera()->Elevation(-22.5);
renderer->ResetCamera();
// This starts the event loop and as a side effect causes an initial
// render.
renderWindow->Render();
interactor->Start();
// Extract the lines from the polydata
vtkIdType numberOfLines = cutter->GetOutput()->GetNumberOfLines();
std::cout << "-----------Lines without using vtkStripper" << std::endl;
std::cout << "There are " << numberOfLines << " lines in the polydata"
<< std::endl;
numberOfLines = stripper->GetOutput()->GetNumberOfLines();
vtkPoints* points = stripper->GetOutput()->GetPoints();
vtkCellArray* cells = stripper->GetOutput()->GetLines();
std::cout << "-----------Lines using vtkStripper" << std::endl;
std::cout << "There are " << numberOfLines << " lines in the polydata"
<< std::endl;
#ifdef VTK_CELL_ARRAY_V2
// Newer versions of vtkCellArray prefer local iterators:
auto cellIter = vtk::TakeSmartPointer(cells->NewIterator());
for (cellIter->GoToFirstCell(); !cellIter->IsDoneWithTraversal();
cellIter->GoToNextCell())
{
std::cout << "Line " << cellIter->GetCurrentCellId() << ":\n";
vtkIdList* cell = cellIter->GetCurrentCell();
for (vtkIdType i = 0; i < cell->GetNumberOfIds(); ++i)
{
double point[3];
points->GetPoint(cell->GetId(i), point);
std::cout << "\t(" << point[0] << ", " << point[1] << ", " << point[2]
<< ")" << std::endl;
}
}
#else // VTK_CELL_ARRAY_V2
// Older implementations of vtkCellArray use internal iterator APIs (not
// thread safe):
vtkIdType* indices;
vtkIdType numberOfPoints;
unsigned int lineCount = 0;
for (cells->InitTraversal(); cells->GetNextCell(numberOfPoints, indices);
lineCount++)
{
std::cout << "Line " << lineCount << ": " << std::endl;
for (vtkIdType i = 0; i < numberOfPoints; i++)
{
double point[3];
points->GetPoint(indices[i], point);
std::cout << "\t(" << point[0] << ", " << point[1] << ", " << point[2]
<< ")" << std::endl;
}
}
#endif // VTK_CELL_ARRAY_V2
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(ExtractPolyLinesFromPolyData)
find_package(VTK COMPONENTS
vtkCommonColor
vtkCommonCore
vtkCommonDataModel
vtkFiltersCore
vtkFiltersSources
vtkInteractionStyle
vtkRenderingContextOpenGL2
vtkRenderingCore
vtkRenderingFreeType
vtkRenderingGL2PSOpenGL2
vtkRenderingOpenGL2
QUIET
)
if (NOT VTK_FOUND)
message("Skipping ExtractPolyLinesFromPolyData: ${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(ExtractPolyLinesFromPolyData MACOSX_BUNDLE ExtractPolyLinesFromPolyData.cxx )
target_link_libraries(ExtractPolyLinesFromPolyData PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
add_executable(ExtractPolyLinesFromPolyData MACOSX_BUNDLE ExtractPolyLinesFromPolyData.cxx )
target_link_libraries(ExtractPolyLinesFromPolyData PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ExtractPolyLinesFromPolyData
MODULES ${VTK_LIBRARIES}
)
endif ()
Download and Build ExtractPolyLinesFromPolyData¶
Click here to download ExtractPolyLinesFromPolyData and its CMakeLists.txt file. Once the tarball ExtractPolyLinesFromPolyData.tar has been downloaded and extracted,
cd ExtractPolyLinesFromPolyData/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:
./ExtractPolyLinesFromPolyData
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.