[vtkusers] Emulating Matlab imagesc() function in VTK
    Prathamesh Kulkarni 
    prathameshmkulkarni at gmail.com
       
    Sat Aug 14 07:02:05 EDT 2010
    
    
  
I think, the same code should work fine if you use a 1D double array instead
of 2D.
Prathamesh
> I am wondering if there is a way in VTK to render the elements of a 2D
> array in a similar fashion to the Matlab imagesc() function.  Suppose
> that I have a 1000-by-1000 array M of values.  In Matlab, calling
> imagesc(M) will display a color-mapped image with the colors scaled to
> the maximum and minimum numbers in the image.  Using the following
> Matlab syntax will plot the 2D array with a colorbar:
>
> imagesc(M);
> colorbar;
>
> Is there a similar way to do this in VTK?  (I am currently working
> with C++.)  In the past, I tried using vtkImageViewer2, but I remember
> that I was not successful.  What would be an example pipeline (from
> start to finish) that would allow me to plot a similar image in VTK?
>
> Since I am writing a Finite-Difference Time-Domain (FDTD) simulation,
> I would like to progressively update the plot at each timestep so that
> the user of the program can view an animation of how the simulation
> progresses.  How would I dynamically update the plot at each timestep?
Well, I've been working on displaying the 2D data as an image.
I've written a function in C++ to try and and test the notion of
displaying the values in a 2D matrix as an image.  This test function
uses the Template Numerical Toolkit (TNT) to store the data in a 2D
array.  The function load_matrix() is responsible for loading the data
from file into the matrix object M.  The data is a 2D matrix object M
which is then converted to a pointer **pm by the assignment pm = M.
This is done due to the TNT Array2D object overloading the equality (=)
operator.
The example compiles without errors and VTK opens a display window.
However, I see nothing but "garbage data" in the display window.
Is there a way to assign data in a 2D array to a vtkImage?  Ultimately I
would like to display this 2D array in a similar fashion to the Matlab
imagesc() function.
Here is the code of the test function:
//BEGIN CODE
void test_VTK()
{
    std::string fileName = "M.txt";
    int Nx = 1000;
    int Ny = 1000;
    int PML_num = 20;
    int Nxp = Nx + 2 * PML_num;
    int Nyp = Ny + 2 * PML_num;
    TNT::Array2D<double> M(Nxp, Nyp);
    double **pm;
    load_matrix( fileName, &M );
    pm = M;  // convert object to pointer
   // Here variable pm contains the data from the 2D Array object
   // so it is similar to a 2D array in C
    vtkSmartPointer<vtkImageImport> import =
            vtkSmartPointer<vtkImageImport>::New();
    import->SetImportVoidPointer((void*)pm);
    import->SetWholeExtent(0,Nxp,0,Nyp,0,0);
    import->SetDataExtent(0,Nxp,0,Nyp,0,0);
    vtkSmartPointer<vtkLookupTable> lookupTable =
            vtkSmartPointer<vtkLookupTable>::New();
    vtkSmartPointer<vtkScalarBarActor> colorbar =
                vtkSmartPointer<vtkScalarBarActor>::New();
    lookupTable->SetNumberOfColors(9344);
    lookupTable->SetTableRange(0,10);
    lookupTable->ForceBuild();
    colorbar->SetLookupTable(lookupTable);
    colorbar->SetWidth(0.05);
    colorbar->SetPosition(0.95, 0.1);
    colorbar->SetLabelFormat("%.3g");
    colorbar->PickableOff();
    colorbar->VisibilityOn();
    vtkSmartPointer<vtkImageViewer2> viewer =
             vtkSmartPointer<vtkImageViewer2>::New();
    viewer->SetInput(import->GetOutput());
    viewer->SetInput( import->GetOutput() );
    vtkSmartPointer<vtkRenderer> renderer =
          vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor ( viewer->GetImageActor() );
    renderer->AddActor(colorbar);
    renderer->ResetCamera();
    vtkSmartPointer<vtkRenderWindow> renderWindow =
          vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer ( renderer );
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
          vtkSmartPointer<vtkRenderWindowInteractor>::New();
    vtkSmartPointer<vtkInteractorStyleImage> style =
          vtkSmartPointer<vtkInteractorStyleImage>::New();
    renderWindowInteractor->SetInteractorStyle( style );
    renderWindowInteractor->SetRenderWindow ( renderWindow );
    renderWindowInteractor->Initialize();
    renderWindowInteractor->Start();
} // end function
// END CODE
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100814/b2b644f7/attachment.htm>
    
    
More information about the vtkusers
mailing list