Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slow 3D rotation with antialiasing in Mathematica 7

3D surface rotation in Mathematica 7 with anti-aliasing turned off is very fast and smooth. However, turning on anti-aliasing, even at a moderate level, drastically reduces the rendered frame rate, making rotation very choppy. This happens to a much greater degree in Mathematica than it does in other 3D applications.

  1. Why is anti-aliasing disproportionately slower in Mathematica?

  2. A way to greatly enhance the feel of 3D graphics would be to turn off AA during rotation, but turn it on as soon as rotation is stopped. Can this be done in Mathematica 7?


An example was requested. I will use a variation of Mike's code. Please try the following with Edit > Preferences > Appearance > Graphics first set to No antialiasing and then Highest quality. Also try the settings in between. For me, any setting besides No antialiasing is not smooth. I can visually distinguish three different levels of AA, so it is not a matter of my GPU forcing all or none, yet all of them are slow.

Animate[Plot3D[{x^2 + y^2, -x^2 - y^2}, {x, -2, 2}, {y, -2, 2},
  ImageSize -> 700,
  ViewPoint -> 
   Dynamic[{Sin[theta] Cos[phi], Sin[theta] Sin[phi], Cos[theta]}]],
 {theta, 0, Pi}, {phi, 0, 2 Pi},
 RefreshRate -> 120
]

UPDATE and OBSERVATION

While playing around with Alexey and Mike's code, something strange and good happened. I suddenly have smooth antialiased rotation! I don't know what precipitated the change, and I have not closed Mathematica for fear that it will go away, but this proves what I suspected, that it CAN be fast.

More strange, I have the slow and fast behavior happening in parallel in the same notebook. One graphic is rotating smoothly, and another produced with the same code is choppy. I theorize that some cell option was modified by running Alexey and Mike's code, and it is having a very desirable effect. I am going to do my best to figure out what it is.


The helpful options were RotationAction -> "Clip", SphericalRegion -> True that appeared in Alexey's code incidentally. These, or their effect (ViewAngle), "stick" with a graphic in the same way that rotation does, therefore I was able to type new code without these options over the old, and run it, and still get the smooth rotation (which explains what I saw above). See answers below for some elaboration.

While a viable workaround has been discovered, I am still interested in an explanation for the choppy rotation with AA on. I do not believe that the "fitting" of the graphic is an explanation, but merely a correlation, as the fitting still needs to take place with AA off, and yet the rotation is smooth.

like image 223
Mr.Wizard Avatar asked Aug 21 '11 08:08

Mr.Wizard


1 Answers

After Mr. Wizard pointed out that changing the ViewAngle improved performance, I had a hunch as to what was causing it.

Normally, when you rotate graphics in Mathematica, you'll notice the box that contains the graphics resizes itself as the image is rotated around. Why? Most of the settings are set to the default of Automatic, so they try to fiddle around to make the graphics fit nicely within the screen. Specifically, Plot3D (and many other 3D Plot functions) have a named parameter called RotationAction. According to the Mathematica documentation:

3D graphics by default resize and move to fit when they are interactively rotated

This parameter defaults to RotationAction->"Fit", which tries to always make sure the entire 3D graphics fits on screen. Instead, you can force the graphics to clip using RotationAction->"Clip". This causes the bounding box to remain the same. Instead of resizing, it merely chops off whatever goes off screen. Performance is improved dramatically as Mathematica doesn't have to continuously reshape the image to make everything fit on screen.

Example code:

Plot3D[{x^2 + y^2, -(x^2 + y^2)}, {x, -2, +2}, {y, -2, +2}, 
 ImageSize -> 700, RotationAction -> "Fit"]

Plot3D[{x^2 + y^2, -(x^2 + y^2)}, {x, -2, +2}, {y, -2, +2}, 
 ImageSize -> 700, RotationAction -> "Clip"]

Some further notes

Upon further analysis, RotationAction->"Clip" has some issues with rotation initially. On the very first rotation of the graphics, it rotates extremely fast. I don't know why this is so, only Wolfram Research could possibly answer that. After the initial rotation though, it rotates at the normal speed as any other graphics though.

The smoothest way of accomplishing nice rotation for me was given by:

 Plot3D[..., RotationAction -> "Clip", ViewAngle -> 0.4]

This is as close to the default clipping volume as possible, without any strange issues with fast rotations or cut off graphics.

like image 115
Mike Bailey Avatar answered Oct 14 '22 10:10

Mike Bailey