function [score, warped_points] = eval_mi_cps_warp(params, warp_centre, r, ref_points, start_image, ref_image, number_of_bins)
% EVAL_MI_CPS_WARP: get similarity score between CPS warped image and
%                    reference. (Evaluate the clamped-plate spline warp using
%                    mean-squared-differences)
%
% Code written by Katherine Smith, 2003
%
%    GENERAL
%
%       [score, warped_points] = eval_mi_cps_warp(params, warp_centre,
%                                r, ref_points, start_image, ref_image)
%
%    INPUT/S
%
%      -params:
%           The paramters of the warps.
%
%           params(1): warp centre
%           params(2): r
%           params(3): d
%
%      -warp centre:
%           Not in parameters already??
%
%      -r:
%           Not in parameters already??
%
%      -start_points_vec:
%           Points before CPS warp.
%
%      -start_image_vec:
%           Image before CPS warp.
%
%      -ref_image_vec:
%           The reference image to be compared to.
%           
%    OUTPUT/S
%
%      -score:
%           The similarity score w.r.t. the reference image. 
%           (MSD)
%
%      -warped_points:
%           The position of points after warping.
%
%
%    PENDING WORK
%
%      -
%
%    KNOWN BUG/S
%
%      -None.
%
%    COMMENT/S
%
%      -
%
%    RELATED FUNCTION/S
%
%      EVAL_MI_CPS_WARP
%
%    ABOUT
%
%      -Created:     November 23rd, 2003
%      -Last update: Novermber 27th, 2003
%      -Revision:    0.0.2
%      -Author:      R. S. Schestowitz, University of Manchester
% ==============================================================

d = params; 
      % copy parameters
start_points = ref_points;
max_d = 0.67; 
      % maximum parameter value allowed??

if (any((abs(d) > max_d)) | any(((warp_centre-r <-1) | (warp_centre+r > 1)))) 

      % ensure for all paramters p,-0.67<p<0.67 and also
      % warp center and radius are not out of normalisation
      % range 1. All must be in bounds

score = realmax; 
      % if data is invalid, set to maximum <Real> value

else 
      % if inputs okay
  for i=1:size(params,2) 
      % for all parameters
    warped_points = cps_warp_1d(start_points,warp_centre(i),r(i),d(i));
      % apply warp
    start_points = warped_points;
      % proceed with warped point to next iteration
  end
  warped_image = interp1(ref_points,start_image,warped_points, 'linear',0);
      % record warped image
  score = mi(ref_image, warped_image, number_of_bins); 
      % score according to msd
end