function [pos, neg, impos] = kitti_data(cls, year)
% Get training data from the PASCAL dataset.
%   [pos, neg, impos] = pascal_data(cls, year)
%
% Return values
%   pos     Each positive example on its own
%   neg     Each negative image on its own
%   impos   Each positive image with a list of foreground boxes
%
% Arguments
%   cls     Object class to get examples for
%   year    PASCAL dataset year

conf       = voc_config('pascal.year', year);

% dataset_fg = conf.training.train_set_fg
% dataset_bg = conf.training.train_set_bg
cachedir   = conf.paths.model_dir;
% VOCopts    = conf.pascal.VOCopts

try
  load([cachedir cls '_' conf.kitti_database '_' conf.kitti_year]);
catch
    
  % Positive examples from the foreground dataset
  pos      = [];
  impos    = [];
  numpos   = 0;
  numimpos = 0;
  dataid   = 0;
  
  for img_idx = 0:conf.nimages-1;
    
    tic_toc_print('%s: parsing positives (%s %s): %d/%d\n', ...
                  cls, conf.kitti_database, conf.kitti_year, img_idx, conf.nimages);
              
    % load labels
    objects = readLabels(conf.paths.label_dir,img_idx);
    
    % for all annotated objects do
    for obj_idx=1:numel(objects)

        if strcmpi(objects(obj_idx).type,cls) == 0
            continue
        end
        
        if objects(obj_idx).occlusion ~= 0
            continue
        end
        
        numpos = numpos +1;
        dataid = dataid +1;
        
        round_box = round([objects(obj_idx).x1 objects(obj_idx).y1 objects(obj_idx).x2 objects(obj_idx).y2]);
        bbox = [max(round_box(1),1), max(round_box(2),1), min(round_box(3),1242-1), min(round_box(4),375-1)];
%         bbox   = [ max(round(objects(obj_idx).x1) floor(objects(obj_idx).y1) floor(objects(obj_idx).x2) floor(objects(obj_idx).y2)];
        
        pos(numpos).im = sprintf('%s/%06d.png',conf.paths.image_dir,img_idx);
        pos(numpos).imdisp = sprintf('%s/%06d.png',conf.paths.disparity_dir,img_idx);

        pos(numpos).x1      = bbox(1);
        pos(numpos).y1      = bbox(2);
        pos(numpos).x2      = bbox(3);
        pos(numpos).y2      = bbox(4);
        
        pos(numpos).boxes   = bbox;
        pos(numpos).flip    = false;
        pos(numpos).trunc   = objects(obj_idx).truncation;
        pos(numpos).dataids = dataid;
        pos(numpos).sizes   = (bbox(3)-bbox(1)+1)*(bbox(4)-bbox(2)+1);
      
        % Create flipped example
        numpos  = numpos + 1;
        dataid  = dataid + 1;
        oldx1   = bbox(1);
        oldx2   = bbox(3);
        bbox(1) = conf.img.size(1) - oldx2 + 1;
        bbox(3) = conf.img.size(1) - oldx1 + 1;

        pos(numpos).im      = sprintf('%s/%06d.png',conf.paths.image_dir,img_idx);
        pos(numpos).imdisp  = sprintf('%s/%06d.png',conf.paths.disparity_dir,img_idx);
        pos(numpos).x1      = bbox(1);
        pos(numpos).y1      = bbox(2);
        pos(numpos).x2      = bbox(3);
        pos(numpos).y2      = bbox(4);
        pos(numpos).boxes   = bbox;
        pos(numpos).flip    = true;
        pos(numpos).trunc   = objects(obj_idx).truncation;
        pos(numpos).dataids = dataid;
        pos(numpos).sizes   = (bbox(3)-bbox(1)+1)*(bbox(4)-bbox(2)+1);
    end
    
    count = 0;
    for obj_idx= 1:numel(objects)
        if strcmpi(objects(obj_idx).type,cls) == 0
            count = count + 1;
        end
    end
    
    numimpos                = numimpos + 1;
    impos(numimpos).im      = sprintf('%s/%06d.png',conf.paths.image_dir,img_idx);
    impos(numimpos).imdisp  = sprintf('%s/%06d.png',conf.paths.disparity_dir,img_idx);
    impos(numimpos).boxes   = zeros(count, 4);
    impos(numimpos).dataids = zeros(count, 1);
    impos(numimpos).sizes   = zeros(count, 1);
    impos(numimpos).flip    = false;

    for obj_idx=1:numel(objects)
      dataid = dataid + 1;
%       bbox   = [floor(objects(obj_idx).x1) floor(objects(obj_idx).y1) floor(objects(obj_idx).x2) floor(objects(obj_idx).y2)];
      round_box = round([objects(obj_idx).x1 objects(obj_idx).y1 objects(obj_idx).x2 objects(obj_idx).y2]);
      bbox = [max(round_box(1),1), max(round_box(2),1), min(round_box(3),1242-1), min(round_box(4),375-1)];
        
      impos(numimpos).boxes(obj_idx,:) = bbox;
      impos(numimpos).dataids(obj_idx,1) = dataid;
      impos(numimpos).sizes(obj_idx)   = (bbox(3)-bbox(1)+1)*(bbox(4)-bbox(2)+1);
    end

    % Create flipped example
    numimpos                = numimpos + 1;
    impos(numimpos).im      = sprintf('%s/%06d.png',conf.paths.image_dir,img_idx);
    impos(numimpos).imdisp  = sprintf('%s/%06d.png',conf.paths.disparity_dir,img_idx);
    impos(numimpos).boxes   = zeros(count, 4);
    impos(numimpos).dataids = zeros(count, 1);
    impos(numimpos).sizes   = zeros(count, 1);
    impos(numimpos).flip    = true;
    unflipped_boxes         = impos(numimpos-1).boxes;
    
    for obj_idx=1:numel(objects)
      dataid  = dataid + 1;
      bbox    = unflipped_boxes(obj_idx,:);
      oldx1   = bbox(1);
      oldx2   = bbox(3);
      bbox(1) = conf.img.size(1) - oldx2 + 1;
      bbox(3) = conf.img.size(1) - oldx1 + 1;

      impos(numimpos).boxes(obj_idx,:) = bbox;
      impos(numimpos).dataids(obj_idx,1) = dataid;
      impos(numimpos).sizes(obj_idx)   = (bbox(3)-bbox(1)+1)*(bbox(4)-bbox(2)+1);
    end
  end
  
  % Negative examples from the background dataset
  neg    = [];
  numneg = 0;
  
  for img_idx = 0:conf.nimages-1;
    tic_toc_print('%s: parsing negatives (%s %s): %d/%d\n', ...
                  cls, conf.kitti_database, conf.kitti_year, img_idx, conf.nimages);
              
    % load labels
    objects = readLabels(conf.paths.label_dir,img_idx);
  
    class_found = false;
    for obj_idx=1:numel(objects)
        if strcmpi(objects(obj_idx).type,cls) == 0
            class_found = true;
            break;
        end
    end

    if ~class_found
        dataid             = dataid + 1;
        numneg             = numneg+1;
        neg(numneg).im     = sprintf('%s/%06d.png',conf.paths.image_dir,img_idx);
        neg(numneg).imdisp = sprintf('%s/%06d.png',conf.paths.disparity_dir,img_idx);
        neg(numneg).flip   = false;
        neg(numneg).dataid = dataid;
    end    
  end
  
  save([cachedir cls '_' conf.kitti_database '_' conf.kitti_year], 'pos', 'neg', 'impos');
end
