%
% IMPORTANT NOTE: The "label_type" parameter denotes the data type used for representing MRF labels in the C++ code.
%                 THEREFORE, THIS PARAMETER SHOULD ALWAYS MATCH THE CORRESPONDING TYPE USED IN THE C++ FASTPD CODE !!!
%                 Its default value is 'uint8' because the default data type used in the C++ code is unsigned char.
%
% unary       : matrix of size num_MRF_nodes x num_labels that contains the unary potentials
% edges       : matrix of size 2 x num_MRF_edges that contains a pair of MRF-node indices at each column 
%               (the indexing of MRF nodes starts from 0).
% edge_weights: a vector containing one weight per MRF edge (MRF edge-potential = edge-weight * pairwise-label-distance)
% dist        : matrix of size num_labels x num_labels containing the pairwise distances between labels
% max_iters   : maximum number of iterations
%
function labeling = FastPD_wrapper( unary, edges, edge_weights, dist, max_iters, label_type )

	if nargin < 6
		label_type = 'uint8';
	end

	input = 'input.bin';
	output = 'output.bin';

	fp = fopen( input, 'wb' );
	if fp == -1
		error( 'cannot write file' );
	end;

	numpoints = size(unary, 1);
	numedges  = size(edges, 2);
	numlabels = size(unary, 2);

	fwrite( fp, numpoints    , 'int32' );
	fwrite( fp, numedges     , 'int32' );
	fwrite( fp, numlabels    , 'int32' );
	fwrite( fp, max_iters    , 'int32' );
	fwrite( fp, unary        , 'int32' );
	fwrite( fp, edges        , 'int32' );
	fwrite( fp, dist         , 'int32' );
	fwrite( fp, edge_weights , 'int32' );
	fclose(fp);

	eval(['!FastPD.exe ' input ' output.bin']);

	fid = fopen( output, 'rb' );
	if fid == -1
		error( ['Cannot open file ' output] );
	end
	labeling = fread( fid, label_type );
	fclose(fid);

