How do I speed up the Manipulate of the Graphics3D objects here? It is currently painfully slow, and I'm at a loss.
I have reviewed many other posts and implemented what I could based on them, but it has had minimal effect on the speed. I have tried implementing:
- Reducing the angles used to 10 degree steps
- Memoization
- Dynamic (applied at various levels to various functions)
- Initialization code for Manipulate
- TrackedSymbols option for Manipulate
- Using With (both inside and outside Manipulate for the functions currently in Initialization)
I am building a demonstration of the rotation of vectors through the Euler angles. I have excluded the last rotation because the speed is already so slow.
(Apologies in advance for posting the entire code and the poor formatting here and there, which I've chosen to leave as is so that it can be copied and pasted into Mathematica.)
Manipulate[ Graphics3D[{ (* Size a graphics box to make visualization area constant *) graphicsBox, (* Original coordinate axes *) origCoordAxes, (* Vectors rotated once *) vectorsRotatedOnce[ψ], (* Vectors rotated twice *) vectorsRotatedTwice[ψ, θ], (* Plane surface first rotation *) planeSurfaceFirstRotation[ψ], (* Plane surface second rotation *) planeSurfaceSecondRotation[ψ, θ] }, BoxStyle -> Directive[Opacity[.1]], ViewVertical -> {0, 0, -1}, ViewPoint -> {-12, -10, -5} ], {{ψ, 60}, 1, 91, 10}, {{θ, 40}, 1, 91, 10}, Initialization :> ( x0 = {2, 0, 0}; y0 = {0, 2, 0}; z0 = {0, 0, 2}; x1[ψ_] := x1[ψ] = RotationMatrix[ψ Degree, z0]. x0; y1[ψ_] := y1[ψ] = RotationMatrix[ψ Degree, z0]. y0; z1[ψ_] := z1[ψ] = RotationMatrix[ψ Degree, z0]. z0; x2[ψ_, θ_] := x2[ψ, θ] = RotationMatrix[θ Degree, y1[ψ]]. x1[ψ]; y2[ψ_, θ_] := y2[ψ, θ] = RotationMatrix[θ Degree, y1[ψ]]. y1[ψ]; z2[ψ_, θ_] := z2[ψ, θ] = RotationMatrix[θ Degree, y1[ψ]]. z1[ψ]; graphicsBox = Sequence[ EdgeForm[None], Opacity[0], Cuboid[{0, 0, 0}, {1.25, 1.25, 1.25}], Cuboid[{0, 0, 0}, {-1.25, -1.25, -1.25}] ]; origCoordAxes = Sequence[ Black, Opacity[.25], Arrow[Tube[{{0, 0, 0}, {1, 0, 0}}, 0.005]], Text[Style["\!\(\*SubscriptBox[\(x\), \(0\)]\)", Black, Large], {1.2, 0, 0}], Arrow[Tube[{{0, 0, 0}, {0, 1, 0}}, 0.005]], Text[Style["\!\(\*SubscriptBox[\(y\), \(0\)]\)", Black, Large], {0, 1.2, 0}], Arrow[Tube[{{0, 0, 0}, {0, 0, 1}}, 0.005]], Text[Style["\!\(\*SubscriptBox[\(z\), \(0\)]\)", Black, Large], {0, 0, 1.2}] ]; vectorsRotatedOnce[ψ_] := vectorsRotatedOnce[ψ] = Sequence[ Red, Opacity[1], Arrow[Tube[{{0, 0, 0}, x1[ψ]}, 0.01]], Text[Style["\!\(\*SubscriptBox[\(x\), \(1\)]\)", Black, Large], (1.2 x1[ψ])], Text[Style["ψ", Lighter@Blue, Large], ((1/3) ( x1[ψ] + x0))], Green, Arrow[Tube[{{0, 0, 0}, y1[ψ]}, 0.01]], Text[Style["\!\(\*SubscriptBox[\(y\), \(1\)]\)", Black, Large], (1.2 y1[ψ])], Text[Style["ψ", Lighter@Blue, Large], ((1/3) ( y1[ψ] + y0))], Blue, Arrow[Tube[{{0, 0, 0}, z1[ψ]}, 0.01]], Text[ Style["\!\(\*SubscriptBox[\(z\), \(1\)]\)", Black,Large], (1.2 z1[ψ])] ]; vectorsRotatedTwice[ψ_, θ_] := vectorsRotatedTwice[ψ, θ] = Sequence[ Lighter@Red, Opacity[.3], Arrow[Tube[{{0, 0, 0}, x2[ψ, θ]}, 0.01]], Text[ Style["\!\(\*SubscriptBox[\(x\), \(2\)]\)", Black, Large], (1.2 x2[ψ, θ])], Text[ Style["θ", Lighter@Green, Large, Opacity[1]], ((1/3) ( x2[ψ, θ] + x1[ψ]))], Lighter@Green, Arrow[Tube[{{0, 0, 0}, y2[ψ, θ]}, 0.01]], Lighter@Blue, Arrow[Tube[{{0, 0, 0}, z2[ψ, θ]}, 0.01]], Text[Style["\!\(\*SubscriptBox[\(z\), \(2\)]\)", Black,Large], (1.2 z2[ψ, θ])], Text[Style["θ", Lighter@Green, Large, Opacity[1]], ((1/3) ( z2[ψ, θ] + z1[ψ]))] ]; planeSurfaceFirstRotation[ψ_] := planeSurfaceFirstRotation[ψ] = Sequence[ Opacity[.2], Directive[Blue, Glow[Blue], Specularity[0]], Dynamic@Rotate[surfaceX2[ψ Degree][[1]], 0, {0, 0, 1}], Dynamic@Rotate[surfaceX2[ψ Degree][[1]], π/2, {0, 0, 1}] ]; planeSurfaceSecondRotation[ψ_, θ_] := planeSurfaceSecondRotation[ψ, θ] = Sequence[ Opacity[.2], Directive[Green, Glow[Green], Specularity[0]], Dynamic@Rotate[Rotate[surfaceX2[θ Degree][[1]], ψ Degree, {0, 0, 1}], 3 π/2, x1[ψ]], Dynamic@Rotate[Rotate[surfaceX2[θ Degree][[1]], ψ Degree - π/2,{0, 0, 1}], 3 π/2, x1[ψ]] ]; surfaceX2[α_] := surfaceX2[α] = ParametricPlot3D[{r Cos[u], r Sin[u], 0}, {u, 0, α}, {r, 0, 1.5}, Mesh -> None, Axes -> None, Boxed -> True, Lighting -> {{"Ambient", Blue}}, PerformanceGoal -> "Speed" ]; ), TrackedSymbols :> {ψ, θ} ]
TubeandArrow[Tube...]can slow down rendering quite a bit - perhaps simply using anArrowwill help acceleration. $\endgroup$ImageSize -> {400, 400}at end of yourGraphics3Dcall. All the shifting is actually making it slow down a little ! and if you do not care about seeing each step, you can addContinuousAction -> Falseand that will make faster. $\endgroup$BSPTreeor similar, see mathematica.stackexchange.com/a/190/131) activated? That really slows down rendering compared to hardware-acceleration. $\endgroup$