Silhouette
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¶
Silhouette.cxx
#include <vtkSmartPointer.h>
#include <vtkPolyDataSilhouette.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkCleanPolyData.h>
#include <vtkProperty.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkCamera.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSphereSource.h>
#include <vtkNamedColors.h>
int main(int argc, char *argv[])
{
vtkSmartPointer<vtkPolyData> polyData;
if (argc < 2)
{
vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
sphereSource->Update();
polyData = sphereSource->GetOutput();
}
else
{
vtkSmartPointer<vtkXMLPolyDataReader> reader =
vtkSmartPointer<vtkXMLPolyDataReader>::New();
reader->SetFileName(argv[1]);
vtkSmartPointer<vtkCleanPolyData> clean =
vtkSmartPointer<vtkCleanPolyData>::New();
clean->SetInputConnection(reader->GetOutputPort());
clean->Update();
polyData = clean->GetOutput();
}
vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();
//create mapper and actor for original model
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polyData);
mapper->ScalarVisibilityOff();
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetInterpolationToFlat();
actor->GetProperty()->SetColor(colors->GetColor3d("Banana").GetData());
//create renderer and renderWindow
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderer->AddActor(actor); //view the original model
//Compute the silhouette
vtkSmartPointer<vtkPolyDataSilhouette> silhouette =
vtkSmartPointer<vtkPolyDataSilhouette>::New();
silhouette->SetInputData(polyData);
silhouette->SetCamera(renderer->GetActiveCamera());
silhouette->SetEnableFeatureAngle(0);
//create mapper and actor for silouette
vtkSmartPointer<vtkPolyDataMapper> mapper2 =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper2->SetInputConnection(silhouette->GetOutputPort());
vtkSmartPointer<vtkActor> actor2 =
vtkSmartPointer<vtkActor>::New();
actor2->SetMapper(mapper2);
actor2->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());
actor2->GetProperty()->SetLineWidth(5);
renderer->AddActor(actor2);
renderer->SetBackground(colors->GetColor3d("Silver").GetData());
renderer->ResetCamera();
renderer->GetActiveCamera()->Azimuth(30);
renderer->GetActiveCamera()->Elevation(30);
renderer->GetActiveCamera()->Dolly(1.5);;
renderer->ResetCameraClippingRange();
vtkSmartPointer<vtkRenderWindowInteractor> iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renderWindow);
//render and interact
renderWindow->SetSize(640, 480);
renderWindow->Render();
iren->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(Silhouette)
find_package(VTK COMPONENTS
vtkvtkCommonColor
vtkvtkCommonCore
vtkvtkCommonDataModel
vtkvtkFiltersCore
vtkvtkFiltersHybrid
vtkvtkFiltersSources
vtkvtkIOXML
vtkvtkInteractionStyle
vtkvtkRenderingContextOpenGL2
vtkvtkRenderingCore
vtkvtkRenderingFreeType
vtkvtkRenderingGL2PSOpenGL2
vtkvtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
message("Skipping Silhouette: ${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(Silhouette MACOSX_BUNDLE Silhouette.cxx )
target_link_libraries(Silhouette PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
add_executable(Silhouette MACOSX_BUNDLE Silhouette.cxx )
target_link_libraries(Silhouette PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS Silhouette
MODULES ${VTK_LIBRARIES}
)
endif ()
Download and Build Silhouette¶
Click here to download Silhouette and its CMakeLists.txt file. Once the tarball Silhouette.tar has been downloaded and extracted,
cd Silhouette/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:
./Silhouette
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.