function buttonafsRAY(hObject, eventdata, parentfig, Empty_Button, Buttons, Footnote, Polysettings, AxesAFS, AxesSOL, SegPlot, SegTrafo, rotoff, IPonoff, FPonoff, lcs, usmn, usmf, lvText, lvSlider)
set(usmn,'Value',0);
set(usmf,'Value',1);

for i=1:3
    set(lvText(2*i),'String','0');
    set(lvSlider(i),'Value',0);
end
    
plot(AxesSOL,1,0,'w');
data = get(Empty_Button,'UserData');
if (isfield(data,'ismatrix')*isfield(data,'isnmf') == 0) || (data.ismatrix*data.isnmf == 0)
    if data.ismatrix == 0 set(Footnote,'String',' Warning, no data has been loaded.','ForegroundColor','red');return;end
    if data.isnmf == 0 set(Footnote,'String',' Warning, no NMF has been computed.','ForegroundColor','red');return;end
end

if data.islv == 1
    Mouse3D_modifiedRAY(5,hObject, eventdata, parentfig, Empty_Button, Buttons, AxesAFS, AxesSOL, SegPlot, rotoff, IPonoff, FPonoff, lvText, lvSlider, usmn, usmf, lcs);
end
data.islv = 0;
data.lvactx = [0 0 0];

set(SegPlot, 'String', 'plot complete AFS', 'Value', 1)
set(SegTrafo, 'String', 'select segment', 'Value', 1)

data.isExpert1 = 0;
data.isExpert2 = 0;
data.isLastClickLockMode = 0;
data.isimport = 0;
data.import = [];
onoffButton(Buttons,[], [8 11]);

data.isplane = 0;
data.issliced = 0;
data.isRib = [0 0 0];

data.plotIP = 0;
data.plotFP = 0;

set(rotoff, 'Value', 1);
set(IPonoff, 'Value', 1);
set(FPonoff, 'Value', 1);

epsRand = getsetvar(Polysettings(2), 1.E-8, 1.E-1, 0, 1);
MaxLang = getsetvar(Polysettings(3), 3, 50000, 1, 0);
RefLevel = getsetvar(Polysettings(4), 0, 30, 1, 0);
MaxFunC = getsetvar(Polysettings(5), 30, 200000, 1, 0);
h0 = 1-0.01*getsetvar(Polysettings(6), 0.5, 50, 0, 0);
epsFun = 1.e-10;

mimi = max(1.E-2, 2*data.miminmf);
noiseC = -getsetvar(Polysettings(1), data.miminmf, mimi, 0, 0);
noiseA = noiseC;

plot(AxesAFS,0,0,'w');
plot(AxesSOL,1,0,'w');

onoffButton(Buttons,[],[8:11]);

singv = diag(data.S);
if singv(2)/singv(1)-1 > -1.E-15
    str = ['No NMF computed: matrix D^TD reducible, ',char(963),'(1)/',char(963),'(2)-1 = ', num2str(singv(2)/singv(1)-1,'%3.3e'),'.'];
    set(Footnote,'String',str,'ForegroundColor','red');
    
    return;
end
posSV = [];
for i=2:data.s
    mi = min(data.V(:,i));
    if (mi > 0) || (-mi < 1.E-16*max(data.V(:,i)))
        posSV = [posSV, i];
    end
end

if isempty(posSV) == 0
    str = 'Warning: matrix D^TD reducable, positiv singular vectors: 1';
    for i=1:length(posSV)
        str = [str, ', ', num2str(posSV(i))];
    end
    set(Footnote,'String',[str, '.'],'ForegroundColor','red');
    return;
end

set(Footnote,'String',' C routine in progress ... (press CTRL + C in the command window to cancel) ... see command window for progress','ForegroundColor','black');
fprintf(' C routine (AFS) in progress ... (press CTRL + C to cancel) ... \n');
set(hObject,'Enable','off');

[OUT2, INN, OUT, InitPoints, TinitStruc, lines, friends, tim, GenInfo] = computeafsRAY(hObject, MaxLang, RefLevel, MaxFunC, epsFun, epsRand, noiseC, noiseA, h0, data.s, data.U, data.S, data.V, data.InitT, data.calcp);

if GenInfo == -3
    fprintf('computation canceled.\n');
    set(Footnote,'String','AFS computation canceled.','ForegroundColor','red');
    return;
end

fprintf('computing time: %2.2f s\n',tim);

if data.s == 3 && GenInfo(1,1) == -2
    set(Footnote,'String',['Warning: Maximal number of funcalls (',num2str(calls) ,') reached.'],'ForegroundColor','red');
    return;
end

if(data.s == 2 && GenInfo(1,1) <= 0)
    set(Footnote,'String',['Warning: AFS not computed.'],'ForegroundColor','red');
    return;
end

data.OUT2 = OUT2;
data.INN = INN;
data.OUT = OUT;
data.friends = friends;
data.InitPoints = InitPoints;
data.afs.TinitStruc = TinitStruc;
data.afs.lines = lines;
data.GenInfo = GenInfo;
data.isafs = 1;

