image thumbnail

spin echo simulation

by

 

NMR, 90,180 deg rotations

h=create_3d_arrow(r,v,varargin)
function h=create_3d_arrow(r,v,varargin)

% h=create_3d_arrow(r,v,cl,al,hpar,r0,r1,h1,h2,R)
% create 3d arrow
% h - handle to arrow surface.
% r - position of arrow start
% v - vector of arrow direction |v| equalt to arrow length
% cl - color
% al - alpha (for transparancy)
% hpar - handle to axes where plot
% r0 - arrow base radius
% r1 - arrow tip radius
% h1 arrow tip height
% h2 arrow tip height internal
% R sin cos values
%
% if some of cl,al,hpar,r0,r1,h1,h2,R not specified then default values
% used
% if some of cl,al,hpar,r0,r1,h1,h2,R empty array [] then default values
% used

global cl_df  al_df  r0_df  r1_df  h1_df  h2_df  R_df % default values





% input processing:
switch nargin
    case 2
        cl=cl_df;
        al=al_df;
        hpar=gca;
        r0=r0_df;
        r1=r1_df;
        h1=h1_df;
        h2=h2_df;
        R=R_df;
    case 3
        cl=varargin{1};
        if isempty(cl)
            cl=cl_df;
        end
        al=al_df;
        hpar=gca;
        r0=r0_df;
        r1=r1_df;
        h1=h1_df;
        h2=h2_df;
        R=R_df;
    case 4
        cl=varargin{1};
        if isempty(cl)
            cl=cl_df;
        end
        al=varargin{2};
        if isempty(al)
            al=al_df;
        end
        hpar=gca;
        r0=r0_df;
        r1=r1_df;
        h1=h1_df;
        h2=h2_df;
        R=R_df;
    case 5
        cl=varargin{1};
        if isempty(cl)
            cl=cl_df;
        end
        al=varargin{2};
        if isempty(al)
            al=al_df;
        end
        hpar=varargin{3};
        if isempty(hpar)
            hpar=gca;
        end
        r0=r0_df;
        r1=r1_df;
        h1=h1_df;
        h2=h2_df;
        R=R_df;
        
    case 6
        cl=varargin{1};
        if isempty(cl)
            cl=cl_df;
        end
        al=varargin{2};
        if isempty(al)
            al=al_df;
        end
        hpar=varargin{3};
        if isempty(hpar)
            hpar=gca;
        end
        r0=varargin{4};
        if isempty(r0)
            r0=r0_df;
        end
        r1=r1_df;
        h1=h1_df;
        h2=h2_df;
        R=R_df;
        
    case 7
        cl=varargin{1};
        if isempty(cl)
            cl=cl_df;
        end
        al=varargin{2};
        if isempty(al)
            al=al_df;
        end
        hpar=varargin{3};
        if isempty(hpar)
            hpar=gca;
        end
        r0=varargin{4};
        if isempty(r0)
            r0=r0_df;
        end
        r1=varargin{5};
        if isempty(r1)
            r1=r1_df;
        end
        h1=h1_df;
        h2=h2_df;
        R=R_df;
        
    case 8
        cl=varargin{1};
        if isempty(cl)
            cl=cl_df;
        end
        al=varargin{2};
        if isempty(al)
            al=al_df;
        end
        hpar=varargin{3};
        if isempty(hpar)
            hpar=gca;
        end
        r0=varargin{4};
        if isempty(r0)
            r0=r0_df;
        end
        r1=varargin{5};
        if isempty(r1)
            r1=r1_df;
        end
        h1=varargin{6};
        if isempty(h1)
            h1=h1_df;
        end
        h2=h2_df;
        R=R_df;
        
    case 9
        cl=varargin{1};
        if isempty(cl)
            cl=cl_df;
        end
        al=varargin{2};
        if isempty(al)
            al=al_df;
        end
        hpar=varargin{3};
        if isempty(hpar)
            hpar=gca;
        end
        r0=varargin{4};
        if isempty(r0)
            r0=r0_df;
        end
        r1=varargin{5};
        if isempty(r1)
            r1=r1_df;
        end
        h1=varargin{6};
        if isempty(h1)
            h1=h1_df;
        end
        h2=varargin{7};
        if isempty(h2)
            h2=h2_df;
        end
        R=R_df;
        
    case 10
        cl=varargin{1};
        if isempty(cl)
            cl=cl_df;
        end
        al=varargin{2};
        if isempty(al)
            al=al_df;
        end
        hpar=varargin{3};
        if isempty(hpar)
            hpar=gca;
        end
        r0=varargin{4};
        if isempty(r0)
            r0=r0_df;
        end
        r1=varargin{5};
        if isempty(r1)
            r1=r1_df;
        end
        h1=varargin{6};
        if isempty(h1)
            h1=h1_df;
        end
        h2=varargin{7};
        if isempty(h2)
            h2=h2_df;
        end
        R=varargin{8};
        if isempty(R)
            R=R_df;
        end
   
        
end

lR=length(R(:,1));

% cl
% al
% hpar
% r0
% r1
% h1
% h2
% R

% normalize:
vln2=v'*v;
vln=sqrt(vln2);
w=v/vln;

% rotation matrix:
if (w(1)==0)&&(w(2)==0)
    if w(3)>0
        Rt=eye(3);
    else
        Rt=-eye(3);
    end
else
    %s=sqrt(1-w(3)^2); % sin(theta)
    w12=w(1)^2;
    w22=w(2)^2;
    iw=(w12+w22);
    w12m=w12/iw;
    w22m=w22/iw;
    R12=-w(1)*w(2)*(1-w(3))/iw;
    Rt=[w22m+(1-w22m)*w(3)   R12                w(1);
         R12                w12m+(1-w12m)*w(3)  w(2);
        -w(1)            -w(2)                  w(3)];  % rotation matrix  3x3
end


% ring at heght h with radius r at position r0:
% bsxfun(@plus,Rt*[r*R'; h*ones(1,lR)],r0)   3xn

X=zeros(lR,5);
Y=zeros(lR,5);
Z=zeros(lR,5);

% start:
X(:,1)=r(1);
Y(:,1)=r(2);
Z(:,1)=r(3);

% ring 1:
rn1=bsxfun(@plus,Rt*[r0*R'; zeros(1,lR)],r);
X(:,2)=rn1(1,:)';
Y(:,2)=rn1(2,:)';
Z(:,2)=rn1(3,:)';

% ring 2:
rn2=bsxfun(@plus,Rt*[r0*R'; (vln-h2)*ones(1,lR)],r);
X(:,3)=rn2(1,:)';
Y(:,3)=rn2(2,:)';
Z(:,3)=rn2(3,:)';

% ring 3:
rn3=bsxfun(@plus,Rt*[r1*R'; (vln-h1)*ones(1,lR)],r);
X(:,4)=rn3(1,:)';
Y(:,4)=rn3(2,:)';
Z(:,4)=rn3(3,:)';

% end:
rv=r+v;
X(:,5)=rv(1);
Y(:,5)=rv(2);
Z(:,5)=rv(3);


h=surf(X,Y,Z,'parent',hpar);
set(h,'FaceAlpha',al,'EdgeColor','none','FaceColor',cl);

Contact us