% % %% 3D Isosurfaces in Matlab:
% % % Hér bý ég til gögnin
% % 
% clear all;
% x = linspace(-10,10,50); 
% y = linspace(-10,10,60); 
% z = linspace(-10,10,70);
% 
% % Creating a paraboloid function
% a=3; b=1; c=2; V0 = 3;
% funV = @(x,y,z) a*x.^2+b*y.^2+c*z.^2;
% 
% % Generate (very regular) volumetric data:
% [X,Y,Z] = meshgrid(x,y,z);
% F = funV(X,Y,Z);

clear all
% load -ascii X0_cont.txt
% load -ascii X1_cont.txt
% load -ascii X2_cont.txt
% [X,Y,Z] = meshgrid(X0_cont,X1_cont,X2_cont);
% load -ascii V_cont.txt
% nx=length(X0_cont);
% ny=length(X1_cont);
% nz=length(X2_cont);

Name = 'LyaFunc5m';
NameExc = 'ExcSimpP';
hold on
A=dlmread([NameExc '.txt']);
plot3(A(:,1),A(:,2),A(:,3),'.')


X0=dlmread(['X0_' Name '.txt']);
X1=dlmread(['X1_' Name '.txt']);
X2=dlmread(['X2_' Name '.txt']);
V=dlmread([['V_' Name '.txt']]);
% X0=fscanf(fileID,formatSpec);
% fclose(fileID);
% fileID = fopen(['X1_' Name '.txt'],'r');
% X1=fscanf(fileID,formatSpec);
% fclose(fileID);
% fileID = fopen(['X2_' Name '.txt'],'r');
% X2=fscanf(fileID,formatSpec);
% fclose(fileID);
% fileID = fopen(['V_' Name '.txt'],'r');
% V=fscanf(fileID,formatSpec);
% fclose(fileID);
[X,Y,Z] = meshgrid(X0,X1,X2);
nx=length(X0);
ny=length(X1);
nz=length(X2);

k=1;
for iz=1:nz
    for iy=1:ny
        for ix=1:nx
            F(iy,ix,iz)=V(k);
            k=k+1;
        end
    end
end

    
%% 3D Isosurfaces in Matlab: Plotting Commands:
% Let MATLAB interpolate the data to create the V = V0 surface:

V0=0.9;
patchObjectV0 = isosurface(X,Y,Z,F,V0);
% Líka hægt að láta MATLAB skila (Simplices,Vertices) gögnum bara:
%[Simplices,Vertices] = isosurface(X,Y,Z,F,V0); 

% Teiknum mynd af isosurface-inu:
patchHandle = patch(patchObjectV0);
reducepatch(patchHandle, .1)
%patchHandle.FaceColor = 'red'
%patchHandle.EdgeColor = 'none'
% Þessi skipun reiknar út normal á hvern 
% simplex og setur þau gögn inn í patch-object-ið
% (einfaldar útreikninga fyrir lýsingu og gerir myndina flottari)
%isonormals(X,Y,Z,F,patchHandle)

% % The hpatch is a handle to a patch-object:
 niceBlue = [0.7294 0.8314 0.9569];
 %set(patchHandle,'FaceColor',niceBlue,'EdgeColor','black')
 set(patchHandle,'FaceColor','red','EdgeColor','none')

 %plot3(A(:,1),A(:,2),A(:,3),'*')
% 
 daspect([1,4,4])
 view([-65,20])
 axis tight
 camlight left; 
 lighting gouraud %phong %
 axis equal
 xlabel('X')
 ylabel('Y')
 zlabel('Z')

% 
% %% Dæmi um að nota sjötta argúmentið. 
% % Liturinn á isosurface-inu verður eins og það gildi... væri
% % sniðugt að prófa setja þar inn magnitude af orbital afleiðunni
% dataW = Z;
% [faces,verts,colors] = isosurface(X,Y,Z,F,V0,dataW); 
% patch('Vertices', verts, 'Faces', faces, ... 
%     'FaceVertexCData', colors, ... 
%     'FaceColor','interp', ... 
%     'edgecolor', 'interp');
% view(30,-15);
% axis vis3d;
% colormap copper
% 
% %annað útlit og view:
% view(183, 1);
% colormap hot

%colormap bone
%colormap pink
