Skip to content


# ProteinRibbons

VTKEx/Cxx/Visualization/ProteinRibbons

## Description

This example reads Protein Data Bank files. The example expects a file in .pdb format.

Here is a sample file.

Other languages

See (Java)

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

ProteinRibbons.cxx

    #include <vtkProteinRibbonFilter.h>

    #include <vtkActor.h>
    #include <vtkCamera.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkNew.h>
    #include <vtkProperty.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderer.h>
    #include <vtkPDBReader.h>
    #include <vtkInteractorStyleSwitch.h>

    #include <vtkNamedColors.h>

    int main(int argc, char *argv[])
    {
      if (argc < 2)
      {
        std::cout << "Usage: " << argv[0] << " file.pdb" << std::endl;
        return EXIT_FAILURE;
      }
      std::string fileName(argv[1]);

      // read protein from pdb
      vtkNew<vtkPDBReader> reader;
      reader->SetFileName(fileName.c_str());

      // setup ribbon filter
      vtkNew<vtkProteinRibbonFilter> ribbonFilter;
      ribbonFilter->SetInputConnection(reader->GetOutputPort());

      // setup poly data mapper
      vtkNew<vtkPolyDataMapper> polyDataMapper;
      polyDataMapper->SetInputConnection(ribbonFilter->GetOutputPort());

      // setup actor
      vtkNew<vtkActor> actor;
      actor->SetMapper(polyDataMapper);
      actor->GetProperty()->SetDiffuse(.7);
      actor->GetProperty()->SetSpecular(.5);
      actor->GetProperty()->SetSpecularPower(80.0);

      // setup render window
      vtkNew<vtkRenderer> renderer;
      vtkNew<vtkRenderWindow> renderWindow;
      renderWindow->AddRenderer(renderer);
      vtkNew<vtkRenderWindowInteractor> interactor;
      interactor->SetRenderWindow(renderWindow);
      vtkInteractorStyleSwitch* style =
        dynamic_cast<vtkInteractorStyleSwitch*>(interactor->GetInteractorStyle());
      style->SetCurrentStyleToTrackballCamera();

      vtkNew<vtkNamedColors> colors;
      renderer->AddActor(actor);
      renderer->SetBackground(colors->GetColor3d("Silver").GetData());
      renderWindow->SetSize(640, 480);

      renderer->ResetCamera();
      renderer->GetActiveCamera()->Zoom(1.5);
      renderer->ResetCameraClippingRange();
      renderWindow->Render();

      // Finally render the scene
      renderWindow->SetMultiSamples(0);
      interactor->Initialize();
      interactor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(ProteinRibbons)

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

### Download and Build ProteinRibbons

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

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

    ./ProteinRibbons

WINDOWS USERS

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