% tetringfilm.m  Simulation des Umstuelpens eines Rings
%   aus 6 verlaengerten Tetraedern
s = 1;
h = sqrt(3)/2;
d = 1.0;
f = 0.93;
%
p1 = [d 0 0 0]' + [0 h 0 0]' + [0 0 s/2 0]' + [0 0 0 1]';
p2 = [d 0 0 0]' + [0 h 0 0]' - [0 0 s/2 0]' + [0 0 0 1]';
p3 = [d-s/2 0 0 1]'; 
p4 = [d+s/2 0 0 1]';
zt = (p1 + p2 + p3 + p4)/4;
zt(4) = 1;
Zm = zt * ones(1,8);
To= [p1 p2 p3 p1 p4 p3 p4 p2]; 
%
px0 = [0 0 0]'; 
px1 = [s 0 0]';
px2 = [-s/2 s*sqrt(3)/2 0]';
px3 = [-s/2 -s*sqrt(3)/2 0]';
pxo = [ 0 0 1]';
pxu = [0 0 -1]';
%
Xa = 1.1*[px0 px1 px0 px2 px0 px3 px0 pxo pxu];
%
plot3( Xa(1,1:7), Xa(2,1:7), Xa(3,1:7),'k' );
     view(-53, 42);
To  = Zm + f*(To-Zm);
Tk = [p1 p2 p3 p1 p4 p3 p4 p2]; 
%plot3(To(1,:), To(2,:), To(3,:) )
%plot3(Tk(1,:), Tk(2,:), Tk(3,:),'g' )
axis([-1.2 1.2 -1.2 1.2 -1.2 1.2]);
axis square;
hold on;
%
Mv = [1 0 0 -d ; 0 1 0 0; 0 0 1 0; 0 0 0 1] ;
Mi =  [1 0 0 0 ; 0 -1 0 0; 0 0 1 0; 0 0 0 1] ;
Rt =  [cos(120*pi/180) -sin(120*pi/180)  0 0 ; 
       sin(120*pi/180) cos(120*pi/180) 0 0; 0 0 1 0; 0 0 0 1] ;
%
w=(0:2:360)*pi/180;
al = atan(sqrt(3)*sin(w));
xca = h*(cos(al)/sqrt(3) + tan(al)/sqrt(3).*sin(al));
%
for k = 1:length(w)
  Ra = [ 1 0 0 0; 0 cos(al(k)) -sin(al(k)) 0; 0 sin(al(k)) cos(al(k)) 0 ;
         0 0 0 1] ;
  Rw = [ cos(w(k)) 0 -sin(w(k))  0; 0 1 0 0; sin(w(k)) 0 cos(w(k)) 0; 
          0 0 0 1];
  xc = h* (  cos(w(k))/sqrt(3) + sin(w(k))*sin(al(k)) ) ;
  Mb = [1 0 0 xca(k) ; 0 1 0 0; 0 0 1 0; 0 0 0 1] ;

  Tt = Mb*Rw*Ra*Mv*To;
  Tf = Mb*Rw*Ra*Mv*Tk;
  if k > 1
    delete(plhd)
    delete(plhdf)
    delete(plhdg)
    delete(plhdm)
    delete(plhdmf)
    delete(plhdmg)
    delete(plhd1)
    delete(plhdm1)
    delete(plhd1f)
    delete(plhd1g)
    delete(plhdm1f)
    delete(plhdm1g)
    delete(plhd2)
    delete(plhdm2)
    delete(plhd2f)
    delete(plhd2g)
    delete(plhdm2f)
    delete(plhdm2g)
  end
  plhd = plot3(Tt(1,:), Tt(2,:), Tt(3,:), 'r' );
  plhdf = plot3(Tf(1,1:2), Tf(2,1:2), Tf(3,1:2), 'b' );
  plhdg = plot3(Tf(1,6:7), Tf(2,6:7), Tf(3,6:7), 'b' );
%
  Tm = Mi*Tt;
  Tmk = Mi*Tf;
  plhdm = plot3(Tm(1,:), Tm(2,:), Tm(3,:), 'r' );
  plhdmf = plot3(Tmk(1,1:2), Tmk(2,1:2), Tmk(3,1:2), 'b' );
  plhdmg = plot3(Tmk(1,6:7), Tmk(2,6:7), Tmk(3,6:7), 'b' );
  T1 = Rt*Tt;
  Tm1 = Rt*Tm;
  T1k = Rt*Tf;
  Tm1k = Rt*Tmk;
  plhd1 = plot3(T1(1,:), T1(2,:), T1(3,:), 'r' );
  plhd1f = plot3(T1k(1,1:2), T1k(2,1:2), T1k(3,1:2), 'b' );
  plhd1g = plot3(T1k(1,6:7), T1k(2,6:7), T1k(3,6:7), 'b' );
  plhdm1 = plot3(Tm1(1,:), Tm1(2,:), Tm1(3,:), 'r' );
  plhdm1f = plot3(Tm1k(1,1:2), Tm1k(2,1:2), Tm1k(3,1:2), 'b' );
  plhdm1g = plot3(Tm1k(1,6:7), Tm1k(2,6:7), Tm1k(3,6:7), 'b' );
  T2 = Rt*Rt*Tt;
  Tm2 = Rt*Rt*Tm;
  T2k = Rt* Rt*Tf;
  Tm2k = Rt*Rt*Tmk;
  plhd2 = plot3(T2(1,:), T2(2,:), T2(3,:), 'r' );
  plhd2f = plot3(T2k(1,1:2), T2k(2,1:2), T2k(3,1:2), 'b' );
  plhd2g = plot3(T2k(1,6:7), T2k(2,6:7), T2k(3,6:7), 'b' );
  plhdm2 = plot3(Tm2(1,:), Tm2(2,:), Tm2(3,:), 'r' );
  plhdm2f = plot3(Tm2k(1,1:2), Tm2k(2,1:2), Tm2k(3,1:2), 'b' );
  plhdm2g = plot3(Tm2k(1,6:7), Tm2k(2,6:7), Tm2k(3,6:7), 'b' );
%  
  if k<2
  pause(0.8)
   else
  pause(0.2)
  end
end
%
hold off

