# Pyramid

VTKExamples/Cxx/GeometricObjects/Pyramid

### Code

Pyramid.cxx

    #include <vtkVersion.h>
    #include <vtkSmartPointer.h>

    #include <vtkActor.h>
    #include <vtkCamera.h>
    #include <vtkCellArray.h>
    #include <vtkDataSetMapper.h>
    #include <vtkPoints.h>
    #include <vtkPyramid.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>
    #include <vtkUnstructuredGrid.h>

    int main(int , char *[])
    {
      vtkSmartPointer<vtkPoints> points =
          vtkSmartPointer<vtkPoints>::New();

      float p0[3] = {1.0, 1.0, 1.0};
      float p1[3] = {-1.0, 1.0, 1.0};
      float p2[3] = {-1.0, -1.0, 1.0};
      float p3[3] = {1.0, -1.0, 1.0};
      float p4[3] = {0.0, 0.0, 0.0};

      points->InsertNextPoint(p0);
      points->InsertNextPoint(p1);
      points->InsertNextPoint(p2);
      points->InsertNextPoint(p3);
      points->InsertNextPoint(p4);

      vtkSmartPointer<vtkPyramid> pyramid =
          vtkSmartPointer<vtkPyramid>::New();
      pyramid->GetPointIds()->SetId(0,0);
      pyramid->GetPointIds()->SetId(1,1);
      pyramid->GetPointIds()->SetId(2,2);
      pyramid->GetPointIds()->SetId(3,3);
      pyramid->GetPointIds()->SetId(4,4);

      vtkSmartPointer<vtkCellArray> cells =
          vtkSmartPointer<vtkCellArray>::New();
      cells->InsertNextCell (pyramid);

      vtkSmartPointer<vtkUnstructuredGrid> ug =
          vtkSmartPointer<vtkUnstructuredGrid>::New();
      ug->SetPoints(points);
      ug->InsertNextCell(pyramid->GetCellType(),pyramid->GetPointIds());

      //Create an actor and mapper
      vtkSmartPointer<vtkDataSetMapper> mapper =
          vtkSmartPointer<vtkDataSetMapper>::New();
    #if VTK_MAJOR_VERSION <= 5
      mapper->SetInput(ug);
    #else
      mapper->SetInputData(ug);
    #endif

      vtkSmartPointer<vtkActor> actor =
          vtkSmartPointer<vtkActor>::New();
      actor->SetMapper(mapper);

      //Create a renderer, render window, and interactor
      vtkSmartPointer<vtkRenderer> renderer =
          vtkSmartPointer<vtkRenderer>::New();
      vtkSmartPointer<vtkRenderWindow> renderWindow =
          vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer(renderer);
      vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
          vtkSmartPointer<vtkRenderWindowInteractor>::New();
      renderWindowInteractor->SetRenderWindow(renderWindow);

      renderer->AddActor(actor);

      // Create a nice view
      renderer->ResetCamera();
      renderer->GetActiveCamera()->Azimuth(180);
      renderer->GetActiveCamera()->Elevation(-20);
      renderer->ResetCameraClippingRange();

      renderer->SetBackground(0.2, 0.3, 0.4);

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

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 2.8)

    PROJECT(Pyramid)

    find_package(VTK REQUIRED)
    include(${VTK_USE_FILE})

    add_executable(Pyramid MACOSX_BUNDLE Pyramid.cxx)

    target_link_libraries(Pyramid ${VTK_LIBRARIES})

### Download and Build Pyramid

Danger

The generation of tar files has not been ported to the new VTKExamples. Some tarballs may be missing or out-of-date.

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

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

    ./Pyramid

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