function [caS2Centroid,SourceLocations,caRGBCentroid] = ObjectSpecificCentroids(yxSizes, BBoxs, SSIdxs ,savename);
%function [caS2Centroid,SourceLocations,caRGBCentroid] = ObjectSpecificCentroids(yxSizes, BBoxs, SSIdxs ,savename);
%
%intent:  record a list of S2 centroids from selected locations in the StreetScenesDatabase.
%         This constitutes a "object-specific" s2 representation
%
%pseudocode:  for each of the yxsizes, choose a random BBox, and corresponding SSIdx 
%             and a random location within this BBox

HelpTextInfo = GetHelpTextInfo;
nS2 = size(yxSizes,1);
load('CAfilelist_FilterSS');%-->CAfilelist;
seedrand;

tic
for i =1:nS2
   fprintf('%d of %d.  %d seconds elapsed\r',i,nS2,round(toc));
   p = randperm(length(SSIdxs));
   SSIdx = SSIdxs(p(1));
   BBox = BBoxs(p(1),:);
   SourceLocations(i).SSIdx = SSIdx;
   SourceLocations(i).ObjRGBBBox = BBox;
   %C1Img = GetC1Img(SSIdx,'all');
   C1Img = GetC1Img(SSIdx,1);
   RGBImg = imread(CAfilelist{SSIdx}.imagename);
   s = yxSizes(i,:);
   SourceLocations(i).size = s;
   yx_RelativeCenterInBBox = rand(1,2)*.8 + .2;
   SourceLocations(i).yx_RelativeCenterInBBox = yx_RelativeCenterInBBox;
   C1BBox = CoordinateRespace(BBox, size(RGBImg), size(C1Img),'bbox');
   SourceLocations(i).ObjC1BBox = C1BBox;
   C1PartBBox = round(GetC1BBox(yx_RelativeCenterInBBox,C1BBox,s));
   RGBPartBBox = round(CoordinateRespace(C1PartBBox, size(C1Img), size(RGBImg), 'bbox'));
   SourceLocations(i).C1PartBBox = C1PartBBox;
   SourceLocations(i).RGBPartBBox = RGBPartBBox;
   caS2Centroid{i} = imcrop_pad(C1Img, C1PartBBox,'symmetric');
   caRGBCentroid{i} = imcrop_pad(RGBImg, RGBPartBBox,'symmetric');
   
   %%DebuggingStuff
   %figure(1);
   %imshow(RGBImg);
   %figure(2);
   %imshow(C1Img(:,:,1:3)*4);
   %figure(1)
   %OrangeBox(BBox);
   %OrangeBox(RGBPartBBox);
   %figure(2)
   %OrangeBox(C1BBox);
   %OrangeBox(C1PartBBox);
   %keyboard;
end
fprintf('\n');
if(nargin > 2);
  save(savename, 'caS2Centroid','SourceLocations','HelpTextInfo','caRGBCentroid');
end

function yxTopLeft = GetRandTopLeft(AvailPos);
seedrand, r = rand(1,2);
yxTopLeft = floor(r .* AvailPos) + 1;

function S2c = CropC1(C1Img, sSource);
a = (1:sSource.size(1)) + sSource.yxTopLeft(1) - 1;
b = (1:sSource.size(2)) + sSource.yxTopLeft(2) - 1;
S2c = C1Img(a,b,:);

function C1PartBBox = GetC1BBox(yx_r,BBox,s)
realcenter = yx_r .* [BBox(4),BBox(3)] + [BBox(2),BBox(1)];
topleft = round(realcenter - s/2);
C1PartBBox = [topleft(2), topleft(1), s(2)-1, s(1)-1];
