Monday 3 June 2013

Rendering simple model in VTK

VTK have two major components vtk visualization pipeline and rendering engine. Pipeline is used to acquire or create data, manipulate data and then either write it or to fed to rendering engine. Rendering engine is composed of classes that are used to display data of visualization pipeline.
VTK is an object oriented system. To use it effectively understanding of its object model is necessary. Following is a brief introduction to vtk object model with some useful links.

VTK object model

The VTK object model is rooted in the superclass vtkObject.
vtkObject is the base class for most objects in the visualization toolkit. 
vtkObject provides methods for tracking modification time, debugging, printing, and event callbacks.
vtkSmartPointer class template provide simple way for object management. In smart pointer implementation pointer automatically manages the reference count.
Here is very useful document on use of vtkSmartPointer.

VTK Visualization Pipeline

Simplest pipeline have a source of data to be visualized, it may be a generator as it this case or a reader. This data then set to the input of mapper which maps data into a visual data object actor, an actor represent an entity in a rendering scene. Then we have to set a renderer window having at least one renderer in which this actor will be visualized. So we will make an object of renderer and add that actor to that then set that to window. Also for user interaction we will create an interactor object and add set that to window. Here is a typical vtk graphic subsystem: 


Here is code of a simple vtk pipeline
RenderingSimpleModel.cpp :
#include "vtkSmartPointer.h"
#include "vtkCylinderSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"

void main()
{

vtkSmartPointer<vtkCylinderSource> source=
          vtkSmartPointer<vtkCylinderSource>::New();

//You can set following parameters in source:
     source->SetCapping(false);
     source->SetHeight(5);
     source->SetRadius(2);
     source->SetResolution(60);
     source->SetCenter(0.0,1.0,1.0);

     vtkSmartPointer<vtkPolyDataMapper> mapper =
          vtkSmartPointer<vtkPolyDataMapper>::New();

     mapper->SetInput(source->GetOutput());

     vtkSmartPointer<vtkActor > actor = vtkSmartPointer
<vtkActor >::New();

     actor->SetMapper(mapper);

     vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer >::New();

     vtkSmartPointer<vtkRenderWindow > renwin = vtkSmartPointer<vtkRenderWindow  > ::New();

     ren->SetBackground(0,0.5,0.5);
     renwin->AddRenderer(ren);
     renwin->SetSize(200,200);
     ren->AddActor(actor);

     vtkSmartPointer<vtkRenderWindowInteractor > renint =
          vtkSmartPointer<vtkRenderWindowInteractor >::New();

     renwin->SetInteractor(renint);
     renwin->Render();
     renint->Start();
}

And Here is CmakeLists.txt:

cmake_minimum_required(VERSION 2.4)
if(COMMAND CMAKE_POLICY)
  cmake_policy(SET CMP0003 NEW)
endif(COMMAND CMAKE_POLICY)

PROJECT( RenderingSimpleModel)

FIND_PACKAGE ( VTK)
IF ( VTK_FOUND)
INCLUDE( ${USE_VTK_FILE} )
ENDIF( VTK_FOUND)
INCLUDE_DIRECTORIES(
${myProject_SOURCE_DIR}
)
ADD_EXECUTABLE( RenderingSimpleModel RenderingSimpleModel.cpp)

TARGET_LINK_LIBRARIES (RenderingSimpleModel
 ${VTK_LIBRARIES}
)


Put both the file in source folder and cmake using Cmake GUI. Possible error may be "VTK dir not found"  while cmake, in this case you just have to copy path of VTK bin to the VTK_DIR variable in Cmake GUI.

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete