CapClip
Description¶
This example shows how to generate a "cap" on a clipped vtkPolyData. After clipping with vtkClipPolyData, it uses a clever ''trick'' to convert polylines into polygons. If a polydata file is provided, it will cap it. Otherwise it caps a clipped sphere.
Warning
The clipping is done with a scalar field provided by vtkPlane and results may depend on the resolution of the input vtkPolyData.
Style
This example collects all of the color definition in the beginning of the example. This makes it easier to make changes to the colors without having to search the code.
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¶
CapClip.cxx
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkClipPolyData.h>
#include <vtkDataSetMapper.h>
#include <vtkFeatureEdges.h>
#include <vtkNamedColors.h>
#include <vtkPlane.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkStripper.h>
#include <vtkXMLPolyDataReader.h>
// Readers
#include <vtkBYUReader.h>
#include <vtkOBJReader.h>
#include <vtkPLYReader.h>
#include <vtkPolyDataReader.h>
#include <vtkSTLReader.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkSphereSource.h>
namespace {
vtkSmartPointer<vtkPolyData> ReadPolyData(std::string const& fileName);
}
//
// Demonstrate the use of clipping and capping on polyhedral data
//
int main(int argc, char* argv[])
{
// Define colors
auto colors = vtkSmartPointer<vtkNamedColors>::New();
auto backgroundColor = colors->GetColor3d("steel_blue");
auto boundaryColor = colors->GetColor3d("banana");
auto clipColor = colors->GetColor3d("tomato");
// PolyData to process
auto polyData = ReadPolyData(argc > 1 ? argv[1] : "");
auto plane = vtkSmartPointer<vtkPlane>::New();
plane->SetOrigin(polyData->GetCenter());
plane->SetNormal(1.0, -1.0, -1.0);
auto clipper = vtkSmartPointer<vtkClipPolyData>::New();
clipper->SetInputData(polyData);
clipper->SetClipFunction(plane);
clipper->SetValue(0);
clipper->Update();
polyData = clipper->GetOutput();
auto clipMapper = vtkSmartPointer<vtkDataSetMapper>::New();
clipMapper->SetInputData(polyData);
auto clipActor = vtkSmartPointer<vtkActor>::New();
clipActor->SetMapper(clipMapper);
clipActor->GetProperty()->SetDiffuseColor(clipColor.GetData());
clipActor->GetProperty()->SetInterpolationToFlat();
clipActor->GetProperty()->EdgeVisibilityOn();
// Now extract feature edges
auto boundaryEdges = vtkSmartPointer<vtkFeatureEdges>::New();
boundaryEdges->SetInputData(polyData);
boundaryEdges->BoundaryEdgesOn();
boundaryEdges->FeatureEdgesOff();
boundaryEdges->NonManifoldEdgesOff();
boundaryEdges->ManifoldEdgesOff();
auto boundaryStrips = vtkSmartPointer<vtkStripper>::New();
boundaryStrips->SetInputConnection(boundaryEdges->GetOutputPort());
boundaryStrips->Update();
// Change the polylines into polygons
auto boundaryPoly = vtkSmartPointer<vtkPolyData>::New();
boundaryPoly->SetPoints(boundaryStrips->GetOutput()->GetPoints());
boundaryPoly->SetPolys(boundaryStrips->GetOutput()->GetLines());
auto boundaryMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
boundaryMapper->SetInputData(boundaryPoly);
auto boundaryActor = vtkSmartPointer<vtkActor>::New();
boundaryActor->SetMapper(boundaryMapper);
boundaryActor->GetProperty()->SetDiffuseColor(boundaryColor.GetData());
// Create graphics stuff
//
auto renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->SetBackground(backgroundColor.GetData());
renderer->UseHiddenLineRemovalOn();
auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(640, 480);
auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
// Add the actors to the renderer, set the background and size
//
renderer->AddActor(clipActor);
renderer->AddActor(boundaryActor);
// Generate an interesting view
//
renderer->ResetCamera();
renderer->GetActiveCamera()->Azimuth(30);
renderer->GetActiveCamera()->Elevation(30);
renderer->GetActiveCamera()->Dolly(1.2);
renderer->ResetCameraClippingRange();
renderWindow->Render();
renderWindow->SetWindowName("CapClip");
renderWindow->Render();
interactor->Start();
return EXIT_SUCCESS;
}
namespace {
vtkSmartPointer<vtkPolyData> ReadPolyData(std::string const& fileName)
{
vtkSmartPointer<vtkPolyData> polyData;
std::string extension = "";
if (fileName.find_last_of(".") != std::string::npos)
{
extension = fileName.substr(fileName.find_last_of("."));
}
// Make the extension lowercase
std::transform(extension.begin(), extension.end(), extension.begin(),
::tolower);
if (extension == ".ply")
{
auto reader = vtkSmartPointer<vtkPLYReader>::New();
reader->SetFileName(fileName.c_str());
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".vtp")
{
auto reader = vtkSmartPointer<vtkXMLPolyDataReader>::New();
reader->SetFileName(fileName.c_str());
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".obj")
{
auto reader = vtkSmartPointer<vtkOBJReader>::New();
reader->SetFileName(fileName.c_str());
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".stl")
{
auto reader = vtkSmartPointer<vtkSTLReader>::New();
reader->SetFileName(fileName.c_str());
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".vtk")
{
auto reader = vtkSmartPointer<vtkPolyDataReader>::New();
reader->SetFileName(fileName.c_str());
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".g")
{
auto reader = vtkSmartPointer<vtkBYUReader>::New();
reader->SetGeometryFileName(fileName.c_str());
reader->Update();
polyData = reader->GetOutput();
}
else
{
// Return a polydata sphere if the extension is unknown.
auto source = vtkSmartPointer<vtkSphereSource>::New();
source->SetThetaResolution(20);
source->SetPhiResolution(11);
source->Update();
polyData = source->GetOutput();
}
return polyData;
}
} // namespace CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(CapClip)
find_package(VTK COMPONENTS
vtkCommonColor
vtkCommonCore
vtkCommonDataModel
vtkFiltersCore
vtkFiltersSources
vtkIOGeometry
vtkIOLegacy
vtkIOPLY
vtkIOXML
vtkInteractionStyle
vtkRenderingContextOpenGL2
vtkRenderingCore
vtkRenderingFreeType
vtkRenderingGL2PSOpenGL2
vtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
message("Skipping CapClip: ${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(CapClip MACOSX_BUNDLE CapClip.cxx )
target_link_libraries(CapClip PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
add_executable(CapClip MACOSX_BUNDLE CapClip.cxx )
target_link_libraries(CapClip PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS CapClip
MODULES ${VTK_LIBRARIES}
)
endif () Download and Build CapClip¶
Click here to download CapClip and its CMakeLists.txt file. Once the tarball CapClip.tar has been downloaded and extracted,
cd CapClip/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:
./CapClip WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.