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

Skip to content

Commit 67f4fca

Browse files
Save inverse projection matrix for speed
1 parent 4331086 commit 67f4fca

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def __init__(
156156
# Enable drawing of axes by Axes3D class
157157
self.set_axis_on()
158158
self.M = None
159+
self.invM = None
159160

160161
# func used to format z -- fall back on major formatters
161162
self.fmt_zdata = None
@@ -447,6 +448,7 @@ def draw(self, renderer):
447448

448449
# add the projection matrix to the renderer
449450
self.M = self.get_proj()
451+
self.invM = np.linalg.inv(self.M)
450452

451453
collections_and_patches = (
452454
artist for artist in self._children
@@ -1102,7 +1104,7 @@ def _calc_coord(self, xv, yv, renderer=None):
11021104
zv = -1 / self._focal_length
11031105

11041106
# Convert point on view plane to data coordinates
1105-
p1 = np.array(proj3d.inv_transform(xv, yv, zv, self.M)).ravel()
1107+
p1 = np.array(proj3d.inv_transform(xv, yv, zv, self.invM)).ravel()
11061108

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

lib/mpl_toolkits/mplot3d/proj3d.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44

55
import numpy as np
6-
import numpy.linalg as linalg
76

87
from matplotlib import _api
98

@@ -190,13 +189,12 @@ def _proj_transform_vec_clip(vec, M):
190189
return txs, tys, tzs, tis
191190

192191

193-
def inv_transform(xs, ys, zs, M):
192+
def inv_transform(xs, ys, zs, invM):
194193
"""
195-
Transform the points by the inverse of the projection matrix *M*.
194+
Transform the points by the inverse of the projection matrix, *invM*.
196195
"""
197-
iM = linalg.inv(M)
198196
vec = _vec_pad_ones(xs, ys, zs)
199-
vecr = np.dot(iM, vec)
197+
vecr = np.dot(invM, vec)
200198
if vecr.shape == (4,):
201199
vecr = vecr.reshape((4, 1))
202200
for i in range(vecr.shape[1]):

lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,13 +1046,14 @@ def _test_proj_make_M():
10461046

10471047
def test_proj_transform():
10481048
M = _test_proj_make_M()
1049+
invM = np.linalg.inv(M)
10491050

10501051
xs = np.array([0, 1, 1, 0, 0, 0, 1, 1, 0, 0]) * 300.0
10511052
ys = np.array([0, 0, 1, 1, 0, 0, 0, 1, 1, 0]) * 300.0
10521053
zs = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1]) * 300.0
10531054

10541055
txs, tys, tzs = proj3d.proj_transform(xs, ys, zs, M)
1055-
ixs, iys, izs = proj3d.inv_transform(txs, tys, tzs, M)
1056+
ixs, iys, izs = proj3d.inv_transform(txs, tys, tzs, invM)
10561057

10571058
np.testing.assert_almost_equal(ixs, xs)
10581059
np.testing.assert_almost_equal(iys, ys)

0 commit comments

Comments
 (0)