Skip to content

ExtractData

Repository source: 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.

Other languages

See (Python), (PythonicAPI)

Question

If you have a question about this example, 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 <vtkNew.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
  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkRenderer> ren1;

  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(ren1);

  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renWin);

  vtkNew<vtkQuadric> quadric;
  quadric->SetCoefficients(0.5, 1, 0.2, 0, 0.1, 0, 0, 0.2, 0, 0);

  vtkNew<vtkSampleFunction> sample;
  sample->SetSampleDimensions(50, 50, 50);
  sample->SetImplicitFunction(quadric);
  sample->ComputeNormalsOff();

  vtkNew<vtkTransform> trans;
  trans->Scale(1, 0.5, 0.333);

  vtkNew<vtkSphere> sphere;
  sphere->SetRadius(0.25);
  sphere->SetTransform(trans);

  vtkNew<vtkTransform> trans2;
  trans2->Scale(0.25, 0.5, 1.0);

  vtkNew<vtkSphere> sphere2;
  sphere2->SetRadius(0.25);
  sphere2->SetTransform(trans2);

  vtkNew<vtkImplicitBoolean> booleanUnion;
  booleanUnion->AddFunction(sphere);
  booleanUnion->AddFunction(sphere2);
  booleanUnion->SetOperationType(0); // boolean Union

  vtkNew<vtkExtractGeometry> extract;
  extract->SetInputConnection(sample->GetOutputPort());
  extract->SetImplicitFunction(booleanUnion);

  vtkNew<vtkShrinkFilter> shrink;
  shrink->SetInputConnection(extract->GetOutputPort());
  shrink->SetShrinkFactor(0.5);

  vtkNew<vtkDataSetMapper> dataMapper;
  dataMapper->SetInputConnection(shrink->GetOutputPort());
  vtkNew<vtkActor> dataActor;
  dataActor->SetMapper(dataMapper);

  // outline
  vtkNew<vtkOutlineFilter> outline;
  outline->SetInputConnection(sample->GetOutputPort());

  vtkNew<vtkPolyDataMapper> outlineMapper;
  outlineMapper->SetInputConnection(outline->GetOutputPort());

  vtkNew<vtkActor> outlineActor;
  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->SetWindowName("ExtractData");
  renWin->Render();

  ren1->GetActiveCamera()->Azimuth(30);
  ren1->GetActiveCamera()->Elevation(30);

  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(ExtractData)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  CommonTransforms
  FiltersExtraction
  FiltersGeneral
  FiltersModeling
  ImagingHybrid
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "ExtractData: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
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}
)

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.