Skip to content


# ConeDemo

VTKEx/Cxx/GeometricObjects/ConeDemo

### Description

vtkConeSource object creates a cone centered at a specified point and pointing in a specified direction. (By default, the center is the origin and the direction is the x-axis.)

Depending upon the resolution of this object, different representations are created. If resolution=0 a line is created; if resolution=1, a single triangle is created; if resolution=2, two crossed triangles are created.

For resolution > 2, a 3D cone (with resolution number of sides) is created.

It also is possible to control whether the bottom of the cone is capped with a (resolution-sided) polygon, and to specify the height and radius of the cone.

The example shows cones with resolutions 0, 1, 2, and 3.

Style

This example collects all of the color definition in the beginning of the example. This makes it easier to make changes to the colors without having to search the code.

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

ConeDemo.cxx

    #include <vtkSmartPointer.h>
    #include <vtkConeSource.h>

    #include <vtkCamera.h>
    #include <vtkProperty.h>
    #include <vtkPolyData.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkActor.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkInteractorStyleTrackballCamera.h>

    #include <vtkNamedColors.h>
    #include <vtkColor.h>

    int main(int, char *[])
    {
      auto colors =
        vtkSmartPointer<vtkNamedColors>::New();

      // Define all of the colors used in the example
      vtkColor3d backgroundColor    = colors->GetColor3d("tan");
      vtkColor3d actorColor         = colors->GetColor3d("orchid");
      vtkColor3d actorBackfaceColor = colors->GetColor3d("PowderBlue");
      vtkColor3d coneLineColor      = colors->GetColor3d("Black");

      auto renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->SetSize(640, 480);
      renderWindow->SetWindowName("ConeDemo");

      auto renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      renderWindowInteractor->SetRenderWindow(renderWindow);

      // Shared camera
      auto camera =
        vtkSmartPointer<vtkCamera>::New();

      // Define viewport ranges
      double xmins[4] = {0,.5,0,.5};
      double xmaxs[4] = {0.5,1,0.5,1};
      double ymins[4] = {0,0,.5,.5};
      double ymaxs[4]= {0.5,0.5,1,1};

      // Each viewport will contain a cone with an increasing resolution
      for(unsigned i = 0; i < 4; i++)
      {
        auto renderer =
          vtkSmartPointer<vtkRenderer>::New();
        renderer->SetBackground(backgroundColor.GetData());

        renderWindow->AddRenderer(renderer);
        renderer->SetViewport(xmins[i],ymins[i],xmaxs[i],ymaxs[i]);

        // Create a cone with different resolutions
        auto coneSource =
          vtkSmartPointer<vtkConeSource>::New();
        coneSource->SetResolution(i);
        coneSource->SetDirection(0, 1, 0);
        if (i == 3)
        {
          coneSource->SetResolution(20);
        }

        // Create a mapper and actor
        auto mapper =
          vtkSmartPointer<vtkPolyDataMapper>::New();
        mapper->SetInputConnection(coneSource->GetOutputPort());

        auto backfaceProp =
          vtkSmartPointer<vtkProperty>::New();
        backfaceProp->SetDiffuseColor(actorBackfaceColor.GetData());

        auto actor = vtkSmartPointer<vtkActor>::New();
        actor->SetMapper(mapper);
        if (i > 0)
        {
          actor->GetProperty()->
            SetDiffuseColor(actorColor.GetData());
        }
        else
        {
          actor->GetProperty()->
            SetDiffuseColor(coneLineColor.GetData());
        }
        actor->GetProperty()->SetLineWidth(2);
        actor->GetProperty()->EdgeVisibilityOn();
        actor->SetBackfaceProperty(backfaceProp);
        renderer->AddActor(actor);
        renderer->SetActiveCamera(camera);
        if (i == 3)
        {
          renderer->ResetCamera();
        }
      }
      camera->Azimuth(120);
      camera->Elevation(45);
      camera->Dolly(1.1);

      auto style =
        vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
      renderWindowInteractor->SetInteractorStyle(style);
      renderWindow->Render();

      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(ConeDemo)

    find_package(VTK COMPONENTS
      vtkCommonColor
      vtkCommonCore
      vtkCommonDataModel
      vtkFiltersSources
      vtkInteractionStyle
      vtkRenderingContextOpenGL2
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingGL2PSOpenGL2
      vtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping ConeDemo: ${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(ConeDemo MACOSX_BUNDLE ConeDemo.cxx )
      target_link_libraries(ConeDemo PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(ConeDemo MACOSX_BUNDLE ConeDemo.cxx )
      target_link_libraries(ConeDemo PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS ConeDemo
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build ConeDemo

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

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

    ./ConeDemo

WINDOWS USERS

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