mpl_util.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from __future__ import annotations
  2. from typing import TYPE_CHECKING, cast
  3. import matplotlib.path as mpath
  4. import numpy as np
  5. from contourpy import FillType, LineType
  6. if TYPE_CHECKING:
  7. from contourpy._contourpy import (
  8. CodeArray, FillReturn, LineReturn, LineReturn_Separate, OffsetArray,
  9. )
  10. def filled_to_mpl_paths(filled: FillReturn, fill_type: FillType) -> list[mpath.Path]:
  11. if fill_type in (FillType.OuterCode, FillType.ChunkCombinedCode):
  12. paths = [mpath.Path(points, codes) for points, codes in zip(*filled) if points is not None]
  13. elif fill_type in (FillType.OuterOffset, FillType.ChunkCombinedOffset):
  14. paths = [mpath.Path(points, offsets_to_mpl_codes(offsets))
  15. for points, offsets in zip(*filled) if points is not None]
  16. elif fill_type == FillType.ChunkCombinedCodeOffset:
  17. paths = []
  18. for points, codes, outer_offsets in zip(*filled):
  19. if points is None:
  20. continue
  21. points = np.split(points, outer_offsets[1:-1])
  22. codes = np.split(codes, outer_offsets[1:-1])
  23. paths += [mpath.Path(p, c) for p, c in zip(points, codes)]
  24. elif fill_type == FillType.ChunkCombinedOffsetOffset:
  25. paths = []
  26. for points, offsets, outer_offsets in zip(*filled):
  27. if points is None:
  28. continue
  29. for i in range(len(outer_offsets)-1):
  30. offs = offsets[outer_offsets[i]:outer_offsets[i+1]+1]
  31. pts = points[offs[0]:offs[-1]]
  32. paths += [mpath.Path(pts, offsets_to_mpl_codes(offs - offs[0]))]
  33. else:
  34. raise RuntimeError(f"Conversion of FillType {fill_type} to MPL Paths is not implemented")
  35. return paths
  36. def lines_to_mpl_paths(lines: LineReturn, line_type: LineType) -> list[mpath.Path]:
  37. if line_type == LineType.Separate:
  38. if TYPE_CHECKING:
  39. lines = cast(LineReturn_Separate, lines)
  40. paths = []
  41. for line in lines:
  42. # Drawing as Paths so that they can be closed correctly.
  43. closed = line[0, 0] == line[-1, 0] and line[0, 1] == line[-1, 1]
  44. paths.append(mpath.Path(line, closed=closed))
  45. elif line_type in (LineType.SeparateCode, LineType.ChunkCombinedCode):
  46. paths = [mpath.Path(points, codes) for points, codes in zip(*lines) if points is not None]
  47. elif line_type == LineType.ChunkCombinedOffset:
  48. paths = []
  49. for points, offsets in zip(*lines):
  50. if points is None:
  51. continue
  52. for i in range(len(offsets)-1):
  53. line = points[offsets[i]:offsets[i+1]]
  54. closed = line[0, 0] == line[-1, 0] and line[0, 1] == line[-1, 1]
  55. paths.append(mpath.Path(line, closed=closed))
  56. else:
  57. raise RuntimeError(f"Conversion of LineType {line_type} to MPL Paths is not implemented")
  58. return paths
  59. def mpl_codes_to_offsets(codes: CodeArray) -> OffsetArray:
  60. offsets = np.nonzero(codes == 1)[0].astype(np.uint32)
  61. offsets = np.append(offsets, len(codes))
  62. return offsets
  63. def offsets_to_mpl_codes(offsets: OffsetArray) -> CodeArray:
  64. codes = np.full(offsets[-1]-offsets[0], 2, dtype=np.uint8) # LINETO = 2
  65. codes[offsets[:-1]] = 1 # MOVETO = 1
  66. codes[offsets[1:]-1] = 79 # CLOSEPOLY 79
  67. return codes