Visualize, Visualize
August 16, 2007
As a researcher in multi-dimensional data processing, one inevitably encounters the problem of data visualization. It is no problem to visualize 1D or 2D data nowadays… After some searching, I realized it isn’t too big a deal to visualize your 3d data either!
Here’s how.
I have abandoned Matlab some time ago, to embrace free software such as Scilab, for scientific calculations. Compared to Matlab, though, Scilab has a little more problems with visualization. Some early endeavors of mine included fighting with Scilab’s 2D plotting functions and using the excellent ENRICO toolbox for 3d data. But now, I’ve discovered VTK…
Well, VTK (visualization toolkit) is way too complicated for me to use. Rather, I’m using front ends based on it: MayaVi and VisIt. The first step is to get data. Then, one needs to covert the data into the vtk format and that’s it, you can plunge into the third dimension! The description of the vtk format is here. I guess the best way is an example.
First, generate some sample data. Scilab code:
********************begin code*********************
clear
m=16;n=16;p=16;
cj=sqrt(-1);
x=linspace(-1,1,m);
y=linspace(-1,1,n);
z=linspace(-1,1,p);
s=hypermat([m,n,p]);
kx=0;ky=10;kz=-10;
for i=1:p,
s(:,:,i)=exp(-cj*ky*y’)*exp(-cj*kx*x)*exp(-cj*kz*z(i));
end
kx=0;ky=-10;kz=-10;
for i=1:p,
s(:,:,i)=s(:,:,i)+exp(-cj*ky*y’)*exp(-cj*kx*x)*exp(-cj*kz*z(i));
end
kx=0;ky=0;kz=10;
for i=1:p,
s(:,:,i)=s(:,:,i)+exp(-cj*ky*y’)*exp(-cj*kx*x)*exp(-cj*kz*z(i));
end
kx=0;ky=0;kz=0;
for i=1:p,
s(:,:,i)=s(:,:,i)+exp(-cj*ky*y’)*exp(-cj*kx*x)*exp(-cj*kz*z(i));
end
kx=-10;ky=10;kz=0;
for i=1:p,
s(:,:,i)=s(:,:,i)+.5*exp(-cj*ky*y’)*exp(-cj*kx*x)*exp(-cj*kz*z(i));
end
************************end code**********************************
This is a set of three-dimensional harmonic functions. To obtain focused points in space, perform the 3D Fourier transform:
*********************begin code***************************
pad=64;
S=hypermat([pad,pad,pad]);
for i=1:m,
for j=1:n,
S(i,j,:)=fftshift(fft(fftshift([matrix(s(i,j,:),1,p),zeros(1,pad-p)])));
end
end
for i=1:pad,
S(:,:,i)=fftshift(fft2(fftshift(S(:,:,i))));
end
******************end code********************************
…and save data in ASCII format.
*********************begin code*****************************
S1=zeros(pad,pad);
for r=1:pad,
S1=[S1;abs(S(:,:,r))];
end
S1=S1(pad+1:$,:);
savematfile(‘data.mat’,'-ascii’,'S1′);
**********************end code*************************
To create a .vtk file, you need a header. For our case, it would look like this:
******************begin header******************
# vtk DataFile Version 2.0
3D harmonic visualization
ASCII
DATASET STRUCTURED_POINTS
DIMENSIONS 64 64 64
ASPECT_RATIO 1 1 1
ORIGIN 0 0 0
POINT_DATA 262144
SCALARS 3D_harmonic double
LOOKUP_TABLE default
*******************end header******************
You could naturally place the header directly in front of the ascii data file, but this isn’t too handy for big files. You can simply join them together using a DOS command:
copy header.txt+data.mat data.vtk
That’s it! Don’t forget the CR character at the end of header (simple enter to create a new line), otherwise the data comes directly after the header text. (Although I didn’t try what that might do, I suppose it could be a problem.)
Then, get VisIt (it’s free) and VISUALIZE!