function vieweconfig
%   This software is protected by german copyright and international treaties.             
%   Copyright 2004 Markus Junghöfer & Peter Peyk. All Rights Reserved.                     
%                                                                                          
%   THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE        
%   NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO,    
%   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE    
%   OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
%   TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT,       
%   INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE      
%   OR DOCUMENTATION.                                                                      



%besafile einlesen
%------------------------
besafile='C:\Dokumente und Einstellungen\Peter\Desktop\ST65n.ELP';
if isempty(besafile)
	[besafilename,besafilepath]=uigetfile('*.elp*','Please choose besa-file:');
	besafile=[besafilepath,besafilename];
end
fid = fopen(besafile,'rt');
theta = [];
phi = [];
line = fgetl(fid);
linezaehler=1;

warning off
while line~=-1
warning on
%tabs
%-------
if ~isempty(findstr(line,char(9)))
    tabs=findstr(char(9),line);
    firstelement=line(1:tabs(1)-1);
    if ~strcmp(firstelement,'EEG') & ~strcmp(firstelement,'poly')
        phi(linezaehler,1)=str2num(firstelement);
        secondelement=line(tabs(1):tabs(2)-1);
        theta(linezaehler,1)=str2num(secondelement);
    else
        firstelement=line(tabs(1):tabs(2)-1);
        phi(linezaehler,1)=str2num(firstelement);
        secondelement=line(tabs(2):tabs(3)-1);
        theta(linezaehler,1)=str2num(secondelement);
    end  
    line=fgetl(fid);
    linezaehler = linezaehler +1;
%space
%--------
elseif ~isempty(findstr(line,char(32)))
    line=strrep(line,'  ',' ');
    line=strrep(line,'  ',' ');
    line=strrep(line,'  ',' ');
    line=strrep(line,'  ',' ');
    whitespace=findstr(char(32),line);
    firstelement=line(1:whitespace(1)-1);
    if ~strcmp(firstelement,'EEG') & ~strcmp(firstelement,'poly')
        phi(linezaehler,1)=str2num(firstelement);
        secondelement=line(whitespace(1):whitespace(2)-1);
        theta(linezaehler,1)=str2num(secondelement);
    else
        firstelement=line(whitespace(1):whitespace(2)-1);
        phi(linezaehler,1)=str2num(firstelement);
        secondelement=line(whitespace(2):whitespace(3)-1);
        theta(linezaehler,1)=str2num(secondelement);
    end  
    line=fgetl(fid);
    linezaehler = linezaehler +1;
end
end
fclose(fid);


%grad-pi-umwandlung
%-----------------
if ~isempty(find(theta>2*pi))
    phi=phi./360*2*pi;
    theta=theta./360*2*pi;
end

%positive Winkel
%------------------
phi(find(phi<0))=phi(find(phi<0))+2*pi;
phi(find(phi>(2*pi)))=phi(find(phi>(2*pi)))-2*pi;
theta(find(theta<0))=theta(find(theta<0))+2*pi;
theta(find(theta>(2*pi)))=theta(find(theta>(2*pi)))-2*pi;




%----------------------------------------------------------------------------------
%View sensor configuration
%---------------------------------------------------------------------------------- 


figure('Name','Sensor-configuration:');

%drehen des sphere-coordinaten-systems;
%-----------------------
theta=theta-pi;
phi=phi+pi/2;

%positive Winkel
%------------------
phi(find(phi<0))=phi(find(phi<0))+2*pi;
phi(find(phi>(2*pi)))=phi(find(phi>(2*pi)))-2*pi;
theta(find(theta<0))=theta(find(theta<0))+2*pi;
theta(find(theta>(2*pi)))=theta(find(theta>(2*pi)))-2*pi;

radians=ones(1,length(theta))*0.0920;
radians=radians';
[x,y,z]=sph2cart(theta,phi,radians);



%electrodes
%---------------
electrodes=plot3(x,y,z,'o','markerfacecolor',[0 0 0]);

%rotation damits Sinn macht... (keine Ahnung wie das Achenkreuz nu in Besa oder Plot2d eigentlich liegt...)
%---------
%x-Richtung
rotate(electrodes,[1 0 0],90);
%y-Richtung
rotate(electrodes,[0 1 0],90);
%z-Richtung
rotate(electrodes,[0 0 1],90);
%x-Richtung
rotate(electrodes,[1 0 0],90);

x=get(electrodes,'XData');
y=get(electrodes,'YData');
z=get(electrodes,'ZData');
electrodes=plot3(x,y,z,'o','markerfacecolor',[0 0 0]);

%Rückwandeln in spherische Coordinates
%-------------
[theta,phi,radians]=cart2sph(x,y,z);


%positive Winkel
%------------------
phi(find(phi<0))=phi(find(phi<0))+2*pi;
phi(find(phi>(2*pi)))=phi(find(phi>(2*pi)))-2*pi;
theta(find(theta<0))=theta(find(theta<0))+2*pi;
theta(find(theta>(2*pi)))=theta(find(theta>(2*pi)))-2*pi;


%Strahlen
%-----------
zerovec=zeros(size(x))';
hold on
for j=1:length(x)
    plot3([zerovec(j),x(j)],[zerovec(j),y(j)],[zerovec(j),z(j)],'color',[0 0 0]);
end

