MultiBlockDataSet

web-test/Cxx/CompositeData/MultiBlockDataSet


Other languages

See (Python)

Question

If you have a simple question about this example contact us at web-testProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

Code

MultiBlockDataSet.cxx

// Demonstrates how to make and use VTK's MultiBlock type data

#include <vtkActor.h>
#include <vtkCompositeDataGeometryFilter.h>
#include <vtkExtractEdges.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkVector.h>

namespace {
vtkSmartPointer<vtkDataSet> CreateSphereDataSet(const vtkVector3d& center,
                                                double radius = 0.5)
{
  vtkNew<vtkSphereSource> leaf;
  leaf->SetCenter(center.GetData());
  leaf->SetRadius(radius);
  leaf->Update();
  return leaf->GetOutput();
}

} // namespace

int main (int, char *[])
{
  // PART 1 Make some Data
  // make a tree
  vtkNew<vtkMultiBlockDataSet> root;

  vtkNew<vtkMultiBlockDataSet> branch;
  root->SetBlock(0, branch);

  // make some leaves
  branch->SetBlock(0, ::CreateSphereDataSet({0, 0, 0}));
  branch->SetBlock(1, ::CreateSphereDataSet({1.75, 2.5, 0.0}, 1.5));
  root->SetBlock(1, ::CreateSphereDataSet({4, 0, 0}, 2.0));

  // uncomment to inspect
  //std::cerr << root->GetClassName() << std::endl;
  //root->PrintSelf(std::cerr, vtkIndent(0));

  // PART 2 Do something with the data
  // a non composite aware filter, the pipeline will iterate
  vtkNew<vtkExtractEdges> edges;
  edges->SetInputData(root);

  // uncomment to inspect
  //edges->Update();
  //cerr << edges->GetOutputDataObject(0)->GetClassName() << endl;
  //edges->GetOutputDataObject(0)->PrintSelf(std::cerr, vtkIndent(0));

  // PART 3 Show the data
  // also demonstrate a composite aware filter
  // this filter aggregates all blocks into one polydata
  // this is handy for display, although fairly limited
  // see vtkCompositePolyDataMapper2 for something better
  vtkNew<vtkCompositeDataGeometryFilter> polydata;
  polydata->SetInputConnection(edges->GetOutputPort());

  // uncomment to inspect
  //polydata->Update();
  //std::cerr << polydata->GetOutput()->GetClassName() << std::endl;
  //polydata->GetOutput()->PrintSelf(std::cerr, vtkIndent(0));

  // display the data
  vtkNew<vtkRenderer> aren;
  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(aren);
  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renWin);

  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(0, polydata->GetOutputPort(0));
  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  aren->AddActor(actor);

  renWin->Render();
  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(MultiBlockDataSet)

find_package(VTK COMPONENTS 
  vtkvtkCommonCore
  vtkvtkCommonDataModel
  vtkvtkFiltersExtraction
  vtkvtkFiltersGeometry
  vtkvtkFiltersSources
  vtkvtkInteractionStyle
  vtkvtkRenderingContextOpenGL2
  vtkvtkRenderingCore
  vtkvtkRenderingFreeType
  vtkvtkRenderingGL2PSOpenGL2
  vtkvtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
  message("Skipping MultiBlockDataSet: ${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(MultiBlockDataSet MACOSX_BUNDLE MultiBlockDataSet.cxx )
  target_link_libraries(MultiBlockDataSet PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(MultiBlockDataSet MACOSX_BUNDLE MultiBlockDataSet.cxx )
  target_link_libraries(MultiBlockDataSet PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS MultiBlockDataSet
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build MultiBlockDataSet

Click here to download MultiBlockDataSet and its CMakeLists.txt file. Once the tarball MultiBlockDataSet.tar has been downloaded and extracted,

cd MultiBlockDataSet/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:

./MultiBlockDataSet

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.