%VISUALCLOUD2D Displays the binned density of the pointcloud in 2D.
% 
% SYNOPSIS:
%   visualizeCloud2D(pointcloud,bins,angle)
% 
% INPUT PARAMETERS:
%   pointcloud
%      [x y z; ...] (for example Particle{10}.points)
%   bins
%      number of bins in all directions
%   diameter
   %    in px
%   angle
%      rotate by this angle
% 
% DEFAULTS:
%   none
% 
% NOTES:
%  none
% 
% (C) Copyright 2015               Diederik Feilzer
%     All rights reserved          Delft University of Technology
% 
% Diederik Feilzer, juli 2015

function [dip,Z] = visualizeCloud2D(X,n,diameter,angle)

Rot = [cos(angle) -sin(angle); sin(angle) cos(angle)];

X = (Rot*(X'))';

X(:,1) = X(:,1) - mean(X(:,1),1);
X(:,2) = X(:,2) - mean(X(:,2),1);

ROIradius = 0.5*diameter;

X = X(find(X(:,1) < ROIradius & X(:,1) > (-ROIradius)),:);
X = X(find(X(:,2) < ROIradius & X(:,2) > (-ROIradius)),:);

xi = linspace(-ROIradius,ROIradius,n);
yi = linspace(-ROIradius,ROIradius,n);

xr = interp1(xi,1:numel(xi),X(:,1),'nearest');
yr = interp1(yi,1:numel(yi),X(:,2),'nearest');

Z = accumarray([xr yr],1, [n n]);

dip = dipshow(Z','lin');
dipmapping(dip,[0 0.5*max(Z(:))],'COLORMAP',hot(256))
end