[vtkusers] Re-post: how to use own 3d array to create image

Wei Huang huangwei at ucar.edu
Wed Feb 10 13:57:42 EST 2010


Liam,

I tried:

...
     int i,j,k,n;
     int nx, ny, nz;
     float *fv;

     //fv = ..., where fv has size [nx*ny*nz].

     cout << "fv[0] = " << fv[0] << "\n";

     vtkSmartPointer<vtkImageData> imageData =  
vtkSmartPointer<vtkImageData>::New();

   //specify the size of the image data
     imageData->SetDimensions(nx,ny,nz);
     imageData->SetNumberOfScalarComponents(1);

   //fill every entry of the image data with "2.0"
     int* dims = imageData->GetDimensions();

     cout << "Dims: " << " x: " << dims[0] << " y: " << dims[1] << "  
z: " << dims[2] << endl;

     cout << "Number of points: " << imageData->GetNumberOfPoints() <<  
endl;
     cout << "Number of cells: " << imageData->GetNumberOfCells() <<  
endl;

     n = 0;
     for(k = 0; k < dims[2]; k++)
     {
         for(j = 0; j < dims[1]; j++)
         {
             for(i = 0; i < dims[0]; i++)
             {
                 imageData->SetScalarComponentFromDouble(i,j,k,0, 
(double) fv[n++]);
             }
         }
     }

.....

This worked for me.

Thanks a lot!

Wei Huang
huangwei at ucar.edu
VETS/CISL
National Center for Atmospheric Research
P.O. Box 3000 (1850 Table Mesa Dr.)
Boulder, CO 80307-3000 USA
(303) 497-8924





On Feb 10, 2010, at 6:56 AM, Liam Kurmos wrote:

> I forgot to reference in my last post your example:
>
> http://www.cmake.org/Wiki/VTK/Examples/IterateImageData
>
> which is the way i do it now.
>
> I was thinking there may be a method that lets you set a 3D array as
> scalars that might be faster for performance critical tasks but
> haven't needed to do that yet.
>
> Liam
>
>
> On Wed, Feb 10, 2010 at 12:39 PM, David Doria <daviddoria+vtk at gmail.com 
> > wrote:
>> On Wed, Feb 10, 2010 at 5:31 AM, Liam Kurmos <quantum.leaf at googlemail.com 
>> >
>> wrote:
>>>
>>> Hi,
>>>
>>> I'm still pretty new to vtk myself and not 100% sure if this is what
>>> you want but have a look at the code below. I think i adapted it  
>>> from
>>> the guide. If it's useful i could make an example.
>>>
>>>    int xDim, yDim,zDim;
>>>    xDim=yDim=zDim=26;
>>>    float z,y,x,kOffset,jOffset,offset;
>>>    vtkImageData *vol = vtkImageData::New();
>>>    vol->SetDimensions(xDim,yDim,zDim);
>>>    vol->SetOrigin(-1,-1,-1);
>>>
>>>    float sp=(float)1.0/(xDim-1);
>>>    vol->SetSpacing(sp,sp,sp);
>>>
>>>    vtkFloatArray *scalars=vtkFloatArray::New();
>>>    for (int k=0;k<zDim;k++){
>>>
>>>        z=-0.5+k*sp;
>>>        kOffset= k*xDim*yDim;
>>>        for(int j=0;j<yDim;j++){
>>>            y=-0.5+j*sp;
>>>            jOffset=j*xDim;
>>>            for (int i=0;i<xDim;i++){
>>>                x=-0.5+i*sp;
>>>                float s=x*x+y*y+z*z-(0.4*0.4);
>>>
>>>                offset=i+jOffset+kOffset;
>>>                scalars->InsertTuple1(offset,s);
>>>            }
>>>        }
>>>    }
>>>
>>>    vol->GetPointData()->SetScalars(scalars);
>>>
>>>    scalars->Delete();
>>>
>>>    vtkMarchingCubes *iso = vtkMarchingCubes::New();
>>>    iso->SetInput(vol);
>>>    iso->SetNumberOfContours(1);
>>>    iso->SetValue(0, 0);
>>>
>>> Liam
>>
>> I believe you could also do this ( can't try it right now)
>> double* volValue = static_cast<double*>vol->GetScalarPointer(i, j,  
>> k);
>> volValue = whatever you want pixel (x,y,z) to equal
>> Setting pixels in linear order always just seems to me like it is  
>> asking for
>> trouble :) This method (if it is legitimate) lets you set the  
>> pixels via
>> their position in the image.
>> Thanks,
>>
>> David
>>
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the VTK FAQ at:
>> http://www.vtk.org/Wiki/VTK_FAQ
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.vtk.org/mailman/listinfo/vtkusers
>>
>>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers




More information about the vtkusers mailing list