classdef eye_data < handle
    properties (Constant) % value can not be changed
        NUM_FIX_PER_SEC = 60; % number of fixations per second (Default = 60).    
        VAL_SIGMA = 1;% sigma value used to create heatmap (Default = 1).
    end
    
    properties
        gx = []; % x coordinates provided by gaze tracker device
        gy = []; % y coordinates provided by gaze tracker device
        valid = []; % valid coordinates provided by gaze tracker device
        image = []; % full screen image
        im_name = ''; % image name from original data set
        id_user = ''; % user identifier (for future versions)
        att = ''; % question based on attribute
        answer = -1; % user answer to question "att": 0 for No and 1 for Yes. (Initialized with -1)
        fix_map = []; % fixation map updated after call create_heatmap
    end
    
    methods
        function obj = create_heatmap(obj, out_file)
			% This function is composed of the following steps:
			% 	* Extract sub image
			%	* Select valid fixations inside sub image
			%	* Create a blur heat map using fixations and sigma value (VAL_SIGMA)
			%	* Visualize blur heat map on top of the image
			
            % Step 1: Extract sub image
			dx = 450;
            dy_up = 435;
            dy_down = 465;
            
            img = obj.image;
            [sx, sy, ~] = size(img);
            
            cx = sx / 2;
            cy = sy / 2;
            
            begin_x = cx - dy_up;
            end_x = cx + dy_down;
            begin_y = cy - dx;
            end_y = cy + dx;

            fix(:,1) = round(obj.gx * sy); % columns
            fix(:,2) = round(obj.gy * sx); % rows
            
            img = img(begin_x:end_x, begin_y:end_y, :);
            
            % Step 2: Select valid fixations inside sub image
            fix_x = fix(:,2);
            fix_y = fix(:,1);
            
            pos_fixations = find(fix_x >= begin_x & fix_x <= end_x & ...
                                fix_y >= begin_y & fix_y <= end_y);
            fix_x = fix_x(pos_fixations);
            fix_y = fix_y(pos_fixations);
            
            % update fixations in relation to sub_image
            trans_x = begin_x - 1;
            trans_y = begin_y - 1;
            
            fix_x = fix_x - trans_x;
            fix_y = fix_y - trans_y;
            
            % update values
            fix = [];
            fix(:,2) = fix_x;
            fix(:,1) = fix_y;

            % Step 3: Create a blur heat map using fixations and sigma value (VAL_SIGMA) on image
            sigma = eye_data.VAL_SIGMA;
            FixHeatMap = zeros(size(img,1), size(img,2));
            Covariance = [sigma^2*size(img,1) 0; 0 sigma^2*size(img,1)];

            [X1,X2] = meshgrid(1:size(img,2), 1:size(img,1));
            X = [X1(:) X2(:)];

            parfor N=1:size(fix,1)
                Y = mvnpdf(X, fix(N,:), Covariance);
                Y = (reshape(Y,size(img,1),size(img,2))); 
                FixHeatMap = FixHeatMap + Y;
            end
            
            FixHeatMap = FixHeatMap / max(FixHeatMap(:)); % normalize heat map
            obj.fix_map = FixHeatMap;
            
            % Step 4: Visualize blur heat map on top of the image
			if strcmp(out_file, '') == 0
                hf = figure('Visible', 'off');
            
                imshow(img), hold on
                hImg = imshow(FixHeatMap);
                set(hImg, 'AlphaData', 0.5); % plot heat map
                colormap jet
                saveas(hImg, out_file);
                delete(hImg); % free memory
                close(hf);

                fprintf('Heatmap generated for image %s on file: %s.\n', obj.im_name, out_file);
            end
        end
    end
end


