function scale_cad_by_scp(baby_measurements, scaling_options, export_folder, file_pointers, format_options, selIdx, key_points)
% Scales a set of source control points to match some key measurements.
%
%
% created by : ABR august@lightness.eu
% date : 2024-06-11

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read data from file
%
k_array = import_k_file_except_nodes(file_pointers.k_file_to_mod);
nMat = import_nodes_from_k_file(file_pointers.k_file_to_mod);
eMat = import_elements_from_k_file(file_pointers.k_file_to_mod);
scp = dlmread(file_pointers.scp_file_to_read);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Center and align the body
%
nMat_original = nMat;
scp_original = scp;
if scaling_options.center_and_align_body
    [nMat, scp] = center_and_align_body(nMat, scp);
else
    disp('Performing no isotropic scaling will lead to poor results. Set scaling_options.center_and_align_body = true .')
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Isotropic scaling
%
if scaling_options.isotropic_match_length
    node_kpis_ref = calculate_key_measurements(nMat, key_points);
    ref_length = node_kpis_ref.total_length;
    isotropic_scale_factor = baby_measurements(1) / ref_length;

    if format_options.step_by_step_reporting
      fprintf('-----> Isotropic length matching ON\n')
      fprintf('-----> Reference total length : %10.1f\n', ref_length)
      fprintf('-----> Isotropic scale factor : %10.3f\n', isotropic_scale_factor)
    end
    nMat(:,2:4) = nMat(:,2:4) .* isotropic_scale_factor;
    scp = scp .* isotropic_scale_factor;
else
    disp('Performing no isotropic scaling will lead to poor results. Set scaling_options.isotropic_match_length = true .')
end

if format_options.plot_flag
  figure()
  scatter(nMat_original(:,2),nMat_original(:,4),'displayname','Original nodal positions','.')
  axis equal; hold on;
  scatter(nMat(:,2),nMat(:,4),'displayname','Nodes rotated & centered','.')
  legend('location','northeastoutside')
  print([file_pointers.plot_dir 'centered_and_aligned_body'],format_options.pngprintOptions{:})
end

node_kpis = calculate_key_measurements(nMat, key_points);
scp_kpis = calculate_key_measurements_control_points(scp, key_points);
baby_volume = calculate_volume_from_surface_mesh(nMat, eMat);

if format_options.step_by_step_reporting
  disp('-----> CALCULATING THE SIZE OF THE ISOTROPICALLY SCALED MODEL')
  report_current_cp_measurements( node_kpis, scp_kpis, baby_measurements, {'Nodes','scp','Target'});
  disp('-----> MASS, REFERENCE')
  fprintf('     Estimated mass (rho = 1000 kg/m^3) = %10.0f g \n', 1000.0 * baby_volume *1e-9 * 1000.0)
end


if format_options.align_bodies
  % Position the groin at [0, 0, 0] for easy visualization
  nMat(:,2:4) = nMat(:,2:4) - node_kpis.C3(:,2:4);
  scp = scp - node_kpis.C3(:,2:4);
  node_kpis = calculate_key_measurements(nMat, key_points);
  scp_kpis = calculate_key_measurements_control_points(scp, key_points);
end

% Calculate the factors
tcp = scp;
factors = baby_measurements(1:4) ./ [scp_kpis.total_length scp_kpis.shoulder_to_groin scp_kpis.shoulder_width scp_kpis.head_circumference];


[tcp , selIdx_temp] = scale_torso_z(scp, tcp, factors, scp_kpis, scaling_options, selIdx.torso, selIdx.arms, file_pointers, format_options);
tcp_kpis_T1 = calculate_key_measurements_control_points(tcp, key_points);
if format_options.step_by_step_reporting
  disp('-----> LONGITUDINAL TORSO SCALING')
  report_current_cp_measurements(scp_kpis, tcp_kpis_T1, baby_measurements, {'scp','tcp','Target'});
end




tcp = scale_torso_xy(scp, tcp, factors, scaling_options, baby_measurements(5), baby_volume, selIdx_temp, file_pointers, format_options);
tcp_kpis_T2 = calculate_key_measurements_control_points(tcp, key_points);
if format_options.step_by_step_reporting
  disp('-----> CIRCUMFERENTIAL TORSO SCALING')
  report_current_cp_measurements(scp_kpis, tcp_kpis_T2, baby_measurements, {'scp','tcp','Target'});
