function [pts, line, kanreal, kanrealout, kanrealinn, nseg, seg, SegStruc, SegTinit] = getsegmentsRAY(pts, friends, KAN, KANOUT, KANINN, TinitStruc, m1, m2, posTinit, OUT, INN)

nn = size(pts,1);
A = sparse(nn,nn);
for i=1:size(KAN,1)
    A(KAN(i,1),KAN(i,2)) = 1;
end

Ao = sparse(nn,nn);
for i=1:size(KANOUT,1)
    Ao(KANOUT(i,1),KANOUT(i,2)) = 1;
end

Ai = sparse(nn,nn);
for i=1:size(KANINN,1)
    Ai(KANINN(i,1),KANINN(i,2)) = 1;
end

infs = [];
for i=1:nn
    if isinf(pts(i,1))
        infs = [infs; i];
    end
end
ninfs = setdiff(1:nn, infs);

Ao = Ao(ninfs,ninfs);
[kanrealout(:,1),kanrealout(:,2),s] = find(Ao);

Ai = Ai(ninfs,ninfs);
[kanrealinn(:,1),kanrealinn(:,2),s] = find(Ai);

ninfsIO = [];
for i=1:length(ninfs)
    if ninfs(i) <= size(OUT,1)
        ninfsIO = [ninfsIO, ninfs(i)];
    end
end

OUT = OUT(ninfsIO,:);
INN = INN(ninfsIO,:);

pts = pts(ninfs,:);
A = A(ninfs,ninfs);
friendsreal = friends(ninfs,:);
nn = size(pts,1);
U = [1:nn]';
nseg = 0;
posTinit = posTinit-length(infs);

[kanreal(:,1),kanreal(:,2),s] = find(A);

ptsInit = pts(posTinit,:);

detinit = 1;
while ~isempty(U)
    if detinit <= 4
        while detinit <= 4
            dis = 0;
            for i=1:length(U)
                dis(i) = sum((pts(U(i),:)-ptsInit(detinit,:)).^2);
            end
            [mi, id] = min(dis);
            if mi < 1.E-14
                u = U(id(1));
                break;
            else
                detinit = detinit+1;
            end
        end
    else
        u = U(1);
    end
    
    ready = [];
    nready = u(1);
    while ~isempty(nready)
        new1 = find(A(nready(1),:) == 1);
        new2 = find(A(:,nready(1)) == 1);
        u = unique([u; new1'; new2]);
        ready = [ready; nready(1)];
        nready = setdiff(u, ready);
    end
    U = setdiff(U,u);
    
    seg{nseg+1}.KAN = [];
    B = zeros(size(A));
    B(u,u) = A(u,u);
    for i=1:nn
        ind = find(B(:,i) == 1);
        seg{nseg+1}.KAN = [seg{nseg+1}.KAN; i*ones(size(ind)) ind];
    end
    if isempty(seg{nseg+1}.KAN)
        seg{nseg+1}.KAN = [u(1) u(1)];
    end
    A(u,:) = 0;
    A(:,u) = 0;
    nseg = nseg+1;
end

for j=1:nseg
    line = [];
    kan = seg{j}.KAN;
    id = 1;
    while ~isempty(kan)
        line{id}.lin = kan(1,:);
        kan = kan(2:end,:);
        if isempty(kan)
            break;
        end
        isfound = 1;
        while isfound
            isfound = 0;
            id1 = find(kan(:,1) == line{id}.lin(end), 1, 'first');
            if ~isempty(id1)
                line{id}.lin(end+1) = kan(id1,2);
                kan = kan([1:id1-1 id1+1:end],:);
                isfound = 1;
            else
                id1 = find(kan(:,2) == line{id}.lin(end), 1, 'first');
                if ~isempty(id1)
                    line{id}.lin(end+1) = kan(id1,1);
                    kan = kan([1:id1-1 id1+1:end],:);
                    isfound = 1;
                end
            end
        end
        if isempty(kan)
            break;
        end
        id = id+1;
    end
    seg{j}.line = line;
end

mi = [];
SegTinit = zeros(4,1);
for i=1:nseg
    idd = [];
    for j=1:length(seg{i}.line)
        idd = unique([idd, seg{i}.line{j}.lin]);
    end
    
    IDD = [];
    i1 = [];
    i2 = [];
    for j=1:length(idd)
        if idd(j) <= size(INN,1)
            IDD = [IDD, idd(j)];
            
            xn = OUT(idd(j),:)/norm(OUT(idd(j),:),2);
            psi = acos(xn(3));
            phi = atan2(xn(2),xn(1));
            
            i1 = [i1, round((phi+pi)*m1/2/pi)];
            i2 = [i2, round(psi*m2/pi)];
        end
    end
    seg{i}.OUT = OUT(IDD,:);
    seg{i}.INN = INN(IDD,:);
    
    seg{i}.i1 = i1;
    seg{i}.i2 = i2;
    
    seg{i}.pts = pts(idd,:);
    seg{i}.friends = friendsreal(idd,:);
    mi(i,1) = min(seg{i}.pts(:,3));
    
    for j=1:4
        if ~isempty(find(idd == posTinit(j), 1, 'first'))
            SegTinit(j,1) = i;
        end
    end
end

for i=1:nseg
    ind = find(SegTinit==i,1,'first');
    if length(ind) == 1
        SegStruc(i,1) = TinitStruc(ind);
        SegStruc(i,2) = ind;
    else
        SegStruc(i,1) = 1;
        SegStruc(i,2) = 0;
    end
end

for i=1:nseg
    id = find(SegTinit == i);
    if length(id) == 1
        seg{i}.col = id;
        seg{i}.no = id;
    else
        seg{i}.col = 7;
        if length(id) > 1
            seg{i}.no = min(id);
        else
            seg{i}.no = 0;
        end
    end
end

kan = kanreal;
id = 1;
while ~isempty(kan)
    line{id}.lin = kan(1,:);
    kan = kan(2:end,:);
    if isempty(kan)
        break;
    end
    isfound = 1;
    while isfound
        isfound = 0;
        id1 = find(kan(:,1) == line{id}.lin(end), 1, 'first');
        if ~isempty(id1)
            line{id}.lin(end+1) = kan(id1,2);
            kan = kan([1:id1-1 id1+1:end],:);
            isfound = 1;
        else
            id1 = find(kan(:,2) == line{id}.lin(end), 1, 'first');
            if ~isempty(id1)
                line{id}.lin(end+1) = kan(id1,1);
                kan = kan([1:id1-1 id1+1:end],:);
                isfound = 1;
            end
        end
    end
    if isempty(kan)
        break;
    end
    id = id+1;
end
