%% Moment or Torque
% A moment, also known as the torque can be described as a "force to generate a rotation", as it generates an agular acceleration if not ballanced.
% A moment has the unit Newtonmeter [Nm], it is a vector, and appears around a point as the product of a moment arm (hävarm), d, and an applied force, F, perpendicular to the moment arm, see the figures below.
%
%
% {\mathit{\mathbf{M}}}_O =\mathit{\mathbf{r}}\times \mathit{\mathbf{F}}
% and the scalar counterpart
% M=F\;d
% The moment vector is defined as the vector \mathit{\mathbf{M}} that is perpendicular to the plan that is spanned by the position vector, \mathit{\mathbf{r}} and the force vector \mathit{\mathbf{F}}. The direction of the moment vector is given by the right-hand rule.
% \mathit{\mathbf{r}} is a position vector from the point about which the moment is being calculated to the line along which \mathit{\mathbf{F}} is acting (action line).
%
%
% In Matlab we compute the cross product by

syms F_x F_y F_z 
syms r_x r_y r_z
F = [F_x F_y F_z].';
r = [r_x r_y r_z].';
M = cross(r,F)

%
% {\mathit{\mathbf{M}}}_O =\left\lbrack \begin{array}{c}
% M_x \\
% M_y \\
% M_z
% \end{array}\right\rbrack =\left\lbrack \begin{array}{c}
% F_z \,r_y -F_y \,r_z \\
% F_x \,r_z -F_z \,r_x \\
% F_y \,r_x -F_x \,r_y
% \end{array}\right\rbrack =\left\lbrack \begin{array}{c}
% F_z \,r_y -F_y \,r_z \\
% -\left(F_z \,r_x -F_x \,r_z \right)\\
% F_y \,r_x -F_x \,r_y
% \end{array}\right\rbrack =\mathit{\mathbf{r}}\times \mathit{\mathbf{F}}
% \mathit{\mathbf{M}}=\mathit{\mathbf{r}}\times \mathit{\mathbf{F}}=-\mathit{\mathbf{F}}\times \mathit{\mathbf{r}}
% \mathit{\mathbf{M}}\bot \mathit{\mathbf{r}}\;\mathrm{and}\;\mathit{\mathbf{M}}\bot \mathit{\mathbf{F}}
%
%
%
%% Example 1 - 2D Trivial case

clear
r = [3,0,0];
F = 10*[0,1,0];
Mz = 3*10
M = cross(r,F)


% t=3
% 
% c = 0;
% for t = [-3:0.1:3,3:-0.1:-3]
%     r = [3,t,0];
%     
%     figure;
%     axis equal
%     axis([-1,5,-3,4])
%     hold on
%     quiver(0,0,r(1),r(2),0,'k')
%     plot([3,3],[-5,5],'k:')
%     plot(0,0,'ok','MarkerFaceColor','k')
%     axis off
%     % quiver(r(1),r(2),0,1,1,'b','LineWidth',2)
%     arrow3(r,r+[0,1,0],'b',1)
%     arrow3([0,0,0],[r(1),0,0],'r',1)
%     if r(2) ~= 0
%         arrow3([0,0,0],[0,r(2),0],'g',1)
%     end
%     text(r(1)+0.2,r(2)+0.3,'$\bf F$','Interpreter','Latex')
%     text(r(1)/2,r(2)/2+0.3,'$\bf r$','Interpreter','Latex')
%     if c == 0
%         exportgraphics(gca,"Moment1.gif","Append",false)
%     else
%         exportgraphics(gca,"Moment1.gif","Append",true)
%     end
%     c = 1;
% end


%% Example 2 - 2D general
%

eF = [4/5, 3/5, 0];
F = 10*eF;
OA = [3,-4,0];
Mz = 5*10
M = cross(OA,F)

t=0
r = OA + [4/5,3/5,0]*t
M = cross(r,F)


