function [SHAPES] = shape2d(M,S,GIIDs);
% SHAPE2(M,S,GIIDs) ----  This function calculates the the 'shape' of each group in 
% field 'M', and modifies the measure so that it will return 1 for a perfect circle,
% and higher values the more the group diverges from a circle. 
%
% See 'shape' for details on the shape index.


% Initialize the SHAPES matrix, to be filled with the shape for each group.
SHAPES = [];

% Now, each group will be taken out, and the shape calculated.

for i = 1:size(GIIDs,1)                 % Loop over each group.
    
    if size(GIIDs{i},2) > 2             % Convhull only works for more than 2 points
                                        %  so any pairs/singles will be ignored.
        mxiid = max(GIIDs{i});          % Finds the max IID for the group.      
        
        
        % Now, the group data will be pulled from the full array.
        GIND = S(i).members;            % Indices of the group members found.
        GRP = [];                       % Initialize the GRP matrix.
        for p = 1:size(GIND,2)          % Loops over all members.
            ind = GIND(1,p);            % Gets individual index...  
            IND = M(ind,:);             % Pulls that row from array.
            GRP = [GRP; IND];           % And builds the group matrix, a subset of 
                                        %  the original M.
        end
        
        % Next, convhull finds the area (A).
        [K A] = convhull(GRP(:,2),GRP(:,3));
        
        % And shape is calculated.
        shape = (mxiid^2) / A; 
        
    % For pairs/stragglers, shape is set to a low number, so it can be ignored later.    
    else
        shape = -999;
    end
    
    % Since the shape function returns (4/pi) for a circle, the index is now multiplied
    % by pi/4 to standardize it to a circle.
    shape = shape * (pi/4);
    
    % Builds SHAPES matrix, of each group's shape value.
    SHAPES = [SHAPES; shape];
end
