问题
I have a code in python to render a few spheres in python that looks like this:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import random
import mayavi
from mayavi import mlab
N = 4;
diams = .4*np.ones([N]);
xvals = np.arange(N);
yvals = np.zeros(N);
zvals = np.zeros(N);
pts = mlab.points3d(xvals, yvals, zvals, diams, scale_factor=1,transparent=True)
mlab.show()
The default view of the figure adds distortion based on the camera position (farther spheres smaller). I'd like to set the to parallel projection (farther spheres same size) by some command so it renders like this automatically.
I didn't find a straightforward solution with google or the documentation. Thanks!
回答1:
Try setting fig.scene.parallel_projection = True
or mlab.gcf().scene.parallel_projection = True
in your case.
As a quick example, compare (zoomed in to magnify differences):
import numpy as np
from mayavi import mlab
np.random.seed(1977)
x, y, z = np.random.random((3, 10))
mlab.points3d(x, y, z)
mlab.show()
And when we set an orthogonal projection:
import numpy as np
from mayavi import mlab
np.random.seed(1977)
x, y, z = np.random.random((3, 10))
mlab.points3d(x, y, z)
mlab.gcf().scene.parallel_projection = True
mlab.show()
回答2:
In addition to the accepted answer, I discovered that when we use the figure.scene.parallel_projection = True
mode, the parameters returned by mlab.view()
are no longer sufficient to describe the camera view completely. There is another parameter that comes into play:
figure.scene.camera.parallel_scale
So, if one wishes to set the view to be the same every time, then they have to (1) set mlab.view(..)
and also (2) set figure.scene.camera.parallel_scale = 5.0
for example.
(Background story: my script plots a surface, then I had set the camera using only mlab.view(..)
, and saw that the rendered images had inconsistent scaling. The reason is: as I plot, TVTK updates the camera's parameters, so they might be different if the plots are not identical. These parameters include parallel_scale
, which affects the projection — it's basically a zoom — but is independent of mlab.view()
.)
来源:https://stackoverflow.com/questions/32514744/setting-parallel-prospective-in-mlab-mayavi-python