BlobbyLogo

VTKExamples/Cxx/Visualization/BlobbyLogo


Description

This is the blobby vtk logo described in chapter 12 of the VTK textbook. The example uses vtkAppendPolyData to combine three vtkPolyData. A vtkImplicitModeller creates a vtkImageData of the distnaces from the polydata triangles. vtkContourFilter extracts an isosurface representing an offset surface.

This examples uses the data v.vtk, t.vtk and k.vtk.

Other Languages

See (Python), (Java)

Code

BlobbyLogo.cxx

//
// use implicit modeller to create the VTK logo
//

#include <vtkActor.h>
#include <vtkAppendPolyData.h>
#include <vtkContourFilter.h>
#include <vtkImplicitModeller.h>
#include <vtkNamedColors.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolyDataNormals.h>
#include <vtkPolyDataReader.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>

int main( int argc, char *argv[] )
{
  if (argc < 4)
  {
    std::cout << "Usage: " << argv[0] << " v.vtk t.vtk k.vtk" << std::endl;
    return EXIT_FAILURE;
  }

  vtkSmartPointer<vtkRenderer> aRenderer =
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> aRenderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  aRenderWindow->AddRenderer(aRenderer);
  vtkSmartPointer<vtkRenderWindowInteractor> anInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  anInteractor->SetRenderWindow(aRenderWindow);
  aRenderWindow->SetSize( 300, 300 );

  // read the geometry file containing the letter v
  vtkSmartPointer<vtkPolyDataReader> letterV =
    vtkSmartPointer<vtkPolyDataReader>::New();
  letterV->SetFileName (argv[1]);

  // read the geometry file containing the letter t
  vtkSmartPointer<vtkPolyDataReader> letterT =
    vtkSmartPointer<vtkPolyDataReader>::New();
  letterT->SetFileName (argv[2]);

  // read the geometry file containing the letter k
  vtkSmartPointer<vtkPolyDataReader> letterK =
    vtkSmartPointer<vtkPolyDataReader>::New();
  letterK->SetFileName (argv[3]);

  // create a transform and transform filter for each letter
  vtkSmartPointer<vtkTransform> VTransform =
    vtkSmartPointer<vtkTransform>::New();
  vtkSmartPointer<vtkTransformPolyDataFilter> VTransformFilter =
    vtkSmartPointer<vtkTransformPolyDataFilter>::New();
  VTransformFilter->SetInputConnection(letterV->GetOutputPort());
  VTransformFilter->SetTransform (VTransform);

  vtkSmartPointer<vtkTransform> TTransform =
    vtkSmartPointer<vtkTransform>::New();
  vtkSmartPointer<vtkTransformPolyDataFilter> TTransformFilter =
    vtkSmartPointer<vtkTransformPolyDataFilter>::New();
  TTransformFilter->SetInputConnection (letterT->GetOutputPort());
  TTransformFilter->SetTransform (TTransform);

  vtkSmartPointer<vtkTransform> KTransform =
    vtkSmartPointer<vtkTransform>::New();
  vtkSmartPointer<vtkTransformPolyDataFilter> KTransformFilter =
    vtkSmartPointer<vtkTransformPolyDataFilter>::New();
  KTransformFilter->SetInputConnection(letterK->GetOutputPort());
  KTransformFilter->SetTransform (KTransform);

  // now append them all
  vtkSmartPointer<vtkAppendPolyData> appendAll =
    vtkSmartPointer<vtkAppendPolyData>::New();
  appendAll->AddInputConnection (VTransformFilter->GetOutputPort());
  appendAll->AddInputConnection (TTransformFilter->GetOutputPort());
  appendAll->AddInputConnection (KTransformFilter->GetOutputPort());

  // create normals
  vtkSmartPointer<vtkPolyDataNormals> logoNormals =
    vtkSmartPointer<vtkPolyDataNormals>::New();
  logoNormals->SetInputConnection (appendAll->GetOutputPort());
  logoNormals->SetFeatureAngle (60);

  // map to rendering primitives
  vtkSmartPointer<vtkPolyDataMapper> logoMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  logoMapper->SetInputConnection (logoNormals->GetOutputPort());

  // now an actor
  vtkSmartPointer<vtkActor> logo =
    vtkSmartPointer<vtkActor>::New();
  logo->SetMapper (logoMapper);

  // now create an implicit model of the same letter
  vtkSmartPointer<vtkImplicitModeller> blobbyLogoImp =
    vtkSmartPointer<vtkImplicitModeller>::New();
  blobbyLogoImp->SetInputConnection(appendAll->GetOutputPort());
  blobbyLogoImp->SetMaximumDistance (.075);
  blobbyLogoImp->SetSampleDimensions (64,64,64); 
  blobbyLogoImp->SetAdjustDistance (0.05);

  // extract an iso surface
  vtkSmartPointer<vtkContourFilter> blobbyLogoIso =
    vtkSmartPointer<vtkContourFilter>::New();
  blobbyLogoIso->SetInputConnection (blobbyLogoImp->GetOutputPort());
  blobbyLogoIso->SetValue (1, 1.5);

  // map to rendering primitives
  vtkSmartPointer<vtkPolyDataMapper> blobbyLogoMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  blobbyLogoMapper->SetInputConnection (blobbyLogoIso->GetOutputPort());
  blobbyLogoMapper->ScalarVisibilityOff ();

  vtkSmartPointer<vtkNamedColors> colors =
    vtkSmartPointer<vtkNamedColors>::New();

  vtkSmartPointer<vtkProperty> tomato =
    vtkSmartPointer<vtkProperty>::New();
  tomato->SetDiffuseColor(colors->GetColor3d("tomato").GetData());
  tomato->SetSpecular(.3);
  tomato->SetSpecularPower(20);

  vtkSmartPointer<vtkProperty> banana =
    vtkSmartPointer<vtkProperty>::New();
  banana->SetDiffuseColor(colors->GetColor3d("banana").GetData());
  banana->SetDiffuse (.7);
  banana->SetSpecular(.4);
  banana->SetSpecularPower(20);

  // now an actor
  vtkSmartPointer<vtkActor> blobbyLogo =
    vtkSmartPointer<vtkActor>::New();
  blobbyLogo->SetMapper (blobbyLogoMapper);
  blobbyLogo->SetProperty (banana);

  // position the letters

  VTransform->Translate (-16.0,0.0,12.5);
  VTransform->RotateY (40);

  KTransform->Translate (14.0, 0.0, 0.0);
  KTransform->RotateY (-40);

  // move the polygonal letters to the front
  logo->SetProperty (tomato);
  logo->SetPosition(0,0,6);

  aRenderer->AddActor(logo);
  aRenderer->AddActor(blobbyLogo);

  aRenderer->SetBackground(colors->GetColor3d("SlateGray").GetData());

  aRenderWindow->Render();

  // interact with data
  anInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(BlobbyLogo)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonTransforms
  vtkFiltersCore
  vtkFiltersGeneral
  vtkFiltersHybrid
  vtkIOLegacy
  vtkInteractionStyle
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
  message("Skipping BlobbyLogo: ${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(BlobbyLogo MACOSX_BUNDLE BlobbyLogo.cxx )
  target_link_libraries(BlobbyLogo PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(BlobbyLogo MACOSX_BUNDLE BlobbyLogo.cxx )
  target_link_libraries(BlobbyLogo PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS BlobbyLogo
    MODULES ${VTK_LIBRARIES}
    )
endif () 

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

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

./BlobbyLogo

WINDOWS USERS

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