%Elektrodennamen
%------------------
ENames=num2str(linspace(1,length(theta),length(theta))');
text(x+x*0.1,y+y*0.1,z+z*0.1,ENames);

%Bereich vergrößern
xlim=get(gca,'xlim');
ylim=get(gca,'ylim');
zlim=get(gca,'zlim');
nullmat=[0 0];
xlim=xlim*1.5;
ylim=ylim*1.5;
zlim=zlim*1.5;
set(gca,'xlim',xlim);
set(gca,'ylim',ylim);
set(gca,'zlim',zlim);
%Achsen
xachse=plot3(xlim,nullmat,nullmat,'color',[0 0 0],'linewidth',2);
yachse=plot3(nullmat,ylim,nullmat,'color',[0 0 0],'linewidth',2);
zachse=plot3(nullmat,nullmat,zlim,'color',[0 0 0],'linewidth',2);
%Achsentitel
text(max(xlim)+max(xlim)*0.3,  0+max(ylim)*0.1,0+max(zlim)*0.1,'X(top)','color',[0 0 0],'fontsize',14);
text(0+max(xlim)*0.1,min(ylim)-max(ylim)*0.3,0+max(zlim)*0.1,'Y(left)','color',[0 0 0],'fontsize',14);
text(0+max(xlim)*0.1,0+max(ylim)*0.1,min(zlim)-max(zlim)*0.3,'Z(back)','color',[0 0 0],'fontsize',14);


%Pfeile der Achsen
%-----------------
%X-Achse
%---------
c1scaling=0.5;
c2scaling=0.5;
c3scaling=0.02;
%Pfeil
[c1,c2,c3] = cylinder(linspace(0,max(xlim)/10,20),20);
c1=c1-ones(size(c1))*mean(mean(c1));
c1=c1*c1scaling;
c2=c2-ones(size(c2))*mean(mean(c2));
c2=c2*c2scaling;
c3=c3-ones(size(c3))*mean(mean(c3));
c3=c3*c3scaling;
cylinderhandle=surf(c1,c2,c3);
%Drehen in Achsenrichtung
rotate(cylinderhandle,[0 1 0],270);
c1=get(cylinderhandle,'XData');
c2=get(cylinderhandle,'YData');
c3=get(cylinderhandle,'ZData');
%Verschiebung in y-Richtung
c1=c1+ones(size(c1))*max(xlim);
cylinderhandle=surf(c1,c2,c3);
%Y-Achse
%---------
c1scaling=0.5;
c2scaling=0.5;
c3scaling=0.02;
%Pfeil
[c1,c2,c3] = cylinder(linspace(0,max(xlim)/10,20),20);
c1=c1-ones(size(c1))*mean(mean(c1));
c1=c1*c1scaling;
c2=c2-ones(size(c2))*mean(mean(c2));
c2=c2*c2scaling;
c3=c3-ones(size(c3))*mean(mean(c3));
c3=c3*c3scaling;
cylinderhandle=surf(c1,c2,c3);
%Drehen in Achsenrichtung
rotate(cylinderhandle,[1 0 0],270);
c1=get(cylinderhandle,'XData');
c2=get(cylinderhandle,'YData');
c3=get(cylinderhandle,'ZData');
%Verschiebung in y-Richtung
c2=c2-ones(size(c2))*max(ylim);
cylinderhandle=surf(c1,c2,c3);
%Z-Achse
%----------
c1scaling=0.5;
c2scaling=0.5;
c3scaling=0.02;
%Pfeil
[c1,c2,c3] = cylinder(linspace(0,max(xlim)/10,20),20);
c1=c1-ones(size(c1))*mean(mean(c1));
c1=c1*c1scaling;
c2=c2-ones(size(c2))*mean(mean(c2));
c2=c2*c2scaling;
c3=c3-ones(size(c3))*mean(mean(c3));
c3=c3*c3scaling;
cylinderhandle=surf(c1,c2,c3);
%Drehen in Achsenrichtung
rotate(cylinderhandle,[0 1 0],0);
c1=get(cylinderhandle,'XData');
c2=get(cylinderhandle,'YData');
c3=get(cylinderhandle,'ZData');
%Verschiebung in z-Richtung
c3=c3-ones(size(c3))*max(zlim);
cylinderhandle=surf(c1,c2,c3);



%Kugel einfügen
%----------------------
[a,b,c]=sphere(20);
scaling=0.085;
a=a*scaling;
b=b*scaling;
c=c*scaling;
axis equal;
colormap(gray);
surfl(a,b,c);
shading interp;


% set(gca,'CameraPosition',[0.0050    0.0050    2.6897]);
% set(gca,'CameraTarget',[0.0050    0.0050    0.0050]);
% set(gca,'CameraUpvector', [0.9998   -0.0175         0]);


%Ansicht von schräg oben
%----------------------------
totalfigure=get(gca,'children');
rotate(totalfigure,[0 1 0],270);
rotate(totalfigure,[0 0 1],0);
hold off

EPosSpher=[theta' phi'];
FileMask='*.ecfg';
ENames=linspace(1,65,65);
[FileName,FilePath]=uiputfile('*.ecfg','Bitte benennen sie das Elektroden-Config-file:');
if isempty(findstr(FileName,'.ecfg'))
    FileName=[FileName,'.ecfg'];
end
FilePath=[FilePath,FileName];

SaveEConfig_pp(EPosSpher,ENames,FileMask,FilePath)



return