How to set parallel perspective on a 3d plot
Asked Answered
S

2

5

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.

Steib answered 11/9, 2015 at 2:53 Comment(0)
S
5

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()

enter image description here

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()

enter image description here

Still answered 11/9, 2015 at 20:1 Comment(0)
M
5

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().)

Mousetail answered 11/3, 2017 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.