Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/matplotlib/tri/_trifinder.py: 43%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

28 statements  

1import numpy as np 

2 

3from matplotlib import _api 

4from matplotlib.tri import Triangulation 

5 

6 

7class TriFinder: 

8 """ 

9 Abstract base class for classes used to find the triangles of a 

10 Triangulation in which (x, y) points lie. 

11 

12 Rather than instantiate an object of a class derived from TriFinder, it is 

13 usually better to use the function `.Triangulation.get_trifinder`. 

14 

15 Derived classes implement __call__(x, y) where x and y are array-like point 

16 coordinates of the same shape. 

17 """ 

18 

19 def __init__(self, triangulation): 

20 _api.check_isinstance(Triangulation, triangulation=triangulation) 

21 self._triangulation = triangulation 

22 

23 def __call__(self, x, y): 

24 raise NotImplementedError 

25 

26 

27class TrapezoidMapTriFinder(TriFinder): 

28 """ 

29 `~matplotlib.tri.TriFinder` class implemented using the trapezoid 

30 map algorithm from the book "Computational Geometry, Algorithms and 

31 Applications", second edition, by M. de Berg, M. van Kreveld, M. Overmars 

32 and O. Schwarzkopf. 

33 

34 The triangulation must be valid, i.e. it must not have duplicate points, 

35 triangles formed from colinear points, or overlapping triangles. The 

36 algorithm has some tolerance to triangles formed from colinear points, but 

37 this should not be relied upon. 

38 """ 

39 

40 def __init__(self, triangulation): 

41 from matplotlib import _tri 

42 super().__init__(triangulation) 

43 self._cpp_trifinder = _tri.TrapezoidMapTriFinder( 

44 triangulation.get_cpp_triangulation()) 

45 self._initialize() 

46 

47 def __call__(self, x, y): 

48 """ 

49 Return an array containing the indices of the triangles in which the 

50 specified *x*, *y* points lie, or -1 for points that do not lie within 

51 a triangle. 

52 

53 *x*, *y* are array-like x and y coordinates of the same shape and any 

54 number of dimensions. 

55 

56 Returns integer array with the same shape and *x* and *y*. 

57 """ 

58 x = np.asarray(x, dtype=np.float64) 

59 y = np.asarray(y, dtype=np.float64) 

60 if x.shape != y.shape: 

61 raise ValueError("x and y must be array-like with the same shape") 

62 

63 # C++ does the heavy lifting, and expects 1D arrays. 

64 indices = (self._cpp_trifinder.find_many(x.ravel(), y.ravel()) 

65 .reshape(x.shape)) 

66 return indices 

67 

68 def _get_tree_stats(self): 

69 """ 

70 Return a python list containing the statistics about the node tree: 

71 0: number of nodes (tree size) 

72 1: number of unique nodes 

73 2: number of trapezoids (tree leaf nodes) 

74 3: number of unique trapezoids 

75 4: maximum parent count (max number of times a node is repeated in 

76 tree) 

77 5: maximum depth of tree (one more than the maximum number of 

78 comparisons needed to search through the tree) 

79 6: mean of all trapezoid depths (one more than the average number 

80 of comparisons needed to search through the tree) 

81 """ 

82 return self._cpp_trifinder.get_tree_stats() 

83 

84 def _initialize(self): 

85 """ 

86 Initialize the underlying C++ object. Can be called multiple times if, 

87 for example, the triangulation is modified. 

88 """ 

89 self._cpp_trifinder.initialize() 

90 

91 def _print_tree(self): 

92 """ 

93 Print a text representation of the node tree, which is useful for 

94 debugging purposes. 

95 """ 

96 self._cpp_trifinder.print_tree()