Skip to content

LinearExtrusion

Repository source: LinearExtrusion


Description

This example creates some text, extrudes it to make it 3D.

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

LinearExtrusion.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkDataSetMapper.h>
#include <vtkLinearExtrusionFilter.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTriangleFilter.h>
#include <vtkVectorText.h>

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

  // Create vector text
  vtkNew<vtkVectorText> vecText;
  vecText->SetText("Text!");

  // Apply linear extrusion
  vtkNew<vtkLinearExtrusionFilter> extrude;
  extrude->SetInputConnection(vecText->GetOutputPort());
  extrude->SetExtrusionTypeToNormalExtrusion();
  extrude->SetVector(0, 0, 1);
  extrude->SetScaleFactor(0.5);

  vtkNew<vtkTriangleFilter> triangleFilter;
  triangleFilter->SetInputConnection(extrude->GetOutputPort());

  vtkNew<vtkDataSetMapper> mapper;
  mapper->SetInputConnection(triangleFilter->GetOutputPort());

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetColor(colors->GetColor3d("PaleGoldenrod").GetData());

  vtkNew<vtkRenderWindow> renderWindow;

  vtkNew<vtkRenderer> renderer;
  renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());

  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("LinearExtrusion");

  renderer->AddActor(actor);

  renderer->ResetCamera();
  // Generate an interesting view
  renderer->ResetCamera();
  renderer->GetActiveCamera()->Azimuth(30);
  renderer->GetActiveCamera()->Elevation(30);
  renderer->GetActiveCamera()->Dolly(1.0);
  renderer->ResetCameraClippingRange();

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

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(LinearExtrusion)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersCore
  FiltersModeling
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "LinearExtrusion: 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(LinearExtrusion MACOSX_BUNDLE LinearExtrusion.cxx )
  target_link_libraries(LinearExtrusion PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS LinearExtrusion
  MODULES ${VTK_LIBRARIES}
)

Download and Build LinearExtrusion

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

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

./LinearExtrusion

WINDOWS USERS

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