HierarchicalBoxPipeline

VTKExamples/Cxx/Untested/HasBugs/CompositeData/HierarchicalBoxPipeline

Description

This example demonstrates how hierarchical box (uniform rectilinear) AMR datasets can be processed using the new vtkHierarchicalBoxDataSet class.

Note

This original source code for this example is here.

Question

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

Code

HierarchicalBoxPipeline.cxx

// This example demonstrates how hierarchical box (uniform rectilinear)
// AMR datasets can be processed using the new vtkHierarchicalBoxDataSet class.
//

#include <vtkSmartPointer.h>
#include <vtkCamera.h>
#include <vtkCellDataToPointData.h>
#include <vtkCompositeDataPipeline.h>
#include <vtkContourFilter.h>
#include <vtkExtractLevel.h>
#include <vtkHierarchicalDataSetGeometryFilter.h>
#include <vtkOutlineCornerFilter.h>
#include <vtkHierarchicalPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkShrinkPolyData.h>
#include <vtkXMLHierarchicalBoxDataReader.h>

int main(int argc, char* argv[])
{
  if (argc < 2)
  {
    std::cout << "Usage: " << argv[0] << " file.vtm" << std::endl;
    return EXIT_FAILURE;
  }
  // Standard rendering classes
  vtkSmartPointer<vtkRenderer> ren =
    vtkSmartPointer<vtkRenderer>::New();

  vtkCamera* cam = ren->GetActiveCamera();
  cam->SetPosition(-5.1828, 5.89733, 8.97969);
  cam->SetFocalPoint(14.6491, -2.08677, -8.92362);
  cam->SetViewUp(0.210794, 0.95813, -0.193784);

  vtkSmartPointer<vtkRenderWindow> renWin =
    vtkSmartPointer<vtkRenderWindow>::New();
  renWin->AddRenderer(ren);
  vtkSmartPointer<vtkRenderWindowInteractor> iren =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  iren->SetRenderWindow(renWin);

  vtkSmartPointer<vtkXMLHierarchicalBoxDataReader> reader =
    vtkSmartPointer<vtkXMLHierarchicalBoxDataReader>::New();
  reader->SetFileName(argv[1]);

  // geometry filter
  vtkSmartPointer<vtkHierarchicalDataSetGeometryFilter> geom =
    vtkSmartPointer<vtkHierarchicalDataSetGeometryFilter>::New();
  geom->SetInputConnection(0, reader->GetOutputPort(0));

  vtkSmartPointer<vtkShrinkPolyData> shrink =
    vtkSmartPointer<vtkShrinkPolyData>::New();
  shrink->SetShrinkFactor(0.5);
  shrink->SetInputConnection(0, geom->GetOutputPort(0));

  // // Rendering objects
   vtkSmartPointer<vtkHierarchicalPolyDataMapper> shMapper =
     vtkSmartPointer<vtkHierarchicalPolyDataMapper>::New();
   shMapper->SetInputConnection(0, shrink->GetOutputPort(0));
   vtkSmartPointer<vtkActor> shActor =
     vtkSmartPointer<vtkActor>::New();
   shActor->SetMapper(shMapper);
   shActor->GetProperty()->SetColor(0, 0, 1);
   ren->AddActor(shActor);

  // corner outline
   vtkSmartPointer<vtkOutlineCornerFilter> ocf =
     vtkSmartPointer<vtkOutlineCornerFilter>::New();
   vtkSmartPointer<vtkCompositeDataPipeline> pipeline =
     vtkSmartPointer<vtkCompositeDataPipeline>::New();
   ocf->SetExecutive(pipeline);
   ocf->SetInputConnection(0, reader->GetOutputPort(0));

  // // Rendering objects
  // // This one is actually just a vtkPolyData so it doesn't need a hierarchical
  // // mapper, but we use this one to test hierarchical mapper with polydata input
   vtkSmartPointer<vtkHierarchicalPolyDataMapper> ocMapper =
     vtkSmartPointer<vtkHierarchicalPolyDataMapper>::New();
   ocMapper->SetInputConnection(0, ocf->GetOutputPort(0));

   vtkSmartPointer<vtkActor> ocActor =
     vtkSmartPointer<vtkActor>::New();
   ocActor->SetMapper(ocMapper);
   ocActor->GetProperty()->SetColor(1, 0, 0);
   ren->AddActor(ocActor);

   // cell 2 point and contour
   vtkSmartPointer<vtkExtractLevel> el =
     vtkSmartPointer<vtkExtractLevel>::New();
   el->SetInputConnection(0, reader->GetOutputPort(0));
   el->AddLevel(2);

   vtkSmartPointer<vtkCellDataToPointData> c2p =
     vtkSmartPointer<vtkCellDataToPointData>::New();
   pipeline = vtkCompositeDataPipeline::New();
   c2p->SetExecutive(pipeline);
   c2p->SetInputConnection(0, el->GetOutputPort(0));

   vtkSmartPointer<vtkContourFilter> contour =
     vtkSmartPointer<vtkContourFilter>::New();

   pipeline = vtkCompositeDataPipeline::New();
   contour->SetExecutive(pipeline);
   contour->SetInputConnection(0, c2p->GetOutputPort(0));
   contour->SetValue(0, -0.013);
   contour->SetInputArrayToProcess(
     0,0,0,vtkDataObject::FIELD_ASSOCIATION_POINTS,"phi");

   // Rendering
   vtkSmartPointer<vtkHierarchicalPolyDataMapper> contMapper =
     vtkSmartPointer<vtkHierarchicalPolyDataMapper>::New();
   contMapper->SetInputConnection(0, contour->GetOutputPort(0));
   vtkSmartPointer<vtkActor> contActor =
     vtkSmartPointer<vtkActor>::New();

   contActor->SetMapper(contMapper);
   contActor->GetProperty()->SetColor(1, 0, 0);
   ren->AddActor(contActor);

   ren->SetBackground(1,1,1);
   renWin->SetSize(300,300);
   ren->ResetCamera();
   iren->Start();

   return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(HierarchicalBoxPipeline)

find_package(VTK COMPONENTS 
  vtkCommonCore
  vtkCommonExecutionModel
  vtkFiltersCore
  vtkFiltersExtraction
  vtkFiltersGeneral
  vtkFiltersGeometry
  vtkFiltersSources
  vtkIOXML
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
  message("Skipping HierarchicalBoxPipeline: ${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(HierarchicalBoxPipeline MACOSX_BUNDLE HierarchicalBoxPipeline.cxx )
  target_link_libraries(HierarchicalBoxPipeline PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(HierarchicalBoxPipeline MACOSX_BUNDLE HierarchicalBoxPipeline.cxx )
  target_link_libraries(HierarchicalBoxPipeline PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS HierarchicalBoxPipeline
    MODULES ${VTK_LIBRARIES}
    )
endif () 

Download and Build HierarchicalBoxPipeline

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

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

./HierarchicalBoxPipeline

WINDOWS USERS

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