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

Skip to content

Commit b903eec

Browse files
committed
Added TriFinder and TrapezoidMapTriFinder classes.
1 parent 1787a1a commit b903eec

8 files changed

Lines changed: 1806 additions & 27 deletions

File tree

doc/api/tri_api.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ triangular grids
55
:mod:`matplotlib.tri`
66
=====================
77

8-
.. automodule:: matplotlib.tri
9-
:members: Triangulation
8+
.. autoclass:: matplotlib.tri.Triangulation
9+
:members:
1010

11+
.. autoclass:: matplotlib.tri.TriFinder
12+
:members:
13+
14+
.. autoclass:: matplotlib.tri.TrapezoidMapTriFinder
15+
:members: __call__
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
Example showing the use of a TriFinder object. As the mouse is moved over the
3+
triangulation, the triangle under the cursor is highlighted and the index of
4+
the triangle is displayed in the plot title.
5+
"""
6+
import matplotlib.pyplot as plt
7+
from matplotlib.tri import Triangulation
8+
from matplotlib.patches import Polygon
9+
import numpy as np
10+
import math
11+
12+
13+
def update_polygon(tri):
14+
if tri == -1:
15+
points = [0, 0, 0]
16+
else:
17+
points = triangulation.triangles[tri]
18+
xs = triangulation.x[points]
19+
ys = triangulation.y[points]
20+
polygon.set_xy(zip(xs, ys))
21+
22+
23+
def motion_notify(event):
24+
if event.inaxes is None:
25+
tri = -1
26+
else:
27+
tri = trifinder(event.xdata, event.ydata)
28+
update_polygon(tri)
29+
plt.title('In triangle %i' % tri)
30+
event.canvas.draw()
31+
32+
33+
# Create a Triangulation.
34+
n_angles = 16
35+
n_radii = 5
36+
min_radius = 0.25
37+
radii = np.linspace(min_radius, 0.95, n_radii)
38+
angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False)
39+
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
40+
angles[:, 1::2] += math.pi / n_angles
41+
x = (radii*np.cos(angles)).flatten()
42+
y = (radii*np.sin(angles)).flatten()
43+
triangulation = Triangulation(x, y)
44+
xmid = x[triangulation.triangles].mean(axis=1)
45+
ymid = y[triangulation.triangles].mean(axis=1)
46+
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)
47+
triangulation.set_mask(mask)
48+
49+
# Use the triangulation's default TriFinder object.
50+
trifinder = triangulation.get_trifinder()
51+
52+
# Setup plot and callbacks.
53+
plt.subplot(111, aspect='equal')
54+
plt.triplot(triangulation, 'bo-')
55+
polygon = Polygon([[0, 0], [0, 0]], facecolor='y') # dummy data for xs,ys
56+
update_polygon(-1)
57+
plt.gca().add_patch(polygon)
58+
plt.gcf().canvas.mpl_connect('motion_notify_event', motion_notify)
59+
plt.show()

lib/matplotlib/tests/test_triangulation.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,91 @@ def test_no_modify():
131131
tri = mtri.Triangulation(points[:,0], points[:,1], triangles)
132132
edges = tri.edges
133133
assert_array_equal(old_triangles, triangles)
134+
135+
def test_trifinder():
136+
# Test points within triangles of masked triangulation.
137+
x,y = np.meshgrid(np.arange(4), np.arange(4))
138+
x = x.ravel()
139+
y = y.ravel()
140+
triangles = [[0,1,4], [1,5,4], [1,2,5], [2,6,5], [2,3,6], [3,7,6], [4,5,8],
141+
[5,9,8], [5,6,9], [6,10,9], [6,7,10], [7,11,10], [8,9,12],
142+
[9,13,12], [9,10,13], [10,14,13], [10,11,14], [11,15,14]]
143+
mask = np.zeros(len(triangles))
144+
mask[8:10] = 1
145+
triang = mtri.Triangulation(x, y, triangles, mask)
146+
trifinder = triang.get_trifinder()
147+
148+
xs = [0.25, 1.25, 2.25, 3.25]
149+
ys = [0.25, 1.25, 2.25, 3.25]
150+
xs,ys = np.meshgrid(xs,ys)
151+
xs = xs.ravel()
152+
ys = ys.ravel()
153+
tris = trifinder(xs, ys)
154+
assert_array_equal(tris, [0,2,4,-1,6,-1,10,-1,12,14,16,-1,-1,-1,-1,-1])
155+
tris = trifinder(xs-0.5, ys-0.5)
156+
assert_array_equal(tris, [-1,-1,-1,-1,-1,1,3,5,-1,7,-1,11,-1,13,15,17])
157+
158+
# Test points exactly on boundary edges of masked triangulation.
159+
xs = [0.5, 1.5, 2.5, 0.5, 1.5, 2.5, 1.5, 1.5, 0.0, 1.0, 2.0, 3.0]
160+
ys = [0.0, 0.0, 0.0, 3.0, 3.0, 3.0, 1.0, 2.0, 1.5, 1.5, 1.5, 1.5]
161+
tris = trifinder(xs, ys)
162+
assert_array_equal(tris, [0,2,4,13,15,17,3,14,6,7,10,11])
163+
164+
# Test points exactly on boundary corners of masked triangulation.
165+
xs = [0.0, 3.0]
166+
ys = [0.0, 3.0]
167+
tris = trifinder(xs, ys)
168+
assert_array_equal(tris, [0,17])
169+
170+
# Test triangles with horizontal colinear points. These are not valid
171+
# triangulations, but we try to deal with the simplest violations.
172+
delta = 0.0 # If +ve, triangulation is OK, if -ve triangulation invalid,
173+
# if zero have colinear points but should pass tests anyway.
174+
x = [1.5, 0, 1, 2, 3, 1.5, 1.5]
175+
y = [-1, 0, 0, 0, 0, delta, 1]
176+
triangles = [[0,2,1], [0,3,2], [0,4,3], [1,2,5], [2,3,5], [3,4,5], [1,5,6],
177+
[4,6,5]]
178+
triang = mtri.Triangulation(x, y, triangles)
179+
trifinder = triang.get_trifinder()
180+
181+
xs = [-0.1, 0.4, 0.9, 1.4, 1.9, 2.4, 2.9]
182+
ys = [-0.1,0.1]
183+
xs,ys = np.meshgrid(xs, ys)
184+
tris = trifinder(xs, ys)
185+
assert_array_equal(tris, [[-1,0,0,1,1,2,-1],[-1,6,6,6,7,7,-1]])
186+
187+
# Test triangles with vertical colinear points. These are not valid
188+
# triangulations, but we try to deal with the simplest violations.
189+
delta = 0.0 # If +ve, triangulation is OK, if -ve triangulation invalid,
190+
# if zero have colinear points but should pass tests anyway.
191+
x = [-1, -delta, 0, 0, 0, 0, 1]
192+
y = [1.5, 1.5, 0, 1, 2, 3, 1.5]
193+
triangles = [[0,1,2], [0,1,5], [1,2,3], [1,3,4], [1,4,5], [2,6,3], [3,6,4],
194+
[4,6,5]]
195+
triang = mtri.Triangulation(x, y, triangles)
196+
trifinder = triang.get_trifinder()
197+
198+
xs = [-0.1,0.1]
199+
ys = [-0.1, 0.4, 0.9, 1.4, 1.9, 2.4, 2.9]
200+
xs,ys = np.meshgrid(xs, ys)
201+
tris = trifinder(xs, ys)
202+
assert_array_equal(tris, [[-1,-1], [0,5], [0,5], [0,6], [1,6], [1,7],
203+
[-1,-1]])
204+
205+
# Test that changing triangulation by setting a mask causes the trifinder
206+
# to be reinitialised.
207+
x = [0, 1, 0, 1]
208+
y = [0, 0, 1, 1]
209+
triangles = [[0,1,2], [1,3,2]]
210+
triang = mtri.Triangulation(x, y, triangles)
211+
trifinder = triang.get_trifinder()
212+
213+
xs = [-0.2, 0.2, 0.8, 1.2]
214+
ys = [0.5, 0.5, 0.5, 0.5]
215+
tris = trifinder(xs, ys)
216+
assert_array_equal(tris, [-1, 0, 1, -1])
217+
218+
triang.set_mask([1, 0])
219+
assert_equal(trifinder, triang.get_trifinder())
220+
tris = trifinder(xs, ys)
221+
assert_array_equal(tris, [-1, -1, 1, -1])

lib/matplotlib/tri/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
from __future__ import print_function
66
from triangulation import *
77
from tricontour import *
8+
from trifinder import *
89
from tripcolor import *
910
from triplot import *

0 commit comments

Comments
 (0)