3
$\begingroup$

I'd like to create a widget that allows you to specify one of two ViewPoints.

Manipulate[
 Graphics3D[
  {
   Cuboid[]
   },
  ViewPoint -> Which[a == 1, {0, 0, -2}, a == 2, {-2, 2, 2}]
  ],
 {a, {1, 2}}
 ]

After executing, you can pick between a==1 or a==2, which will indeed change the point of view. But if you rotate the image with the mouse, this no longer works.

I'd like to allow rotation by the user (which should deselect buttons 1 and 2 under a), but also be able to snap back to one of the two presets if the user then presses one of the a buttons again.

$\endgroup$

1 Answer 1

1
$\begingroup$

I will suggest that the viewpoints be vectors of the same length. Otherwise the perspective changes, which I found disorienting. The fix is based on the fact that graphics parameters may be controlled in the front end via Dynamic.

Manipulate[Graphics3D[{Cuboid[]}, ViewPoint -> Dynamic@a],
 {a, {N@{0, 0, -2 Sqrt[3]} -> 1, N@{-2, 2, 2} -> 2}}]

Update incorporating my comment and response to @DavidG.Stork's comment:

The above resets the ViewPoint correctly. It does not reset ViewVertical or ViewAngle or ViewCenter or any of the other parameters that go into the ViewMatrix that can be changed by manipulating the graphics with the mouse (in case I've omitted to mention one). It does not seem possible to interactively change ViewRange. See Extract values for ViewMatrix from a Graphics3D for introduction to view parameters.

Manipulate[Graphics3D[{Cuboid[]},
  ViewPoint -> Dynamic@a[[1]],
  ViewVertical -> Dynamic@a[[2]],
  ViewAngle -> Dynamic@a[[3]],
  ViewCenter -> Dynamic@a[[4]]],
 {a, {
   N@{{0, 0, -2 Sqrt[3]}, {0, 0, 1}, Automatic, Automatic} -> 1, 
   N@{{-2, 2, 2}, {0, 0, 1}, Automatic, Automatic} -> 2}}
 ]
$\endgroup$
6
  • $\begingroup$ As far as I understand the OP, your code does solve his problem. After I rotate the cube I click the "other" preset button... I get the proper view but the cube remains rotated. I think the OP wants to reinitialize the view AND the orientation of the cube. $\endgroup$ Jun 7, 2023 at 18:57
  • $\begingroup$ Ooops... does not solve the problem. $\endgroup$ Jun 7, 2023 at 19:04
  • $\begingroup$ @DavidG.Stork No, it resets the ViewPoint correctly. It does not reset ViewVertical or ViewAngle or ViewCenter or any of the other parameters that go into the ViewMatrix that can be changed by manipulating the graphics with the mouse (in case I've omitted to mention one). The above works exactly the way I interpreted the problem, but differently than you interpreted it. $\endgroup$
    – Michael E2
    Jun 7, 2023 at 19:27
  • 1
    $\begingroup$ @DavidG.Stork If you want a solution that solves your interpretation, then use something like Manipulate[ Graphics3D[{Cuboid[]}, ViewPoint -> Dynamic@a[[1]], ViewVertical -> Dynamic@a[[2]]], {a, {N@{{0, 0, -2 Sqrt[3]}, {0, 0, 1}} -> 1, N@{{-2, 2, 2}, {0, 0, 1}} -> 2}}] -- you can add list elements and options for ViewCenter and ViewAngle if you want to reset pan/zoom actions by the user. $\endgroup$
    – Michael E2
    Jun 7, 2023 at 19:36
  • $\begingroup$ It all centers on what the OP means by "preset": just the viewpoint, or the viewpoint plus the original configuration of the cube. @rhomboidRhipper: please weigh in! $\endgroup$ Jun 7, 2023 at 20:46

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.