tt = data.D*data.V;
for i=1:size(data.D,1)
    data.INNPOL.t(i,1:3) = tt(i,2:4)/tt(i,1);
end
data.INNPOL.surf = convhulln(data.INNPOL.t(:,1:3));%, 'simplify', true);%, data.INNPOL.t(:,3));%, 'simplify', true);

% if size(data.D,2) < 250
%     M = nchoosek(1:size(data.D,2),3);
% else
%     M = nchoosek(round(linspace(1,size(data.D,2),250)),3);
% end
% ni = 0;
% for i=1:size(M,1)
%     in = M(i,:);
%     al = -data.V(in,2:4)\data.V(in,1);
%     if min(min([1 al']*data.V')) > -1.E-15
%         data.FIRPOL.t(ni+1,:) = al';
%         ni = ni+1;
%     end
% end
% % data.FIRPOL.surf = convhulln(data.FIRPOL.t(:,1:3));%, 'simplify', true);
% 
% data.xm = [min(min(data.OUT(:,1)), min(data.FIRPOL.t(:,1))) max(max(data.OUT(:,1)), max(data.FIRPOL.t(:,1)))];
% data.ym = [min(min(data.OUT(:,2)), min(data.FIRPOL.t(:,2))) max(max(data.OUT(:,2)), max(data.FIRPOL.t(:,2)))];
% data.zm = [min(min(data.OUT(:,3)), min(data.FIRPOL.t(:,3))) max(max(data.OUT(:,3)), max(data.FIRPOL.t(:,3)))];

data.xm = [min(data.OUT(:,1)) max(data.OUT(:,1))];
data.ym = [min(data.OUT(:,2)) max(data.OUT(:,2))];
data.zm = [min(data.OUT(:,3)) max(data.OUT(:,3))];

data.plane.m2 = round(sqrt(size(OUT,1)/2));
data.plane.m1 = 2*data.plane.m2;
data.plane.POINTS = [OUT2;INN];
data.plane.nn = size(OUT2,1);

data.afs.KAN    = [data.plane.nn+1 data.plane.nn+2];
data.afs.KANOUT = [];
data.afs.KANINN = [];

for i=0:data.plane.m1-1
    n1 = i*data.plane.m2+1:(i+1)*data.plane.m2;
    if i < data.plane.m1/2
        n1(end+1) = (i+data.plane.m1/2)*data.plane.m2+data.plane.m2;
    end
    
    data.afs.KAN    = [data.afs.KAN; n1(1:end-1)' n1(2:end)'; data.plane.nn+n1(1:end-1)' data.plane.nn+n1(2:end)'];
    data.afs.KANOUT = [data.afs.KANOUT; n1(1:end-1)' n1(2:end)'];
    data.afs.KANINN = [data.afs.KANINN; data.plane.nn+n1(1:end-1)' data.plane.nn+n1(2:end)'];
    
    if i < data.plane.m1-1
        N1 = i*data.plane.m2+1:(i+1)*data.plane.m2;
        N2 = (i+1)*data.plane.m2+1:(i+2)*data.plane.m2;
        n1 = [];
        for j=1:data.plane.m2
            n1 = [n1 N1(j) N2(j)];
        end
        
        data.afs.KAN    = [data.afs.KAN; n1(1:end-1)' n1(2:end)'; data.plane.nn+n1(1:end-1)' data.plane.nn+n1(2:end)'];
        data.afs.KANOUT = [data.afs.KANOUT; n1(1:end-1)' n1(2:end)'];
        data.afs.KANINN = [data.afs.KANINN; data.plane.nn+n1(1:end-1)' data.plane.nn+n1(2:end)'];
    else
        N1 = i*data.plane.m2+1:(i+1)*data.plane.m2;
        N2 = 1:data.plane.m2;
        n1 = [];
        for j=1:data.plane.m2
            n1 = [n1 N1(j) N2(j)];
        end
        
        data.afs.KAN    = [data.afs.KAN; n1(1:end-1)' n1(2:end)'; data.plane.nn+n1(1:end-1)' data.plane.nn+n1(2:end)'];
        data.afs.KANOUT = [data.afs.KANOUT; n1(1:end-1)' n1(2:end)'];
        data.afs.KANINN = [data.afs.KANINN; data.plane.nn+n1(1:end-1)' data.plane.nn+n1(2:end)'];
    end
    for j=1:data.plane.m2
        if isfinite(INN(i*data.plane.m2+j,1)) && ((i<data.plane.m1-1 && ~isfinite(INN((i+1)*data.plane.m2+j,1))) || (i>0 && ~isfinite(INN((i-1)*data.plane.m2+j,1))) || (j<data.plane.m2 && ~isfinite(INN(i*data.plane.m2+j+1,1))) || (j>1 && ~isfinite(INN(i*data.plane.m2+j-1,1))))
            
            data.afs.KAN    = [data.afs.KAN; i*data.plane.m2+j data.plane.nn+i*data.plane.m2+j];
            data.afs.KANINN = [data.afs.KANINN; i*data.plane.m2+j data.plane.nn+i*data.plane.m2+j];
        end
    end
