function buttonPlotSegRAY(hObject, eventdata, parentfig, Empty_Button, Buttons, AxesAFS, AxesSOL, SegPlot, rotoff, IPonoff, FPonoff, lvText, lvSlider, usmn, usmf, selmov, is3dmouse, lcs, POS)

data = get(Empty_Button,'UserData');
if (isfield(data,'isafs') == 0) || (data.isafs == 0)
    return
end

if (selmov > 0) && (data.islv == 1)
    Mouse3D_modifiedRAY(5,hObject, eventdata, parentfig, Empty_Button, Buttons, AxesAFS, AxesSOL, SegPlot, rotoff, IPonoff, FPonoff, lvText, lvSlider, usmn, usmf, lcs);
    
    set(usmn,'Value',0);
    set(usmf,'Value',1);
    data.islv = 0;
    set(Empty_Button,'UserData',data);
end

AFSview = get(AxesAFS,'view');
AFScam = get(AxesAFS,'cameraPosition');
AFStar = get(AxesAFS, 'cameraTarget');
AFSupv = get(AxesAFS, 'cameraUpVector');
pv = get(SegPlot, 'Value');

if pv == 1
    plotafsRAY(parentfig, data, AxesAFS, AxesSOL, lcs);
    
    xm = data.xm;
    ym = data.ym;
    zm = data.zm;
    
else
    [xm, ym, zm] = plotsegRAY(data, AxesAFS, pv, lcs);

end

if (data.islv) || (is3dmouse == 0)
    v1 = AFScam-AFStar;
    v2 = AFSupv;
    v3 = null([v1; v2]);
    
    if det([v1;v2;v3']) < 0
        ViewTrafo = [v1; v2; -v3'];
    else
        ViewTrafo = [v1; v2; v3'];
    end
    
    if is3dmouse == 0
        actx = POS;
    else
        dactx = ((ViewTrafo)^(-1)*([1 1 -1].*POS([2 3 1]))')';
        actx = data.lvactx + dactx;
    end
    
    if actx(1) < xm(1)   actx(1) = xm(1);  end
    if actx(1) > xm(2)   actx(1) = xm(2);  end
    if actx(2) < ym(1)   actx(2) = ym(1);  end
    if actx(2) > ym(2)   actx(2) = ym(2);  end
    if actx(3) < zm(1)   actx(3) = zm(1);  end
    if actx(3) > zm(2)   actx(3) = zm(2);  end
    
    data.lvactx = actx;
    
    xn = actx/norm(actx,2);
    psi = acos(xn(3));
    phi = atan2(xn(2),xn(1));
    
    i1m = floor((phi+pi)*data.raybase.m1/2/pi);
    i1p = ceil((phi+pi)*data.raybase.m1/2/pi);
    i2m = floor(psi*data.raybase.m2/pi);
    i2p = ceil(psi*data.raybase.m2/pi);
    
    if pv > 1
        
        idmm = find((data.afs.seg{pv-1}.i1==i1m).*(data.afs.seg{pv-1}.i2==i2m),1);
        idmp = find((data.afs.seg{pv-1}.i1==i1m).*(data.afs.seg{pv-1}.i2==i2p),1);
        idpm = find((data.afs.seg{pv-1}.i1==i1p).*(data.afs.seg{pv-1}.i2==i2m),1);
        idpp = find((data.afs.seg{pv-1}.i1==i1p).*(data.afs.seg{pv-1}.i2==i2p),1);
        
        ok = 0;
        actcol = [0 0 0];
        all = [idmm idmp idpm idpp];
        if length(all) >= 3
            if length(all) == 4
                TRI = [all(1:3); all([1 2 4]); all([1 3 4]); all([2 3 4])];
            else
                TRI = all;
            end
            for i=size(TRI,1)
                tri = TRI(i,:);
                
                ninn = data.afs.seg{pv-1}.INN(tri,:)\[1;1;1];
                nout = data.afs.seg{pv-1}.OUT(tri,:)\[1;1;1];
                if data.afs.seg{pv-1}.INN(tri(1),:)*ninn < 0
                    ninn = -ninn;
                end
                if data.afs.seg{pv-1}.OUT(tri(1),:)*nout < 0
                    nout = -nout;
                end
                
                if ((data.afs.seg{pv-1}.INN(tri(1),:)*ninn) < (actx*ninn)) && ((data.afs.seg{pv-1}.OUT(tri(1),:)*nout) > (actx*nout))
                    ok = 1;
                    actcol = lcs(pv-1,:);
                    break;
                end
            end
        end
    else
        ok = 0;
        actcol = [0 0 0];
        for seg=1:data.afs.nseg
            
            idmm = find((data.afs.seg{seg}.i1==i1m).*(data.afs.seg{seg}.i2==i2m),1);
            idmp = find((data.afs.seg{seg}.i1==i1m).*(data.afs.seg{seg}.i2==i2p),1);
            idpm = find((data.afs.seg{seg}.i1==i1p).*(data.afs.seg{seg}.i2==i2m),1);
            idpp = find((data.afs.seg{seg}.i1==i1p).*(data.afs.seg{seg}.i2==i2p),1);
            
            all = [idmm idmp idpm idpp];
            if length(all) >= 3
                if length(all) == 4
                    TRI = [all(1:3); all([1 2 4]); all([1 3 4]); all([2 3 4])];
                else
                    TRI = all;
                end
                for i=size(TRI,1)
                    tri = TRI(i,:);
                    
                    ninn = data.afs.seg{seg}.INN(tri,:)\[1;1;1];
                    nout = data.afs.seg{seg}.OUT(tri,:)\[1;1;1];
                    if data.afs.seg{seg}.INN(tri(1),:)*ninn < 0
                        ninn = -ninn;
                    end
                    if data.afs.seg{seg}.OUT(tri(1),:)*nout < 0
                        nout = -nout;
                    end

                    if ((data.afs.seg{seg}.INN(tri(1),:)*ninn) < (actx*ninn)) && ((data.afs.seg{seg}.OUT(tri(1),:)*nout) > (actx*nout))
                        ok = 1;
                        actcol = lcs(seg,:);
                        break;
                    end
                end
            end
            if ok
                break;
            end
        end
    end
    
    hold(AxesAFS,'on')
    if data.islv
        plot3(data.lvactx(1), data.lvactx(2), data.lvactx(3),'kx','Linewidth',2,'Markersize',10);
    else
        plot3(POS(1), POS(2), POS(3),'kx','Linewidth',2,'Markersize',10);
    end
    hold(AxesAFS,'off');
    
    set(lvText(2),'String',num2str(actx(1)));
    set(lvText(4),'String',num2str(actx(2)));
    set(lvText(6),'String',num2str(actx(3)));
    
    set(lvSlider(1),'Value',actx(1));
    set(lvSlider(2),'Value',actx(2));
    set(lvSlider(3),'Value',actx(3));
    
    if ok
        plot(AxesSOL,data.x,[1 actx]*data.V','color',actcol,'Linewidth',2);
    else
        plot(AxesSOL,data.x,[1 actx]*data.V','k--','Linewidth',2);
    end
    
    set(Empty_Button,'UserData',data);
end


if data.islv
    set(AxesAFS, 'cameraPosition', AFScam);
    set(AxesAFS, 'cameraTarget', AFStar);
    set(AxesAFS, 'cameraUpVector', AFSupv);
else
    set(AxesAFS,'view',AFSview);
end
