function tcp = scale_legs_y(scp, tcp, factors, scaling_options, selIdx_legs, scp_kpis, tcp_kpis, file_pointers, format_options)
% scales the legs in the y direction.
%
% :param scp:                      Source control points
% :param tcp:                      Current target control points
% :param factors:                  Scaling factors
% :param scaling_options:          Settings for the scaling
% :param selIdx_legs:              Indicies for the cps associated with the legs
% :param scp_kpis:                 Key measurements based on scp
% :param tcp_kpis:                 Key measurements based on tcp prior to this step
% :param file_pointers:            File pointers for I/O operations
% :param format_options:           Misc formating options
%
% :return tcp:                     Updated tcp positions
%
% Created by : ABR august@lightness.eu
% Date : 2024-06-13
selIdx_legs_temp = false(size(tcp));
selIdx_legs_temp(selIdx_legs) = true;
selIdx_legs = selIdx_legs_temp;

leg_vectors_to_groin = tcp(selIdx_legs,:) - tcp_kpis.C3;
if scaling_options.legs_y == 1
  translation_of_yneg_shoulder = tcp_kpis.C5 - scp_kpis.C5;
  translation_of_ypos_shoulder = tcp_kpis.C6 - scp_kpis.C6;

  selIdx_legs_yneg = selIdx_legs & (tcp(:,2) < 0.0);
  selIdx_legs_ypos = selIdx_legs & (tcp(:,2) > 0.0);

  tcp(selIdx_legs_yneg,2) = tcp(selIdx_legs_yneg,2) + translation_of_yneg_shoulder(2);
  tcp(selIdx_legs_ypos,2) = tcp(selIdx_legs_ypos,2) + translation_of_ypos_shoulder(2);

elseif scaling_options.scale_torso_in_plane_method == 1
  disp('Entered scaling_options.scale_torso_in_plane_method == 1')
  tcp(selIdx_legs, [1 2]) = tcp_kpis.C3([1 2]) + factors(3) .* leg_vectors_to_groin(:,1:2);
elseif scaling_options.scale_torso_in_plane_method == 2 || scaling_options.scale_torso_in_plane_method == 3
  disp('Entered scaling_options.scale_torso_in_plane_method == 2 | 3')
  tcp(selIdx_legs, [2]) = tcp_kpis.C3(2) + factors(3) .* leg_vectors_to_groin(:,2);
else
  disp(['scaling_options.scale_torso_in_plane_method = ' num2str(scaling_options.scale_torso_in_plane_method) ' is not supported.'])
  disp(stop)
end



if format_options.plot_flag
  figure()
  scatter(scp(:,1), scp(:,2))
  hold on
  axis equal
  scatter(tcp(:,1), tcp(:,2))
  scatter( 0 , 0 , 'sr', 'filled')
  xlabel('X'); ylabel('Y');
  print([file_pointers.plot_dir 'XY_scale5'],format_options.pngprintOptions{:})

  figure()
  scatter(scp(:,1), scp(:,3))
  hold on
  axis equal
  scatter(tcp(:,1), tcp(:,3))
  xlabel('X'); ylabel('Z');
  scatter( 0 , 0 , 'sr', 'filled')
  print([file_pointers.plot_dir 'XZ_scale5'],format_options.pngprintOptions{:})

  figure()
  scatter(scp(:,2), scp(:,3))
  hold on
  axis equal
  scatter(tcp(:,2), tcp(:,3))
  scatter( 0 , 0 , 'sr', 'filled')
  xlabel('Y'); ylabel('Z');
  print([file_pointers.plot_dir 'YZ_scale5'],format_options.pngprintOptions{:})
end
