plot.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright (C) 2016 Intel Corporation. All rights reserved.
  2. #
  3. # This file is free software: you can redistribute it and/or modify it
  4. # under the terms of the GNU General Public License as published by the
  5. # Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This file is distributed in the hope that it will be useful, but
  9. # WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. # See the GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License along
  14. # with this program. If not, see <http://www.gnu.org/licenses/>.
  15. import matplotlib.pyplot as plt
  16. import matplotlib.patches as mpatches
  17. from mpl_toolkits.mplot3d import Axes3D
  18. from mpl_toolkits.mplot3d.art3d import Poly3DCollection
  19. import icosahedron as ico
  20. import grid
  21. fig = plt.figure()
  22. ax = fig.add_subplot(111, projection='3d')
  23. ax.set_xlim3d(-2, 2)
  24. ax.set_ylim3d(-2, 2)
  25. ax.set_zlim3d(-2, 2)
  26. ax.set_xlabel('x')
  27. ax.set_ylabel('y')
  28. ax.set_zlabel('z')
  29. ax.invert_zaxis()
  30. ax.invert_xaxis()
  31. ax.set_aspect('equal')
  32. added_polygons = set()
  33. added_sections = set()
  34. def polygons(polygons):
  35. for p in polygons:
  36. polygon(p)
  37. def polygon(polygon):
  38. added_polygons.add(polygon)
  39. def section(s):
  40. added_sections.add(s)
  41. def sections(sections):
  42. for s in sections:
  43. section(s)
  44. def show(subtriangles=False):
  45. polygons = []
  46. facecolors = []
  47. triangles_indexes = set()
  48. subtriangle_facecolors = (
  49. '#CCCCCC',
  50. '#CCE5FF',
  51. '#E5FFCC',
  52. '#FFCCCC',
  53. )
  54. if added_sections:
  55. subtriangles = True
  56. for p in added_polygons:
  57. try:
  58. i = ico.triangles.index(p)
  59. except ValueError:
  60. polygons.append(p)
  61. continue
  62. if subtriangles:
  63. sections(range(i * 4, i * 4 + 4))
  64. else:
  65. triangles_indexes.add(i)
  66. polygons.append(p)
  67. facecolors.append('#DDDDDD')
  68. for s in added_sections:
  69. triangles_indexes.add(int(s / 4))
  70. subtriangle_index = s % 4
  71. polygons.append(grid.section_triangle(s))
  72. facecolors.append(subtriangle_facecolors[subtriangle_index])
  73. ax.add_collection3d(Poly3DCollection(
  74. polygons,
  75. facecolors=facecolors,
  76. edgecolors="#777777",
  77. ))
  78. for i in triangles_indexes:
  79. t = ico.triangles[i]
  80. mx = my = mz = 0
  81. for x, y, z in t:
  82. mx += x
  83. my += y
  84. mz += z
  85. ax.text(mx / 2.6, my / 2.6, mz / 2.6, i, color='#444444')
  86. if subtriangles:
  87. ax.legend(
  88. handles=tuple(
  89. mpatches.Patch(color=c, label='Sub-triangle #%d' % i)
  90. for i, c in enumerate(subtriangle_facecolors)
  91. ),
  92. )
  93. plt.show()