% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
% Function that organizes the Visual_Data supermatrix. The first process   %
% is to organize the rows by area (from higher to lower), then check for   %
% invalid rows (features that can't always be detected) and eliminate      %
% these rows and finally correct the angles obtained from regionprops      %
% which have values between [-90;90] while we need angles in the range of  %
% [-180;180]                                                               %
% Inputs:                                                                  %
%     Visual_Data       data supermatix                                    %
%     area_percentage	threshold for feature validation                   %
% Outputs:                                                                 %
%     results(a,b,c) = Visual_Data (organized)                             %
%         a = number of the feature detected                               %
%         b = property of a certain feature                                %
%         c = instant in time                                              %
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %

function results = function_organizeVisualData(Visual_Data,area_percentage)
for i=1:size(Visual_Data,3)
    % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
    % Organize matrix by area value (highest values at the top)
    % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
    check = true;
    while(check)
        % checks for all lines (features)
        for j = 1:size(Visual_Data,1)-1
            % if a value of area is greater than the next value, then there
            % is no need to change the order. If that's the case for the
            % last line, then the matriz is organized and we can observe
            % the next instant in time (k)
            if(Visual_Data(j,2,i) >= Visual_Data(j+1,2,i))
                if (j == size(Visual_Data,1)-1); check = false; end
            % if the condition is not true, then the rows are re-organized
            % and the cycle begins from scratch (continue)
            else
                Temp1 = Visual_Data(j,:,i);
                Visual_Data(j,:,i) = Visual_Data(j+1,:,i);
                Visual_Data(j+1,:,i) = Temp1;
                continue
            end
        end
    end
    % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
end
result_non_exist = true;
    for j = 1:size(Visual_Data,1)
    % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
    % Check for nonexistent features (features that occur occasionally)   %
    % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
    % After organizing the results, if on any line, the area is greater or
    % smaller than a threshold (a percentage times the first measured
    % value), then that line is considered invalid
    check = true;
    minValue = Visual_Data(j,2,1) - area_percentage*Visual_Data(j,2,end);
    maxValue = Visual_Data(j,2,1) + area_percentage*Visual_Data(j,2,end);
    for i = 1:size(Visual_Data,3)
        if(Visual_Data(j,2,i)<minValue | Visual_Data(j,2,i)>maxValue)
            check = false;
            continue
        end
    end
    if(check); results(j,:,:) = Visual_Data(j,:,:); result_non_exist = false; end
    if(result_non_exist); results = 0; end
end
for i = 4:size(Visual_Data,3)-3
    % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
    % Correct angle calculation values                                    %
    % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
    % If an angle value is positive (or negative) and three values before
    % and three values after the signal is opposite, then the measured
    % value is not correct and we should subtract (or add) 180º to the
    % measured angle. (Using regionprops the angles for orientation are
    % between [-90;90], so if an angle should be -91, it will be  measured
    % as 89). This script correct this values. 
    for j = 1:size(results,1)
        if (results(j,5,i) > 0 && results(j,5,i-3) < 0 && results(j,5,i+3) < 0)
           results(j,5,i) = results(j,5,i) - 180; 
        end
        if (results(j,5,i) < 0 && results(j,5,i-3) > 0 && results(j,5,i+3) > 0)
           results(j,5,i) = results(j,5,i) + 180; 
        end
    end
end