classdef GroundTruthDatabase < handle
    % GROUNDTRUTHDATABASE Ground Truth Database class
    %  self = GROUNDTRUTHDATABASE (config)
    %
    %  Input:
    %   - config = Ground Truth configuration INI file or an already-parsed 
    %     config structure, as produced by ROK_PARSE_CONFIG_INI().
    %     The structure must contain the following fields:
    %      - config.data_file = Ground truth data file; either an absolute path
    %        or path relative to the path of config INI file
    %      - config.people = names of people
    %      - config.frame_offset = frame offset, in case first stored 
    %        ground truth does not correspond to frame #1.
    %
    %  Output:
    %   - self = @GroundTruthDatabase instance
    %
    % Copyright (C) 2011-2012, Rok Mandeljc <rok.mandeljc@fe.uni-lj.si>
    
    properties (SetAccess = private, GetAccess = private)
        data;
        frameOffset = 0;
    end
    
    properties (SetAccess = private, GetAccess = public)
        people = {};
    end
    
    methods
        % Constructor
        function self = GroundTruthDatabase (config)
            %% Parse the config INI
            if ischar(config),
                config = rok_parse_config_ini(config);
            end
            
            self.people = config.people;

            %% Apply offset
            self.frameOffset = config.frame_offset;
            
            %% *** Load Ground Truth data ***
            dataFile = rok_path_find_file(config.data_file, config.source_ini_filename);
            
            % Matlab vs. binary files; we support both
            [ ~, ~, ext ] = fileparts(dataFile);
            
            % Data file is assumed to be MATLAB file...
            if strcmpi(ext, '.mat'),
                % MAT file
                tmp = open(dataFile);
                self.data = tmp.positions;
            else
                % Raw binary file               
                fid = fopen(dataFile, 'r', 'l');
                num_people = fread(fid, 1, 'uint32');
                num_coords = fread(fid, 1, 'uint32');
                num_frames = fread(fid, 1, 'uint32');
                tmp_data = fread(fid, inf, 'float32=>double');
                fclose(fid);
                
                % Reshape the data
                self.data = reshape(tmp_data, [ num_people, num_coords, num_frames ]);
            end
        end
    end
end