ntheta = 100;
nphi   = 100;
nentries = ntheta*nphi;

fid = fopen('sph.dat', 'r');
dat = fscanf(fid, '%g', [3, nentries]);
fclose(fid);

disp(nentries);

theta  = dat(1,1:nentries);
phi    = dat(2,1:nentries);
radius = dat(3,1:nentries);

tri = delaunay(theta, phi);
% triplot(tri, theta, phi);

disp(size(tri));
s = size(tri);
disp(s(1));
x = radius.*(sin(phi).*cos(theta));
y = radius.*(sin(phi).*sin(theta));
z = radius.*cos(phi);

fid = fopen('sph.obj', 'w');
for i=1:nentries
    fprintf(fid, 'v %12.3f %12.3f %12.3f\n', x(i), y(i), z(i));
end
s = size(tri);
for i=1:s(1)
    fprintf(fid, 'f %7d %7d %7d\n', tri(i,2), tri(i,1), tri(i,3));
end
fclose(fid);


