Skip to content


# EllipticalCylinder

VTKEx/Cxx/GeometricObjects/EllipticalCylinder

### Description

The example creates an elliptical cross-section and stores it in a vtkPolyData. Then, the vtkLinearExtrusionFilter creates an elliptical cylinder by extruding the vtkPolyLine along a vector. The example sets the backface property of the vtkActor to show the front and back face of the cylinder.

See also

EllipticalCylinderDemo to see the vtkPolyLine and the vector represented as an oriented arrow.

Other languages

See (Python), (Java)

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

EllipticalCylinder.cxx

    #include <vtkActor.h>
    #include <vtkCamera.h>
    #include <vtkInteractorStyleTrackballCamera.h>
    #include <vtkLinearExtrusionFilter.h>
    #include <vtkNamedColors.h>
    #include <vtkPoints.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkPolyLine.h>
    #include <vtkProperty.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkSmartPointer.h>

    int main(int /* argc */, char * /* argv */ [])
    {

      double angle = 0;
      double r1, r2;
      double centerX, centerY;
      r1 = 50;
      r2 = 30;
      centerX = 10.0;
      centerY = 5.0;

      vtkSmartPointer<vtkPoints> points =
        vtkSmartPointer<vtkPoints>::New();
      int id = 0;
      while (angle <= 2.0 * vtkMath::Pi() + (vtkMath::Pi() / 60.0))
      {
        points->InsertNextPoint(r1 * cos(angle) + centerX,
                                r2 * sin(angle) + centerY,
                                0.0);
        angle = angle + (vtkMath::Pi() / 60.0);
        ++id;
      }

      vtkSmartPointer<vtkPolyLine> line =
        vtkSmartPointer<vtkPolyLine>::New();
      line->GetPointIds()->SetNumberOfIds(id);
      for(unsigned int i = 0; i < static_cast<unsigned int>(id); ++i)
      {
        line->GetPointIds()->SetId(i,i);
      }

      vtkSmartPointer<vtkCellArray> lines =
        vtkSmartPointer<vtkCellArray>::New();
      lines->InsertNextCell(line);

      vtkSmartPointer<vtkPolyData> polyData =
        vtkSmartPointer<vtkPolyData>::New();
      polyData->SetPoints(points);
      polyData->SetLines(lines);

      vtkSmartPointer<vtkLinearExtrusionFilter> extrude =
        vtkSmartPointer<vtkLinearExtrusionFilter>::New();
      extrude->SetInputData(polyData);
      extrude->SetExtrusionTypeToNormalExtrusion();
      extrude->SetVector(0, 0, 100.0);
      extrude->Update();

      vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();

      vtkSmartPointer<vtkPolyDataMapper> lineMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      lineMapper->SetInputData(polyData);

      vtkSmartPointer<vtkActor> lineActor =
        vtkSmartPointer<vtkActor>::New();
      lineActor->SetMapper(lineMapper);
      lineActor->GetProperty()->SetColor(colors->GetColor3d("Peacock").GetData());

      vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      mapper->SetInputConnection(extrude->GetOutputPort());

      vtkSmartPointer<vtkProperty> back =
        vtkSmartPointer<vtkProperty>::New();
      back->SetColor(colors->GetColor3d("Tomato").GetData());

      vtkSmartPointer<vtkActor> actor =
        vtkSmartPointer<vtkActor>::New();
      actor->SetMapper(mapper);
      actor->GetProperty()->SetColor(colors->GetColor3d("Banana").GetData());
      actor->SetBackfaceProperty(back);

      vtkSmartPointer<vtkRenderer> ren =
        vtkSmartPointer<vtkRenderer>::New();
      ren->SetBackground(colors->GetColor3d("SlateGray").GetData());
      ren->AddActor(actor);
      ren->AddActor(lineActor);

      vtkSmartPointer<vtkRenderWindow> renWin =
        vtkSmartPointer<vtkRenderWindow>::New();
      renWin->SetWindowName("Elliptical Cylinder");
      renWin->AddRenderer(ren);
      renWin->SetSize(600, 600);

      vtkSmartPointer<vtkRenderWindowInteractor> iren =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      iren->SetRenderWindow(renWin);

      vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
        vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
      iren->SetInteractorStyle(style);

      vtkSmartPointer<vtkCamera> camera =
        vtkSmartPointer<vtkCamera>::New();
      camera->SetPosition (0, 1, 0);
      camera->SetFocalPoint (0, 0, 0);
      camera->SetViewUp (0, 0, 1);
      camera->Azimuth(30);
      camera->Elevation(30);

      ren->SetActiveCamera(camera);
      ren->ResetCamera();
      ren->ResetCameraClippingRange();

      renWin->Render();
      iren->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(EllipticalCylinder)

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

### Download and Build EllipticalCylinder

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

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

    ./EllipticalCylinder

WINDOWS USERS

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