function [param_list, warped_points, warped_image, score] = optimise_warps_model(image_set, points_set, 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, show_warps_while_optimising, precision_required)
% OPTIMISE_WAPRS_MODEL: 
%
% Code written by Katherine Smith, 2003
%
%    GENERAL
%
%      [param_list, warped_points, warped_image, score] = 
%         optimise_warps_model(image_set, points_set, unwarped_image,
%         unwarped_points, spline_type, placement_method, n_points,
%         verbose, do_plot, subplot_fig, pane,
%         image_width, number_of_images, verbose_score)
%
%    INPUT/S
%
%      -X:
%          X
%           
%    OUTPUT/S
%
%      -X:
%           X
%
%    PENDING WORK
%      
%      
%
%    KNOWN BUG/S
%
%      
%
%    COMMENT/S
%
%      
%
%    RELATED FUNCTION/S
%
%      OPTIMISE_WARPS...
%
%    ABOUT
%
%      -Created:     November 23rd, 2003
%      -Last update: January 23rd, 2004
%      -Revision:    0.1.5
%      -Author:      R. S. Schestowitz, University of Manchester
% ==============================================================

% optimise the warps model. Similar to <optimise_warps>
      
[grid, steps] = setup_grid(2, unwarped_image, unwarped_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

start_points = unwarped_points;

param_list = [];
start_image = unwarped_image;
%last_score = msd(ref_image, unwarped_image);
if(strcmp(spline_type,'single_point')),
  last_score = eval_model_cps_warp([0],grid(1),steps(1), unwarped_image, unwarped_points, image_set, points_set, n_modes, start_points, model_evaluation_method, weighting_normalisation_method, shape_weight, variation_kept, show_warps_while_optimising);
  for i=1:size(grid)
    [params, score] = fminsearch(@eval_model_cps_warp, [0], optimset('Display', verbose, 'TolX', precision_required), grid(i),steps(i), unwarped_image, unwarped_points, image_set, points_set, n_modes, start_points, model_evaluation_method, weighting_normalisation_method, shape_weight, variation_kept, show_warps_while_optimising);
    param_list(size(param_list,1) + 1,:).d = params * steps(i);
    param_list(size(param_list,1),:).r = steps(i);
    param_list(size(param_list,1),:).centre = grid(i);
    start_points = cps_warp_1d(start_points, grid(i), steps(i), params);
    start_image = interp1(unwarped_points,unwarped_image,start_points, 'linear',0);
  end
  warped_points = start_points;
  warped_image = start_image;
elseif(strcmp(spline_type,'multi_point')),
  warped_grid = grid;
  warp_params.green = 'biharmCPS';
  last_score = eval_model_multi_warp(zeros(size(grid)),grid,unwarped_image, unwarped_points, image_set, points_set, n_modes, start_points, warp_params, model_evaluation_method, weighting_normalisation_method, shape_weight, variation_kept, show_warps_while_optimising);
  [params,score, data] = fminsearch(@eval_model_multi_warp,zeros(size(grid)),optimset('Display', verbose, 'TolX', precision_required),warped_grid,unwarped_image, unwarped_points, image_set, points_set, n_modes, start_points, warp_params, model_evaluation_method, weighting_normalisation_method, shape_weight, variation_kept, show_warps_while_optimising);
  param_list(size(param_list,1) + 1,:).d = params;
  warped_grid = grid + params;
  warped_points = nrr_trans_1d(start_points, grid, warped_grid, warp_params,[]);
  warped_image = interp1(unwarped_points,unwarped_image,warped_points, 'linear',0);
else
  error('Unknown spline type');    
end

% NOTE: Changes might be applied with objective function value that is
% higher

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

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
