[vtkusers] Problems about vtkImageData
林立凡
kigihanko at sina.com
Sat Aug 8 06:50:23 EDT 2009
Hey guys,I have got a trouble problem,hope that you can help me
Recently my work has related to displaying a raw data(512x512x331,short array),I am succeed in doing that by using vtkImageData,my code is as follows:
(platform : vs 2008.net language:C# )
private void volumeRendering(String filePath, int[] dims, int []shrinkFactor,vtkRenderWindow renWin)
{
//Read dat data
FileStream fs = null;
byte[] point = new byte[2];
if (!File.Exists(filePath))
{
MessageBox.Show("Raw Data doesn't exist!");
return;
}
fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
int i, j, k;
short[,,]rawData = new short[dims[0], dims[1], dims[2]];
for (i = 0; i < dims[0]; i++)
{
for (j = 0; j < dims[1]; j++)
{
for (k = 0; k < dims[2]; k++)
{
point[0] = (byte)fs.ReadByte();
point[1] = (byte)fs.ReadByte();
rawData[i, j, k] = BitConverter.ToInt16(point, 0);
}
}
}
//Volume Rendeing
vtkImageData id = new vtkImageData();
id.SetDimensions(dims[0], dims[1], dims[2]);
id.SetScalarTypeToShort();
id.SetNumberOfScalarComponents(1);
id.AllocateScalars();
vtkImageShrink3D mask = new vtkImageShrink3D();
mask.SetInput(id);
mask.SetShrinkFactors(shrinkFactor[0], shrinkFactor[1], shrinkFactor[2]);
unsafe
{
short* a = (short*)(id.GetScalarPointer().ToPointer());
for (i = 0; i < dims[0]; i++)
{
for (j = 0; j < dims[1]; j++)
{
for (k = 0; k < dims[2]; k++)
{
*a++ = rawData[i, j, k];
}
}
}
}
vtkContourFilter cf = new vtkContourFilter();
cf.SetInputConnection(mask.GetOutputPort());
vtkPolyDataMapper mapper = new vtkPolyDataMapper();
mapper.SetInputConnection(cf.GetOutputPort());
mapper.ScalarVisibilityOff();
mapper.SetScalarRange(-2048, 2047);
actor = new vtkLODActor();
actor.SetMapper(mapper);
.....
as you see, I simply read the data from a .dat file,and associate it with the vtkImageData by the code written in the unsafe period.
However,what we are intergrating the system,my classmate just pass a array(which is 3D reconstructed data) to me(He didn't read it from file).And I got nothing displayed!I feel puzzled,because I pass the data from .dat file to an array ,and then pass this array to the function I write,it indeed could display!And it seems that a random array could,'t be displayed!
Do anybody know this?This is the first part of my trouble
So I modified my strategy,I randomly produce the data by simulating the .dat file I have mentioned above.And use another version of code:
vtkShortArray arr = new vtkShortArray();
arr.Allocate(dims[0] * dims[1] * dims[2], 1);
arr.SetArray(array, dims[0] * dims[1] * dims[2], 1);
//Volume Rendeing
vtkImageData id = new vtkImageData();
id.GetPointData().SetScalars(arr);
id.SetDimensions(dims[0], dims[1], dims[2]);
id.SetScalarTypeToShort();
id.SetNumberOfScalarComponents(3);
id.AllocateScalars();
And then I got another problem,if the data is too large(My is 512x512x331),then I got the exception:
ERROR:In m:\dev\cur\vtkdotnet\branch\50\common\vtkDataArrayTemplate\vtkFloatArray[06CDAE30]:unable to allocate 21577254
elements of size 4
But 50x50x50 could be displayed!
I don't why,I have encountered this kind of error lots of time,could anyone tell me something about this?
Thank you very much!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20090808/90aafb28/attachment.htm>
More information about the vtkusers
mailing list