ExtractData
VTKEx/Cxx/VisualizationAlgorithms/ExtractData
Description¶
This example takes advantage of the properties of implicit functions to select and cut data. In particular it uses the region separation property to select data. Selecting or extracting data with an implicit function means choosing cells and points (and associated attribute data) that lie within a particular region of the function. To determine whether a point x-y-z lies within a region, we simply evaluate the point and examine the sign of the result. A cell lies in a region if all its points lie in the region. Here, two ellipses are used in combination to select voxels within a volume dataset. Note that extracting data often changes the structure of the dataset. In this example the input type is a image data dataset, while the output type is an unstructured grid dataset.
Info
See Figure 6-24 in Chapter 6 the VTK Textbook.
Other languages
See (Python)
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¶
ExtractData.cxx
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkDataSetMapper.h>
#include <vtkExtractGeometry.h>
#include <vtkImplicitBoolean.h>
#include <vtkNamedColors.h>
#include <vtkOutlineFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkQuadric.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSampleFunction.h>
#include <vtkShrinkFilter.h>
#include <vtkSphere.h>
#include <vtkTransform.h>
int main (int, char *[])
{
// extract data
vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();
vtkSmartPointer<vtkRenderer> ren1 =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(ren1);
vtkSmartPointer<vtkRenderWindowInteractor> iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
vtkSmartPointer<vtkQuadric> quadric =
vtkSmartPointer<vtkQuadric>::New();
quadric->SetCoefficients(.5, 1, .2, 0, .1, 0, 0, .2, 0, 0);
vtkSmartPointer<vtkSampleFunction> sample =
vtkSmartPointer<vtkSampleFunction>::New();
sample->SetSampleDimensions(50, 50, 50);
sample->SetImplicitFunction(quadric);
sample->ComputeNormalsOff();
vtkSmartPointer<vtkTransform> trans =
vtkSmartPointer<vtkTransform>::New();
trans->Scale(1, .5, .333);
vtkSmartPointer<vtkSphere> sphere =
vtkSmartPointer<vtkSphere>::New();
sphere->SetRadius(0.25);
sphere->SetTransform(trans);
vtkSmartPointer<vtkTransform> trans2 =
vtkSmartPointer<vtkTransform>::New();
trans2->Scale(.25, .5, 1.0);
vtkSmartPointer<vtkSphere> sphere2 =
vtkSmartPointer<vtkSphere>::New();
sphere2->SetRadius(0.25);
sphere2->SetTransform(trans2);
vtkSmartPointer<vtkImplicitBoolean> booleanUnion =
vtkSmartPointer<vtkImplicitBoolean>::New();
booleanUnion->AddFunction(sphere);
booleanUnion->AddFunction(sphere2);
booleanUnion->SetOperationType(0); // boolean Union
vtkSmartPointer<vtkExtractGeometry> extract =
vtkSmartPointer<vtkExtractGeometry>::New();
extract->SetInputConnection(sample->GetOutputPort());
extract->SetImplicitFunction(booleanUnion);
vtkSmartPointer<vtkShrinkFilter> shrink =
vtkSmartPointer<vtkShrinkFilter>::New();
shrink->SetInputConnection(extract->GetOutputPort());
shrink->SetShrinkFactor(0.5);
vtkSmartPointer<vtkDataSetMapper> dataMapper =
vtkSmartPointer<vtkDataSetMapper>::New();
dataMapper->SetInputConnection(shrink->GetOutputPort());
vtkSmartPointer<vtkActor> dataActor =
vtkSmartPointer<vtkActor>::New();
dataActor->SetMapper(dataMapper);
// outline
vtkSmartPointer<vtkOutlineFilter> outline =
vtkSmartPointer<vtkOutlineFilter>::New();
outline->SetInputConnection(sample->GetOutputPort());
vtkSmartPointer<vtkPolyDataMapper> outlineMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
outlineMapper->SetInputConnection(outline->GetOutputPort());
vtkSmartPointer<vtkActor> outlineActor =
vtkSmartPointer<vtkActor>::New();
outlineActor->SetMapper(outlineMapper);
outlineActor->GetProperty()->SetColor(0, 0, 0);
// Add the actors to the renderer, set the background and size
//
ren1->AddActor(outlineActor);
ren1->AddActor(dataActor);
ren1->SetBackground(colors->GetColor3d("SlateGray").GetData());
renWin->SetSize(640, 480);
renWin->Render();
ren1->GetActiveCamera()->Azimuth(30);
ren1->GetActiveCamera()->Elevation(30);
ren1->GetActiveCamera()->Zoom(1.5);
iren->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(ExtractData)
find_package(VTK COMPONENTS
vtkvtkCommonColor
vtkvtkCommonDataModel
vtkvtkCommonTransforms
vtkvtkFiltersExtraction
vtkvtkFiltersGeneral
vtkvtkFiltersModeling
vtkvtkImagingHybrid
vtkvtkInteractionStyle
vtkvtkRenderingContextOpenGL2
vtkvtkRenderingCore
vtkvtkRenderingFreeType
vtkvtkRenderingGL2PSOpenGL2
vtkvtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
message("Skipping ExtractData: ${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(ExtractData MACOSX_BUNDLE ExtractData.cxx )
target_link_libraries(ExtractData PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
add_executable(ExtractData MACOSX_BUNDLE ExtractData.cxx )
target_link_libraries(ExtractData PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ExtractData
MODULES ${VTK_LIBRARIES}
)
endif ()
Download and Build ExtractData¶
Click here to download ExtractData and its CMakeLists.txt file. Once the tarball ExtractData.tar has been downloaded and extracted,
cd ExtractData/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:
./ExtractData
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.