Skip to content


# HyperTreeGridSource

VTKEx/Cxx/HyperTreeGrid/HyperTreeGridSource

Other languages

See (Python), (Java)

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

HyperTreeGridSource.cxx

    #include <vtkHyperTreeGridSource.h>
    #include <vtkHyperTreeGridToUnstructuredGrid.h>

    #include <vtkActor.h>
    #include <vtkCamera.h>
    #include <vtkDataSetMapper.h>
    #include <vtkNamedColors.h>
    #include <vtkPolyData.h>
    #include <vtkProperty.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>
    #include <vtkShrinkFilter.h>
    #include <vtkSmartPointer.h>
    #include <vtkVersion.h>

    #include <cstdlib>

    int main(int, char*[])
    {
      // Create hyper tree grid source
      auto source = vtkSmartPointer<vtkHyperTreeGridSource>::New();
    #if VTK_VERSION_NUMBER >= 89000000000ULL
      source->SetMaxDepth(6);
    #else
      source->SetMaximumLevel(6);
    #endif
      source->SetDimensions(4, 4, 3); // GridCell 3, 3, 2
      source->SetGridScale(1.5, 1.0, 0.7);
      source->SetBranchFactor(4);
      source->SetDescriptor(
          "RRR .R. .RR ..R ..R .R.|R.......................... "
          "........................... ........................... "
          ".............R............. ....RR.RR........R......... "
          ".....RRRR.....R.RR......... ........................... "
          "........................... "
          "...........................|........................... "
          "........................... ........................... "
          "...RR.RR.......RR.......... ........................... "
          "RR......................... ........................... "
          "........................... ........................... "
          "........................... ........................... "
          "........................... ........................... "
          "............RRR............|........................... "
          "........................... .......RR.................. "
          "........................... ........................... "
          "........................... ........................... "
          "........................... ........................... "
          "........................... "
          "...........................|........................... "
          "...........................");
      source->Update();

      // Hyper tree grid to unstructured grid filter
      auto htg2ug = vtkSmartPointer<vtkHyperTreeGridToUnstructuredGrid>::New();
      htg2ug->SetInputConnection(source->GetOutputPort());
      htg2ug->Update();

      auto shrink = vtkSmartPointer<vtkShrinkFilter>::New();
      shrink->SetInputConnection(htg2ug->GetOutputPort());
      shrink->SetShrinkFactor(0.8);

      auto mapper = vtkSmartPointer<vtkDataSetMapper>::New();
      mapper->SetInputConnection(shrink->GetOutputPort());
      mapper->ScalarVisibilityOff();

      auto colors = vtkSmartPointer<vtkNamedColors>::New();

      auto actor = vtkSmartPointer<vtkActor>::New();
      actor->SetMapper(mapper);
      actor->GetProperty()->SetDiffuseColor(
          colors->GetColor3d("Burlywood").GetData());

      // Create the RenderWindow, Renderer and Interactor
      //
      auto renderer = vtkSmartPointer<vtkRenderer>::New();
      auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer(renderer);
      auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
      interactor->SetRenderWindow(renderWindow);

      renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
      renderer->AddActor(actor);
      renderer->ResetCamera();
      renderer->GetActiveCamera()->Azimuth(150);
      renderer->GetActiveCamera()->Elevation(30);
      renderer->ResetCameraClippingRange();

      renderWindow->SetSize(640, 480);
      renderWindow->Render();
      renderWindow->SetWindowName("HyperTreeGridSource");
      interactor->Start();
      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(HyperTreeGridSource)

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

### Download and Build HyperTreeGridSource

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

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

    ./HyperTreeGridSource

WINDOWS USERS

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