Skip to content

Hello

vtk-examples/Cxx/VisualizationAlgorithms/Hello


Description

Implicit modelling used to thicken a stroked font. Original lines can be seen within the translucent implicit surface.

Other languages

See (Python)

Question

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

Code

Hello.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkContourFilter.h>
#include <vtkImplicitModeller.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolyDataReader.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

int main(int argc, char* argv[])
{
  if (argc < 2)
  {
    std::cout << "Usage: " << argv[0] << " hello.vtk" << std::endl;
    return EXIT_FAILURE;
  }
  vtkNew<vtkNamedColors> colors;

  // Create lines which serve as the "seed" geometry. The lines spell the
  // word "hello".
  //
  vtkNew<vtkPolyDataReader> reader;
  reader->SetFileName(argv[1]);

  vtkNew<vtkPolyDataMapper> lineMapper;
  lineMapper->SetInputConnection(reader->GetOutputPort());

  vtkNew<vtkActor> lineActor;
  lineActor->SetMapper(lineMapper);
  lineActor->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());
  lineActor->GetProperty()->SetLineWidth(3.0);

  // Create implicit model with vtkImplicitModeller. This computes a scalar
  // field which is the distance from the generating geometry. The contour
  // filter then extracts the geometry at the distance value 0.25 from the
  // generating geometry.
  //
  vtkNew<vtkImplicitModeller> imp;
  imp->SetInputConnection(reader->GetOutputPort());
  imp->SetSampleDimensions(110, 40, 20);
  imp->SetMaximumDistance(0.25);
  imp->SetModelBounds(-1.0, 10.0, -1.0, 3.0, -1.0, 1.0);

  vtkNew<vtkContourFilter> contour;
  contour->SetInputConnection(imp->GetOutputPort());
  contour->SetValue(0, 0.25);

  vtkNew<vtkPolyDataMapper> impMapper;
  impMapper->SetInputConnection(contour->GetOutputPort());
  impMapper->ScalarVisibilityOff();

  vtkNew<vtkActor> impActor;
  impActor->SetMapper(impMapper);
  impActor->GetProperty()->SetColor(colors->GetColor3d("Peacock").GetData());
  impActor->GetProperty()->SetOpacity(0.5);

  // Create the usual graphics stuff.
  // Create the RenderWindow, Renderer and both Actors
  //
  vtkNew<vtkRenderer> ren1;

  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(ren1);

  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renWin);

  // Add the actors to the renderer, set the background and size
  //
  ren1->AddActor(lineActor);
  ren1->AddActor(impActor);
  ren1->SetBackground(colors->GetColor3d("Wheat").GetData());
  renWin->SetSize(640, 480);
  renWin->SetWindowName("Hello");

  vtkNew<vtkCamera> camera;
  camera->SetFocalPoint(4.5, 1, 0);
  camera->SetPosition(4.5, 1.0, 6.73257);
  camera->SetViewUp(0, 1, 0);

  ren1->SetActiveCamera(camera);
  ren1->ResetCamera();
  camera->Dolly(1.3);
  camera->SetClippingRange(1.81325, 90.6627);

  renWin->Render();
  iren->Start();
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(Hello)

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

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

Download and Build Hello

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

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

./Hello

WINDOWS USERS

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