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

Skip to content

Commit 8b8c7aa

Browse files
Code review cleanup
1 parent a24160f commit 8b8c7aa

3 files changed

Lines changed: 35 additions & 78 deletions

File tree

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,9 @@ def draw(self, renderer):
199199
pos3d = np.array([self._x, self._y, self._z], dtype=float)
200200

201201
dir_end = pos3d + self._dir_vec
202-
proj = proj3d._proj_trans_points_scaled(
203-
[pos3d, dir_end], self.axes)
202+
points = np.asarray([pos3d, dir_end])
203+
proj = proj3d._scale_proj_transform(
204+
points[:, 0], points[:, 1], points[:, 2], self.axes)
204205
dx = proj[0][1] - proj[0][0]
205206
dy = proj[1][1] - proj[1][0]
206207
angle = math.degrees(math.atan2(dy, dx))
@@ -336,7 +337,7 @@ def draw(self, renderer):
336337
dtype=float, mask=mask).filled(np.nan)
337338
else:
338339
xs3d, ys3d, zs3d = self._verts3d
339-
xs, ys, zs, tis = proj3d._proj_transform_clip_scaled(
340+
xs, ys, zs, tis = proj3d._scale_proj_transform_clip(
340341
xs3d, ys3d, zs3d, self.axes)
341342
self.set_data(xs, ys)
342343
super().draw(renderer)
@@ -428,7 +429,7 @@ def do_3d_projection(self):
428429
vs_list = [np.ma.array(vs, mask=np.broadcast_to(
429430
_viewlim_mask(*vs.T, self.axes), vs.shape))
430431
for vs in vs_list]
431-
xyzs_list = [proj3d._proj_transform_scaled(
432+
xyzs_list = [proj3d._scale_proj_transform(
432433
vs[:, 0], vs[:, 1], vs[:, 2], self.axes) for vs in vs_list]
433434
self._paths = [mpath.Path(np.ma.column_stack([xs, ys]), cs)
434435
for (xs, ys, _), (_, cs) in zip(xyzs_list, self._3dverts_codes)]
@@ -520,7 +521,7 @@ def do_3d_projection(self):
520521
mask = mask | viewlim_mask
521522

522523
xyzs = np.ma.array(
523-
proj3d._proj_transform_vectors_scaled(segments, self.axes),
524+
proj3d._scale_proj_transform_vectors(segments, self.axes),
524525
mask=mask)
525526
segments_2d = xyzs[..., 0:2]
526527
LineCollection.set_segments(self, segments_2d)
@@ -604,7 +605,7 @@ def do_3d_projection(self):
604605
dtype=float, mask=mask).filled(np.nan)
605606
else:
606607
xs, ys, zs = zip(*s)
607-
vxs, vys, vzs, vis = proj3d._proj_transform_clip_scaled(
608+
vxs, vys, vzs, vis = proj3d._scale_proj_transform_clip(
608609
xs, ys, zs, self.axes)
609610
self._path2d = mpath.Path(np.ma.column_stack([vxs, vys]))
610611
return min(vzs)
@@ -665,7 +666,7 @@ def do_3d_projection(self):
665666
dtype=float, mask=mask).filled(np.nan)
666667
else:
667668
xs, ys, zs = zip(*s)
668-
vxs, vys, vzs, vis = proj3d._proj_transform_clip_scaled(
669+
vxs, vys, vzs, vis = proj3d._scale_proj_transform_clip(
669670
xs, ys, zs, self.axes)
670671
self._path2d = mpath.Path(np.ma.column_stack([vxs, vys]), self._code3d)
671672
return min(vzs)
@@ -809,7 +810,7 @@ def do_3d_projection(self):
809810
xs, ys, zs = np.ma.array(self._offsets3d, mask=mask)
810811
else:
811812
xs, ys, zs = self._offsets3d
812-
vxs, vys, vzs, vis = proj3d._proj_transform_clip_scaled(
813+
vxs, vys, vzs, vis = proj3d._scale_proj_transform_clip(
813814
xs, ys, zs, self.axes)
814815
self._vzs = vzs
815816
if np.ma.isMA(vxs):
@@ -1026,7 +1027,7 @@ def do_3d_projection(self):
10261027
xyzs = np.ma.array(self._offsets3d, mask=mask)
10271028
else:
10281029
xyzs = self._offsets3d
1029-
vxs, vys, vzs, vis = proj3d._proj_transform_clip_scaled(
1030+
vxs, vys, vzs, vis = proj3d._scale_proj_transform_clip(
10301031
*xyzs, self.axes)
10311032
self._data_scale = _get_data_scale(vxs, vys, vzs)
10321033
# Sort the points based on z coordinates
@@ -1361,7 +1362,7 @@ def do_3d_projection(self):
13611362
# Some faces might contain masked vertices, so we want to ignore any
13621363
# errors that those might cause
13631364
with np.errstate(invalid='ignore', divide='ignore'):
1364-
pfaces = proj3d._proj_transform_vectors_scaled(
1365+
pfaces = proj3d._scale_proj_transform_vectors(
13651366
self._faces, self.axes)
13661367

13671368
if self._axlim_clip:

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def _transformed_cube(self, vals):
271271
(maxx, miny, maxz),
272272
(maxx, maxy, maxz),
273273
(minx, maxy, maxz)]
274-
return proj3d._proj_points(xyzs, self.M)
274+
return np.column_stack(proj3d._proj_trans_points(xyzs, self.M))
275275

276276
def _update_transScale(self):
277277
"""
@@ -1138,7 +1138,7 @@ def _set_axis_scale(self, axis, get_lim, set_lim, value, **kwargs):
11381138
"""
11391139
axis._set_axes_scale(value, **kwargs)
11401140
# After setting scale, constrain limits using scale's limit_range_for_scale
1141-
if hasattr(axis, '_scale') and axis._scale is not None:
1141+
if getattr(axis, '_scale', None) is not None:
11421142
vmin, vmax = get_lim()
11431143
minpos = getattr(axis, '_minpos', 1e-300)
11441144
new_vmin, new_vmax = axis._scale.limit_range_for_scale(
@@ -1378,7 +1378,7 @@ def _untransform_point(self, x, y, z):
13781378
return x_data, y_data, z_data
13791379

13801380
def _set_lims_from_transformed(self, xmin_t, xmax_t, ymin_t, ymax_t,
1381-
zmin_t, zmax_t):
1381+
zmin_t, zmax_t):
13821382
"""
13831383
Set axis limits from transformed coordinates.
13841384
@@ -1683,10 +1683,7 @@ def _calc_coord(self, xv, yv, renderer=None):
16831683
vec = self._get_camera_loc() - p1
16841684

16851685
# Get the pane locations for each of the axes
1686-
pane_locs_data = []
1687-
for axis in self._axis_map.values():
1688-
xys, loc = axis.active_pane()
1689-
pane_locs_data.append(loc)
1686+
pane_locs_data = [axis.active_pane()[1] for axis in self._axis_map.values()]
16901687

16911688
# Find the distance to the nearest pane by projecting the view vector
16921689
scales = np.zeros(3)

lib/mpl_toolkits/mplot3d/proj3d.py

Lines changed: 20 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -166,27 +166,24 @@ def _proj_transform_vec(vec, M):
166166
return ts[0], ts[1], ts[2]
167167

168168

169-
def _proj_transform_vectors(vecs, M):
169+
def _scale_proj_transform_vectors(vecs, axes):
170170
"""
171-
Vectorized version of ``_proj_transform_vec``.
171+
Apply scale transforms and project vectors.
172172
173173
Parameters
174174
----------
175175
vecs : ... x 3 np.ndarray
176-
Input vectors
177-
M : 4 x 4 np.ndarray
178-
Projection matrix
176+
Input vectors.
177+
axes : Axes3D
178+
The 3D axes (used for scale transforms and projection matrix).
179179
"""
180-
vecs_shape = vecs.shape
181-
vecs = vecs.reshape(-1, 3).T
182-
183-
vecs_pad = np.empty((vecs.shape[0] + 1,) + vecs.shape[1:])
184-
vecs_pad[:-1] = vecs
185-
vecs_pad[-1] = 1
186-
product = np.dot(M, vecs_pad)
180+
result_shape = vecs.shape
181+
xs, ys, zs = _apply_scale_transforms(
182+
vecs[..., 0], vecs[..., 1], vecs[..., 2], axes)
183+
vec = _vec_pad_ones(xs.ravel(), ys.ravel(), zs.ravel())
184+
product = np.dot(axes.M, vec)
187185
tvecs = product[:3] / product[3]
188-
189-
return tvecs.T.reshape(vecs_shape)
186+
return tvecs.T.reshape(result_shape)
190187

191188

192189
def _proj_transform_vec_clip(vec, M, focal_length):
@@ -240,21 +237,19 @@ def proj_transform(xs, ys, zs, M):
240237

241238
@_api.deprecated("3.10")
242239
def proj_transform_clip(xs, ys, zs, M):
243-
return _proj_transform_clip(xs, ys, zs, M, focal_length=np.inf)
240+
vec = _vec_pad_ones(xs, ys, zs)
241+
return _proj_transform_vec_clip(vec, M, focal_length=np.inf)
244242

245243

246-
def _proj_transform_clip(xs, ys, zs, M, focal_length):
244+
def _scale_proj_transform_clip(xs, ys, zs, axes):
247245
"""
248-
Transform the points by the projection matrix
249-
and return the clipping result
250-
returns txs, tys, tzs, tis
246+
Apply scale transforms, project, and return clipping result.
247+
248+
Returns txs, tys, tzs, tis.
251249
"""
250+
xs, ys, zs = _apply_scale_transforms(xs, ys, zs, axes)
252251
vec = _vec_pad_ones(xs, ys, zs)
253-
return _proj_transform_vec_clip(vec, M, focal_length)
254-
255-
256-
def _proj_points(points, M):
257-
return np.column_stack(_proj_trans_points(points, M))
252+
return _proj_transform_vec_clip(vec, axes.M, axes._focal_length)
258253

259254

260255
def _proj_trans_points(points, M):
@@ -263,31 +258,7 @@ def _proj_trans_points(points, M):
263258
return proj_transform(xs, ys, zs, M)
264259

265260

266-
def _proj_transform_clip_scaled(xs, ys, zs, axes):
267-
"""
268-
Apply scale transforms and project with clipping.
269-
270-
Combines `_apply_scale_transforms` and `_proj_transform_clip` into a
271-
single call. Returns txs, tys, tzs, tis.
272-
"""
273-
xs, ys, zs = _apply_scale_transforms(xs, ys, zs, axes)
274-
return _proj_transform_clip(xs, ys, zs, axes.M, axes._focal_length)
275-
276-
277-
def _proj_transform_vectors_scaled(vecs, axes):
278-
"""
279-
Apply scale transforms and project vectors.
280-
281-
Combines `_apply_scale_transforms` and `_proj_transform_vectors` into a
282-
single call. *vecs* has shape ``(..., 3)``.
283-
"""
284-
xs, ys, zs = _apply_scale_transforms(
285-
vecs[..., 0], vecs[..., 1], vecs[..., 2], axes)
286-
vecs = np.stack([xs, ys, zs], axis=-1)
287-
return _proj_transform_vectors(vecs, axes.M)
288-
289-
290-
def _proj_transform_scaled(xs, ys, zs, axes):
261+
def _scale_proj_transform(xs, ys, zs, axes):
291262
"""
292263
Apply scale transforms and project.
293264
@@ -296,15 +267,3 @@ def _proj_transform_scaled(xs, ys, zs, axes):
296267
"""
297268
xs, ys, zs = _apply_scale_transforms(xs, ys, zs, axes)
298269
return proj_transform(xs, ys, zs, axes.M)
299-
300-
301-
def _proj_trans_points_scaled(points, axes):
302-
"""
303-
Apply scale transforms and project points.
304-
305-
Combines `_apply_scale_transforms` and `_proj_trans_points` into a single
306-
call.
307-
"""
308-
points = np.asanyarray(points)
309-
xs, ys, zs = points[:, 0], points[:, 1], points[:, 2]
310-
return _proj_transform_scaled(xs, ys, zs, axes)

0 commit comments

Comments
 (0)