Skip to content

ConeDemo

vtk-examples/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 question about this example, please use the VTK Discourse Forum

Code

ConeDemo.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkConeSource.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

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

  // 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");

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetSize(640, 480);
  renderWindow->SetWindowName("ConeDemo");

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Shared camera.
  vtkNew<vtkCamera> camera;

  // 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++)
  {
    vtkNew<vtkRenderer> renderer;
    renderer->SetBackground(backgroundColor.GetData());

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

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

    // Create a mapper and actor
    vtkNew<vtkPolyDataMapper> mapper;
    mapper->SetInputConnection(coneSource->GetOutputPort());

    vtkNew<vtkProperty> backfaceProp;
    backfaceProp->SetDiffuseColor(actorBackfaceColor.GetData());

    vtkNew<vtkActor> actor;
    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);

  vtkNew<vtkInteractorStyleTrackballCamera> style;
  renderWindowInteractor->SetInteractorStyle(style);
  renderWindow->Render();

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(ConeDemo)

find_package(VTK COMPONENTS 
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "ConeDemo: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
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}
)

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.