Skip to content

Commit 6745c1b

Browse files
Save inverse projection matrix for speed
1 parent 4e56f0d commit 6745c1b

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

lib/mpl_toolkits/mplot3d/axes3d.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ def __init__(
157157
# Enable drawing of axes by Axes3D class
158158
self.set_axis_on()
159159
self.M = None
160+
self.invM = None
160161

161162
# func used to format z -- fall back on major formatters
162163
self.fmt_zdata = None
@@ -456,6 +457,7 @@ def draw(self, renderer):
456457

457458
# add the projection matrix to the renderer
458459
self.M = self.get_proj()
460+
self.invM = np.linalg.inv(self.M)
459461

460462
collections_and_patches = (
461463
artist for artist in self._children
@@ -1109,7 +1111,7 @@ def _calc_coord(self, xv, yv, renderer=None):
11091111
zv = -1 / self._focal_length
11101112

11111113
# Convert point on view plane to data coordinates
1112-
p1 = np.array(proj3d.inv_transform(xv, yv, zv, self.M)).ravel()
1114+
p1 = np.array(proj3d.inv_transform(xv, yv, zv, self.invM)).ravel()
11131115

11141116
# Get the vector from the camera to the point on the view plane
11151117
vec = self._get_camera_loc() - p1

lib/mpl_toolkits/mplot3d/proj3d.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,12 @@ def _proj_transform_vec_clip(vec, M):
190190
return txs, tys, tzs, tis
191191

192192

193-
def inv_transform(xs, ys, zs, M):
193+
def inv_transform(xs, ys, zs, invM):
194194
"""
195-
Transform the points by the inverse of the projection matrix *M*.
195+
Transform the points by the inverse of the projection matrix, `invM`.
196196
"""
197-
iM = linalg.inv(M)
198197
vec = _vec_pad_ones(xs, ys, zs)
199-
vecr = np.dot(iM, vec)
198+
vecr = np.dot(invM, vec)
200199
if vecr.shape == (4,):
201200
vecr = vecr.reshape((4, 1))
202201
for i in range(vecr.shape[1]):

lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1023,13 +1023,14 @@ def _test_proj_make_M():
10231023

10241024
def test_proj_transform():
10251025
M = _test_proj_make_M()
1026+
invM = np.linalg.inv(M)
10261027

10271028
xs = np.array([0, 1, 1, 0, 0, 0, 1, 1, 0, 0]) * 300.0
10281029
ys = np.array([0, 0, 1, 1, 0, 0, 0, 1, 1, 0]) * 300.0
10291030
zs = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1]) * 300.0
10301031

10311032
txs, tys, tzs = proj3d.proj_transform(xs, ys, zs, M)
1032-
ixs, iys, izs = proj3d.inv_transform(txs, tys, tzs, M)
1033+
ixs, iys, izs = proj3d.inv_transform(txs, tys, tzs, invM)
10331034

10341035
np.testing.assert_almost_equal(ixs, xs)
10351036
np.testing.assert_almost_equal(iys, ys)

0 commit comments

Comments
 (0)