end



tcp = scale_head_circumference(scp, tcp, factors(4), selIdx.head, scp_kpis, file_pointers, format_options);
tcp_kpis_T3 = calculate_key_measurements_control_points(tcp, key_points);
if format_options.step_by_step_reporting
  disp('-----> CIRCUMFERENTIAL HEAD SCALING')
  report_current_cp_measurements(scp_kpis, tcp_kpis_T3, baby_measurements, {'scp','tcp','Target'});
end


tcp = scale_head_z(scp, tcp, factors, scaling_options, scp_kpis, tcp_kpis_T3, selIdx.head, file_pointers, format_options);
tcp_kpis_T4 = calculate_key_measurements_control_points(tcp, key_points);
if format_options.step_by_step_reporting
  disp('-----> LONGITUDINAL HEAD SCALING')
  report_current_cp_measurements(scp_kpis, tcp_kpis_T4, baby_measurements, {'scp','tcp','Target'});
end



leg_scaling_factor = calculate_scaling_factor_legs_z(tcp, tcp_kpis_T4, baby_measurements);

% Inputs for scaling of thighs
zero_point = tcp_kpis_T4.C16;
rotation_angle = atand((tcp_kpis_T4.C15(3) - zero_point(3) )/(tcp_kpis_T4.C15(1) - zero_point(1) ));

tcp = scale_legs(tcp, leg_scaling_factor, selIdx.knee_to_hip, zero_point, rotation_angle);

% Move the knee_to_foot part to coincide with the location of the new knee location.
tcp(selIdx.foot_to_knee,:) = tcp(selIdx.foot_to_knee,:) + (tcp(37,:) - tcp_kpis_T4.C15);


% Inputs for scaling of calves
zero_point = tcp(37,:);
rotation_angle = atand((tcp_kpis_T4.C4(3) - tcp_kpis_T4.C15(3) )/(tcp_kpis_T4.C4(1) - tcp_kpis_T4.C15(1) ));
tcp = scale_legs(tcp, leg_scaling_factor, selIdx.foot_to_knee, zero_point, rotation_angle);

tcp = scale_legs_y(scp, tcp, factors, scaling_options, selIdx.legs, scp_kpis, tcp_kpis_T4, file_pointers, format_options);


tcp_kpis_T6 = calculate_key_measurements_control_points(tcp, key_points);

if format_options.step_by_step_reporting
  disp('-----> LEG SCALING')
  report_current_cp_measurements(scp_kpis, tcp_kpis_T6, baby_measurements, {'scp','tcp','Target'});
end

tcp = scale_arms(scaling_options, tcp, tcp_kpis_T4, scp_kpis, selIdx.arms_yneg, selIdx.arms_ypos);


% Write outputs
write_k_file_updated_nodes([export_folder file_pointers.k_file_out_name], k_array, nMat);
dlmwrite_without_blank_last_line([export_folder file_pointers.scp_out_name], scp, format_options.dlmwrite_options);
dlmwrite_without_blank_last_line([export_folder file_pointers.tcp_out_name], tcp, format_options.dlmwrite_options);




%%%%
%[tcp , selIdx_temp] = scale_torso_z(scp, tcp, scp_kpis, scaling_options, selIdx.torso, selIdx.arms, factors, format_options);
%tcp = scale_torso_xy(scp, tcp, scaling_options, factors, baby_measurements(5), selIdx_temp, format_options);
%tcp = scale_head_circumference(scp, tcp, factors(4), selIdx.head, scp_kpis, format_options);
%tcp = scale_legs(tcp, selIdx.foot_to_knee, leg_scaling_factor, zero_point, rotation_angle);
%tcp = scale_head_z(scp, tcp, scaling_options, factors, scp_kpis, tcp_kpis_T3, selIdx.head, format_options);
%tcp = scale_legs_y(tcp, scaling_options, factors, selIdx.legs, scp_kpis, tcp_kpis_T4, format_options);

