function example_3D()
    % Load example data
    %**********************************************************************
    % Data structure:
    %**********************************************************************
    % - scans: List of scans taken with the RGB-D camera   
    %   - scans{i}.vertices: List of vertices of the P.C. taken with the cam
    %       - scans{i}.vertices(1,:): X coordinates of the i'th scan    
    %       - scans{i}.vertices(2,:): Y coordinates of the i'th scan
    %       - scans{i}.vertices(3,:): Z coordinates of the i'th scan
    %
    %   - scans{i}.colors: List of colors of the P.C. taken with the cam
    %       - scansList{i}.colors(:,1): R components of i'th scan
    %       - scansList{i}.colors(:,2): G components of i'th scan
    %       - scansList{i}.colors(:,3): B components of i'th scan
    %
    %   - scans{i}.position: Position of the RGB-D cam  when taking the i'th scan
    %       - position(1): X coordinate
    %       - position(2): Y coordinate
    %       - position(3): Z coordinate
    %       - position(4): yaw 
    %       - position(5): pitch
    %       - position(6): roll    
    load 'scans_3D' scans;
    
    % Get number of scans.
    numScans = length(scans);
    
    % Initialize M and colors
    S = [];
    colors = [];

    % Initialize graphic objects
    figure
    mainAx = axes;
    pcObj = [];
        
    % Iterate over each image taken with the RGB-D camera
    for scanID = 1:numScans
        % Load Template PC.
        T = scans{scanID}.vertices;
        camPos = scans{scanID}.position;
        scanColors = scans{scanID}.colors;
        
        % Calculate new cam position and update M
        S = PCMerge(S, T, camPos); 
        
        % Assign colors to new points.
        colors = cat(1,colors,scanColors);
        
        % Update reconstruction.
        pcObj = displayPC_RGB(S', colors, pcObj, mainAx, 'Real time reconstruction');

        % Display progress
        disp(['Scan ',num2str(scanID),' out of ', num2str(numScans)]);
        
        % Refresh interface
        refresh;
        drawnow;
    end
end

function pcObj = displayPC_RGB(xyz, colormap, pcObj, targetAx, titleStr)
    ptCloud = pointCloud(xyz,'Color',colormap);
    if isempty(pcObj)   
        pcObj = pcshow(ptCloud,'Parent', targetAx);      
        title(targetAx,titleStr);
    else
        try
            pcObj.XLim = ptCloud.XLimits;
            pcObj.YLim = ptCloud.YLimits;
            pcObj.ZLim = ptCloud.ZLimits;
            pcObj.Children.XData = xyz(:,1);
            pcObj.Children.YData = xyz(:,2);
            pcObj.Children.ZData = xyz(:,3);
            pcObj.Children.CData = colormap;
        catch
        end
    end
end