% Copyright (c) 2015 The Johns Hopkins University/
% Applied Physics Laboratory. (JHU/APL)
% 
% This material was developed by JHU/APL under U.S. Government contract.
% The U.S. Government has Unlimited Rights in this material per
% DFARS 252.227-7013/7014, as appropriate.
% 
% For any other permission, please contact the Legal Office at JHU/APL.
%

if ~exist('lasdata', 'file')
    addpath('../3rdParty')
end


% INPUTS

% Reference Data  (All GeoTIFFs references to same UTM zone)
% 1) DSM  
% 2) DTM
% 3) Building Labels

% Test Data
% 1) Dense LAS  (LAS)
% 2) Buildng Labels  (GeoTIFF)
% 3) Registraton Offset  (Text file)

if isunix
    dirRootAosShare      = '/mnt/aos/AOSShare/';
else
    dirRootAosShare      = '//dom1/AOSShare/';    
end

FILL_VALUE     = -10000;

BUILDING_VALUE_REF  = 6;  
BUILDING_VALUE_TEST = 240;  


% Used if test model is GEOTIFF DSM
MODEL_FILL_VALUE    = -10000;
% Used if test model is LAS point cloud
MODEL_DSM_METHOD    = 1;  %  1:MAX, 2:MIN, 3:MEAN


DO_PLOTS = 1;
SAVE_PLOTS = 0;
SAVE_FIGS = 0;


if isunix
    dirRootAosShare      = '/mnt/aos/AOSShare/';
    dirRootU             = '/mnt/u/';
else
    dirRootAosShare      = 'A:/AOSShare/';
    dirRootU             = 'U:';
end


saveDir = [];

if DO_PLOTS
    caxDTM = [15 40];
    caxObj = [10 30] - 10;
end


testModelDir         = fullfile(dirRootAosShare, 'IARPA-CORE3D/SiteDataPackage/SanFernando/OpenSourceMVS/PCs_registered');
testMaskDir          = fullfile(dirRootAosShare, 'IARPA-CORE3D/SiteDataPackage/SanFernando/OpenSourceMVS/PCs_registered/shr3d');

refDataDir           = fullfile(dirRootAosShare, 'SOCOM-3D-Data-Engineering/2016/160921-TEXPERT-Analysis/Data for Tests/SanFernando/50cm');
refMaskDir           = fullfile(dirRootAosShare, 'IARPA-CORE3D/Workspace/Results/Ground Truth/San Fernando/');

    
% ------
refDSMFilename       = fullfile(refDataDir, 'Master2.las_DSM.tif');
refDTMFilename       = fullfile(refDataDir, 'Master2.las_DTM.tif');
refMaskFilename      = fullfile(refMaskDir, 'SanFernando_AOI2_GT_refined.tif');


testName = 'JHUAPL';
testModelFilename    = fullfile(testModelDir,  [testName '.las_adj.las']);
testMaskFilename     = fullfile(testMaskDir, [testName '.las_adj.las_buildings.tif']);



fprintf('\nReference DSM GEOTIFF file:\n')
fprintf('\t%s\n', refDSMFilename)

fprintf('\nReference DTM GEOTIFF file:\n')
fprintf('\t%s\n', refDTMFilename)

fprintf('\nReference footprint GEOTIFF file:\n')
fprintf('\t%s\n', refMaskFilename)

fprintf('\nTest densse LAS point cloud Model:\n')
fprintf('\t%s\n', testModelFilename)

fprintf('\nTest GEOTIFF footprint GEOTIFF file:\n')
fprintf('\t%s\n', testMaskFilename)


%%
% ----- Load Reference Data -------------------------------------------
fprintf('START Prepping Reference Data...'); refftic = tic;

if DO_PLOTS > 0
    DO_PLOTS = 200;
end

parms = [];
params.DSM_FILL_VALUE  = FILL_VALUE;  % TODO
params.DTM_FILL_VALUE  = FILL_VALUE;  % TODO
params.MASK_TRUE       = BUILDING_VALUE_REF;      % TODO
params.DO_PLOTS        = DO_PLOTS;
params.SAVE_PLOTS      = SAVE_PLOTS;
params.SAVE_FIGS       = SAVE_FIGS;
params.SAVE_DIR        = saveDir;
params.REF_IMAGE       = 3;

[refObj, refMask, refDTM, refInfo] = loadReferenceData(refDSMFilename, refDTMFilename, refMaskFilename, params);


tRefPrepTime = toc(refftic);
fprintf('END Prepping Reference Data...     (%0.3f)\n', tRefPrepTime);
 

%%
% ----- Load Test Data -------------------------------------------
fprintf('START Prepping Test Data...\n'); testtic = tic;

if DO_PLOTS > 0
    DO_PLOTS = 200;
end

parms = [];
params.DSM_FILL_VALUE  = MODEL_FILL_VALUE;
params.MASK_TRUE       = BUILDING_VALUE_TEST;     
params.DO_PLOTS        = DO_PLOTS;
params.SAVE_PLOTS      = SAVE_PLOTS;
params.SAVE_FIGS       = SAVE_FIGS;
params.SAVE_DIR        = saveDir;
params.MODEL_NAME      = testName;
params.DSM_METHOD      = MODEL_DSM_METHOD;    
    
offsets = [0 0 0];
% offsets = filename.txt;

[testObj, testMask, testRaster] = loadTestData(testModelFilename, testMaskFilename, refInfo, refDTM, offsets, params);


tTestPrepTime = toc(testtic);
fprintf('END Prepping Test Data...     (%0.3f)\n', tTestPrepTime);

    
%% Compute Metrics


% ----- Load Performer Data -------------------------------------------
fprintf('START Metric Computation...\n'); metrictic = tic;

if DO_PLOTS > 0
    DO_PLOTS = 500;
end    
    
parms = [];
params.DO_PLOTS        = DO_PLOTS;
params.SAVE_PLOTS      = SAVE_PLOTS;
params.SAVE_FIGS       = SAVE_FIGS;
params.SAVE_DIR        = saveDir;
params.MODEL_NAME      = testName;
[metrics2D,  metrics3D,  metrics3Ds] = compute25DMetrics( testMask, testObj, refMask, refObj, refInfo, params);
 
    
tMetricTime = toc(metrictic);
fprintf('END Metric Computation...     (%0.3f)\n', tMetricTime);
    

fprintf('DONE (%0.3f)\n', tRefPrepTime+tTestPrepTime+tMetricTime);


