-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Expand file tree
/
Copy pathtest_art3d.py
More file actions
119 lines (95 loc) · 3.68 KB
/
test_art3d.py
File metadata and controls
119 lines (95 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import numpy as np
import numpy.testing as nptest
import pytest
import matplotlib.pyplot as plt
from matplotlib.backend_bases import MouseEvent
from mpl_toolkits.mplot3d.art3d import (
get_dir_vector,
Line3DCollection,
Poly3DCollection,
_all_points_on_plane,
)
@pytest.mark.parametrize("zdir, expected", [
("x", (1, 0, 0)),
("y", (0, 1, 0)),
("z", (0, 0, 1)),
(None, (0, 0, 0)),
((1, 2, 3), (1, 2, 3)),
(np.array([4, 5, 6]), (4, 5, 6)),
])
def test_get_dir_vector(zdir, expected):
res = get_dir_vector(zdir)
assert isinstance(res, np.ndarray)
nptest.assert_array_equal(res, expected)
def test_scatter_3d_projection_conservation():
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
# fix axes3d projection
ax.roll = 0
ax.elev = 0
ax.azim = -45
ax.stale = True
x = [0, 1, 2, 3, 4]
scatter_collection = ax.scatter(x, x, x)
fig.canvas.draw_idle()
# Get scatter location on canvas and freeze the data
scatter_offset = scatter_collection.get_offsets()
scatter_location = ax.transData.transform(scatter_offset)
# Yaw -44 and -46 are enough to produce two set of scatter
# with opposite z-order without moving points too far
for azim in (-44, -46):
ax.azim = azim
ax.stale = True
fig.canvas.draw_idle()
for i in range(5):
# Create a mouse event used to locate and to get index
# from each dots
event = MouseEvent("button_press_event", fig.canvas,
*scatter_location[i, :])
contains, ind = scatter_collection.contains(event)
assert contains is True
assert len(ind["ind"]) == 1
assert ind["ind"][0] == i
def test_zordered_error():
# Smoke test for https://github.com/matplotlib/matplotlib/issues/26497
lc = [(np.fromiter([0.0, 0.0, 0.0], dtype="float"),
np.fromiter([1.0, 1.0, 1.0], dtype="float"))]
pc = [np.fromiter([0.0, 0.0], dtype="float"),
np.fromiter([0.0, 1.0], dtype="float"),
np.fromiter([1.0, 1.0], dtype="float")]
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
ax.add_collection(Line3DCollection(lc), autolim="_datalim_only")
ax.scatter(*pc, visible=False)
plt.draw()
def test_all_points_on_plane():
# Non-coplanar points
points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]])
assert not _all_points_on_plane(*points.T)
# Duplicate points
points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 0]])
assert _all_points_on_plane(*points.T)
# NaN values
points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, np.nan]])
assert _all_points_on_plane(*points.T)
# Less than 3 unique points
points = np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]])
assert _all_points_on_plane(*points.T)
# All points lie on a line
points = np.array([[0, 0, 0], [0, 1, 0], [0, 2, 0], [0, 3, 0]])
assert _all_points_on_plane(*points.T)
# All points lie on two lines, with antiparallel vectors
points = np.array([[-2, 2, 0], [-1, 1, 0], [1, -1, 0],
[0, 0, 0], [2, 0, 0], [1, 0, 0]])
assert _all_points_on_plane(*points.T)
# All points lie on a plane
points = np.array([[0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 1, 0], [1, 2, 0]])
assert _all_points_on_plane(*points.T)
def test_generate_normals():
# Smoke test for https://github.com/matplotlib/matplotlib/issues/29156
vertices = ((0, 0, 0), (0, 5, 0), (5, 5, 0), (5, 0, 0))
shape = Poly3DCollection([vertices], edgecolors='r', shade=True)
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.add_collection3d(shape)
plt.draw()