function Translat

% http://geometrie.hs-nb.de/MATLAB-Programme/Translat.m   Bearbeitungsstand: 7.3.2005
% (C) Martin Nitschke, nitschke@hs-nb.de

R=4; r=2;  a=3; b=5;
deg=pi/180;
syms('t','s','u','v','real');
clc;
close('all')
c1=(0.9+0.1*(cos(3*t)))*cos(t);
c2=(0.9+0.1*(cos(4*t)))*sin(t);
Dc=[0 2*pi];
cInl=inline(['[' char(c1) ',' char(c2) '];'],'t');
[ti ci]=fplot(cInl,Dc);
x=ci(:,1)';
y=ci(:,2)';
xx=x([2:end 1]);
yy=y([2:end 1]);
S=[[(x.^2+x.*xx+xx.^2)*(yy-y)';(y.^2+y.*yy+yy.^2)*(x-xx)']/(6*polyarea(x,y));0];
Dm=[0 0.45]*pi;
m=[(R+r*cos(b*s))*[cos(a*s);sin(a*s)];sin(b*s)];
m=vpa(S-double(subs(m,s,Dm(1)))+m); % Verschiebung so dass m in S beginnt
mInl=inline(['[' char(m(1)) ',' char(m(2)) ',' char(m(3)) '];'],'s');
[si mi]=fplot(mInl,Dm);

[Phi,T,N1,N2]=VRK(c1,c2,t,m,s,u,v);
xRKInl=inline(vectorize(Phi(1)),'u','v');
yRKInl=inline(vectorize(Phi(2)),'u','v');
zRKInl=inline(vectorize(Phi(3)),'u','v');
TInl=inline([ '[' vectorize(T(1)) ';' vectorize(T(2)) ';' vectorize(T(3)) ']' ],'s');
NInl=inline([ '[' vectorize(N1(1)) ' ' vectorize(N2(1)) ';' ...
                  vectorize(N1(2)) ' ' vectorize(N2(2)) ';' ...
                  vectorize(N1(3)) ' ' vectorize(N2(3)) ']' ],'s');
