_trifinder.py 3.4 KB

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