function plot_clusters(tree, level, iC)
%PLOT_CLUSTERS display one level of the cluster tree
%   PLOT_CLUSTERS(TREE, LEVEL, iC)  displays one level of the cluster tree.
%   The clusters are displayed as colored cubes. The source cluster is
%   green, its nearfield clusters are red, the interaction list clusters
%   are blue and the far field is white.
% Parameters:
%   TREE  : Cluster tree generated by CLUSTERTREE
%   LEVEL : the current level to be plot (0, 1, 2, ...)
%   iC    : The index of the source cluster

%% Color definitions
scol = [0 1 0]; % source
% ncol = [1 0 0]; % nearfield
icol = [0 0 1]; % interaction list
% fcol = [1 1 1]; % far field;
%% Plot clusters
L = tree(level+1); % the current level
D = L.diameter;
% select the source cluster
f = find(L.source);
iC = f(iC);
% plot source cluster
plot_cluster(L.coord(iC,:), D, scol);
if level >= 1
%     % plot near field clusters
%     near = setdiff(L.nearfield(iC,:),[0 iC]);
%     for n = 1 : length(near)
%         plot_cluster(L.coord(near(n),:), D, ncol);
%     end
    if level >= 2
        % plot interaction list clusters
        inter = setdiff(L.interlist(iC,:),0);
        for n = 1 : length(inter)
            plot_cluster(L.coord(inter(n),:), D, icol);
        end
%         % plot far field clusters
%         far = setdiff(1:size(L.coord,1), [near inter iC]);
%         for n = 1 : length(far)
%             plot_cluster(L.coord(far(n),:), D, fcol);
%         end
    end
end
% figure settings
axis equal;
axis tight;
alpha .7;


function plot_cluster(center, diameter, color)
%PLOT_CLUSTER   Plot one single cluster cube
%   PLOT_CLUSTER(CENTER, DIAMETER, COLOR) plots a cube centered at CENTER
%   with side length equal to DIAMETER colored by COLOR
vert = [
    -1 -1 -1
    1 -1 -1
    1  1 -1
    -1  1 -1
    -1 -1 +1
    1 -1 +1
    1  1 +1
    -1  1 +1
    ];
fac = [
    1 2 3 4
    5 6 7 8
    1 2 6 5
    2 3 7 6
    3 4 8 7
    4 1 5 8
    ];

v = vert * diameter/2 + repmat(center,8,1);
patch('faces', fac, 'vertices', v, 'facecolor', color);