Skip to content

CreateBFont

vtk-examples/Cxx/VisualizationAlgorithms/CreateBFont


Description

A scanned image clipped with a scalar value of 1/2 its maximum intensity produces a mixture of quadrilaterals and triangles.

Other languages

See (Python)

Question

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

Code

CreateBFont.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkClipPolyData.h>
#include <vtkImageDataGeometryFilter.h>
#include <vtkImageGaussianSmooth.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPNMReader.h>
#include <vtkPointData.h>
#include <vtkPolyDataMapper.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] << " image.pgm e.g. B.pgm" << std::endl;
    return EXIT_FAILURE;
  }
  // Now create the RenderWindow, Renderer and Interactor
  //
  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkRenderer> ren1;

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

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

  vtkNew<vtkPNMReader> imageIn;
  imageIn->SetFileName(argv[1]);

  vtkNew<vtkImageGaussianSmooth> gaussian;
  gaussian->SetStandardDeviations(2, 2);
  gaussian->SetDimensionality(2);
  gaussian->SetRadiusFactors(1, 1);
  gaussian->SetInputConnection(imageIn->GetOutputPort());

  vtkNew<vtkImageDataGeometryFilter> geometry;
  geometry->SetInputConnection(gaussian->GetOutputPort());

  vtkNew<vtkClipPolyData> aClipper;
  aClipper->SetInputConnection(geometry->GetOutputPort());
  aClipper->SetValue(127.5);
  aClipper->GenerateClipScalarsOff();
  aClipper->InsideOutOn();
  aClipper->GetOutput()->GetPointData()->CopyScalarsOff();
  aClipper->Update();

  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(aClipper->GetOutputPort());
  mapper->ScalarVisibilityOff();

  vtkNew<vtkActor> letter;
  letter->SetMapper(mapper);

  ren1->AddActor(letter);
  letter->GetProperty()->SetDiffuseColor(
      colors->GetColor3d("LampBlack").GetData());
  letter->GetProperty()->SetRepresentationToWireframe();

  ren1->SetBackground(colors->GetColor3d("WhiteSmoke").GetData());
  ren1->ResetCamera();
  ren1->GetActiveCamera()->Dolly(1.2);
  ren1->ResetCameraClippingRange();

  renWin->SetSize(640, 480);
  renWin->SetWindowName("CreateBFont");

  // render the image
  //
  renWin->Render();
  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(CreateBFont)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersCore
  vtkFiltersGeometry
  vtkIOImage
  vtkImagingGeneral
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping CreateBFont: ${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(CreateBFont MACOSX_BUNDLE CreateBFont.cxx )
  target_link_libraries(CreateBFont PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(CreateBFont MACOSX_BUNDLE CreateBFont.cxx )
  target_link_libraries(CreateBFont PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS CreateBFont
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build CreateBFont

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

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

./CreateBFont

WINDOWS USERS

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