Project configuration on VC++2005

Include path: (somewhere)\VTK5.0.4\include\vtk-5.0
Library path: (somewhere)\VTK5.0.4\lib
Library files: vtkCommon.lib vtkIO.lib vtkDICOMParser.lib vtkexoIIc.lib vtkexpat.lib vtkFiltering.lib vtkfreetype.lib vtkftgl.lib vtkGenericFiltering.lib vtkGraphics.lib vtkHybrid.lib vtkImaging.lib vtkjpeg.lib vtkNetCDF.lib vtkpng.lib vtkRendering.lib vtksys.lib vtktiff.lib vtkVolumeRendering.lib vtkWidgets.lib vtkzlib.lib

In order to go debug into VTK source codes, you have to change the library path like following:
Library path: (somewhere)\vtk-5.0.4_bin\bin\debug


Show debug message

 vtkUnstructuredGridReader *polyDataReader = vtkUnstructuredGridReader::New();

Read all scalar and vector data

VTK with default setting loads only one data.

Get number of points and number of cells

 int num_points = polyData->GetPoints()->GetNumberOfPoints();
 int num_celss = polyData->GetNumberOfCells();

Get array of scalar and vector data

 vtkPointData *pd = polyData->GetPointData();
 vtkDataArray *arrP = pd->GetScalars("PRESSURE1");
 vtkDataArray *arrV = pd->GetVectors("velocity");

Calculate distance between two point

 double point1[3] = {0,0,0}, point2[3] = {100,0,0};
 double d = vtkMath::Distance2BetweenPoints(point1, point2);
 d = sqrt(d);

Example1: Visualization with VTK

// vtk_test.cpp : Defines the entry point for the console application.
#include "stdafx.h"
// first include the required header files for the vtk classes we are using
#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkCamera.h"
int _tmain(int argc, _TCHAR* argv[])
  // Next we create an instance of vtkConeSource and set some of its 
  // properties
  vtkConeSource *cone = vtkConeSource::New();
  cone->SetHeight( 3.0 );
  cone->SetRadius( 1.0 );
  cone->SetResolution( 10 );
  // We create an instance of vtkPolyDataMapper to map the polygonal data 
  // into graphics primitives. We connect the output of the cone souece 
  // to the input of this mapper 
  vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();
  coneMapper->SetInput( cone->GetOutput() );
  // create an actor to represent the cone. The actor coordinates rendering of
  // the graphics primitives for a mapper. We set this actor's mapper to be
  // coneMapper which we created above.
  vtkActor *coneActor = vtkActor::New();
  coneActor->SetMapper( coneMapper );
  // Create the Renderer and assign actors to it. A renderer is like a
  // viewport. It is part or all of a window on the screen and it is
  // responsible for drawing the actors it has.  We also set the background
  // color here
  vtkRenderer *ren1= vtkRenderer::New();
  ren1->AddActor( coneActor );
  ren1->SetBackground( 0.1, 0.2, 0.4 );
  // Finally we create the render window which will show up on the screen
  // We put our renderer into the render window using AddRenderer. We also
  // set the size to be 300 pixels by 300
  vtkRenderWindow *renWin = vtkRenderWindow::New();
  renWin->AddRenderer( ren1 );
  renWin->SetSize( 300, 300 );
  // now we loop over 360 degreeees and render the cone each time
  int i;
  for (i = 0; i < 360; ++i)
    // render the image
    // rotate the active camera by one degree
    ren1->GetActiveCamera()->Azimuth( 1 );
  // Free up any objects we created
  return 0;

Example2: Load and access VTK data

vtkUnstructuredGridReader *polyDataReader = vtkUnstructuredGridReader::New();
//  polyDataReader->DebugOn();
  polyDataReader->ReadAllScalarsOn(); //NEVER FORGET THIS
  polyDataReader->ReadAllVectorsOn(); //NEVER FORGET THIS
  vtkUnstructuredGrid *polyData = polyDataReader->GetOutput();
  if (polyData == NULL) {
    printf("##ERROR: polyData == NULL\n");
  printf("NUMBER OF POINTS: %d\n", polyData->GetPoints()->GetNumberOfPoints());
  printf("NUMBER OF CELLS: %d\n", polyData->GetNumberOfCells());
  printf("POINTS EXAMPLE: %d\n");
  for (int i=0; i<3; i++) {
    printf("%lf %lf %lf\n", polyData->GetPoint(i)[0], polyData->GetPoint(i)[1], polyData->GetPoint(i)[2]);
  printf("ELEMENTS EXAMPLE: %d\n");
  for (int i=0; i<3; i++) {
    printf("%d ", polyData->GetCell(i)->GetCellType());
    for (int j=0; j<polyData->GetCell(i)->GetNumberOfPoints(); j++)
      printf("%d ", polyData->GetCell(i)->GetPointId(j));
  printf("POINT DATA: \n");
  for (int i=0; i<pd->GetNumberOfArrays(); i++) {
    printf("%s\n", pd->GetArrayName(i));
  vtkPointData *pd = polyData->GetPointData();
  vtkDataArray *arrT = pd->GetScalars("TEMPERATURE1");
  double sum_T = 0.0;
  double tmp_T = 0.0;
  for (long i=0; i<arrT->GetNumberOfTuples(); i++) {
    tmp_T = arrT->GetComponent(i, 0);
    sum_T += tmp_T;


ヘルプ / FAQ もご覧ください。