function plot_3D_afs(AFS)
% plots an 3D AFS of a four component system
% 
% input:
% AFS ........ area of feasible solutions computed by facpack
% 
% example:
% plot_3D_afs(AFS)

col = [0     0.4470     0.7410;
    0.8500    0.3250    0.0980;
    0.9290    0.6940    0.1250;
    0.4940    0.1840    0.5560;
    0.4660    0.6740    0.1880;
    0.3010    0.7450    0.9330];

figure
for seg = 1:AFS.nseg
    if AFS.SegStruc(seg,1) == 2
        if AFS.seg{seg}.isplane
            fill3(AFS.seg{seg}.plane(:,1), AFS.seg{seg}.plane(:,2), AFS.seg{seg}.plane(:,3), col(AFS.seg{seg}.col,:), 'facealpha',.15);
            hold on;
        end
    elseif AFS.SegStruc(seg,1) == 3
        pts = AFS.lines{AFS.SegStruc(seg,2)}.pts;
        plot3(pts(:,1), pts(:,2), pts(:,3), 'color', col(AFS.seg{seg}.col,:), 'Linewidth',3);
        hold on;
    end
    for j=1:length(AFS.seg{seg}.line)
        if AFS.seg{seg}.col <= size(col,1)
            plot3(AFS.pts(AFS.seg{seg}.line{j}.lin,1), AFS.pts(AFS.seg{seg}.line{j}.lin,2), AFS.pts(AFS.seg{seg}.line{j}.lin,3), 'color', col(AFS.seg{seg}.col,:));
        else
            plot3(AFS.pts(AFS.seg{seg}.line{j}.lin,1), AFS.pts(AFS.seg{seg}.line{j}.lin,2), AFS.pts(AFS.seg{seg}.line{j}.lin,3), 'k');
        end
        hold on;
    end
end
hold off;
axis tight;

xm = get(gca, 'XLim');
ym = get(gca, 'YLim');
zm = get(gca, 'ZLim');

if xm(1) < 0 xm(1) = xm(1)*1.05; else xm(1) = xm(1)*0.95;end
if xm(2) > 0 xm(2) = xm(2)*1.05; else xm(2) = xm(2)*0.95;end

if ym(1) < 0 ym(1) = ym(1)*1.05; else ym(1) = ym(1)*0.95;end
if ym(2) > 0 ym(2) = ym(2)*1.05; else ym(2) = ym(2)*0.95;end

if zm(1) < 0 zm(1) = zm(1)*1.05; else zm(1) = zm(1)*0.95;end
if zm(2) > 0 zm(2) = zm(2)*1.05; else zm(2) = zm(2)*0.95;end

xlabel('$x_1$', 'interpreter', 'latex', 'fontsize',12)
ylabel('$x_2$', 'interpreter', 'latex', 'fontsize',12)
zlabel('$x_3$', 'interpreter', 'latex', 'fontsize',12)

title('Area of feasible solutions','FontWeight','Normal')
set(gca, 'XLim', xm, 'YLim', ym, 'ZLim', zm);
