Skip to content


# OctreeVisualize

VTKEx/Cxx/DataStructures/OctreeVisualize

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

OctreeVisualize.cxx

    #include <vtkActor.h>
    #include <vtkCallbackCommand.h>
    #include <vtkCommand.h>
    #include <vtkInteractorStyleTrackballCamera.h>
    #include <vtkMath.h>
    #include <vtkNamedColors.h>
    #include <vtkNew.h>
    #include <vtkOctreePointLocator.h>
    #include <vtkPointSource.h>
    #include <vtkPolyData.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkProperty.h>
    #include <vtkProperty2D.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>
    #include <vtkSliderRepresentation2D.h>
    #include <vtkSliderWidget.h>
    #include <vtkSmartPointer.h>
    #include <vtkSphereSource.h>
    #include <vtkTextProperty.h>
    #include <vtkWidgetEvent.h>
    #include <vtkWidgetEventTranslator.h>

    #include <cmath>

    namespace {
    class vtkSliderCallback : public vtkCommand {
    public:
      static vtkSliderCallback *New()

      {
        return new vtkSliderCallback;
      }
      vtkSliderCallback() : Octree(0), Level(0), PolyData(0), Renderer(0) {}

      virtual void Execute(vtkObject *caller, unsigned long, void *) {
        vtkSliderWidget *sliderWidget = reinterpret_cast<vtkSliderWidget *>(caller);
        this->Level = vtkMath::Round(static_cast<vtkSliderRepresentation *>(
                                         sliderWidget->GetRepresentation())
                                         ->GetValue());

        this->Octree->GenerateRepresentation(this->Level, this->PolyData);
        this->Renderer->Render();
      }

      vtkOctreePointLocator *Octree;
      int Level;
      vtkPolyData *PolyData;
      vtkRenderer *Renderer;
    };
    } // namespace

    int main(int, char *[]) {
      vtkNew<vtkNamedColors> colors;

      // Create a point cloud
      vtkSmartPointer<vtkSphereSource> pointSource =
          vtkSmartPointer<vtkSphereSource>::New();
      pointSource->SetPhiResolution(50);
      pointSource->SetThetaResolution(50);
      vtkSmartPointer<vtkPolyDataMapper> pointsMapper =
          vtkSmartPointer<vtkPolyDataMapper>::New();
      pointsMapper->SetInputConnection(pointSource->GetOutputPort());
      pointSource->Update();
      vtkSmartPointer<vtkActor> pointsActor = vtkSmartPointer<vtkActor>::New();
      pointsActor->SetMapper(pointsMapper);
      pointsActor->GetProperty()->SetInterpolationToFlat();
      pointsActor->GetProperty()->SetRepresentationToPoints();
      pointsActor->GetProperty()->SetColor(colors->GetColor4d("Yellow").GetData());

      // Create the tree
      vtkSmartPointer<vtkOctreePointLocator> octree =
          vtkSmartPointer<vtkOctreePointLocator>::New();
      octree->SetMaximumPointsPerRegion(5);
      octree->SetDataSet(pointSource->GetOutput());
      octree->BuildLocator();

      vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
      octree->GenerateRepresentation(0, polydata);

      vtkSmartPointer<vtkPolyDataMapper> octreeMapper =
          vtkSmartPointer<vtkPolyDataMapper>::New();
      octreeMapper->SetInputData(polydata);

      vtkSmartPointer<vtkActor> octreeActor = vtkSmartPointer<vtkActor>::New();
      octreeActor->SetMapper(octreeMapper);
      octreeActor->GetProperty()->SetInterpolationToFlat();
      octreeActor->GetProperty()->SetRepresentationToWireframe();
      octreeActor->GetProperty()->SetColor(
          colors->GetColor4d("SpringGreen").GetData());

      // A renderer and render window
      vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
      vtkSmartPointer<vtkRenderWindow> renderWindow =
          vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer(renderer);

      // An interactor
      vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
          vtkSmartPointer<vtkRenderWindowInteractor>::New();
      renderWindowInteractor->SetRenderWindow(renderWindow);

      // Add the actors to the scene
      renderer->AddActor(pointsActor);
      renderer->AddActor(octreeActor);
      renderer->SetBackground(colors->GetColor3d("MidnightBlue").GetData());

      // Render an image (lights and cameras are created automatically)
      renderWindow->SetWindowName("OctreeVisualize");
      renderWindow->SetSize(600, 600);
      renderWindow->Render();

      vtkSmartPointer<vtkSliderRepresentation2D> sliderRep =
          vtkSmartPointer<vtkSliderRepresentation2D>::New();
      sliderRep->SetMinimumValue(0);
      sliderRep->SetMaximumValue(octree->GetLevel());
      sliderRep->SetValue(0);
      sliderRep->SetTitleText("Level");
      sliderRep->GetPoint1Coordinate()->SetCoordinateSystemToNormalizedDisplay();
      sliderRep->GetPoint1Coordinate()->SetValue(.2, .2);
      sliderRep->GetPoint2Coordinate()->SetCoordinateSystemToNormalizedDisplay();
      sliderRep->GetPoint2Coordinate()->SetValue(.8, .2);
      sliderRep->SetSliderLength(0.075);
      sliderRep->SetSliderWidth(0.05);
      sliderRep->SetEndCapLength(0.05);
      sliderRep->GetTitleProperty()->SetColor(
          colors->GetColor3d("Beige").GetData());
      sliderRep->GetCapProperty()->SetColor(
          colors->GetColor3d("MistyRose").GetData());
      sliderRep->GetSliderProperty()->SetColor(
          colors->GetColor3d("LightBlue").GetData());
      sliderRep->GetSelectedProperty()->SetColor(
          colors->GetColor3d("Violet").GetData());

      vtkSmartPointer<vtkSliderWidget> sliderWidget =
          vtkSmartPointer<vtkSliderWidget>::New();
      sliderWidget->SetInteractor(renderWindowInteractor);
      sliderWidget->SetRepresentation(sliderRep);
      sliderWidget->SetAnimationModeToAnimate();
      sliderWidget->EnabledOn();

      vtkSmartPointer<vtkSliderCallback> callback =
          vtkSmartPointer<vtkSliderCallback>::New();
      callback->Octree = octree;
      callback->PolyData = polydata;
      callback->Renderer = renderer;

      sliderWidget->AddObserver(vtkCommand::InteractionEvent, callback);

      renderWindowInteractor->Initialize();
      renderWindow->Render();

      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(OctreeVisualize)

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

### Download and Build OctreeVisualize

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

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

    ./OctreeVisualize

WINDOWS USERS

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