(* ::Package:: *)

(************************************************************************)
(* This file was generated automatically by the Mathematica front end.  *)
(* It contains Initialization cells from a Notebook file, which         *)
(* typically will have the same name as this file except ending in      *)
(* ".nb" instead of ".m".                                               *)
(*                                                                      *)
(* This file is intended to be loaded into the Mathematica kernel using *)
(* the package loading commands Get or Needs.  Doing so is equivalent   *)
(* to using the Evaluate Initialization Cells menu command in the front *)
(* end.                                                                 *)
(*                                                                      *)
(* DO NOT EDIT THIS FILE.  This entire file is regenerated              *)
(* automatically each time the parent Notebook file is saved in the     *)
(* Mathematica front end.  Any changes you make to this file will be    *)
(* overwritten.                                                         *)
(************************************************************************)



(* ::Input::Initialization:: *)
SetDirectory["C:\\drorbn\\AcademicPensieve\\2008-12"]


(* ::Input::Initialization:: *)
Switch[Online,
Online, ideal=Import["http://katlas.org/w/images/d/d2/Ideal.txt.gz", "String"],
Offline, ideal=Import["C:\\drorbn\\People\\Gilbert\\ideal.txt", "String"]
]


(* ::Input::Initialization:: *)
data=Cases[ImportString[ideal, "XML"], XMLElement["AB", ab___] :> AB[ab], Infinity];


(* ::Input::Initialization:: *)
Length[data]


(* ::Input::Initialization:: *)
ProcessAB[ab_AB] := Module[
{Id, CS, L, n,c,k,K,coeffs},
{Id, CS, L} = {"Id", "Conway", "L"} /. ab[[1]];
{n,c,k} = ToExpression /@ StringSplit[Id, ":"];
If[n==10 && 161<k<=166, --k];
K=Knot[n,k];
coeffs=ab[[2]] /. XMLElement["Coeff", l_List, {}] :> {
ToExpression["I" /. l],
ToExpression["{"<>("A" /. l)<>"}"],
ToExpression["{"<>("B" /. l)<>"}"]
};
If[c == 1,
RopeLength[K] = ToExpression[L];
ConwayString[K] = CS;
IdealPresentationData[K] = coeffs;
K,
Print["Trouble in ", K]
]
];
IdealPresentation[K_Knot][t_] := Plus @@ (IdealPresentationData[K] /.
{i_,A_List,B_List} :> A Cos[i t] + B Sin[i t]
)


(* ::Input::Initialization:: *)
Ks = Union[ProcessAB /@ data];


(* ::Input::Initialization:: *)
Max[RopeLength /@ Ks]


(* ::Input::Initialization:: *)
RopeLength[Knot[0,1]]=N[Pi];
IdealPresentationData[Knot[0,1]] = {{1, {1,0,0}, {0,1,0}}};


(* ::Input::Initialization:: *)
l=Length[Ks]


(* ::Input::Initialization:: *)
Clear[DrawKnot];
RandomUnitVector[] := (
v={1.15*Random[], Random[], Random[]};
v/Sqrt[v . v]
);
RandomColour[] := RGBColor@@RandomUnitVector[];
DrawKnot[K_Knot, opts___Rule] := DrawKnot[K,opts] = Module[
{background = Background /. {opts} /. Background -> Black},
ImageCrop[Rasterize[Graphics3D[{
Specularity[Random[], 20Random[]],
{X1,X2,X3}= Compile[{t}, #]& /@ IdealPresentation[K][t];
T =8RopeLength[K];
Tube[Table[{X1[2Pi k/T], X2[2Pi k/T], X3[2Pi k/T]}, {k, 0, T}], 0.1+0.4Random[]]
},
Boxed -> False, Background -> background, ImagePadding->None,
PlotRangePadding->0,
ViewPoint -> 10 RandomUnitVector[],
Lighting -> {
{"Directional", RandomColour[], RandomUnitVector[]},
{"Point", RandomColour[], 2RandomUnitVector[]}
}
]]]
];



