function data = computeaddfullafsCA4(object, data, Footnote, Polysettings)

k = data.k;
n = data.n;
s = data.s;
noiseC = data.noiseC;
V = data.V;
U = data.U;
S = data.S;

data.full.isC = 0;
data.full.isA = 0;

data.full.AFSC = [];
data.full.AFSA = [];

epsRand = 1.E-3;
MaxFunC = 200000;
epsFun = 1.e-10;
RefLevel = getsetvar(Polysettings(3), 0, 10, 1, 0);
MaxLang = getsetvar(Polysettings(2), 50, 25000, 1, 0);
noiseC = -getsetvar(Polysettings(1), 1.E-12, 3.E-2, 0, 0);
noiseA = noiseC;
h0 = 0.975;

U = data.U;
S = data.S;
V = data.V;

[k,n] = size(data.D);
s = 4;

fprintf(' C routine (AFS for factor A) in progress ... (press CTRL + C to cancel) ... ');
set(object, 'Enable','off');

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

if GenInfo == -3
    data.full.isA = 0;
    colfn = 'r';
    
    fprintf('computation canceled.\n');
    set(Footnote,'String','AFS computation canceled.','ForegroundColor','red');
else
    fprintf('ready, time: %2.2f s.\n',tim);
    strfn = [' AFS computation time for A: ',num2str(tim,'%2.2f'),' s'];
    data.full.isA = 1;
end

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

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

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

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

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

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

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

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

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

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

for i=1:4
    icf = [floor(data.afsA.InitPoints.i1i2(i,1)) mod(ceil(data.afsA.InitPoints.i1i2(i,1)),m1)];
    ICF = [floor(data.afsA.InitPoints.i1i2(i,2)) mod(ceil(data.afsA.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.afsA.afs.KAN    = [data.afsA.afs.KAN; 2*data.afsA.plane.nn+i ind; 2*data.afsA.plane.nn+4+i data.afsA.plane.nn+ind];
        data.afsA.afs.KANOUT = [data.afsA.afs.KANOUT; 2*data.afsA.plane.nn+i ind];
        data.afsA.afs.KANINN = [data.afsA.afs.KANINN; 2*data.afsA.plane.nn+4+i data.afsA.plane.nn+ind];
    end
    
    if ~isfine
        data.afsA.afs.KAN = [data.afsA.afs.KAN; 2*data.afsA.plane.nn+i 2*data.afsA.plane.nn+4+i];
        data.afsA.afs.KANINN = [data.afsA.afs.KANINN; 2*data.afsA.plane.nn+i 2*data.afsA.plane.nn+4+i];
    end
end

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

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

% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
% Ende Berechnung von AFS fuer A
% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++


s = k;
k = n;
n = s;
s = 4;

InitT = pinv(data.InitT)'*data.S(1:4,1:4)';
for i=1:4
    InitT(i,:) = InitT(i,:)/InitT(i,1);
end

U = data.V;
S = data.S';
V = data.U;

fprintf(' C routine (AFS for factor C) in progress ... (press CTRL + C to cancel) ... ');

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

if GenInfo == -3
    data.full.isA = 0;
    colfn = 'r';
    
    fprintf('computation canceled.\n');
    set(Footnote,'String','AFS computation canceled.','ForegroundColor','red');
else
    fprintf('ready, time: %2.2f s.\n',tim);
    strfn = [' AFS computation time for A: ',num2str(tim,'%2.2f'),' s'];
    data.full.isA = 1;
end

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

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

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

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

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

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

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

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

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

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

for i=1:4
    icf = [floor(data.afsC.InitPoints.i1i2(i,1)) mod(ceil(data.afsC.InitPoints.i1i2(i,1)),m1)];
    ICF = [floor(data.afsC.InitPoints.i1i2(i,2)) mod(ceil(data.afsC.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.afsC.afs.KAN    = [data.afsC.afs.KAN; 2*data.afsC.plane.nn+i ind; 2*data.afsC.plane.nn+4+i data.afsC.plane.nn+ind];
        data.afsC.afs.KANOUT = [data.afsC.afs.KANOUT; 2*data.afsC.plane.nn+i ind];
        data.afsC.afs.KANINN = [data.afsC.afs.KANINN; 2*data.afsC.plane.nn+4+i data.afsC.plane.nn+ind];
    end
    
    if ~isfine
        data.afsC.afs.KAN = [data.afsC.afs.KAN; 2*data.afsC.plane.nn+i 2*data.afsC.plane.nn+4+i];
        data.afsC.afs.KANINN = [data.afsC.afs.KANINN; 2*data.afsC.plane.nn+i 2*data.afsC.plane.nn+4+i];
    end
end

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

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