clc; clear; close all;

% Manufacturing evaluation

% This script compares 4 wheel architectures (Hoop, Spiral, Tweel,
% and Twin) from a manufacturing point of view.
%
% For each architecture, scores from 1 (worst) to 5 (best) are assigned based on:
% - geometric complexity
% - ease of production
% - ease of post-processing
% - tolerance robustness
% - estimated cost
%
% Each criterion is associated with a weight according to its importance.
% The final manufacturing score is calculated as the weighted sum
% of the individual contributions.
%
% Script outputs:
% - table of scores for each architecture
% - final manufacturing score
% - bar chart for visual comparison
%
% Note:
% a higher score indicates a more favorable solution from a
% manufacturing perspective.

criteria = {'GeomComplexity','EaseProduction', ...
            'EaseAssembly','ToleranceRobustness','EstimatedCost'};

weights = [0.2 0.2 0.2 0.15 0.25];

arch_names = {'Hoop','Spiral','Tweel','Twin'};

scores = [
    4 4 4 3 2 ;   % Hoop
    3 3 3 4 4 ;   % Spiral
    4 5 5 5 3 ;   % Tweel
    2 2 2 2 5    % Twin
];

final_score = scores * weights';

T_manufact = array2table(scores, 'VariableNames', criteria, 'RowNames', arch_names);
disp(T_manufact)

disp('Punteggi finali di manifattura:')
disp(table(final_score, 'RowNames', arch_names))

figure('Color','w')
bar(final_score)
set(gca,'XTickLabel',arch_names,'FontSize',11)
ylabel('Manufacturing score')
title('Confronto di manifattura tra architetture')
grid on
ylim([0 5])

%% ===================== HORIZONTAL BAR CHART =====================
figure('Color','w','Position',[100 100 900 500]);

b = barh(final_score, 0.6, 'FaceColor', 'flat', 'LineWidth', 1.2);
b.CData = [
    0.20 0.45 0.70   % Hoop - blu
    0.85 0.33 0.10   % Spiral - arancio
    0.47 0.67 0.19   % Tweel - verde
    0.49 0.18 0.56   % Twin - viola
];

grid on
box off

set(gca, ...
    'YTick', 1:numel(arch_names), ...
    'YTickLabel', arch_names, ...
    'FontSize', 14, ...
    'LineWidth', 1)

xlabel('Final manufacturing score', 'FontSize', 20, 'FontWeight', 'bold', Interpreter='latex')
%title('Manufacturing comparison among wheel architectures', ...
%    'FontSize', 14, 'FontWeight', 'bold')

xlim([0 5])

ax = gca;
ax.FontSize = 16;   
ax.LineWidth = 1;   
ax.TickLabelInterpreter = "latex";

% valori alla fine delle barre
for i = 1:numel(final_score)
    text(final_score(i)+0.05, i, sprintf('%.2f', final_score(i)), ...
        'VerticalAlignment','middle', ...
        'FontWeight','bold', ...
        'FontSize',15, ...
        'Color',[0.15 0.15 0.15],Interpreter = "latex");
end