<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>&gt; I am wondering if there is a way in VTK to render the elements of a 2D<br>&gt; array in a similar fashion to the Matlab imagesc() function.  Suppose<br>&gt; that I have a 1000-by-1000 array M of values.  In Matlab, calling<br>
&gt; imagesc(M) will display a color-mapped image with the colors scaled to<br>&gt; the maximum and minimum numbers in the image.  Using the following<br>&gt; Matlab syntax will plot the 2D array with a colorbar:<br>&gt;<br>
&gt; imagesc(M);<br>&gt; colorbar;<br>&gt;<br>&gt; Is there a similar way to do this in VTK?  (I am currently working<br>&gt; with C++.)  In the past, I tried using vtkImageViewer2, but I remember<br>&gt; that I was not successful.  What would be an example pipeline (from<br>
&gt; start to finish) that would allow me to plot a similar image in VTK?<br>&gt;<br>&gt; Since I am writing a Finite-Difference Time-Domain (FDTD) simulation,<br>&gt; I would like to progressively update the plot at each timestep so that<br>
&gt; the user of the program can view an animation of how the simulation<br>&gt; progresses.  How would I dynamically update the plot at each timestep?<br>Well, I&#39;ve been working on displaying the 2D data as an image.<br>
<br>I&#39;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 &quot;garbage data&quot; 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 = &quot;M.txt&quot;;<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&lt;double&gt; M(Nxp, Nyp);<br>    double **pm;<br>    load_matrix( fileName, &amp;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&lt;vtkImageImport&gt; import =<br>
            vtkSmartPointer&lt;vtkImageImport&gt;::New();<br><br>    import-&gt;SetImportVoidPointer((void*)pm);<br>    import-&gt;SetWholeExtent(0,Nxp,0,Nyp,0,0);<br>    import-&gt;SetDataExtent(0,Nxp,0,Nyp,0,0);<br><br>
    vtkSmartPointer&lt;vtkLookupTable&gt; lookupTable =<br>            vtkSmartPointer&lt;vtkLookupTable&gt;::New();<br><br>    vtkSmartPointer&lt;vtkScalarBarActor&gt; colorbar =<br>                vtkSmartPointer&lt;vtkScalarBarActor&gt;::New();<br>
<br>    lookupTable-&gt;SetNumberOfColors(9344);<br>    lookupTable-&gt;SetTableRange(0,10);<br>    lookupTable-&gt;ForceBuild();<br><br>    colorbar-&gt;SetLookupTable(lookupTable);<br>    colorbar-&gt;SetWidth(0.05);<br>
    colorbar-&gt;SetPosition(0.95, 0.1);<br>    colorbar-&gt;SetLabelFormat(&quot;%.3g&quot;);<br>    colorbar-&gt;PickableOff();<br>    colorbar-&gt;VisibilityOn();<br><br>    vtkSmartPointer&lt;vtkImageViewer2&gt; viewer =<br>
             vtkSmartPointer&lt;vtkImageViewer2&gt;::New();<br>    viewer-&gt;SetInput(import-&gt;GetOutput());<br>    viewer-&gt;SetInput( import-&gt;GetOutput() );<br><br>    vtkSmartPointer&lt;vtkRenderer&gt; renderer =<br>
          vtkSmartPointer&lt;vtkRenderer&gt;::New();<br>    renderer-&gt;AddActor ( viewer-&gt;GetImageActor() );<br>    renderer-&gt;AddActor(colorbar);<br>    renderer-&gt;ResetCamera();<br><br>    vtkSmartPointer&lt;vtkRenderWindow&gt; renderWindow =<br>
          vtkSmartPointer&lt;vtkRenderWindow&gt;::New();<br>    renderWindow-&gt;AddRenderer ( renderer );<br><br>    vtkSmartPointer&lt;vtkRenderWindowInteractor&gt; renderWindowInteractor =<br>          vtkSmartPointer&lt;vtkRenderWindowInteractor&gt;::New();<br>
    vtkSmartPointer&lt;vtkInteractorStyleImage&gt; style =<br>          vtkSmartPointer&lt;vtkInteractorStyleImage&gt;::New();<br>    renderWindowInteractor-&gt;SetInteractorStyle( style );<br><br>    renderWindowInteractor-&gt;SetRenderWindow ( renderWindow );<br>
    renderWindowInteractor-&gt;Initialize();<br>    renderWindowInteractor-&gt;Start();<br><br>} // end function<br><br>// END CODE<br></span></div>