[U,V]=meshgrid(ti,si);
X=xRKInl(U,V);
Y=yRKInl(U,V);
Z=zRKInl(U,V);
surf(X,Y,Z,U);
c=0.5+0.375*(cos(pi*[0:size(V,2)-1]/(size(V,2)-1))).^2;
colormap(repmat(c',1,3));
hold('on')
plot3(X([1 end],:)',Y([1 end],:)',Z([1 end],:)','k','linewidth',2.0)
vRef=round(size(X,2));
Schrift([X(1,vRef);Y(1,vRef);Z(1,vRef)],{'Profil-' 'kurve'},'HorizontalAlignment','right','VerticalAlignment','top','erase','none')
Br=unique(round(linspace(1,size(X,2),32)));
plot3(X(:,Br),Y(:,Br),Z(:,Br),'k')
Polygon3M([mi(:,1) mi(:,2) mi(:,3)]',0.4,'ok','erase','none','MarkerSize',9)
view(28,39)
axis('equal')
axis([0.9*min(X(:)) 0.6*max(X(:)) 0.9*min(Y(:)) 0.5*max(Y(:)) 0.1 0.4])
axis('off')
shading('interp')
print('-deps','Translat-1','-opengl')

q=0.55;
uRef=(1-q)*Dm(1)+q*Dm(end);
MRef=mInl(uRef)';
TRef=7.2*TInl(uRef);
NRef=6*NInl(uRef);
z=6*[0;0;1];
plot3(xRKInl(ti,uRef),yRKInl(ti,uRef),zRKInl(ti,uRef),'k','linewidth',1.0,'erase','none');
plot3(xRKInl(ti,uRef),yRKInl(ti,uRef),zRKInl(ti,uRef),'k','linewidth',2.0);
plot3(MRef(1),MRef(2),MRef(3),'.k','erase','none','MarkerSize',27)
plot3(MRef(1)+[0 TRef(1)],MRef(2)+[0 TRef(2)],MRef(3)+[0 TRef(3)],'k','erase','none');
plot3(MRef(1)+[0 NRef(1,1)],MRef(2)+[0 NRef(2,1)],MRef(3)+[0 NRef(3,1)],'k','erase','none');
plot3(MRef(1)+[0 NRef(1,2)],MRef(2)+[0 NRef(2,2)],MRef(3)+[0 NRef(3,2)],'k','erase','none');
plot3(MRef(1)+[0 z(1)],MRef(2)+[0 z(2)],MRef(3)+[0 z(3)],'--k','erase','none');
A=MRef-0.75*NRef(:,1)-0.75*NRef(:,2);
B=MRef+0.75*NRef(:,1)-0.75*NRef(:,2);
C=MRef+0.75*NRef(:,1)+0.75*NRef(:,2);
D=MRef-0.75*NRef(:,1)+0.75*NRef(:,2);
plot3([A(1) B(1) C(1) D(1) A(1)],[A(2) B(2) C(2) D(2) A(2)],[A(3) B(3) C(3) D(3) A(3)],'k','erase','none')
Spitze(MRef+TRef,TRef,1,30*deg,'k','erase','none')
Spitze(MRef+NRef(:,1),NRef(:,1),1,30*deg,'k','erase','none')
Spitze(MRef+NRef(:,2),NRef(:,2),1,30*deg,'k','erase','none')
Spitze(MRef+z,z,1,30*deg,'k','erase','none')
Schrift(MRef-0.03*TRef,'{\itM}({\its})','BackgroundColor','white','HorizontalAlignment','left','VerticalAlignment','bottom','erase','none')
Schrift(MRef+TRef,'{\itM}_{ }''({\its})_{ }','HorizontalAlignment','left','VerticalAlignment','bottom','erase','none')
Schrift(MRef+NRef(:,1),'{\it\bfn}_1({\its})','HorizontalAlignment','center','VerticalAlignment','top','erase','none')
Schrift(MRef+NRef(:,2),'{\it\bfn}_2({\its})','HorizontalAlignment','center','VerticalAlignment','top','erase','none')
Schrift(MRef+z,'  \it\bfz','HorizontalAlignment','left','VerticalAlignment','top','erase','none')
zlim([min(zlim) max(zlim)+3.5]);
print('-deps','Translat-2','-opengl')

function [Phi,T,N1,N2]=VRK(c1,c2,t,m,s,u,v);
dm=simple(diff(m,s));
T=simple(dm/sqrt(dot(dm,dm)));
Z=sym([0;0;1]);
N1=cross(Z,T);
N1=simple(N1/sqrt(dot(N1,N1)));
N2=cross(N1,T);
N2=simple(N2/sqrt(dot(N2,N2)));
Phi=subs(m,s,v)+subs(c1,t,u)*subs(N1,s,v)+subs(c2,t,u)*subs(N2,s,v);

function Schrift(P,txt,varargin)
text(P(1),P(2),P(3),txt,'FontName','TimesNewRoman','FontSize',24,varargin{:});

function drawPolygon3(draw,P,varargin);
feval(draw,P(1,:),P(2,:),P(3,:),varargin{:})

function Polygon3M(P,d,varargin)
lastK=1;
drawPolygon3(@plot3,P(:,1),varargin{:});
for k=2:size(P,2)
  v=P(:,k)-P(:,k-1);
  t=linspace(0,1,round(norm(v)/d));
  if length(t)==1
    pl=norm(P(:,lastK)-P(:,k))>d;
  else
    pl=1;
  end
  if pl
    lastK=k;
    drawPolygon3(@plot3,repmat(P(:,k-1),1,length(t))+repmat(t,length(P(:,k-1)),1).*repmat(v,1,length(t)),varargin{:})
  end
end

function Spitze(P,v,L,lambda,varargin);
deg=pi/180;
if nargin<=2
  L=1;
end
if nargin<=3
  lambda=7.5*deg;
end
v=L*v./repmat(sqrt(dot(v,v)),size(P,1),1);
Q=P-v;
t=[0:6:360]*deg;
for k=1:size(P,2)
  N=tan(lambda/2)*null(v(:,k)');
  for w=2:length(t)
    plot3(...
      [P(1,k) Q(1,k)+cos(t(w))*L(:,k)*N(1,1)+sin(t(w))*L(:,k)*N(1,2) Q(1,k)+cos(t(w-1))*L(:,k)*N(1,1)+sin(t(w-1))*L(:,k)*N(1,2) P(1,k)],...
      [P(2,k) Q(2,k)+cos(t(w))*L(:,k)*N(2,1)+sin(t(w))*L(:,k)*N(2,2) Q(2,k)+cos(t(w-1))*L(:,k)*N(2,1)+sin(t(w-1))*L(:,k)*N(2,2) P(2,k)],...
      [P(3,k) Q(3,k)+cos(t(w))*L(:,k)*N(3,1)+sin(t(w))*L(:,k)*N(3,2) Q(3,k)+cos(t(w-1))*L(:,k)*N(3,1)+sin(t(w-1))*L(:,k)*N(3,2) P(3,k)],...
      varargin{:});
    end
  end