% c = 0;
% for t = [-3:0.1:3,3:-0.1:-3]
%     r = OA + [4/5,3/5,0]*t
% 
%     rs = OA + [4/5,3/5,0]*-3;
%     re = OA + [4/5,3/5,0]*3;
%     
%     figure;
%     
%     axis([-0.2,6,-6,1])
%     axis equal
%     hold on
%     quiver(0,0,r(1),r(2),0,'k')
%     plot([rs(1),re(1)],[rs(2),re(2)],'k:')
%     plot(0,0,'ok','MarkerFaceColor','k')
% %     axis off
%     arrow3(r,r+eF,'b',1)
%     arrow3([0,0,0],[r(1),0,0],'r',1)
%     if r(2) ~= 0
%         arrow3([0,0,0],[0,r(2),0],'g',1)
%     end
%     arrow3(r,r+[eF(1),0,0],'r',1)
%     arrow3(r,r+[0,eF(2),0],'g',1)
%     text(r(1)+0.2,r(2)+0.3,'$\bf F$','Interpreter','Latex')
%     text(r(1)/2,r(2)/2+0.3,'$\bf r$','Interpreter','Latex')
%     
%     r(1)*F(2)-r(2)*F(1)
%     M = cross(r,F)
% 
%     annotation(gcf,'textbox',...
%     [0.20 0.856142858774892 0.594642857142858 0.0785714269393967],...
%     'String',['$M_{oz}=r_xF_y-r_yF_x=',sprintf('%0.1f',r(1)),'\cdot', ...
%     sprintf('%0.1f',F(2)),'-',sprintf('%0.1f',r(2)),'\cdot',sprintf('%0.1f',F(1)),'=', ...
%     sprintf('%0.1f',M(3)),'$'],...
%     'LineStyle','none',...
%     'Interpreter','latex',...
%     'FontSize',14,...
%     'FitBoxToText','off');
% 
% 
%     if c == 0
%         exportgraphics(gca,"Moment2.gif","Append",false)
%     else
%         exportgraphics(gca,"Moment2.gif","Append",true)
%     end
%     c = 1;
% end

%
%% Example 3 - 3D case
%
% Let F=100\mathrm{N} in the line AB. Determine the moment O. Determine the moment around the z-axis.
% \vec{\textrm{OA}} ={\left\lbrack 5,0,2\right\rbrack }^T
% \vec{\textrm{OB}} ={\left\lbrack 3,1,0\right\rbrack }^T
% \mathit{\mathbf{r}}=\vec{\textrm{OB}}
% \mathit{\mathbf{F}}=F\;{\mathit{\mathbf{e}}}_{\textrm{AB}}
% {\mathit{\mathbf{e}}}_{\textrm{AB}} =\frac{\vec{\textrm{AB}} }{|\vec{\textrm{AB}} |}
% {\mathit{\mathbf{M}}}_O =\mathit{\mathbf{r}}\times \mathit{\mathbf{F}}
% M_{\textrm{Oz}} =M_O \cdot {\mathit{\mathbf{e}}}_z

clear
OA = [5,0,2]';
OB = [3,1,0]';
AB = OB-OA;
eAB = AB/norm(AB)

F = 100*eAB

r = OB;


MO = cross(OA,F)
MO = cross(OB,F)

MOz = dot(MO,[0,0,1])

%
%
%% Example 4 - General projection
%
% Compute the moment around the OA axis.

OA = [1,4,0].';
OB = [1,4,2].';
OC = [5,0,0].';
BC = OC-OB;
F = 120*BC/norm(BC)
MO = cross(OB,F)
MO = cross(OC,F)

eOA = OA/norm(OA)

% M_{\textrm{OA}} ={\mathit{\mathbf{M}}}_O \cdot e_{\textrm{OA}} =\left|\frac{M_O \cdot \vec{\textrm{OA}} }{\vec{\textrm{OA}} \cdot \vec{\textrm{OA}} }\vec{\textrm{OA}} \right|

MOA = dot(MO,eOA)
MOA = norm(dot(MO,OA)/dot(OA,OA)*OA)

%
%
%
%