function [param_list, warped_points, warped_image, score] = optimise_groupwise(ref_image, ref_points, unwarped_image, unwarped_points, spline_type, placement_method, n_points, verbose, do_plot, subplot_fig, pane, column, image_width, number_of_images, verbose_score, model_evaluation_method, n_modes, weighting_normalisation_method, shape_weight, variation_kept, pdf_evalution_method, kernel, precision_required)
% OPTIMISE_GROUPWISE: Uses PDF's to minimise group-wise complexity
%
%    GENERAL
%
%      [param_list, warped_points, warped_image, score] = 
%       optimise_groupwise(ref_image, ref_points, unwarped_image, 
%       unwarped_points, spline_type, placement_method, n_points, 
%       verbose, do_plot, subplot_fig, pane, column, image_width, 
%       number_of_images, verbose_score, model_evaluation_method, 
%       n_modes, weighting_normalisation_method, shape_weight, variation_kept)
%
% 
%    INPUT/S
%
%      -X:
%          X
%           
%    OUTPUT/S
%
%      -X:
%           X
%
%    PENDING WORK
%      
%      
%
%    KNOWN BUG/S
%
%      
%
%    COMMENT/S
%
%      
%
%    RELATED FUNCTION/S
%
%      
%
%    ABOUT
%
%      -Created:     February 9th, 2004
%      -Last update: February 2004
%      -Revision:    0.1.0
%      -Author:      R. S. Schestowitz, University of Manchester
% ==============================================================

[grid, steps] = setup_grid(2, unwarped_image, ref_points, placement_method, n_points);

if (do_plot),
  figure(subplot_fig);
  subplot(number_of_images, column, pane, 'replace');
  plot(unwarped_image);
  axis([0 image_width 0 1]);
  drawnow;
end

warp_params.green = 'biharmCPS';
param_list = [];
start_image = unwarped_image;
start_points = unwarped_points;
%last_score = msd(ref_image, unwarped_image);
if(strcmp(spline_type,'single_point'))
  last_score = eval_groupwise_single_point(zeros(size(grid)),grid, steps, unwarped_image, unwarped_points, ref_image, ref_points, n_modes, start_points, model_evaluation_method, weighting_normalisation_method, shape_weight, variation_kept, pdf_evalution_method, kernel);
  [params, score] = fminsearch(@eval_groupwise_single_point,zeros(size(grid)),optimset('Display',verbose,'TolX', precision_required),grid,steps,start_image, start_points, ref_image, ref_points, n_modes, start_points, model_evaluation_method, weighting_normalisation_method, shape_weight, variation_kept, pdf_evalution_method, kernel);
  param_list(size(param_list,1)+1).d = params;
  [dummy,start_points] = eval_groupwise_single_point(params,grid,steps, start_image, start_points, ref_image, ref_points, n_modes, start_points, model_evaluation_method, weighting_normalisation_method, shape_weight, variation_kept, pdf_evalution_method, kernel);
  start_image = interp1(unwarped_points,unwarped_image,start_points, 'linear',0); 
elseif(strcmp(spline_type,'multi_point'))
  warped_grid = grid;
  warp_params.green = 'biharmCPS';
  last_score = eval_groupwise_multi_point(zeros(size(grid)),grid, unwarped_image, unwarped_points, ref_image, ref_points, n_modes, start_points, warp_params, model_evaluation_method, weighting_normalisation_method, shape_weight, variation_kept, pdf_evalution_method, kernel);
  [params, score] = fminsearch(@eval_groupwise_multi_point,zeros(size(grid)),optimset('Display',verbose,'TolX', precision_required),grid, unwarped_image, unwarped_points, ref_image, ref_points, n_modes, start_points, warp_params, model_evaluation_method, weighting_normalisation_method, shape_weight, variation_kept, pdf_evalution_method, kernel);
  param_list(size(param_list,1)+1,:).d = params;
  warped_grid = grid + params;
  start_points = nrr_trans_1d(start_points, grid, warped_grid, warp_params,[]);
  start_image = interp1(unwarped_points,unwarped_image,start_points, 'linear',0);
else
  error('Unknown spline type');  
end


if (verbose_score == 1),
    disp(['Initial score: ',num2str(last_score),' Final score: ', num2str(score), ' Difference: ', num2str(last_score - score)]);
end

warped_points = start_points;
warped_image = start_image;


if (do_plot),
  figure(subplot_fig);
  subplot(number_of_images, column ,pane, 'replace');
  plot(warped_image);
  axis([0 image_width 0 1]);
  title(num2str(score));
  drawnow;
end