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

Skip to content

Commit c99765f

Browse files
committed
Convert functions in art3d to use transforms
1 parent 148f1b7 commit c99765f

File tree

2 files changed

+38
-50
lines changed

2 files changed

+38
-50
lines changed

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,11 @@ def set_3d_properties(self, z=0, zdir='z'):
148148
@artist.allow_rasterization
149149
def draw(self, renderer):
150150
position3d = np.array((self._x, self._y, self._z))
151-
proj = proj3d._proj_trans_points(
152-
[position3d, position3d + self._dir_vec], self.axes.M)
153-
dx = proj[0][1] - proj[0][0]
154-
dy = proj[1][1] - proj[1][0]
151+
proj = self.axes.M.transform([position3d, position3d + self._dir_vec])
152+
dx = proj[1][0] - proj[0][0]
153+
dy = proj[1][1] - proj[0][1]
155154
angle = math.degrees(math.atan2(dy, dx))
156-
with cbook._setattr_cm(self, _x=proj[0][0], _y=proj[1][0],
155+
with cbook._setattr_cm(self, _x=proj[0][0], _y=proj[0][1],
157156
_rotation=_norm_text_angle(angle)):
158157
mtext.Text.draw(self, renderer)
159158
self.stale = False
@@ -267,8 +266,8 @@ def get_data_3d(self):
267266
@artist.allow_rasterization
268267
def draw(self, renderer):
269268
xs3d, ys3d, zs3d = self._verts3d
270-
xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, self.axes.M)
271-
self.set_data(xs, ys)
269+
points = self.axes.M.transform(np.column_stack((xs3d, ys3d, zs3d)))
270+
self.set_data(points[:, 0], points[:, 1])
272271
super().draw(renderer)
273272
self.stale = False
274273

@@ -349,11 +348,11 @@ class Collection3D(Collection):
349348

350349
def do_3d_projection(self):
351350
"""Project the points according to renderer matrix."""
352-
xyzs_list = [proj3d.proj_transform(*vs.T, self.axes.M)
353-
for vs, _ in self._3dverts_codes]
354-
self._paths = [mpath.Path(np.column_stack([xs, ys]), cs)
355-
for (xs, ys, _), (_, cs) in zip(xyzs_list, self._3dverts_codes)]
356-
zs = np.concatenate([zs for _, _, zs in xyzs_list])
351+
path_vertices = [self.axes.M.transform(vs) for vs, _ in self._3dverts_codes]
352+
self._paths = [mpath.Path(vertices[:, :2], codes)
353+
for (vertices, (_, codes))
354+
in zip(path_vertices, self._3dverts_codes)]
355+
zs = np.concatenate(path_vertices)[:, 2]
357356
return zs.min() if len(zs) else 1e9
358357

359358

@@ -390,15 +389,14 @@ def do_3d_projection(self):
390389
"""
391390
Project the points according to renderer matrix.
392391
"""
393-
xyslist = [proj3d._proj_trans_points(points, self.axes.M)
394-
for points in self._segments3d]
395-
segments_2d = [np.column_stack([xs, ys]) for xs, ys, zs in xyslist]
392+
segments_3d = [self.axes.M.transform(segment) for segment in self._segments3d]
393+
segments_2d = [segment[:, :2] for segment in segments_3d]
396394
LineCollection.set_segments(self, segments_2d)
397395

398396
# FIXME
399397
minz = 1e9
400-
for xs, ys, zs in xyslist:
401-
minz = min(minz, min(zs))
398+
for segment in segments_3d:
399+
minz = min(minz, segment[0][2], segment[1][2])
402400
return minz
403401

404402

@@ -456,12 +454,10 @@ def get_path(self):
456454
return self._path2d
457455

458456
def do_3d_projection(self):
459-
s = self._segment3d
460-
xs, ys, zs = zip(*s)
461-
vxs, vys, vzs, vis = proj3d.proj_transform_clip(xs, ys, zs,
462-
self.axes.M)
463-
self._path2d = mpath.Path(np.column_stack([vxs, vys]))
464-
return min(vzs)
457+
segments = self.axes.M.transform(self._segment3d)
458+
self._path2d = mpath.Path(segments[:, :2])
459+
460+
return min(segments[:, 2])
465461

466462

