% This file contains the scripts needed to run the LabelMe3D algorithm.
% If you use this toolbox, please cite the following paper:
%
% B. C. Russell and A. Torralba. Building a Database of 3D Scenes from
% User Annotations. In CVPR, 2009.
%
% To start, change the paths below so that they point to the location of
% the LabelMe and LabelMe3D MATLAB Toolboxes.

% Add LabelMe and LabelMe3D MATLAB Toolboxes to your path:
addpath '~/work/MatlabLibraries/LabelMeToolbox';
addpath '~/work/MatlabLibraries/LabelMe3dToolbox';

% Set this if you want to write the outputs:
OUTDIR = '';

% LabelMe3D structures:
load Params3D.mat;

% Get image and LabelMe annotation:
fname_jpg = 'example.jpg';
fname_xml = 'example.xml';

img = imread(fname_jpg);
annotation = getfield(loadXML(fname_xml),'annotation');
  
% Recover 3D scene components:
imageSize = [size(img,1) size(img,2)];
[annotation,valid] = Recover3DSceneComponents(annotation,Params3D,imageSize);

if valid
  % Get XYZN maps:
  [Xmap,Ymap,Zmap,Nmap] = getXYZmaps(annotation);
  
  % Plot XYZ maps:
  figure;
  subplot(1,3,1); plotXmap(Xmap); title('X map');
  subplot(1,3,2); plotYmap(Ymap); title('Y map');
  subplot(1,3,3); plotZmap(Zmap); title('Z map');
  
  % Plot polygon and edge types:
  seg = plotPolyEdgeTypes(annotation,img,'valid');
  figure;
  imshow(seg);
  title('Polygon and edge types');
  
  if ~isempty(OUTDIR)
    % Write 3D XML file:
    writeXML3D(annotation,fullfile(OUTDIR,'example.out.xml'));
  
    % Create object meshes (we need this for VRML and book outputs):
    annotation = InflateSceneNew(annotation);
    
    % Create VRML file:
    vrmlfilename  = 'example.wrl';
    imwrite(img,fullfile(OUTDIR,'example.jpg'),'jpg','quality',95);
    LM2VRMLfill(annotation,OUTDIR,vrmlfilename,'example.jpg',img);
    
    % Create book
    BOOK = get3DBook(annotation,img);
    imwrite(BOOK,fullfile(OUTDIR,'example_book.jpg'),'jpg','quality',100);

    % Write XYZN maps:
    writeXmap(Xmap,fullfile(OUTDIR,'example.X.png'));
    writeYmap(Ymap,fullfile(OUTDIR,'example.Y.png'));
    writeZmap(Zmap,fullfile(OUTDIR,'example.Z.png'));
    writeNmap(Nmap,fullfile(OUTDIR,'example.N.png'));
  end
end
