plot_curve.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import pyglet.gl as pgl
  2. from sympy.core import S
  3. from sympy.plotting.pygletplot.plot_mode_base import PlotModeBase
  4. class PlotCurve(PlotModeBase):
  5. style_override = 'wireframe'
  6. def _on_calculate_verts(self):
  7. self.t_interval = self.intervals[0]
  8. self.t_set = list(self.t_interval.frange())
  9. self.bounds = [[S.Infinity, S.NegativeInfinity, 0],
  10. [S.Infinity, S.NegativeInfinity, 0],
  11. [S.Infinity, S.NegativeInfinity, 0]]
  12. evaluate = self._get_evaluator()
  13. self._calculating_verts_pos = 0.0
  14. self._calculating_verts_len = float(self.t_interval.v_len)
  15. self.verts = []
  16. b = self.bounds
  17. for t in self.t_set:
  18. try:
  19. _e = evaluate(t) # calculate vertex
  20. except (NameError, ZeroDivisionError):
  21. _e = None
  22. if _e is not None: # update bounding box
  23. for axis in range(3):
  24. b[axis][0] = min([b[axis][0], _e[axis]])
  25. b[axis][1] = max([b[axis][1], _e[axis]])
  26. self.verts.append(_e)
  27. self._calculating_verts_pos += 1.0
  28. for axis in range(3):
  29. b[axis][2] = b[axis][1] - b[axis][0]
  30. if b[axis][2] == 0.0:
  31. b[axis][2] = 1.0
  32. self.push_wireframe(self.draw_verts(False))
  33. def _on_calculate_cverts(self):
  34. if not self.verts or not self.color:
  35. return
  36. def set_work_len(n):
  37. self._calculating_cverts_len = float(n)
  38. def inc_work_pos():
  39. self._calculating_cverts_pos += 1.0
  40. set_work_len(1)
  41. self._calculating_cverts_pos = 0
  42. self.cverts = self.color.apply_to_curve(self.verts,
  43. self.t_set,
  44. set_len=set_work_len,
  45. inc_pos=inc_work_pos)
  46. self.push_wireframe(self.draw_verts(True))
  47. def calculate_one_cvert(self, t):
  48. vert = self.verts[t]
  49. return self.color(vert[0], vert[1], vert[2],
  50. self.t_set[t], None)
  51. def draw_verts(self, use_cverts):
  52. def f():
  53. pgl.glBegin(pgl.GL_LINE_STRIP)
  54. for t in range(len(self.t_set)):
  55. p = self.verts[t]
  56. if p is None:
  57. pgl.glEnd()
  58. pgl.glBegin(pgl.GL_LINE_STRIP)
  59. continue
  60. if use_cverts:
  61. c = self.cverts[t]
  62. if c is None:
  63. c = (0, 0, 0)
  64. pgl.glColor3f(*c)
  65. else:
  66. pgl.glColor3f(*self.default_wireframe_color)
  67. pgl.glVertex3f(*p)
  68. pgl.glEnd()
  69. return f