function toVtk(image,spacing,z,filename)
%this function saves the processed volume into a txt file that can be 
%visualized with the VTK

%image . . . sorted uniform volume set as a 3D matrix 
%spacing . . .the voxel size
%z . . . the y/coordinate of the origin
%the text file name and the path to it is defined by the user as the variable filename% 

[nx, ny, nz] = size(image);                                                                                                                                                                                                                 
name = fopen(strcat('~/Plocha/python/vtk/bc/SMTK1VMS/',filename),'wt');
fprintf(name, '# vtk DataFile Version 2.0\n');
    
fprintf(name, 'One structure to be visualized with VTK\n');
fprintf(name, 'ASCII\n');
fprintf(name, '\n');
fprintf(name, 'DATASET STRUCTURED_POINTS\n');
fprintf(name, 'DIMENSIONS %d %d %d\n',nx,ny,nz);
fprintf(name, 'ORIGIN 0.000 0.000 %5.3f\n',z);
fprintf(name, 'SPACING %5.3f %5.3f %5.3f\n',spacing(1),spacing(2),spacing(3));

 fprintf(name, 'POINT_DATA   %d\n', nx*ny*nz);
 fprintf(name, 'SCALARS my_scalars unsigned_char 1\n');
 fprintf(name, 'LOOKUP_TABLE default\n');
 fprintf(name, '\n');
    for c=1:nz
        for b=1:ny
            for a=1:nx
                fprintf(name, '%3u ', image(a,b,c));   
            end
            fprintf(name, '\n');                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        end
    end
    
    fclose(name);
return
