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

Skip to content

Commit af5946b

Browse files
committed
Add transforms needed for Axes3D
1 parent 76efb2a commit af5946b

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

lib/mpl_toolkits/mplot3d/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ python_sources = [
44
'axes3d.py',
55
'axis3d.py',
66
'proj3d.py',
7+
'transform3d.py',
78
]
89

910
py3.install_sources(python_sources, subdir: 'mpl_toolkits/mplot3d')
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import numpy as np
2+
import matplotlib.transforms as mtransforms
3+
4+
5+
class NonAffine3D(mtransforms.Transform):
6+
input_dims = output_dims = 3
7+
is_affine = False
8+
9+
def __init__(self, *args, matrix=None, **kwargs):
10+
super().__init__(*args, **kwargs)
11+
if matrix is None:
12+
matrix = np.identity(4)
13+
self._mtx = matrix.copy()
14+
self._invalid = 0
15+
16+
def transform_non_affine(self, values):
17+
mtx = self.get_matrix()
18+
19+
if isinstance(values, np.ma.MaskedArray):
20+
tpoints = mtransforms.matrix_transform(values.data, mtx)
21+
return np.ma.MaskedArray(tpoints, mask=np.ma.getmask(values))
22+
return mtransforms.matrix_transform(values, mtx)
23+
24+
def inverted(self):
25+
return NonAffine3D(matrix=np.linalg.inv(self._mtx))
26+
27+
28+
class WorldTransform(mtransforms.Affine3D):
29+
def __init__(self, xmin, xmax, ymin, ymax, zmin, zmax, pb_aspect=None):
30+
dx = xmax - xmin
31+
dy = ymax - ymin
32+
dz = zmax - zmin
33+
if pb_aspect is not None:
34+
ax, ay, az = pb_aspect
35+
dx /= ax
36+
dy /= ay
37+
dz /= az
38+
mtx = np.array([
39+
[1/dx, 0, 0, -xmin/dx],
40+
[0, 1/dy, 0, -ymin/dy],
41+
[0, 0, 1/dz, -zmin/dz],
42+
[0, 0, 0, 1]
43+
])
44+
super().__init__(matrix=mtx)
45+
46+
47+
class PerspectiveTransform(NonAffine3D):
48+
def __init__(self, zfront, zback, focal_length):
49+
e = focal_length
50+
a = 1
51+
b = (zfront + zback) / (zfront - zback)
52+
c = -2 * (zfront * zback) / (zfront - zback)
53+
mtx = np.array([[e, 0, 0, 0],
54+
[0, e/a, 0, 0],
55+
[0, 0, b, c],
56+
[0, 0, -1, 0]])
57+
super().__init__(matrix=mtx)
58+
59+
60+
class OrthographicTransform(NonAffine3D):
61+
def __init__(self, zfront, zback):
62+
a = -(zfront + zback)
63+
b = -(zfront - zback)
64+
mtx = np.array([[2, 0, 0, 0],
65+
[0, 2, 0, 0],
66+
[0, 0, -2, 0],
67+
[0, 0, a, b]])
68+
super().__init__(matrix=mtx)
69+
70+
71+
class ViewTransform(mtransforms.Affine3D):
72+
def __init__(self, u, v, w, E):
73+
"""
74+
Return the view transformation matrix.
75+
76+
Parameters
77+
----------
78+
u : 3-element numpy array
79+
Unit vector pointing towards the right of the screen.
80+
v : 3-element numpy array
81+
Unit vector pointing towards the top of the screen.
82+
w : 3-element numpy array
83+
Unit vector pointing out of the screen.
84+
E : 3-element numpy array
85+
The coordinates of the eye/camera.
86+
"""
87+
self._u = u
88+
self._v = v
89+
self._w = w
90+
91+
Mr = np.eye(4)
92+
Mt = np.eye(4)
93+
Mr[:3, :3] = [u, v, w]
94+
Mt[:3, -1] = -E
95+
mtx = np.dot(Mr, Mt)
96+
super().__init__(matrix=mtx)

0 commit comments

Comments
 (0)