function model_handle = show_combined_model(combined_model, ref_points_vec, n_sds, max_modes, title, image_width)
% SHOW_COMBINED_MODEL: Shows the combined model for each one of the modes.
%
% Code written by Katherine Smith, 2003
%
%    GENERAL
%
%      show_combined_model(combined_model, ref_points_vec, n_sds, max_modes, title)
%
%    INPUT/S
%
%      -combined_model:
%           The combined model to be shown.
%
%      -ref_points_vec:
%           Vector of reference points.
%
%      -n_sds:
%           Number of standard deviations to be displayed.
%
%      -max_modes: 
%           Maximum number of modes?
%
%      -title:
%           A title for the figure to be displayed.
%
%           
%    OUTPUT/S
%
%      -model_handle:
%           The figure handle
%
%    PENDING WORK
%
%      -
%
%    KNOWN BUG/S
%
%      -None.
%
%    COMMENT/S
%
%      -Number of standard variation input seems to contol the number of
%       figures shown. For 3, it will generate it for -3,-2,-1,-,1,2,3.
%
%    RELATED FUNCTION/S
%
%      SHOW_INTENSITY_MODEL, SHOW_SHAPE_MODEL
%
%    ABOUT
%
%      -Created:     November 23rd, 2003
%      -Last update: March 18th, 2004
%      -Revision:    0.0.5
%      -Author:      R. S. Schestowitz, University of Manchester
% ==============================================================

% visualise combined model
% intensities = intensity_mean + intensity_pcs * intensity_combined_pcs * combined_params
% - gives shape free image
% shape = shape_mean + shape_pcs * shapeweight^-1 * shape_combined_pcs * combined_params
% - warp the shape free image to get the final image

shape_model = combined_model.shape_model;
                   % get shape model from the combined model
intensity_model = combined_model.intensity_model;
                   % get intensity model from the combined model
shapeweight = repmat(combined_model.shape_weight, size(shape_model.pcs,1),1);
                   % get the weight W of the shape.
n_modes = min(size(combined_model.pcs,2),max_modes);
                   % get maximum modes UNLESS less than asked for
                   % are available in the model.
n_shape_modes = size(shape_model.pcs,2);
                   % Get number of shape model modes.
fig = figure('Name',([title]));
                   % name the figure according to the input
                   
figure_handle = figure(fig); 
hold on;

for m=1:n_modes,
  for index = -n_sds:n_sds,
                   % for all the modes and all the standard deviation range
                   % (see related function for similarity in code)
    example_shape_params = zeros(1,1);
    example_intensity_params = zeros(1,1);
                   % set shape and intesity parameters to 0
    params = zeros(1,size(combined_model.params,2));
                   % set all parameters to 0
    params(m) = index * combined_model.sd(1,m);
                   % CODE WAS MODIFIED HERE
    
    example = construct_model_example(params, combined_model, ref_points_vec);
                   % construct example from the combined model
                   % with mode set to appropriate deviation
%    figure(fig),subplot(1,n_sds*2+1,i+n_sds+1),plot(example),axis([0 50 0 1.2]);
    
    subplot(n_modes, n_sds * 2 + 1, (m - 1) * (n_sds * 2 + 1) + index + n_sds + 1);
    plot(example);
    axis([0 image_width -1 1.4]);
                   % and display that example
                   
  end
end
text(1,1,'Combined Model');
hold off;
model_handle = figure(figure_handle);