Mul.flatten -- optimize for all-objects-are-commutative case
[sympy.git] / sympy / plotting / plot_curve.py
blobb674e1f391a9c049d472b9cd7907aa1e02e2a422
1 from pyglet.gl import *
2 from plot_mode_base import PlotModeBase
3 from sympy.core.basic import S
4 from util import scale_value, scale_value_list
5 #from time import sleep
7 class PlotCurve(PlotModeBase):
9 style_override = 'wireframe'
11 def _on_calculate_verts(self):
12 self.t_interval = self.intervals[0]
13 self.t_set = list(self.t_interval.frange())
14 self.bounds = [ [S.Infinity,-S.Infinity,0],[S.Infinity,-S.Infinity,0],[S.Infinity,-S.Infinity,0] ]
15 evaluate = self._get_evaluator()
17 self._calculating_verts_pos = 0.0
18 self._calculating_verts_len = float(self.t_interval.v_len)
20 self.verts = list()
21 b = self.bounds
22 for t in self.t_set:
23 try: _e = evaluate(t) # calculate vertex
24 except: _e = None
25 if _e is not None: # update bounding box
26 for axis in xrange(3):
27 b[axis][0] = min([b[axis][0], _e[axis]])
28 b[axis][1] = max([b[axis][1], _e[axis]])
29 self.verts.append(_e)
30 self._calculating_verts_pos += 1.0
32 for axis in xrange(3):
33 b[axis][2] = b[axis][1] - b[axis][0]
34 if b[axis][2] == 0.0: b[axis][2] = 1.0
36 self.push_wireframe(self.draw_verts(False))
38 def _on_calculate_cverts(self):
39 if not self.verts or not self.color: return
40 def set_work_len(n): self._calculating_cverts_len = float(n)
41 def inc_work_pos(): self._calculating_cverts_pos += 1.0
42 set_work_len(1); self._calculating_cverts_pos = 0
43 self.cverts = self.color.apply_to_curve(self.verts, self.t_set, set_len=set_work_len, inc_pos=inc_work_pos)
44 self.push_wireframe(self.draw_verts(True))
46 def calculate_one_cvert(self, t):
47 vert = self.verts[t]
48 return self.color(vert[0], vert[1], vert[2],
49 self.t_set[t], None)
51 def draw_verts(self, use_cverts):
52 def f():
53 glBegin(GL_LINE_STRIP)
54 for t in xrange( len(self.t_set) ):
55 p = self.verts[t]
56 if p is None:
57 glEnd()
58 glBegin(GL_LINE_STRIP)
59 continue
60 if use_cverts:
61 c = self.cverts[t]
62 if c is None: c = (0, 0, 0)
63 glColor3f(*c)
64 else: glColor3f(*self.default_wireframe_color)
65 glVertex3f(*p)
66 glEnd()
67 return f