Skip to content

Polyhedron

vtk-examples/Cxx/GeometricObjects/Polyhedron


Other languages

See (Python)

Question

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

Code

Polyhedron.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCellArray.h>
#include <vtkCellData.h>
#include <vtkDataArray.h>
#include <vtkDataSetMapper.h>
#include <vtkIdList.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyhedron.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkUnstructuredGrid.h>
#include <vtkXMLUnstructuredGridWriter.h>

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

  // create polyhedron (cube)
  vtkIdType pointIds[8] = {0, 1, 2, 3, 4, 5, 6, 7};

  vtkNew<vtkPoints> points;
  points->InsertNextPoint(-1.0, -1.0, -1.0);
  points->InsertNextPoint(1.0, -1.0, -1.0);
  points->InsertNextPoint(1.0, 1.0, -1.0);
  points->InsertNextPoint(-1.0, 1.0, -1.0);
  points->InsertNextPoint(-1.0, -1.0, 1.0);
  points->InsertNextPoint(1.0, -1.0, 1.0);
  points->InsertNextPoint(1.0, 1.0, 1.0);
  points->InsertNextPoint(-1.0, 1.0, 1.0);

  vtkNew<vtkIdList> faces;
  vtkIdType face0[4] = {0, 3, 2, 1};
  vtkIdType face1[4] = {0, 4, 7, 3};
  vtkIdType face2[4] = {4, 5, 6, 7};
  vtkIdType face3[4] = {5, 1, 2, 6};
  vtkIdType face4[4] = {0, 1, 5, 4};
  vtkIdType face5[4] = {2, 3, 7, 6};

  auto addFace = [&](const vtkIdType face[4]) {
    faces->InsertNextId(4);
    for (int i = 0; i < 4; ++i)
    {
      faces->InsertNextId(face[i]);
    }
  };

  addFace(face0);
  addFace(face1);
  addFace(face2);
  addFace(face3);
  addFace(face4);
  addFace(face5);

  vtkNew<vtkUnstructuredGrid> ugrid;
  ugrid->SetPoints(points);
  ugrid->InsertNextCell(VTK_POLYHEDRON, 8, pointIds, 6, faces->GetPointer(0));

  // Here we write out the cube.
  vtkNew<vtkXMLUnstructuredGridWriter> writer;
  writer->SetInputData(ugrid);
  writer->SetFileName("polyhedron.vtu");
  writer->SetDataModeToAscii();
  writer->Update();

  // Create a mapper and actor
  vtkNew<vtkDataSetMapper> mapper;
  mapper->SetInputData(ugrid);

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

  // Visualize
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetWindowName("Polyhedron");
  renderWindow->AddRenderer(renderer);
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("Salmon").GetData());
  renderer->ResetCamera();
  renderer->GetActiveCamera()->Azimuth(30);
  renderer->GetActiveCamera()->Elevation(30);
  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(Polyhedron)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkIOXML
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping Polyhedron: ${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(Polyhedron MACOSX_BUNDLE Polyhedron.cxx )
  target_link_libraries(Polyhedron PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(Polyhedron MACOSX_BUNDLE Polyhedron.cxx )
  target_link_libraries(Polyhedron PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS Polyhedron
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build Polyhedron

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

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

./Polyhedron

WINDOWS USERS

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