2
$\begingroup$

I made a dynamic vector visualization

vec3D[x_,y_,z_,m_,d1_,d2_,c_]:=
{c,Arrow[{{x,y,z},x+m*Cos[d1]*Cos[d2], 
                  y+m*Sin[d1]*Cos[d2], z+m*Sin[d2]}}]}

Manipulate[
   Graphics3D[vec3D[0,0,0,m,d1,d2, Blue], Axes->True,
        PlotRange->{{-1,1},{-1,1},{-1,1}}],
         {{m,0.5,"Magnitude"},0,1},{{d1,0,"Angle 1"},-\[Pi],\[Pi]},
           {d2,0,"Angle 2"},-\[Pi]/2,\[Pi]/2}]

I then decided to add axes in different colors intersecting at the origin.

Manipulate[Show[
Graphics3D[vec3D[0,0,0,m,d1,d2, Blue], Axes->True, 
PlotRange->{{-1,1},{-1,1},{-1,1}}],
ParametricPlot3D[Labeled[{t,0,0},"x"],{t,-1,1},
PlotStyle->{Directive[LightBlue,Dashed]}],
ParametricPlot3D[Labeled[{0,t,0},"y"],{t,-1,1},
PlotStyle->{Directive[LightPurple,Dashed]}],
ParametricPlot3D[Labeled[{0,0,t},"z"],{t,-1,1},
PlotStyle->{Directive[LightGray,Dashed]}]],
{{m,0.5,"Magnitude"},0,1},
{{d1,0,"Angle 1"},-\[Pi],\[Pi]},{{d2,0,"Angle 2"},-\[Pi]/2,\[Pi]/2}]

The second one runs much slower than the first one, which I expect has something to do with the ParametricPlot3D objects getting updated even though they are static. How can I tell Manipulate to not update them but still show them in the same graphic?

I found this post but I don't understand the answer.

$\endgroup$

3 Answers 3

2
$\begingroup$

see if this makes it faster. I am just cheating here by using same method in post you linked to

Manipulate[
 Show[Graphics3D[vec3D[0, 0, 0, m, d1, d2, Blue], Axes -> True, 
   PlotRange -> {{-1, 1}, {-1, 1}, {-1, 1}}],
  p1, p2, p3],
 {{m, 0.5, "Magnitude"}, 0, 1, Appearance -> "Labeled"},
 {{d1, 0, "Angle 1"}, -π, π, Appearance -> "Labeled"},
 {{d2, 0, "Angle 2"}, -π/2, π/2, Appearance -> "Labeled"},
 TrackedSymbols :> {m, d1, d2},
 Initialization :> (
   p1 = ParametricPlot3D[Labeled[{t, 0, 0}, "x"], {t, -1, 1}, 
     PlotStyle -> {Directive[LightBlue, Dashed]}];
   p2 = ParametricPlot3D[Labeled[{0, t, 0}, "y"], {t, -1, 1}, 
     PlotStyle -> {Directive[LightPurple, Dashed]}];
   p3 = ParametricPlot3D[Labeled[{0, 0, t}, "z"], {t, -1, 1}, 
     PlotStyle -> {Directive[LightGray, Dashed]}]
   )
 ]

The idea is that if you put the static plots in the Initialization section, those get evaluated once. And you should always use TrackedSymbols.

It seems faster for me on V 14

enter image description here

$\endgroup$
2
$\begingroup$
$Version

(* "14.0.0 for Mac OS X ARM (64-bit) (December 13, 2023)" *)

Clear["Global`*"]

vec3D[x_, y_, z_, m_, d1_, d2_, c_] := {c, 
  Arrow[{{x, y, z}, {x + m*Cos[d1]*Cos[d2], y + m*Sin[d1]*Cos[d2], 
     z + m*Sin[d2]}}]}

Manipulate[
 Graphics3D[
  {Thick, vec3D[0, 0, 0, m, d1, d2, Blue]},
  Axes -> True,
  AxesOrigin -> {0, 0, 0},
  AxesStyle -> {{Blue, Dashed}, {Purple, Dashed}, {Gray, Dashed}},
  AxesLabel -> {"x", "y", "z"},
  Boxed -> False,
  PlotRange -> {{-1, 1}, {-1, 1}, {-1, 1}}],
 {{m, 0.5, "Magnitude"}, 0, 1, 0.01, Appearance -> "Labeled"},
 {{d1, 0, "Angle 1"}, -3.14, 3.14, 0.01, Appearance -> "Labeled"},
 {{d2, 0, "Angle 2"}, -1.57, 1.57, 0.01, Appearance -> "Labeled"},
 TrackedSymbols :> {m, d1, d2}]

enter image description here

Include Ticks -> None if you don't want the ticks.

$\endgroup$
2
$\begingroup$

In a Manipulate, if you wrap a specific part of your code in a Dynamic , Mathematica will try to update only this specific part when you move a slider.
It is not always possible, but in your case it works. Try :

Manipulate[
 Show[Graphics3D[Dynamic @ vec3D[0, 0, 0, m, d1, d2, Blue], 
   Axes -> True, PlotRange -> {{-1, 1}, {-1, 1}, {-1, 1}}], 
  ParametricPlot3D[Labeled[{t, 0, 0}, "x"], {t, -1, 1}, 
   PlotStyle -> {Directive[LightBlue, Dashed]}], 
  ParametricPlot3D[Labeled[{0, t, 0}, "y"], {t, -1, 1}, 
   PlotStyle -> {Directive[LightPurple, Dashed]}], 
  ParametricPlot3D[Labeled[{0, 0, t}, "z"], {t, -1, 1}, 
   PlotStyle -> {Directive[LightGray, Dashed]}]], {{m, 0.5, 
   "Magnitude"}, 0, 
  1}, {{d1, 0, "Angle 1"}, -\[Pi], \[Pi]}, {{d2, 0, 
   "Angle 2"}, -\[Pi]/2, \[Pi]/2}]  

I quote the tutorial "Advanced Manipulate Functionality" :

In short, Manipulate always wraps Dynamic around the expression given in its first argument, and normally any changes to variables used in the first argument will trigger updates of that Dynamic. But when a variable occurs only inside an explicit Dynamic nested inside the one implicitly created by Manipulate, an update of the outer Dynamic will not be triggered, only an update of the inner Dynamic in which it resides.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.