8000 Vectorize output of proj_transform_vec · matplotlib/matplotlib@9c13619 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c13619

Browse files
Vectorize output of proj_transform_vec
1 parent dd5926c commit 9c13619

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
127127

128128
def draw(self, renderer):
129129
xs3d, ys3d, zs3d = self._verts3d
130-
xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
131-
self.set_data(xs, ys)
130+
xyz = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
131+
self.set_data(xyz[0], xyz[1])
132132
lines.Line2D.draw(self, renderer)
133133
self.stale = False
134134

@@ -271,11 +271,11 @@ def get_facecolor(self):
271271
def do_3d_projection(self, renderer):
272272
s = self._segment3d
273273
xs, ys, zs = list(zip(*s))
274-
vxs, vys,vzs, vis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M)
275-
self._path2d = mpath.Path(list(zip(vxs, vys)))
274+
vxyzis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M)
275+
self._path2d = mpath.Path(vxyzis[0:2].T)
276276
# FIXME: coloring
277277
self._facecolor2d = self._facecolor3d
278-
return min(vzs)
278+
return min(vxyzis[2])
279279

280280
def draw(self, renderer):
281281
Patch.draw(self, renderer)
@@ -299,11 +299,11 @@ def set_3d_properties(self, path, zs=0, zdir='z'):
299299
def do_3d_projection(self, renderer):
300300
s = self._segment3d
301301
xs, ys, zs = list(zip(*s))
302-
vxs, vys,vzs, vis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M)
303-
self._path2d = mpath.Path(list(zip(vxs, vys)), self._code3d)
302+
vxyzis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M)
303+
self._path2d = mpath.Path(vxyzis[0:2].T, self._code3d)
304304
# FIXME: coloring
305305
self._facecolor2d = self._facecolor3d
306-
return min(vzs)
306+
return min(vxyzis[2])
307307

308308
def get_patch_verts(patch):
309309
"""Return a list of vertices for the path of a patch."""
@@ -379,21 +379,21 @@ def set_3d_properties(self, zs, zdir):
379379

380380
def do_3d_projection(self, renderer):
381381
xs, ys, zs = self._offsets3d
382-
vxs, vys, vzs, vis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M)
382+
vxyzis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M)
383383

384-
fcs = (zalpha(self._facecolor3d, vzs) if self._depthshade else
384+
fcs = (zalpha(self._facecolor3d, vxyzis[2]) if self._depthshade else
385385
self._facecolor3d)
386386
fcs = mcolors.colorConverter.to_rgba_array(fcs, self._alpha)
387387
self.set_facecolors(fcs)
388388

389-
ecs = (zalpha(self._edgecolor3d, vzs) if self._depthshade else
389+
ecs = (zalpha(self._edgecolor3d, vxyzis[2]) if self._depthshade else
390390
self._edgecolor3d)
391391
ecs = mcolors.colorConverter.to_rgba_array(ecs, self._alpha)
392392
self.set_edgecolors(ecs)
393-
PatchCollection.set_offsets(self, list(zip(vxs, vys)))
393+
PatchCollection.set_offsets(self, vxyzis[0:2].T)
394394

395-
if vzs.size > 0:
396-
return min(vzs)
395+
if len(vxyzis) > 0:
396+
return min(vxyzis[2])
397397
else:
398398
return np.nan
399399

@@ -447,22 +447,22 @@ def set_3d_properties(self, zs, zdir):
447447

448448
def do_3d_projection(self, renderer):
449449
xs, ys, zs = self._offsets3d
450-
vxs, vys, vzs, vis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M)
450+
vxyzis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M)
451451

452-
fcs = (zalpha(self._facecolor3d, vzs) if self._depthshade else
452+
fcs = (zalpha(self._facecolor3d, vxyzis[2]) if self._depthshade else
453453
self._facecolor3d)
454454
fcs = mcolors.colorConverter.to_rgba_array(fcs, self._alpha)
455455
self.set_facecolors(fcs)
456456

457-
ecs = (zalpha(self._edgecolor3d, vzs) if self._depthshade else
457+
ecs = (zalpha(self._edgecolor3d, vxyzis[2]) if self._depthshade else
458458
self._edgecolor3d)
459459
ecs = mcolors.colorConverter.to_rgba_array(ecs, self._alpha)
460460
self.set_edgecolors(ecs)
461-
PathCollection.set_offsets(self, list(zip(vxs, vys)))
461+
PathCollection.set_offsets(self, vxyzis[0:2].T)
462462

463-
if vzs.size > 0 :
464-
return min(vzs)
465-
else :
463+
if len(vxyzis) > 0:
464+
return min(vxyzis[2])
465+
else:
466466
return np.nan
467467

468468

@@ -545,7 +545,10 @@ def set_zsort(self, zsort):
545545

546546
def get_vector(self, segments3d):
547547
"""Optimize points for projection"""
548+
# Segments 3d are given in shape (n_segments, 3, 3)
549+
# Flatten them
548550
xys = segments3d.reshape((-1, 3)).T
551+
# Add a fourth dimension with only ones
549552
ones = np.ones(xys.shape[1])
550553
self._vec = np.vstack([xys, ones])
551554

@@ -589,8 +592,7 @@ def do_3d_projection(self, renderer):
589592
self.update_scalarmappable()
590593
self._facecolors3d = self._facecolors
591594

592-
# XXX proj_transform_vec can work with arrays only
593-
xys = np.array(proj3d.proj_transform_vec(self._vec, renderer.M))
595+
xys = proj3d.proj_transform_vec(self._vec, renderer.M)
594596
xyzlist = np.transpose(xys.T.reshape((-1, 3, 3)), axes=[0, 2, 1])
595597

596598
# This extra fuss is to re-order face / edge colors

lib/mpl_toolkits/mplot3d/proj3d.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,22 @@ def persp_transformation(zfront, zback):
156156

157157
def proj_transform_vec(vec, M):
158158
vecw = np.dot(M, vec)
159-
w = vecw[3]
160-
# clip here..
161-
txs, tys, tzs = vecw[0]/w, vecw[1]/w, vecw[2]/w
162-
return txs, tys, tzs
159+
vecw /= vecw[3]
160+
return vecw[0:3]
163161

164162
def proj_transform_vec_clip(vec, M):
165163
vecw = np.dot(M, vec)
166-
w = vecw[3]
164+
# Determine clipping before rescaling
165+
tis = np.logical_and(vecw[0] >= 0, vecw[0] <= 1,
166+
vecw[1] >= 0, vecw[1] <= 1)
167167
# clip here..
168-
txs, tys, tzs = vecw[0]/w, vecw[1]/w, vecw[2]/w
169-
tis = (vecw[0] >= 0) * (vecw[0] <= 1) * (vecw[1] >= 0) * (vecw[1] <= 1)
168+
# Can anybody comment on this piece of code? I don't understand it...
170169
if np.sometrue(tis):
171-
tis = vecw[1] < 1
172-
return txs, tys, tzs, tis
170+
tis = vecw[1] < 1
171+
vecw /= vecw[3]
172+
# Integrating tis in the numpy array for optimization purposes
173+
vecw[3, :] = tis
174+
return vecw
173175

174176
def inv_transform(xs, ys, zs, M):
175177
iM = linalg.inv(M)

0 commit comments

Comments
 (0)
0