Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/matplotlib/tri/trifinder.py : 42%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import numpy as np
3from matplotlib import cbook
4from matplotlib.tri import Triangulation
7class TriFinder:
8 """
9 Abstract base class for classes used to find the triangles of a
10 Triangulation in which (x, y) points lie.
12 Rather than instantiate an object of a class derived from TriFinder, it is
13 usually better to use the function
14 :func:`matplotlib.tri.Triangulation.get_trifinder`.
16 Derived classes implement __call__(x, y) where x and y are array-like point
17 coordinates of the same shape.
18 """
19 def __init__(self, triangulation):
20 cbook._check_isinstance(Triangulation, triangulation=triangulation)
21 self._triangulation = triangulation
24class TrapezoidMapTriFinder(TriFinder):
25 """
26 :class:`~matplotlib.tri.TriFinder` class implemented using the trapezoid
27 map algorithm from the book "Computational Geometry, Algorithms and
28 Applications", second edition, by M. de Berg, M. van Kreveld, M. Overmars
29 and O. Schwarzkopf.
31 The triangulation must be valid, i.e. it must not have duplicate points,
32 triangles formed from colinear points, or overlapping triangles. The
33 algorithm has some tolerance to triangles formed from colinear points, but
34 this should not be relied upon.
35 """
36 def __init__(self, triangulation):
37 from matplotlib import _tri
38 TriFinder.__init__(self, triangulation)
39 self._cpp_trifinder = _tri.TrapezoidMapTriFinder(
40 triangulation.get_cpp_triangulation())
41 self._initialize()
43 def __call__(self, x, y):
44 """
45 Return an array containing the indices of the triangles in which the
46 specified *x*, *y* points lie, or -1 for points that do not lie within
47 a triangle.
49 *x*, *y* are array-like x and y coordinates of the same shape and any
50 number of dimensions.
52 Returns integer array with the same shape and *x* and *y*.
53 """
54 x = np.asarray(x, dtype=np.float64)
55 y = np.asarray(y, dtype=np.float64)
56 if x.shape != y.shape:
57 raise ValueError("x and y must be array-like with the same shape")
59 # C++ does the heavy lifting, and expects 1D arrays.
60 indices = (self._cpp_trifinder.find_many(x.ravel(), y.ravel())
61 .reshape(x.shape))
62 return indices
64 def _get_tree_stats(self):
65 """
66 Return a python list containing the statistics about the node tree:
67 0: number of nodes (tree size)
68 1: number of unique nodes
69 2: number of trapezoids (tree leaf nodes)
70 3: number of unique trapezoids
71 4: maximum parent count (max number of times a node is repeated in
72 tree)
73 5: maximum depth of tree (one more than the maximum number of
74 comparisons needed to search through the tree)
75 6: mean of all trapezoid depths (one more than the average number
76 of comparisons needed to search through the tree)
77 """
78 return self._cpp_trifinder.get_tree_stats()
80 def _initialize(self):
81 """
82 Initialize the underlying C++ object. Can be called multiple times if,
83 for example, the triangulation is modified.
84 """
85 self._cpp_trifinder.initialize()
87 def _print_tree(self):
88 """
89 Print a text representation of the node tree, which is useful for
90 debugging purposes.
91 """
92 self._cpp_trifinder.print_tree()