trifinder.py 3.4 KB

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