Skip to content


# Hello

VTKEx/Cxx/VisualizationAlgorithms/Hello

### Description

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

Info

See Figure 6-28 in Chapter 6 the VTK Textbook.

Other languages

See (Python)

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

Hello.cxx

    #include <vtkActor.h>
    #include <vtkCamera.h>
    #include <vtkContourFilter.h>
    #include <vtkImplicitModeller.h>
    #include <vtkNamedColors.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;
      }
      vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();

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

      vtkSmartPointer<vtkPolyDataMapper> lineMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      lineMapper->SetInputConnection(reader->GetOutputPort());

      vtkSmartPointer<vtkActor> lineActor =
        vtkSmartPointer<vtkActor>::New();
      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.
    //
      vtkSmartPointer<vtkImplicitModeller> imp =
        vtkSmartPointer<vtkImplicitModeller>::New();
      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);

      vtkSmartPointer<vtkContourFilter> contour =
        vtkSmartPointer<vtkContourFilter>::New();
      contour->SetInputConnection(imp->GetOutputPort());
      contour->SetValue(0, 0.25);

      vtkSmartPointer<vtkPolyDataMapper> impMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      impMapper->SetInputConnection(contour->GetOutputPort());
      impMapper->ScalarVisibilityOff();

      vtkSmartPointer<vtkActor> impActor =
        vtkSmartPointer<vtkActor>::New();
      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
    //
      vtkSmartPointer<vtkRenderer> ren1 =
        vtkSmartPointer<vtkRenderer>::New();

      vtkSmartPointer<vtkRenderWindow> renWin =
        vtkSmartPointer<vtkRenderWindow>::New();
      renWin->AddRenderer(ren1);

      vtkSmartPointer<vtkRenderWindowInteractor> iren =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      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);

      vtkSmartPointer<vtkCamera> camera =
        vtkSmartPointer<vtkCamera>::New();
      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.3 FATAL_ERROR)

    project(Hello)

    find_package(VTK COMPONENTS
      vtkCommonColor
      vtkFiltersCore
      vtkFiltersHybrid
      vtkIOLegacy
      vtkInteractionStyle
      vtkRenderingContextOpenGL2
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingGL2PSOpenGL2
      vtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping Hello: ${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(Hello MACOSX_BUNDLE Hello.cxx )
      target_link_libraries(Hello PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      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}
        )
    endif ()

### 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.