[vtkusers] Emulating Matlab imagesc() function in VTK

Nicholas Kinar n.kinar at usask.ca
Sat Aug 14 13:49:45 EDT 2010


>
> I think, the same code should work fine if you use a 1D double array 
> instead of 2D.
>
> Prathamesh
>
>

Prathamesh,

Thank you very much for your response; this is greatly appreciated!  I 
tried changing my example code to use a 1D double array, but I still 
receive a similar result.  The window that is displayed by VTK shows 
only "garbage" data.  The rendered data appears as a series of white 
lines which I believe to be indicative of uninitialized memory.

What am I doing wrong with my example code?  Perhaps I am not utilizing 
the SetWholeExtent() and SetDataExtent() functions properly?  Might 
there be another way to display the matrix?

Here is the example function that I am now attempting to run.  What I 
now do is copy the data into a 1D array before passing it into the 
SetImportVoidPointer() function:

void test_VTK()
{
     std::string fileName = "M.txt";
     double deltax = 1.0e-3;
     double deltay = 1.0e-3;
     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 );

     vtkSmartPointer<vtkImageImport> import =
             vtkSmartPointer<vtkImageImport>::New();

     // copy into 1D Array
     std::cout << "Copying data " << std::endl;
     int num = Nxp * Nyp;
     TNT::Array1D<double> pm(num);
     std::cout << "Have now created the matrix" << std::endl;
     int counter = 0;
     for(int i = 0; i < Nxp; i++)
     {
         for(int j = 0; j < Nyp; j++)
         {
             pm[counter++] = M[i][j];

         }
     }
     std::cout << "Finishing copying data" << std::endl;

     import->SetImportVoidPointer((void*)pm);
     import->SetWholeExtent(0,Nxp,0,Nyp,0,0);
     import->SetDataExtent(0,Nxp,0,Nyp,0,0);

     //vtkSmartPointer<vtkImageData> data =
     //        vtkSmartPointer<vtkImageData>::New();
     //data->setDimensions( 1040, 1040, 0 );

     vtkSmartPointer<vtkLookupTable> lookupTable =
             vtkSmartPointer<vtkLookupTable>::New();

     vtkSmartPointer<vtkScalarBarActor> colorbar =
                 vtkSmartPointer<vtkScalarBarActor>::New();

     // add a colorbar
     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();

     //std::string bunny = "633px-Bunny.jpg";

     //read the image
     //vtkSmartPointer<vtkJPEGReader> jPEGReader =
     //        vtkSmartPointer<vtkJPEGReader>::New();
     //jPEGReader->SetFileName ( bunny.c_str() );
     //jPEGReader->Update();

     vtkSmartPointer<vtkImageViewer2> viewer =
              vtkSmartPointer<vtkImageViewer2>::New();
     viewer->SetInput(import->GetOutput());
     viewer->SetInput( import->GetOutput() );

     //setup renderer
     vtkSmartPointer<vtkRenderer> renderer =
           vtkSmartPointer<vtkRenderer>::New();
     renderer->AddActor ( viewer->GetImageActor() );
     renderer->AddActor(colorbar);
     renderer->ResetCamera();

     //setup render window
     vtkSmartPointer<vtkRenderWindow> renderWindow =
           vtkSmartPointer<vtkRenderWindow>::New();
     renderWindow->AddRenderer ( renderer );

     //setup render window interactor
     vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
           vtkSmartPointer<vtkRenderWindowInteractor>::New();
     vtkSmartPointer<vtkInteractorStyleImage> style =
           vtkSmartPointer<vtkInteractorStyleImage>::New();

     renderWindowInteractor->SetInteractorStyle( style );

     //render and start interaction
     renderWindowInteractor->SetRenderWindow ( renderWindow );
     renderWindowInteractor->Initialize();
     renderWindowInteractor->Start();


} // end function

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100814/bc21d31d/attachment.htm>


More information about the vtkusers mailing list