Skip to content

Commit de8bc33

Browse files
authored
Merge pull request #7310 from QuLogic/mplot3d-proj3d-test
TST: Make proj3d tests into real tests
2 parents 3127815 + de67c8c commit de8bc33

File tree

4 files changed

+118
-89
lines changed

4 files changed

+118
-89
lines changed

lib/mpl_toolkits/mplot3d/proj3d.py

+1-87
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import six
1010
from six.moves import zip
1111

12-
from matplotlib.collections import LineCollection
13-
from matplotlib.patches import Circle
1412
import numpy as np
1513
import numpy.linalg as linalg
1614

@@ -69,27 +67,6 @@ def line2d_seg_dist(p1, p2, p0):
6967

7068
return d
7169

72-
def test_lines_dists():
73-
import pylab
74-
ax = pylab.gca()
75-
76-
xs, ys = (0,30), (20,150)
77-
pylab.plot(xs, ys)
78-
points = list(zip(xs, ys))
79-
p0, p1 = points
80-
81-
xs, ys = (0,0,20,30), (100,150,30,200)
82-
pylab.scatter(xs, ys)
83-
84-
dist = line2d_seg_dist(p0, p1, (xs[0], ys[0]))
85-
dist = line2d_seg_dist(p0, p1, np.array((xs, ys)))
86-
for x, y, d in zip(xs, ys, dist):
87-
c = Circle((x, y), d, fill=0)
88-
ax.add_patch(c)
89-
90-
pylab.xlim(-200, 200)
91-
pylab.ylim(-200, 200)
92-
pylab.show()
9370

9471
def mod(v):
9572
"""3d vector length"""
@@ -105,12 +82,6 @@ def world_transformation(xmin, xmax,
10582
[0,0,1.0/dz,-zmin/dz],
10683
[0,0,0,1.0]])
10784

108-
def test_world():
109-
xmin, xmax = 100, 120
110-
ymin, ymax = -100, 100
111-
zmin, zmax = 0.1, 0.2
112-
M = world_transformation(xmin, xmax, ymin, ymax, zmin, zmax)
113-
print(M)
11485

11586
def view_transformation(E, R, V):
11687
n = (E - R)
@@ -218,69 +189,12 @@ def proj_trans_clip_points(points, M):
218189
xs, ys, zs = list(zip(*points))
219190
return proj_transform_clip(xs, ys, zs, M)
220191

221-
def test_proj_draw_axes(M, s=1):
222-
import pylab
223-
xs, ys, zs = [0, s, 0, 0], [0, 0, s, 0], [0, 0, 0, s]
224-
txs, tys, tzs = proj_transform(xs, ys, zs, M)
225-
o, ax, ay, az = (txs[0], tys[0]), (txs[1], tys[1]), \
226-
(txs[2], tys[2]), (txs[3], tys[3])
227-
lines = [(o, ax), (o, ay), (o, az)]
228-
229-
ax = pylab.gca()
230-
linec = LineCollection(lines)
231-
ax.add_collection(linec)
232-
for x, y, t in zip(txs, tys, ['o', 'x', 'y', 'z']):
233-
pylab.text(x, y, t)
234-
235-
def test_proj_make_M(E=None):
236-
# eye point
237-
E = E or np.array([1, -1, 2]) * 1000
238-
#E = np.array([20,10,20])
239-
R = np.array([1, 1, 1]) * 100
240-
V = np.array([0, 0, 1])
241-
viewM = view_transformation(E, R, V)
242-
perspM = persp_transformation(100, -100)
243-
M = np.dot(perspM, viewM)
244-
return M
245-
246-
def test_proj():
247-
import pylab
248-
M = test_proj_make_M()
249-
250-
ts = ['%d' % i for i in [0,1,2,3,0,4,5,6,7,4]]
251-
xs, ys, zs = [0,1,1,0,0, 0,1,1,0,0], [0,0,1,1,0, 0,0,1,1,0], \
252-
[0,0,0,0,0, 1,1,1,1,1]
253-
xs, ys, zs = [np.array(v)*300 for v in (xs, ys, zs)]
254-
#
255-
test_proj_draw_axes(M, s=400)
256-
txs, tys, tzs = proj_transform(xs, ys, zs, M)
257-
ixs, iys, izs = inv_transform(txs, tys, tzs, M)
258-
259-
pylab.scatter(txs, tys, c=tzs)
260-
pylab.plot(txs, tys, c='r')
261-
for x, y, t in zip(txs, tys, ts):
262-
pylab.text(x, y, t)
263-
264-
pylab.xlim(-0.2, 0.2)
265-
pylab.ylim(-0.2, 0.2)
266-
267-
pylab.show()
268192

269193
def rot_x(V, alpha):
270194
cosa, sina = np.cos(alpha), np.sin(alpha)
271195
M1 = np.array([[1,0,0,0],
272196
[0,cosa,-sina,0],
273197
[0,sina,cosa,0],
274-
[0,0,0,0]])
198+
[0,0,0,1]])
275199

276200
return np.dot(M1, V)
277-
278-
def test_rot():
279-
V = [1,0,0,1]
280-
print(rot_x(V, np.pi/6))
281-
V = [0,1,0,1]
282-
print(rot_x(V, np.pi/6))
283-
284-
285-
if __name__ == "__main__":
286-
test_proj()

lib/mpl_toolkits/tests/test_mplot3d.py

+117-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from nose.tools import assert_raises
2-
from mpl_toolkits.mplot3d import Axes3D, axes3d
2+
from mpl_toolkits.mplot3d import Axes3D, axes3d, proj3d
33
from matplotlib import cm
44
from matplotlib.testing.decorators import image_comparison, cleanup
5+
from matplotlib.collections import LineCollection
6+
from matplotlib.patches import Circle
57
import matplotlib.pyplot as plt
68
import numpy as np
79

