(* ::Package:: *)

(*<<Graphics`Animation`;*)
(*<<Graphics`Polyhedra`;*)

v1 = {4, 2, 1};v2 = {4, 4, 1};v3 = {1, 3, 1};v4 = {2, 2, 4}; 
P1 = Polygon[{v1, v2, v4}];P2 = Polygon[{v1, v2, v3}];P3 = Polygon[{v1, v3, v4}];P4 = Polygon[{v2, v3, v4}];
Tri = {P1, P2, P3, P4};

DefRec[a_,b_,c_] := {
Polygon[{{0,0,0},{a,0,0},{a,0,c},{0,0,c}}], 
Polygon[{{0,0,0},{0,b,0},{a,b,0},{a,0,0}}],
Polygon[{{0,0,0},{0,b,0},{0,b,c},{0,0,c}}], 
Polygon[{{0,b,0},{a,b,0},{a,b,c},{0,b,c}}],
Polygon[{{0,0,c},{0,b,c},{a,b,c},{a,0,c}}],
Polygon[{{a,0,0},{a,b,0},{a,b,c},{a,0,c}}]};

Rec = DefRec[2,3,1];

(*Rec = {Polygon[{{0,0,0},{2,0,0},{2,0,1},{0,0,1}}], Polygon[{{0,0,0},{0,3,0},{2,3,0},{2,0,0}}],Polygon[{{0,0,0},{0,3,0},{0,3,1},{0,0,1}}], Polygon[{{0,3,0},{2,3,0},{2,3,1},{0,3,1}}],Polygon[{{0,0,1},{0,3,1},{2,3,1},{2,0,1}}],Polygon[{{2,0,0},{2,3,0},{2,3,1},{2,0,1}}]};

Recs = {Polygon[{{0,0,0},{1,0,0},{1,0,1},{0,0,1}}], Polygon[{{0,0,0},{0,3,0},{1,3,0},{1,0,0}}],Polygon[{{0,0,0},{0,3,0},{0,3,1},{0,0,1}}], Polygon[{{0,3,0},{1,3,0},{1,3,1},{0,3,1}}],Polygon[{{0,0,1},{0,3,1},{1,3,1},{1,0,1}}],Polygon[{{1,0,0},{1,3,0},{1,3,1},{1,0,1}}]};
*)

Recs = DefRec[1,3,1]; 

l1 = Line[{{0,0,0}, {10,0,0}}];
l2 = Line[{{0,0,0}, {0,10,0}}];
l3 = Line[{{0,0,0}, {0,0,10}}];

coord = {l1, l2, l3, Text["O",{-1, 0, -1}], Text["x", {10, 0, -1}], Text["y", {0, 10, -1}], Text["z", {0, -1, 10}]};
(*
Rx[t_] := (With[{td=t/180.0*Pi},{{1, 0, 0, 0}, {0, Cos[td], -Sin[td], 0},{0, Sin[td], Cos[td], 0}, {0, 0, 0, 1}}]);
Ry[t_] := (With[{td=t/180.0*Pi},{{Cos[td], 0, Sin[td], 0}, {0, 1, 0, 0},{-Sin[td], 0, Cos[td], 0}, {0, 0, 0, 1}}]);
Rz[t_] := (With[{td=t/180.0*Pi},{{Cos[td], -Sin[td], 0, 0}, {Sin[td], Cos[td], 0, 0},{0, 0, 1, 0}, {0, 0, 0, 1}}]);
 *)
Rx[t_] := (With[{td=t},{{1, 0, 0, 0}, {0, Cos[td], -Sin[td], 0},{0, Sin[td], Cos[td], 0}, {0, 0, 0, 1}}]);
Ry[t_] := (With[{td=t},{{Cos[td], 0, Sin[td], 0}, {0, 1, 0, 0},{-Sin[td], 0, Cos[td], 0}, {0, 0, 0, 1}}]);
Rz[t_] := (With[{td=t},{{Cos[td], -Sin[td], 0, 0}, {Sin[td], Cos[td], 0, 0},{0, 0, 1, 0}, {0, 0, 0, 1}}]);

