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