ImplicitBoolean
VTKExamples/Cxx/Filtering/ImplicitBoolean
Code¶
ImplicitBoolean.cxx
#include <vtkSmartPointer.h> #include <vtkSampleFunction.h> #include <vtkContourFilter.h> #include <vtkOutlineFilter.h> #include <vtkPolyDataMapper.h> #include <vtkActor.h> #include <vtkProperty.h> #include <vtkRenderWindow.h> #include <vtkRenderer.h> #include <vtkRenderWindowInteractor.h> #include <vtkImageData.h> #include <vtkImplicitBoolean.h> #include <vtkSphere.h> #include <vtkNamedColors.h> #include <vtkColor.h> int main (int, char *[]) { // Define colors vtkSmartPointer<vtkNamedColors> colors = vtkSmartPointer<vtkNamedColors>::New(); vtkColor3d background = colors->GetColor3d("SlateGray"); vtkColor3d actorColor = colors->GetColor3d("hot_pink"); vtkSmartPointer<vtkSphere> sphere1 = vtkSmartPointer<vtkSphere>::New(); sphere1->SetCenter(.9,0,0); vtkSmartPointer<vtkSphere> sphere2 = vtkSmartPointer<vtkSphere>::New(); sphere2->SetCenter(-.9,0,0); vtkSmartPointer<vtkImplicitBoolean> implicitBoolean = vtkSmartPointer<vtkImplicitBoolean>::New(); implicitBoolean->AddFunction(sphere1); implicitBoolean->AddFunction(sphere2); implicitBoolean->SetOperationTypeToUnion(); // implicitBoolean->SetOperationTypeToIntersection(); // implicitBoolean->SetOperationTypeToDifference(); // Sample the function vtkSmartPointer<vtkSampleFunction> sample = vtkSmartPointer<vtkSampleFunction>::New(); sample->SetSampleDimensions(50,50,50); sample->SetImplicitFunction(implicitBoolean); double value = 3.0; double xmin = -value, xmax = value, ymin = -value, ymax = value, zmin = -value, zmax = value; sample->SetModelBounds(xmin, xmax, ymin, ymax, zmin, zmax); // Create the 0 isosurface vtkSmartPointer<vtkContourFilter> contours = vtkSmartPointer<vtkContourFilter>::New(); contours->SetInputConnection(sample->GetOutputPort()); contours->GenerateValues(1, 1, 1); // Map the contours to graphical primitives vtkSmartPointer<vtkPolyDataMapper> contourMapper = vtkSmartPointer<vtkPolyDataMapper>::New(); contourMapper->SetInputConnection(contours->GetOutputPort()); contourMapper->ScalarVisibilityOff(); // Create an actor for the contours vtkSmartPointer<vtkActor> contourActor = vtkSmartPointer<vtkActor>::New(); contourActor->SetMapper(contourMapper); contourActor->GetProperty()->SetDiffuseColor(actorColor.GetData()); contourActor->GetProperty()->SetDiffuse(.8); contourActor->GetProperty()->SetSpecular(.2); contourActor->GetProperty()->SetSpecularPower(60.0); // Visualize vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New(); vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->AddRenderer(renderer); renderWindow->SetSize(640, 480); vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); interactor->SetRenderWindow(renderWindow); renderer->AddActor(contourActor); renderer->SetBackground(background.GetData()); renderWindow->Render(); interactor->Start(); return EXIT_SUCCESS; }
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR) project(ImplicitBoolean) find_package(VTK COMPONENTS vtkCommonColor vtkCommonCore vtkCommonDataModel vtkFiltersCore vtkFiltersModeling vtkImagingHybrid vtkInteractionStyle vtkRenderingCore vtkRenderingFreeType vtkRenderingOpenGL2 QUIET) if (NOT VTK_FOUND) message("Skipping ImplicitBoolean: ${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(ImplicitBoolean MACOSX_BUNDLE ImplicitBoolean.cxx ) target_link_libraries(ImplicitBoolean PRIVATE ${VTK_LIBRARIES}) else () # include all components add_executable(ImplicitBoolean MACOSX_BUNDLE ImplicitBoolean.cxx ) target_link_libraries(ImplicitBoolean PRIVATE ${VTK_LIBRARIES}) # vtk_module_autoinit is needed vtk_module_autoinit( TARGETS ImplicitBoolean MODULES ${VTK_LIBRARIES} ) endif ()
Download and Build ImplicitBoolean¶
Click here to download ImplicitBoolean and its CMakeLists.txt file. Once the tarball ImplicitBoolean.tar has been downloaded and extracted,
cd ImplicitBoolean/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:
./ImplicitBoolean
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.