467463
class PathPatch3D(Patch3D):
@@ -503,12 +499,10 @@ def set_3d_properties(self, path, zs=0, zdir='z'):
503499
self._code3d = path.codes
504500

505501
def do_3d_projection(self):
506-
s = self._segment3d
507-
xs, ys, zs = zip(*s)
508-
vxs, vys, vzs, vis = proj3d.proj_transform_clip(xs, ys, zs,
509-
self.axes.M)
510-
self._path2d = mpath.Path(np.column_stack([vxs, vys]), self._code3d)
511-
return min(vzs)
502+
segments = self.axes.M.transform(self._segment3d)
503+
self._path2d = mpath.Path(segments[:, :2], self._code3d)
504+
505+
return min(segments[:, 2])
512506

513507

514508
def _get_patch_verts(patch):
@@ -610,14 +604,13 @@ def set_3d_properties(self, zs, zdir):
610604
self.stale = True
611605

612606
def do_3d_projection(self):
613-
xs, ys, zs = self._offsets3d
614-
vxs, vys, vzs, vis = proj3d.proj_transform_clip(xs, ys, zs,
615-
self.axes.M)
616-
self._vzs = vzs
617-
super().set_offsets(np.column_stack([vxs, vys]))
607+
points = self.axes.M.transform(np.column_stack(self._offsets3d))
608+
super().set_offsets(points[:, :2])
609+
610+
self._vzs = points[:, 2]
618611

619-
if vzs.size > 0:
620-
return min(vzs)
612+
if self._vzs.size > 0:
613+
return min(self._vzs)
621614
else:
622615
return np.nan
623616

@@ -751,37 +744,31 @@ def set_depthshade(self, depthshade):
751744
self.stale = True
752745

753746
def do_3d_projection(self):
754-
xs, ys, zs = self._offsets3d
755-
vxs, vys, vzs, vis = proj3d.proj_transform_clip(xs, ys, zs,
756-
self.axes.M)
757747
# Sort the points based on z coordinates
758748
# Performance optimization: Create a sorted index array and reorder
759749
# points and point properties according to the index array
760-
z_markers_idx = self._z_markers_idx = np.argsort(vzs)[::-1]
761-
self._vzs = vzs
750+
points = self.axes.M.transform(np.column_stack(self._offsets3d))
751+
z_markers_idx = self._z_markers_idx = np.argsort(points[:, 2])[::-1]
752+
self._vzs = points[:, 2]
762753

763754
# we have to special case the sizes because of code in collections.py
764755
# as the draw method does
765756
# self.set_sizes(self._sizes, self.figure.dpi)
766757
# so we cannot rely on doing the sorting on the way out via get_*
767-
768758
if len(self._sizes3d) > 1:
769759
self._sizes = self._sizes3d[z_markers_idx]
770760

771761
if len(self._linewidths3d) > 1:
772762
self._linewidths = self._linewidths3d[z_markers_idx]
773763

774-
PathCollection.set_offsets(self, np.column_stack((vxs, vys)))
764+
PathCollection.set_offsets(self, points[:, :2])
775765

776766
# Re-order items
777-
vzs = vzs[z_markers_idx]
778-
vxs = vxs[z_markers_idx]
779-
vys = vys[z_markers_idx]
767+
points = points[z_markers_idx]
780768

781769
# Store ordered offset for drawing purpose
782-
self._offset_zordered = np.column_stack((vxs, vys))
783-
784-
return np.min(vzs) if vzs.size else np.nan
770+
self._offset_zordered = points[:, :2]
771+
return np.min(self._vzs) if self._vzs.size else np.nan
785772

786773
@contextmanager
787774
def _use_zordered_offset(self):

lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ def test_axes3d_repr():
113113
"title={'center': 'title'}, xlabel='x', ylabel='y', zlabel='z'>")
114114

115115

116-
@mpl3d_image_comparison(['axes3d_primary_views.png'], style='mpl20')
116+
@mpl3d_image_comparison(['axes3d_primary_views.png'], style='mpl20', tol=0 if
117+
platform.machine() == 'x86_64' else 0.045)
117118
def test_axes3d_primary_views():
118119
# (elev, azim, roll)
119120
views = [(90, -90, 0), # XY

0 commit comments

Comments
 (0)