function seg = computePlaneSegmentRAY(seg, U, S, V, pts, friends, plaid, s, MaxLang, MaxFunC, epsFun, epsRand, noiseC, noiseA, calcp)

k = size(U,1);
n = size(V,1);
nn = size(pts,1);

PLA = [1 1 1 -1].*V(plaid,[2 3 4 1]);

[ma,id] = max(abs(PLA(1:3)));

if id == 1
    pol = pts([1:end 1],2:3);
    pol = pol(convhull(pol(:,1),pol(:,2)),:);
    A = 0;
    for i=1:size(pol,1)-1
        A = A+pol(i,1)*pol(i+1,2)-pol(i+1,1)*pol(i,2);
    end;
    A = A/2;
    YS = 0;
    ZS = 0;
    for i=1:size(pol,1)-1
        YS = YS+(pol(i,1)+pol(i+1,1))*(pol(i,1)*pol(i+1,2)-pol(i+1,1)*pol(i,2));
        ZS = ZS+(pol(i,2)+pol(i+1,2))*(pol(i,1)*pol(i+1,2)-pol(i+1,1)*pol(i,2));
    end;
    YS = YS/6/A;
    ZS = ZS/6/A;
    
    dis = zeros(nn,1);
    for i=1:nn
        dis(i) = sum((YS-pts(i,:)).^2);
    end
    [mi, jd] = min(dis);
    
    YS = pts(jd,2);
    ZS = pts(jd,3);
    XS = (PLA(4)-PLA(2)*YS-PLA(3)*ZS)/PLA(1);
    
    InitT = [1 XS YS ZS; 1 friends(jd, [1 4 7]); 1 friends(jd,[2 5 8]); 1 friends(jd,[3 6 9])];
elseif id == 2
    pol = pts([1:end 1],[1 3]);
    pol = pol(convhull(pol(:,1),pol(:,2)),:);
    A = 0;
    for i=1:size(pol,1)-1
        A = A+pol(i,1)*pol(i+1,2)-pol(i+1,1)*pol(i,2);
    end;
    A = A/2;
    XS = 0;
    ZS = 0;
    for i=1:size(pol,1)-1
        XS = XS+(pol(i,1)+pol(i+1,1))*(pol(i,1)*pol(i+1,2)-pol(i+1,1)*pol(i,2));
        ZS = ZS+(pol(i,2)+pol(i+1,2))*(pol(i,1)*pol(i+1,2)-pol(i+1,1)*pol(i,2));
    end;
    XS = XS/6/A;
    ZS = ZS/6/A;
    
    dis = zeros(nn,1);
    for i=1:nn
        dis(i) = sum((XS-pts(i,:)).^2);
    end
    [mi, jd] = min(dis);
    
    XS = pts(jd,1);
    ZS = pts(jd,3);
    YS = (PLA(4)-PLA(1)*XS-PLA(3)*ZS)/PLA(2);
    
    InitT = [1 XS YS ZS; 1 friends(jd, [1 4 7]); 1 friends(jd,[2 5 8]); 1 friends(jd,[3 6 9])];
else
    pol = pts([1:end 1],1:2);
    pol = pol(convhull(pol(:,1),pol(:,2)),:);
    A = 0;
    for i=1:size(pol,1)-1
        A = A+pol(i,1)*pol(i+1,2)-pol(i+1,1)*pol(i,2);
    end;
    A = A/2;
    XS = 0;
    YS = 0;
    for i=1:size(pol,1)-1
        XS = XS+(pol(i,1)+pol(i+1,1))*(pol(i,1)*pol(i+1,2)-pol(i+1,1)*pol(i,2));
        YS = YS+(pol(i,2)+pol(i+1,2))*(pol(i,1)*pol(i+1,2)-pol(i+1,1)*pol(i,2));
    end;
    XS = XS/6/A;
    YS = YS/6/A;
    
    dis = zeros(nn,1);
    for i=1:nn
        dis(i) = sum((XS-pts(i,:)).^2);
    end
    [mi, jd] = min(dis);
    
    XS = pts(jd,1);
    YS = pts(jd,2);
    ZS = (PLA(4)-PLA(1)*XS-PLA(2)*YS)/PLA(3);
    
    InitT = [1 XS YS ZS; 1 friends(jd, [1 4 7]); 1 friends(jd,[2 5 8]); 1 friends(jd,[3 6 9])];
end

actpwd = pwd;
cd(calcp);

epsKante = 0.1*epsRand;

dlmwrite('wrap/T0inn.txt',[XS; YS; ZS],'precision','%15.15e','delimiter',' ');
dlmwrite('wrap/T0out.txt',[XS; YS; ZS],'precision','%15.15e','delimiter',' ');

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

arch = computer('arch');

set(gcf,'Pointer','Watch');
pause(0.5);
if (strcmp(arch,'maci64') == 1) || (strcmp(arch,'maca64') == 1)
    system('./AFSslicingM64.exe 4');
elseif (strcmp(arch,'win32') == 1) || (strcmp(arch,'win64') == 1)
    system('AFSslicingW32.exe 4');
elseif strcmp(arch,'glnx86') == 1
    system('./AFSslicinU32.exe 4');
elseif strcmp(arch,'glnxa64') == 1
    system('./AFSslicingU64.exe 4');
else
    fprintf(2,'No executable version for this type of system.\n');
    GenInfo = -2;
    tim = 0;
    set(gcf,'Pointer','arrow');
    cd(actpwd);
    return;
end

tim = load('wrap/timPLA.txt');

if tim(1) == 0
    GenInfo = -3;
    cd(actpwd);
    return;
else
    GenInfo = 2;
    AFSslice = load('wrap/AFSslice.txt');
    
    seg.isplane = 1;
    seg.plane = AFSslice(2:end,:);
    
    tim = tim(2);
    set(gcf,'Pointer','arrow');
    cd(actpwd);
end
