(* :Title: ShowLive *)

(* :Author: Mark Fisher *)

(* :Date: December 2004 *)

(* :Summary: Implements LiveGraphics3D stuff *)

(* :Requirements:
live.jar must be in the Java classpath.
For example, put live.jar in this directory:
	ToFileName[{$UserAddOnsDirectory, "Applications", "Live3D", "Java"}]
*)

(* :References:
live.jar is available at the LiveGraphics3D homepage:
	http://www.vis.uni-stuttgart.de/~kraus/LiveGraphics3D/

Source for a proto version of ShowLive:
	http://omegaconsultinggroup.com/Services/ezine19.html
*)

Needs["JLink`"]

InstallJava[]

ShowLive::usage = "ShowLive[graphics] takes a 3-d graphics object and
displays a \"live\" version of it in a separate window. (SurfaceGraphics,
ContourGraphics, and DensityGraphcs are converted to Graphics3D.) ShowLive
takes the options LiveWindowSize, LiveSpin, and LiveMagnification. (See the
usage notes.) If graphics is a list, ShowLive produces an animation. The
options AnimationDirection (Forward, Backward, or ForwardBackward) and
AnimationDisplayTime (per frame in seconds) can be specified."

LiveWindowSize::usage = "LiveWindowSize is an option for ShowLive which
specifies the size the window. The usage is LiveWindowSize -> size or
LiveWindowSize -> {xSize, ySize}. The default is LiveWindowSize -> 500."

LiveSpin::usage = "LiveSpin is an option for ShowLive which specifies the
initial rate of spin. The usage is LiveSpin -> spin or LiveSpin -> {xspin,
yspin}. The default is LiveSpin -> 0."

LiveMagnification::usage = "LiveMagnification is an option for ShowLive
which specifies the magnification. The LiveMagnification -> 1."

Options[ShowLive] = {LiveWindowSize -> 500, LiveSpin -> 0,
	LiveMagnification -> 1}

ShowLive[graphics_, opts___?OptionQ] :=
	Module[{size, spin, mag, aniOpts, graphics3d, liveform},
	{size, spin, mag} = {LiveWindowSize, LiveSpin, LiveMagnification} /.
		{opts} /. Options[ShowLive];
	If[!VectorQ[size], size = {size}];
	If[!VectorQ[spin], spin = {spin}];
	aniOpts = Cases[{opts}, Literal[AnimationDirection | AnimationDisplayTime -> _]];
	graphics3d = graphics /.
			x:(SurfaceGraphics|ContourGraphics|DensityGraphics)[__] :>
				Graphics3D[SurfaceGraphics[x]];
	liveform = Switch[graphics3d,
		_List, 	HoldForm[ShowAnimation][NumberForm[InputForm[N[graphics3d]], 5],
					InputForm[N[Flatten[{animOpts}]]]],
		_, 		NumberForm[InputForm[N[graphics3d]], 5]
		];
	AppletViewer["Live",
		{"INPUT=" <> ToString[ liveform ],
		"WIDTH="  <> ToString[ size[[ 1]] ],
		"HEIGHT=" <> ToString[ size[[-1]] ],
		"SPIN_X=" <> ToString[ spin[[ 1]] ],
		"SPIN_Y=" <> ToString[ spin[[-1]] ],
		"MAGNIFICATION=" <> ToString[ mag ]}
		]
	]

