[vtkusers] Re-post: how to use own 3d array to create image
David Doria
daviddoria+vtk at gmail.com
Wed Feb 10 07:39:18 EST 2010
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100210/35887bda/attachment.htm>
More information about the vtkusers
mailing list