Skip to content


# Cube

VTKEx/Cxx/GeometricObjects/Cube

### Description

This is based on the C++ example Examples/DataManipulation/Cxx/Cube.cxx in the VTK source distribution.

It illustrates the manual use of vtkPolyData to construct a cube and differs from the Wiki examples Cube1.cxx and Cube1.py, which use vtkCubeSource.

Other languages

See (Python), (CSharp)

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

Cube.cxx

    /*=========================================================================

      Program:   Visualization Toolkit
      Module:    Cube.cxx

      Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
      All rights reserved.
      See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

         This software is distributed WITHOUT ANY WARRANTY; without even
         the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
         PURPOSE.  See the above copyright notice for more information.

    =========================================================================*/
    // This example shows how to manually create vtkPolyData.

    // For a python version, please see:
    // [Cube](https://lorensen.github.io/VTKExamples/site/Python/GeometricObjects/Cube/)

    #include <vtkActor.h>
    #include <vtkCamera.h>
    #include <vtkCellArray.h>
    #include <vtkFloatArray.h>
    #include <vtkNamedColors.h>
    #include <vtkNew.h>
    #include <vtkPointData.h>
    #include <vtkPoints.h>
    #include <vtkPolyData.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>

    #include <array>

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

      std::array<std::array<double, 3>, 8> pts = {{{{0, 0, 0}},
                                                   {{1, 0, 0}},
                                                   {{1, 1, 0}},
                                                   {{0, 1, 0}},
                                                   {{0, 0, 1}},
                                                   {{1, 0, 1}},
                                                   {{1, 1, 1}},
                                                   {{0, 1, 1}}}};
      // The ordering of the corner points on each face.
      std::array<std::array<vtkIdType, 4>, 6> ordering = {{{{0, 1, 2, 3}},
                                                           {{4, 5, 6, 7}},
                                                           {{0, 1, 5, 4}},
                                                           {{1, 2, 6, 5}},
                                                           {{2, 3, 7, 6}},
                                                           {{3, 0, 4, 7}}}};

      // We'll create the building blocks of polydata including data attributes.
      vtkNew<vtkPolyData> cube;
      vtkNew<vtkPoints> points;
      vtkNew<vtkCellArray> polys;
      vtkNew<vtkFloatArray> scalars;

      // Load the point, cell, and data attributes.
      for (auto i = 0ul; i < pts.size(); ++i)
      {
        points->InsertPoint(i, pts[i].data());
        scalars->InsertTuple1(i, i);
      }
      for (auto&& i : ordering)
      {
        polys->InsertNextCell(vtkIdType(i.size()), i.data());
      }

      // We now assign the pieces to the vtkPolyData.
      cube->SetPoints(points);
      cube->SetPolys(polys);
      cube->GetPointData()->SetScalars(scalars);

      // Now we'll look at it.
      vtkNew<vtkPolyDataMapper> cubeMapper;
      cubeMapper->SetInputData(cube);
      cubeMapper->SetScalarRange(cube->GetScalarRange());
      vtkNew<vtkActor> cubeActor;
      cubeActor->SetMapper(cubeMapper);

      // The usual rendering stuff.
      vtkNew<vtkCamera> camera;
      camera->SetPosition(1, 1, 1);
      camera->SetFocalPoint(0, 0, 0);

      vtkNew<vtkRenderer> renderer;
      vtkNew<vtkRenderWindow> renWin;
      renWin->AddRenderer(renderer);

      vtkNew<vtkRenderWindowInteractor> iren;
      iren->SetRenderWindow(renWin);

      renderer->AddActor(cubeActor);
      renderer->SetActiveCamera(camera);
      renderer->ResetCamera();
      renderer->SetBackground(colors->GetColor3d("Cornsilk").GetData());

      renWin->SetSize(600, 600);

      // interact with data
      renWin->Render();
      iren->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(Cube)

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

### Download and Build Cube

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

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

    ./Cube

WINDOWS USERS

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