function [AFS,totalpoin,calls,bisec,tim,parts,dimP,lenP,LENP,valid,lines,mima,GenInfo,switchpolinv] = computeAFS(MaxLang, MaxFunC, epsFun, epsKante, epsRand, noiseC, noiseA, s, U, S, V, InitT, opt, calcp, uinv)
k = size(U,1);
n = size(V,1);
    
mima = [];

actpwd = pwd;
cd(calcp);

dlmwrite('wrap/allg.txt',[k;n;s;MaxLang;MaxFunC],'precision','%d');
dlmwrite('wrap/allg.txt',[epsFun;epsKante;epsRand;noiseC;noiseA],'-append','precision','%e');
dlmwrite('wrap/L.txt',U*S(:,1:s),'precision','%15.15e','delimiter',' ');
dlmwrite('wrap/R.txt',V(:,1:s)','precision','%15.15e','delimiter',' ');
dlmwrite('wrap/InitT.txt',InitT,'precision','%15.15e','delimiter',' ');
dlmwrite('wrap/tim.txt',0,'precision','%d');

arch = computer('arch');

set(gcf,'Pointer','Watch');
pause(0.5);
if (strcmp(arch,'maci64') == 1) || (strcmp(arch,'maca64') == 1)
    system(['./AFScomputationMAC64.exe ',num2str(opt)]);
elseif strcmp(arch,'win32') == 1
    system(['AFScomputationWINDOWS32.exe ',num2str(opt)]);
elseif strcmp(arch,'win64') == 1
    system(['AFScomputationWINDOWS64.exe ',num2str(opt)]);
elseif strcmp(arch,'glnx86') == 1
    system(['./AFScomputationUNIX32.exe ',num2str(opt)]);
elseif strcmp(arch,'glnxa64') == 1
    system(['./AFScomputationUNIX64.exe ',num2str(opt)]);
else
    fprintf(2,'No executable version for this type of system.\n');
    GenInfo = -2;
    AFS = [];
    totalpoin = 0;
    calls = 0;
    bisec = 0;
    parts = 0;
    dimP = 0;
    lenP = 0;
    LENP = 0;
    valid = [];
    lines = [];
    tim = 0;
    mima = 0;
    set(gcf,'Pointer','arrow');
    cd(actpwd);
    return;
end
set(gcf,'Pointer','arrow');

tim = load('wrap/tim.txt');
switchpolinv = 0;

if tim(1) == 0
    GenInfo = -3;
    AFS = [];
    totalpoin = 0;
    calls = 0;
    bisec = 0;
    parts = 0;
    dimP = 0;
    lenP = 0;
    LENP = 0;
    valid = [];
    lines = [];
    tim = 0;
    cd(actpwd);
    return;
else
    Back = load('wrap/AFS.txt');
    GenInfo = Back(1,:);
    R = Back(2:end,:);
    cd(actpwd);
    
    if s == 2
        if GenInfo > 0
            [ini, id] = max(abs(InitT(:,2)));
            if (InitT(id,2) < 0 && id ==2) || (InitT(id,2) > 0 && id ==1)
                temp = R(1:2);
                R(1:2) = R(3:4);
                R(3:4) = temp;
            end
            AFS{1} = R(1:2);
            AFS{2} = R(3:4);
            totalpoin = 4;
            calls = R(5);
            bisec = R(6);
            parts = 2;
            dimP = [];
            lenP = [];
            LENP = [];
            valid = [1;1];
            lines = 2;

            c1 = U*S(:,1:2)*pinv([1 R(1); 1 R(3)]);
            c2 = U*S(:,1:2)*pinv([1 R(1); 1 R(4)]);
            c3 = U*S(:,1:2)*pinv([1 R(2); 1 R(3)]);
            c4 = U*S(:,1:2)*pinv([1 R(2); 1 R(4)]);

            mima = [R(1) R(2) min(min([1 R(1)]*V(:,1:2)'), min([1 R(2)]*V(:,1:2)')) max(max([1 R(1)]*V(:,1:2)'), max([1 R(2)]*V(:,1:2)')) ...
                min([min(c1(:,2)), min(c2(:,2)), min(c3(:,2)), min(c4(:,2))]) ...
                max([max(c1(:,2)), max(c2(:,2)), max(c3(:,2)), max(c4(:,2))]);...
                R(3) R(4) min(min([1 R(3)]*V(:,1:2)'), min([1 R(4)]*V(:,1:2)')) max(max([1 R(3)]*V(:,1:2)'), max([1 R(4)]*V(:,1:2)')) ...
                min([min(c1(:,1)), min(c2(:,1)), min(c3(:,1)), min(c4(:,1))]) ...
                max([max(c1(:,1)), max(c2(:,1)), max(c3(:,1)), max(c4(:,1))])];
        else
            AFS = [];
            totalpoin = 0;
            calls = 0;
            bisec = 0;
            parts = 0;
            dimP = 0;
            lenP = 0;
            LENP = 0;
            valid = [];
            lines = [];
            tim = tim(2);
            return;
        end
    else
        if GenInfo(1,1) == -2
            AFS = [];
            totalpoin = 0;
            calls = R(3,1);
            bisec = R(3,2);
            parts = 0;
            dimP = 0;
            lenP = 0;
            LENP = 0;
            valid = [];
            lines = [];
            tim = tim(2);
            return;
        end

        if (R(1,1) == -2) || (R(1,1) == -1)
            d = 0;

            calls = R(3,1);
            bisec = R(3,2);
            totalpoin = R(4,1)+R(4,2);

            valid = 0;

            AFS = [];

            mima = [];

            parts = R(1,1)+1;
            lines = 0;
        elseif R(1,1) == -3
            d(1) = R(4,1);
            d(2) = R(4,2);

            calls = R(3,1);
            bisec = R(3,2);
            totalpoin = R(4,1)+R(4,2);

            valid = min(d,1);

            AFS{1} = R(5:4+d(1),:);
            AFS{2} = R(5+d(1):4+d(1)+d(2),:);

            mima = zeros(3,6);
            for i=1:2
                if valid(i) == 1
                    mima(i,:) = [min(AFS{i}(:,1)), max(AFS{i}(:,1)), min(AFS{i}(:,2)), max(AFS{i}(:,2)),...
                        min(min([ones(size(AFS{i},1),1) AFS{i}]*V(:,1:3)')), max(max([ones(size(AFS{i},1),1) AFS{i}]*V(:,1:3)'))];
                end
            end

            parts = R(1,1);
            lines = 2;
        elseif R(1,1) == 1
            d(1) = R(4,1);
            d(2) = R(4,2);

            calls = R(3,1);
            bisec = R(3,2);
            totalpoin = R(4,1)+R(4,2);

            valid = min(d,1);

            AFS{1} = R(5:4+d(1),:);
            AFS{2} = R(5+d(1):4+d(1)+d(2),:);
            AFS{3} = [];

            mima = zeros(3,6);
            for i=1:2
                if valid(i) == 1
                    mima(i,:) = [min(AFS{i}(:,1)), max(AFS{i}(:,1)), min(AFS{i}(:,2)), max(AFS{i}(:,2)),...
                        min(min([ones(size(AFS{i},1),1) AFS{i}]*V(:,1:3)')), max(max([ones(size(AFS{i},1),1) AFS{i}]*V(:,1:3)'))];
                end
            end

            parts = 1;
            lines = 2;
        elseif R(1,1) == 2
            d(1) = R(1,2);
            d(2) = R(2,1);
            d(3) = R(2,2);
            calls = R(3,1);
            bisec = R(3,2);
            totalpoin = sum(d);

            valid = min(d,1);

            AFS{1} = R(5:4+d(1),:);
            AFS{2} = R(5+d(1):4+d(1)+d(2),:);
            AFS{3} = R(5+d(1)+d(2):4+d(1)+d(2)+d(3),:);
            
            onesegment = [0 0 0];
            mima = zeros(3,6);
            for i=1:3
                if valid(i) == 1
                    mima(i,:) = [min(AFS{i}(:,1)), max(AFS{i}(:,1)), min(AFS{i}(:,2)), max(AFS{i}(:,2)),...
                        min(min([ones(size(AFS{i},1),1) AFS{i}]*V(:,1:3)')), max(max([ones(size(AFS{i},1),1) AFS{i}]*V(:,1:3)'))];
                    if (mima(i,1) < 0) && (mima(i,2) > 0) && (mima(i,3) < 0) && (mima(i,4) > 0)
                        onesegment(i) = 1;
                    end
                end
            end
            
            if max(onesegment) == 1
                set(uinv,'Value',1);
                firsttim = tim(2);
                [AFS,totalpoin,calls,bisec,tim,parts,dimP,lenP,LENP,valid,lines,mima,GenInfo,switchpolinv] = computeAFS(MaxLang, MaxFunC, epsFun, epsKante, epsRand, noiseC, noiseA, s, U, S, V, InitT, opt-1, calcp, uinv);
                switchpolinv = 1;
                tim = firsttim+tim;
                return;
            end;

            parts = 3;
            lines = 3;
        elseif R(1,1) == 3
            d(1) = R(1,2);
            d(2) = R(2,1);
            d(3) = R(2,2);
            calls = R(3,1);
            bisec = R(3,2);
            totalpoin = R(4,1)+R(4,2);

            valid = min(d,1);

            AFS{1} = R(5:4+d(1),:);
            AFS{2} = R(5+d(1):4+d(1)+d(2),:);
            AFS{3} = R(5+d(1)+d(2):4+d(1)+d(2)+d(3),:);

            mima = zeros(3,6);
            for i=1:3
                if valid(i) == 1
                    mima(i,:) = [min(AFS{i}(:,1)), max(AFS{i}(:,1)), min(AFS{i}(:,2)), max(AFS{i}(:,2)),...
                        min(min([ones(size(AFS{i},1),1) AFS{i}]*V(:,1:3)')), max(max([ones(size(AFS{i},1),1) AFS{i}]*V(:,1:3)'))];
                end
            end

            ADJ = inf(3,3);
            for l=1:3
                x = InitT(l,2:3);
                for i=1:3
                    if valid(i) == 1
                        summ = 0;
                        for j=1:d(i)-1
                            a = x-AFS{i}(j,:);
                            b = x-AFS{i}(j+1,:);
                            summ = summ + acos(a*b'/norm(a,2)/norm(b,2));
                        end
                        ADJ(i,l) = summ-2*pi;
                    end
                end
            end

            per = zeros(3,1);
            TEMP = AFS;
            for i=1:3
                [mi,id] = min(abs(ADJ));
                [mi,jd] = min(mi);

                per(id(jd),1) = jd;

                ADJ(:,jd) = inf;
                ADJ(id(jd),:) = inf;

                AFS{jd} = TEMP{id(jd)};
            end

            mima(per,:)  = mima;
            d(per)       = d;
            valid(per)   = valid;

            parts = 3;
            lines = 3;
            
        else
            parts = R(1,1);
            lines = parts;
            
            calls = R(3,1);
            bisec = R(3,2);
            totalpoin = R(4,1)+R(4,2);
            
            d = R(5:4+parts,1);
            
            valid = min(d,1);
            
            R = R(5+parts:end,:);
            
            for i=1:parts
                AFS{i} = R(1+sum(d(1:i-1)):sum(d(1:i)),:);
            end
            
            mima = zeros(parts,6);
            for i=1:parts
                if valid(i) == 1
                    mima(i,:) = [min(AFS{i}(:,1)), max(AFS{i}(:,1)), min(AFS{i}(:,2)), max(AFS{i}(:,2)),...
                        min(min([ones(size(AFS{i},1),1) AFS{i}]*V(:,1:3)')), max(max([ones(size(AFS{i},1),1) AFS{i}]*V(:,1:3)'))];
                end
            end

            ADJ = inf(parts,3);
            for l=1:3
                x = InitT(l,2:3);
                for i=1:parts
                    if valid(i) == 1
                        summ = 0;
                        for j=1:d(i)-1
                            a = x-AFS{i}(j,:);
                            b = x-AFS{i}(j+1,:);
                            summ = summ + acos(a*b'/norm(a,2)/norm(b,2));
                        end
                        ADJ(i,l) = summ-2*pi;
                    end
                end
            end
            
            TEMP = AFS;
            fullP = 1:parts;
            for i=1:3
                [mi,id] = min(abs(ADJ(:,i)));
                AFS{i} = TEMP{id};
                
                resort(i,1) = id;
                fullP = setdiff(fullP,id);
                
                ADJ(id,:) = inf;
            end
            
            resort(4:parts,1) = fullP;
            
            for i=4:parts
                AFS{i} = TEMP{resort(i)};
            end

            mima  = mima(resort,:);
            d     = d(resort);
            valid = valid(resort);
        end

        if parts > 0
            L = zeros(lines,max(d));
            for i=1:lines
                l(i) = 0;
                for j=1:d(i)-1
                    L(i,j) = norm(AFS{i}(j,:)-AFS{i}(j+1,:),2);
                    l(i) = l(i)+L(i,j);
                end
            end
        else
            l = 0;
            L = 0;
        end

        dimP = d;
        lenP = l;
        LENP = L;
    end
    tim = tim(2);
end
