# 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

### Code¶

ConeDemo.cxx

#include <vtkNew.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 *[])
{
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());

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->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.3 FATAL_ERROR)

project(ConeDemo)

find_package(VTK COMPONENTS
vtkCommonColor
vtkCommonCore
vtkCommonDataModel
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
target_link_libraries(ConeDemo PRIVATE ${VTK_LIBRARIES}) # vtk_module_autoinit is needed vtk_module_autoinit( TARGETS ConeDemo MODULES${VTK_LIBRARIES}
)
endif ()


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.