draft implementation of arbitrary PDF
[sympy.git] / sympy / plotting / util.py
blob7aaf16161c61ca4018369c62d373907457836b49
1 from pyglet.gl import *
2 from sympy.core.basic import S
4 def get_model_matrix(array_type=c_float, glGetMethod=glGetFloatv):
5 """
6 Returns the current modelview matrix.
7 """
8 m = (array_type*16)()
9 glGetMethod(GL_MODELVIEW_MATRIX, m)
10 return m
12 def get_projection_matrix(array_type=c_float, glGetMethod=glGetFloatv):
13 """
14 Returns the current modelview matrix.
15 """
16 m = (array_type*16)()
17 glGetMethod(GL_PROJECTION_MATRIX, m)
18 return m
20 def get_viewport():
21 """
22 Returns the current viewport.
23 """
24 m = (c_int*4)()
25 glGetIntegerv(GL_VIEWPORT, m)
26 return m
28 def get_direction_vectors():
29 m = get_model_matrix()
30 return ((m[0], m[4], m[8]),
31 (m[1], m[5], m[9]),
32 (m[2], m[6], m[10]))
34 def get_view_direction_vectors():
35 m = get_model_matrix()
36 return ((m[0], m[1], m[2]),
37 (m[4], m[5], m[6]),
38 (m[8], m[9], m[10]))
40 def get_basis_vectors():
41 return ((1,0,0), (0,1,0), (0,0,1))
43 def screen_to_model(x,y,z):
44 m = get_model_matrix(c_double, glGetDoublev)
45 p = get_projection_matrix(c_double, glGetDoublev)
46 w = get_viewport()
47 mx,my,mz = c_double(),c_double(),c_double()
48 gluUnProject(x,y,z,m,p,w,mx,my,mz)
49 return float(mx.value),float(my.value),float(mz.value)
51 def model_to_screen(x,y,z):
52 m = get_model_matrix(c_double, glGetDoublev)
53 p = get_projection_matrix(c_double, glGetDoublev)
54 w = get_viewport()
55 mx,my,mz = c_double(),c_double(),c_double()
56 gluProject(x,y,z,m,p,w,mx,my,mz)
57 return float(mx.value),float(my.value),float(mz.value)
59 def vec_subs(a,b):
60 return tuple(a[i]-b[i] for i in xrange(len(a)))
62 def billboard_matrix():
63 """
64 Removes rotational components of
65 current matrix so that primitives
66 are always drawn facing the viewer.
68 |1|0|0|x|
69 |0|1|0|x|
70 |0|0|1|x| (x means left unchanged)
71 |x|x|x|x|
72 """
73 m = get_model_matrix()
74 m[0] =1;m[1] =0;m[2] =0
75 m[4] =0;m[5] =1;m[6] =0
76 m[8] =0;m[9] =0;m[10]=1
77 glLoadMatrixf(m)
79 def create_bounds():
80 return [ [S.Infinity,-S.Infinity,0],[S.Infinity,-S.Infinity,0],[S.Infinity,-S.Infinity,0] ]
82 def update_bounds(b, v):
83 if v is None: return
84 for axis in xrange(3):
85 b[axis][0] = min([b[axis][0], v[axis]])
86 b[axis][1] = max([b[axis][1], v[axis]])
88 def interpolate(a_min, a_max, a_ratio):
89 return a_min + a_ratio * (a_max - a_min)
91 def rinterpolate(a_min, a_max, a_value):
92 a_range = a_max-a_min
93 if a_range == 0:
94 a_range = 1.0
95 return (a_value - a_min) / float(a_range)
97 def interpolate_color(color1, color2, ratio):
98 return tuple(interpolate(color1[i], color2[i], ratio) for i in xrange(3))
100 def scale_value(v, v_min, v_len):
101 return (v-v_min)/v_len
103 def scale_value_list(flist):
104 v_min, v_max = min(flist), max(flist)
105 v_len = v_max-v_min
106 return list(scale_value(f,v_min,v_len) for f in flist)
108 def strided_range(r_min, r_max, stride, max_steps=50):
109 o_min, o_max = r_min, r_max
110 if abs(r_min-r_max) < 0.001: return []
111 try: xrange(int(r_min-r_max))
112 except: return []
113 assert r_min < r_max
114 r_min_s = (r_min % stride)
115 r_max_s = stride - (r_max % stride)
116 if abs(r_max_s-stride) < 0.001:
117 r_max_s = 0.0
118 r_min -= r_min_s
119 r_max += r_max_s
120 r_steps = int( (r_max-r_min) / stride )
121 if max_steps and r_steps > max_steps:
122 return strided_range(o_min, o_max, stride*2)
123 return [r_min] + list( r_min+e*stride for e in xrange(1, r_steps+1) ) + [r_max]
125 def parse_option_string(s):
126 if not isinstance(s, str):
127 return None
128 options = {}
129 for token in s.split(';'):
130 pieces = token.split('=')
131 if len(pieces) == 1:
132 option, value = pieces[0], ""
133 elif len(pieces) == 2:
134 option, value = pieces
135 else:
136 raise ValueError("Plot option string '%s' is malformed." % (s))
137 options[option.strip()] = value.strip()
138 return options
140 def dot_product(v1, v2):
141 return sum(v1[i]*v2[i] for i in xrange(3))
143 def vec_sub(v1, v2):
144 return tuple(v1[i]-v2[i] for i in xrange(3))
146 def vec_mag(v):
147 return sum(v[i]**2 for i in xrange(3))**(0.5)