@@ -340,7 +342,120 @@ def test_plotsurface_1d_raises():
340342
fig = plt.figure(figsize=(14,6))
341343
ax = fig.add_subplot(1, 2, 1, projection='3d')
342344
assert_raises(ValueError, ax.plot_surface, X, Y, z)
343-
345+
346+
347+
def _test_proj_make_M():
348+
# eye point
349+
E = np.array([1000, -1000, 2000])
350+
R = np.array([100, 100, 100])
351+
V = np.array([0, 0, 1])
352+
viewM = proj3d.view_transformation(E, R, V)
353+
perspM = proj3d.persp_transformation(100, -100)
354+
M = np.dot(perspM, viewM)
355+
return M
356+
357+
358+
def test_proj_transform():
359+
M = _test_proj_make_M()
360+
361+
xs = np.array([0, 1, 1, 0, 0, 0, 1, 1, 0, 0]) * 300.0
362+
ys = np.array([0, 0, 1, 1, 0, 0, 0, 1, 1, 0]) * 300.0
363+
zs = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1]) * 300.0
364+
365+
txs, tys, tzs = proj3d.proj_transform(xs, ys, zs, M)
366+
ixs, iys, izs = proj3d.inv_transform(txs, tys, tzs, M)
367+
368+
np.testing.assert_almost_equal(ixs, xs)
369+
np.testing.assert_almost_equal(iys, ys)
370+
np.testing.assert_almost_equal(izs, zs)
371+
372+
373+
def _test_proj_draw_axes(M, s=1, *args, **kwargs):
374+
xs = [0, s, 0, 0]
375+
ys = [0, 0, s, 0]
376+
zs = [0, 0, 0, s]
377+
txs, tys, tzs = proj3d.proj_transform(xs, ys, zs, M)
378+
o, ax, ay, az = zip(txs, tys)
379+
lines = [(o, ax), (o, ay), (o, az)]
380+
381+
fig, ax = plt.subplots(*args, **kwargs)
382+
linec = LineCollection(lines)
383+
ax.add_collection(linec)
384+
for x, y, t in zip(txs, tys, ['o', 'x', 'y', 'z']):
385+
ax.text(x, y, t)
386+
387+
return fig, ax
388+
389+
390+
@image_comparison(baseline_images=['proj3d_axes_cube'], extensions=['png'],
391+
remove_text=True, style='default')
392+
def test_proj_axes_cube():
393+
M = _test_proj_make_M()
394+
395+
ts = '0 1 2 3 0 4 5 6 7 4'.split()
396+
xs = np.array([0, 1, 1, 0, 0, 0, 1, 1, 0, 0]) * 300.0
397+
ys = np.array([0, 0, 1, 1, 0, 0, 0, 1, 1, 0]) * 300.0
398+
zs = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1]) * 300.0
399+
400+
txs, tys, tzs = proj3d.proj_transform(xs, ys, zs, M)
401+
402+
fig, ax = _test_proj_draw_axes(M, s=400)
403+
404+
ax.scatter(txs, tys, c=tzs)
405+
ax.plot(txs, tys, c='r')
406+
for x, y, t in zip(txs, tys, ts):
407+
ax.text(x, y, t)
408+
409+
ax.set_xlim(-0.2, 0.2)
410+
ax.set_ylim(-0.2, 0.2)
411+
412+
413+
def test_rot():
414+
V = [1, 0, 0, 1]
415+
rotated_V = proj3d.rot_x(V, np.pi / 6)
416+
np.testing.assert_allclose(rotated_V, [1, 0, 0, 1])
417+
418+
V = [0, 1, 0, 1]
419+
rotated_V = proj3d.rot_x(V, np.pi / 6)
420+
np.testing.assert_allclose(rotated_V, [0, np.sqrt(3) / 2, 0.5, 1])
421+
422+
423+
def test_world():
424+
xmin, xmax = 100, 120
425+
ymin, ymax = -100, 100
426+
zmin, zmax = 0.1, 0.2
427+
M = proj3d.world_transformation(xmin, xmax, ymin, ymax, zmin, zmax)
428+
np.testing.assert_allclose(M,
429+
[[5e-2, 0, 0, -5],
430+
[0, 5e-3, 0, 5e-1],
431+
[0, 0, 1e1, -1],
432+
[0, 0, 0, 1]])
433+
434+
435+
@image_comparison(baseline_images=['proj3d_lines_dists'], extensions=['png'],
436+
remove_text=True, style='default')
437+
def test_lines_dists():
438+
fig, ax = plt.subplots(figsize=(4, 6), subplot_kw=dict(aspect='equal'))
439+
440+
xs = (0, 30)
441+
ys = (20, 150)
442+
ax.plot(xs, ys)
443+
p0, p1 = zip(xs, ys)
444+
445+
xs = (0, 0, 20, 30)
446+
ys = (100, 150, 30, 200)
447+
ax.scatter(xs, ys)
448+
449+
dist = proj3d.line2d_seg_dist(p0, p1, (xs[0], ys[0]))
450+
dist = proj3d.line2d_seg_dist(p0, p1, np.array((xs, ys)))
451+
for x, y, d in zip(xs, ys, dist):
452+
c = Circle((x, y), d, fill=0)
453+
ax.add_patch(c)
454+
455+
ax.set_xlim(-50, 150)
456+
ax.set_ylim(0, 300)
457+
458+
344459
if __name__ == '__main__':
345460
import nose
346461
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)