Skip to content

HyperTreeGridSource

web-test/Cxx/HyperTreeGrid/HyperTreeGridSource



Other languages

See (Python), (PythonicAPI), (Java)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

HyperTreeGridSource.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkDataSetMapper.h>
#include <vtkHyperTreeGridSource.h>
#include <vtkHyperTreeGridToUnstructuredGrid.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkShrinkFilter.h>
#include <vtkVersion.h>

#include <cstdlib>

int main(int, char*[])
{
  // Create hyper tree grid source
  vtkNew<vtkHyperTreeGridSource> source;
#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
  vtkNew<vtkHyperTreeGridToUnstructuredGrid> htg2ug;
  htg2ug->SetInputConnection(source->GetOutputPort());
  htg2ug->Update();

  vtkNew<vtkShrinkFilter> shrink;
  shrink->SetInputConnection(htg2ug->GetOutputPort());
  shrink->SetShrinkFactor(0.8);

  vtkNew<vtkDataSetMapper> mapper;
  mapper->SetInputConnection(shrink->GetOutputPort());
  mapper->ScalarVisibilityOff();

  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetDiffuseColor(
      colors->GetColor3d("Burlywood").GetData());

  // Create the RenderWindow, Renderer and Interactor
  //
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  vtkNew<vtkRenderWindowInteractor> interactor;
  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.12 FATAL_ERROR)

project(HyperTreeGridSource)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersGeneral
  FiltersHyperTree
  FiltersSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "HyperTreeGridSource: 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(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}
)

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.