[vtkusers] Getting started with plotting 2D matrix as 2D image

Nicholas Kinar n.kinar at usask.ca
Fri Jun 12 13:37:53 EDT 2009


Hello!

I am trying to develop an application which displays data contained in a 
2D matrix as a 2D image.  The idea is to display the data in a similar 
fashion to the Matlab imagesc() function, with appropriate scaling and 
colorbar.  The application is being compiled under Mac OS X 10.5.7, 
using the Apple plain-vanilla gcc 4.0.1.  I am using the following 
CMakeLists.txt script to compile my application:

cmake_minimum_required(VERSION 2.6)
project(model)

find_path(VTK_DIR UseVTK.cmake $ENV{VTK_DIR} )
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

include_directories(/Volumes/FILES/Programs/common/TNT)

set(CMAKE_BUILD_TYPE CMAKE_CXX_FLAGS_DEBUG)
set(CMAKE_OSX_ARCHITECTURES x86_64)
add_executable(model model.cpp model.h)
target_link_libraries(model vtkRendering vtkCommon)


I have set the path to VTK_DIR, and I am also including the directories 
of the Template Numerical Toolkit Library, a number of header files 
which provide support for arrays useful in scientific computing.   Using 
the cmake GUI, I have added CMAKE_CXX_FLAGS_DEBUG to the 
CMAKE_BUILD_TYPE variable.  In addition, CMAKE_OSX_ARCHITECTURES is set 
to x86_64.  I have built the VTK 5.4.2 library with these variables.

I am using the following code to set up the image display:


// Function to set up VTK support
// num = number of elements in a num-by-num matrix
void create_VTK_window(int num)
{
      image_viewer = vtkImageViewer2::New();
      import = vtkImageImport::New();
      lookupTable = vtkLookupTable::New();
      colorbar = vtkScalarBarActor::New();
      render = vtkRenderer::New();
      interactor = vtkRenderWindowInteractor::New();

      import->SetDataExtent(0,num,0,num,0,0);
      import->SetWholeExtent(0,num,0,num,0,0);
      import->SetDataScalarTypeToDouble();

      image_viewer->SetInput(import->GetOutput());

      //add a colorbar
      lookupTable->SetNumberOfColors(9344);
      lookupTable->SetTableRange(0,1.0);
      lookupTable->ForceBuild();

      colorbar->SetLookupTable(lookupTable);
      colorbar->SetWidth(0.05);
      colorbar->SetPosition(0.95, 0.1);
      colorbar->SetLabelFormat("%.3g");
      colorbar->PickableOff();
      colorbar->VisibilityOn();

      render->AddActor(colorbar);
      image_viewer->SetupInteractor(interactor);
      image_viewer->SetRenderer(render);
}



// Function to clean up once we are done
void cleanup_VTK()
{
    image_viewer->Delete();
    import->Delete();
    lookupTable->Delete();
    colorbar->Delete();
    render->Delete();
    interactor->Delete();
}



// Function to plot data to the screen
void plot_data_VTK(TNT::Fortran_Array2D<double> *m)
{
    int N = m->dim1();
    TNT::Array2D<double>temp(N,N,0.0);
    double **pm;
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            temp[i-1][j-1] = (*m)(i,j);
        }
    }
    pm = temp;

    import->SetImportVoidPointer(pm);
    import->SetWholeExtent(0,N,0,N,0,0);
    import->SetDataExtent(0,N,0,N,0,0);
    image_viewer->SetInput(import->GetOutput());
    image_viewer->Render();
}


A few strange things are happening:

(1) Although a window does open, and I am able to see the color-bar on a 
black background, I cannot see the data that I have added using the 
plot_data_VTK function.  Displayed on the screen are a few "speckled" 
white pixels at the bottom of the plot.  I have verified that the pm 
pointer contains valid data when accessed as a 2D array.  I have also 
used the GetImportVoidPointer() function of the vtkImageImport class to 
verify that the void pointer is being imported.

(2) I am unable to debug my program using gdb.  The debugger complains 
that "Program is not a recognized executable" and "Not a valid archive 
file".  However, I am able to run the program at the command-line, and 
the program does compile.  I am using Eclipse IDE for C/C++ development.

(3) The pointer changes to "busy" (i.e. pinwheel) when the mouse passes 
into the VTK window.  What do I need to change to make this window 
similar to the plot window displayed by Matlab?  The plot window in 
Matlab stays open and does not display a "busy" pointer. 

Thank you,

Nicholas




More information about the vtkusers mailing list