Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6f4cc13

Browse files
authored
Merge pull request #8326 from TeamArmstrong/fix-537
Orthographic projection for mplot3d
2 parents 1805226 + cc425f6 commit 6f4cc13

File tree

8 files changed

+648
-2
lines changed

8 files changed

+648
-2
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Orthographic projection for mplot3d
2+
-----------------------------------
3+
:class:`~mpl_toolkits.mplot3d.axes3d.Axes3D` now accepts ``proj_type`` kwarg and has a method :meth:`~mpl_toolkits.mplot3d.axes3d.Axes3D.set_proj_type`. The default option is ``'persp'`` as before, and supplying ``'ortho'`` enables orthographic view.

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def __init__(self, fig, rect=None, *args, **kwargs):
6363
*elev* Elevation viewing angle (default 30)
6464
*zscale* [%(scale)s]
6565
*sharez* Other axes to share z-limits with
66+
*proj_type* 'persp' or 'ortho' (default 'persp')
6667
================ =========================================
6768
6869
.. versionadded :: 1.2.1
@@ -78,6 +79,7 @@ def __init__(self, fig, rect=None, *args, **kwargs):
7879
self.initial_elev = kwargs.pop('elev', 30)
7980
zscale = kwargs.pop('zscale', None)
8081
sharez = kwargs.pop('sharez', None)
82+
self.set_proj_type(kwargs.pop('proj_type', 'persp'))
8183

8284
self.xy_viewLim = unit_bbox()
8385
self.zz_viewLim = unit_bbox()
@@ -959,6 +961,23 @@ def view_init(self, elev=None, azim=None):
959961
else:
960962
self.azim = azim
961963

964+
def set_proj_type(self, proj_type):
965+
"""
966+
Set the projection type.
967+
968+
Parameters
969+
----------
970+
proj_type : str
971+
Type of projection, accepts 'persp' and 'ortho'.
972+
973+
"""
974+
if proj_type == 'persp':
975+
self._projection = proj3d.persp_transformation
976+
elif proj_type == 'ortho':
977+
self._projection = proj3d.ortho_transformation
978+
else:
979+
raise ValueError("unrecognized projection: %s" % proj_type)
980+
962981
def get_proj(self):
963982
"""
964983
Create the projection matrix from the current viewing position.
@@ -1001,9 +1020,9 @@ def get_proj(self):
10011020
zfront, zback = -self.dist, self.dist
10021021

10031022
viewM = proj3d.view_transformation(E, R, V)
1004-
perspM = proj3d.persp_transformation(zfront, zback)
1023+
projM = self._projection(zfront, zback)
10051024
M0 = np.dot(viewM, worldM)
1006-
M = np.dot(perspM, M0)
1025+
M = np.dot(projM, M0)
10071026
return M
10081027

10091028
def mouse_init(self, rotate_btn=1, zoom_btn=3):

lib/mpl_toolkits/mplot3d/proj3d.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ def persp_transformation(zfront, zback):
124124
[0,0,-1,0]
125125
])
126126

127+
def ortho_transformation(zfront, zback):
128+
# note: w component in the resulting vector will be (zback-zfront), not 1
129+
a = -(zfront + zback)
130+
b = -(zfront - zback)
131+
return np.array([[2,0,0,0],
132+
[0,2,0,0],
133+
[0,0,-2,0],
134+
[0,0,a,b]
135+
])
136+
127137
def proj_transform_vec(vec, M):
128138
vecw = np.dot(M, vec)
129139
w = vecw[3]
Binary file not shown.

0 commit comments

Comments
 (0)