end

data.afs.all = [OUT2; INN; InitPoints.points(:,1:3); InitPoints.points(:,4:6)];
data.afs.allfriends = [friends(1:data.plane.m1*data.plane.m2,1:9); friends(1:data.plane.m1*data.plane.m2,10:18); ...
    friends(1+data.plane.m1*data.plane.m2:4+data.plane.m1*data.plane.m2,1:9); friends(1+data.plane.m1*data.plane.m2:4+data.plane.m1*data.plane.m2,10:18)]; 

posTinit = size(OUT2,1)*2+[1:4]';

m2 = round(sqrt(size(OUT,1)/2));
m1 = 2*m2;

data.raybase.m2 = m2;
data.raybase.m1 = m1;

for i=1:4
    icf = [floor(data.InitPoints.i1i2(i,1)) mod(ceil(data.InitPoints.i1i2(i,1)),m1)];
    ICF = [floor(data.InitPoints.i1i2(i,2)) mod(ceil(data.InitPoints.i1i2(i,2)),m2)];
    
    add = [icf(1) ICF(1); icf(1) ICF(2); icf(2) ICF(1); icf(2) ICF(2)];
    
    isfine = 1;
    for j=1:4
        ind = add(j,1)*m2+add(j,2)+1;
        
        if ~isfinite(INN(ind,1))
            isfine = 0;
        end
        
        data.afs.KAN    = [data.afs.KAN; 2*data.plane.nn+i ind; 2*data.plane.nn+4+i data.plane.nn+ind];
        data.afs.KANOUT = [data.afs.KANOUT; 2*data.plane.nn+i ind];
        data.afs.KANINN = [data.afs.KANINN; 2*data.plane.nn+4+i data.plane.nn+ind];
    end
    
    if ~isfine
        data.afs.KAN = [data.afs.KAN; 2*data.plane.nn+i 2*data.plane.nn+4+i];
        data.afs.KANINN = [data.afs.KANINN; 2*data.plane.nn+i 2*data.plane.nn+4+i];
    end
end

[data.afs.pts, data.afs.line, data.afs.kanreal, data.afs.kanrealout, data.afs.kanrealinn, data.afs.nseg, data.afs.seg, data.afs.SegStruc, data.afs.SegTinit] = ...
    getsegmentsRAY(data.afs.all, data.afs.allfriends, data.afs.KAN, data.afs.KANOUT, data.afs.KANINN, data.afs.TinitStruc, data.raybase.m1, data.raybase.m2, posTinit, OUT2, INN);

for i=1:data.afs.nseg
    if data.afs.SegStruc(i,1) == 2
        
        plapts = data.afs.seg{i}.pts;
        for j=1:size(data.D,2)
            err(j,1) = ones(1, size(plapts,1))*([ones(size(plapts,1),1) plapts]*data.V(j,:)'-noiseA);
        end
        [mi, id] = min(err);
        
        if mi < size(data.D,1)*0.01
            data.afs.seg{i} = computePlaneSegmentRAY(data.afs.seg{i}, data.U, data.S, data.V, data.afs.seg{i}.pts, data.afs.seg{i}.friends, ...
                id, data.s, MaxLang, MaxFunC, epsFun, epsRand, noiseC, noiseA, data.calcp);
        end
    end
end

set(lvSlider(1),'Min',1.1*data.xm(1));
set(lvSlider(1),'Max',1.1*data.xm(2));
set(lvSlider(2),'Min',1.1*data.ym(1));
set(lvSlider(2),'Max',1.1*data.ym(2));
set(lvSlider(3),'Min',1.1*data.zm(1));
set(lvSlider(3),'Max',1.1*data.zm(2));

str = 'plot complete AFS';
str2 = '';
for i=1:data.afs.nseg
    if data.afs.SegStruc(i,1) == 2
        str = [str, '| plot segment ',num2str(i),' (plane)'];
        str2 = [str2, '| feasible bands segm. ',num2str(i),' (plane)'];
    elseif data.afs.SegStruc(i,1) == 3
        str = [str, '| plot segment ',num2str(i),' (line)'];
        str2 = [str2, '| feasible bands segm. ',num2str(i),' (line)'];
    elseif data.afs.SegStruc(i,1) == 4
        str = [str, '| plot segment ',num2str(i),' (point)'];
        str2 = [str2, '| feasible bands segm. ',num2str(i),' (point)'];
    else
        str = [str, '| plot segment ',num2str(i)];
        str2 = [str2, '| feasible bands segment ',num2str(i)];
    end
end

set(SegPlot, 'String', str);
set(SegTrafo, 'String', str2(2:end));

plotafsRAY(parentfig, data, AxesAFS, AxesSOL, lcs);

set(Footnote,'String',['AFS computation time:  ',num2str(tim),' s.'],'ForegroundColor','black');
onoffButton(Buttons, [8:11], []);

set(Empty_Button,'UserData',data);