Tx[d_] := {{1,0,0,d}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1}}; 
Ty[d_] := {{1,0,0,0}, {0,1,0,d}, {0,0,1,0}, {0,0,0,1}}; 
Tz[d_] := {{1,0,0,0}, {0,1,0,0}, {0,0,1,d}, {0,0,0,1}}; 


trans[H_, ob_] := (Module[{R, d, pn, n, m, poly, M, N, new, i, j, v}, 
			 R={{H[[1,1]], H[[1,2]],H[[1,3]]},
			    {H[[2,1]], H[[2,2]], H[[2,3]]}, 
			    {H[[3,1]], H[[3,2]], H[[3,3]]}};
			 d= {H[[1,4]], H[[2,4]], H[[3,4]]};

		       pn = Dimensions[ob];
		       n = pn[[1]];
		       new = {};
		       For[i=1,i<=n,i++,
			  poly = ob[[i]];
			  M = poly[[1]];
			  pn = Dimensions[M];
			  m = pn[[1]];
			  N = {};
			  For[j=1,j<=m,j++,
			     v = M[[j]];
			     v = R.v+d;
			     N = Append[N, v];
			  ];
			  new = Append[new, Polygon[N]];
		       ];
		       Return[new];])

dispv[ob_, p_,view_]:= (Show[Graphics3D[{coord, ob}], PlotRange ->{{-2,12},{-2,12},{-2,12}},ViewPoint->p, ViewCenter->view];)

(*disp[ob_]:= (Show[Graphics3D[{coord, ob}], PlotRange ->{{-2,12},{-2,12},{-2,12}}];)*)

disp[ob_]:= Show[Graphics3D[{coord, ob}, PlotRange ->{{-2,12},{-2,12},{-2,12}}]];


Hr := 	{{Cos[theta],0,  -Sin[theta], x},{0, 1, 0, y}, {Sin[theta],0,  Cos[theta], z},{0,0,0,1}};

Rec = DefRec[1,3,1];


x =0;
y =0;
z =0;

(*
f1 := x2[t];
f2 := (-mu*x2[t]-m*g*l*Cos[x1[t]])/(m*l*l);
m = 1.0;
mu = 0.01;
l = 0.5;
g = 9.8;
sol = NDSolve[{x1'[t]==f1, x2'[t]== f2,x1[0]==0.0, x2[0]==1.0},{x1,x2},{t,0,10}];
Plot[Evaluate[{x1[t],x2[t]} /. sol] ,{t,0,10}];

view = {};

For[i=1,i<=10,i++,
   theta = (x1[i] /. sol)[[1]];
   Recs1 = trans[Rx[theta],Rec];
   view = Append[view, Graphics3D[{coord, Recs1}, PlotRange ->{{-2,12},{-2,12},{-2,12}}]];
  view = Append[view, Graphics3D[{coord, Recs1},
         DisplayFunction -> Identity, PlotRange ->{{-2,12},{-2,12},{-2,12}}]];
]

ListAnimate[view]
 *)

(*
For[i=1,i<=10,i++,
      theta = 0.1*i;
      x = 0.1*i;y = 0.1*i;z=0.1*i;
   view = Append[view, Show[Graphics3D[{coord, trans[Hr, Rec]}],DisplayFunction -> Identity, PlotRange ->{{-2,12},{-2,12},{-2,12}}]];
]

ShowAnimation[Show[Graphics3D[{coord, trans[Hr, Rec]}], PlotRange ->{{-2,12},{-2,12},{-2,12}}], {x, 0, 1.0, 0.1}];
<<Graphics`PlotField`;

steady[{x_,y_}] := {{x,y}, {1,0}};
data = Table[steady[{x,y}],{x, -5, 5,2},{y,-4,4,1}];

Animate[
	ListPlotVectorField[Part[data,i],
			   ScaleFactor -> 0.75,
			   HeadScaling -> Relative,
			   HeadLength -> .5,
			   Frame -> True,
			   FrameTicks -> None,
			   PlotRange -> {{-6,6},{-4.5, 4.5}}],
	  {i, 1, Length[data],1}];

*)

