green={46,204,64}/255//RGBColor;
blue = {73,167,233}/255//RGBColor;
orange = {250,177,37}/255//RGBColor;
toruscol = {214,230,235}/255//RGBColor;

{z,w}={Exp[I α]Sin[γ],Exp[I β]Cos[γ]};
SterProj[{z_,w_}]:=Module[{x=Re[z],y=Im[z],u=Re[w],v=Im[w]},{(x/(1-v)),(y/(1-v)),(u/(1-v))}//ComplexExpand]
σ[{z_,w_}]:={w,z}

ZW[γ_,α_,β_]:={Exp[I α]Sin[γ],Exp[I β]Cos[γ]}

p=2;
q=3;

m=p/q;
m2=q/p;
f2[x_]:=
Piecewise[Flatten[Table[{{m (x-2π k/p) - π/(2q),2k π/p≤x≤(2k+1)π/p},{-m (x-2π k/p)+2π/q- π/(2q),(2k+1) π/p≤x≤(2k+2)π/p}},{k,0,p}],{1,2}]]
g2[x_]:=
Piecewise[Flatten[Table[{{m2 (x-2π k/q) - π/(2p),2k π/q≤x≤(2k+1)π/q},{-m2(x-2π k/q)+2π/p- π/(2p),(2k+1) π/q≤x≤(2k+2)π/q}},{k,0,q}],{1,2}]]

fi2=Interpolation[{#,f2[Mod[#,2π]]}&/@RandomReal[{-10π,10π},1000]];
gi2=Interpolation[{#,g2[Mod[#,2π]]}&/@RandomReal[{-10π,10π},1000]];

inside=((Abs[4/π γ])^4)gi2[ β-t/q]-q/p( t/q)+2π n/p;
outside=((Abs[4/π γ])^4)fi2[ β+t/p]+p/q(t/p)+2π n/q;

TorusLockWashers[T_]:=Module[{τ=T-0.00001},
(*If[τ==π,τ=π-0.00001,None];*)

insidewashers=Table[SterProj@ZW[γ, β-π/(q 2),inside/.t->τ],{n,0,2}];
insideplot=ParametricPlot3D[insidewashers,{γ,0,π/4},{β,0, 2π},MeshStyle->None,PlotStyle->blue,PerformanceGoal->"Quality",PlotPoints->40];

outsidewashers=Table[SterProj@σ@ZW[γ, β-π/(p 2),outside/.t->τ],{n,0,4}];
outsideplot=ParametricPlot3D[outsidewashers,{γ,0,π/4},{β,0, 2π},MeshStyle->None,PlotStyle->orange,Exclusions->None,PlotPoints->30];
{insideplot,outsideplot}]

torus = ParametricPlot3D[SterProj@ZW[π/4,α,β],{α,0,2π},{β,0,2π},PlotStyle->{toruscol,Opacity[0.2]},MeshStyle->None,PlotRange->{{-3,3},{-3,3},{-3,3}},Axes->False,Boxed->False];

knot =ParametricPlot3D[RotationTransform[π/3,{0,0,1}]@SterProj@σ@ZW[π/4,  θ,-p/q θ],{θ,0,q 2π},PlotStyle->{Thick,green}];

{insides,outsides}=TorusLockWashers[π/2];
Show[torus,knot,insides,outsides]

frames=ParallelTable[TorusLockWashers[i],{i,0,2π,π/10}];

gifframes=Show[torus,knot,#]&/@frames;

SetDirectory["/Users/jesse/Google Drive/Master's Project Code/gifs"];

Export["torusanimate23.gif",gifframes]

Show[
ParametricPlot3D[Table[{θ,r,((Abs[r])^4)gi2[ θ]+2π n/p},{n,0,q+1}],{θ,0,2π},{r,0,1},PlotRange->{{0,2π},{0,2},{0,2π}},MeshStyle->None,Boxed->False,Ticks->None,AxesLabel->{"α","γ","β"},LabelStyle->Directive[Bold,Large]],

ParametricPlot3D[Table[{((Abs[r])^4)fi2[ θ]+2n π/q+π/p
,1+(1-r),θ+π/4},{n,0,q+1}],{θ,-2π,4π},{r,0,1},PlotRange->{{0,2π},{0,2},{0,2π}},PlotStyle->Blue,MeshStyle->None],

ParametricPlot3D[{Mod[θ+π/2q,2π],1,Mod[q/p θ,2π]},{θ,0,p*q π-0.01},Exclusions->True,PlotStyle->{Thick,Green}]
]

flattorus=ParametricPlot3D[{Mod[θ+π/2q,2π],1,Mod[q/p θ,2π]},{θ,0,p*q π-0.01},Exclusions->True,PlotStyle->{Thick,Green}];

flatframes=Table[
Show[
ParametricPlot3D[Table[{θ,r,((Abs[r])^4)gi2[ θ-t/3]+2π n/p+q/p( t/3)},{n,-q,q+1}],{θ,0,2π},{r,0,1},PlotRange->{{0,2π},{0,2},{0,2π}},MeshStyle->None,Boxed->False,Ticks->None,AxesLabel->{"α","γ","β"},LabelStyle->Directive[Bold,Large]],

ParametricPlot3D[Table[{((Abs[r])^4)fi2[ θ-t/2]+2n π/q+π/p+p/q (t/2)
,1+(1-r),θ+π/4},{n,-q,q+1}],{θ,-2π,4π},{r,0,1},PlotRange->{{0,2π},{0,2},{0,2π}},PlotStyle->Blue,MeshStyle->None],
flattorus

],{t,0,2π,π/4}]

ListAnimate[flatframes]

SetDirectory[NotebookDirectory[]]

Export["flatanimation.gif",flatframes]