root/trunk/RBRapier/RBRapier/Formats/Attic/SVG.old/Renderers/RapierObjects.py

Revision 665, 10.4 kB (checked in by sholloway, 5 years ago)

*** empty log message ***

Line 
1 #!/usr/bin/env python
2 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 ##~ License
4 ##~
5 ##- The RuneBlade Foundation library is intended to ease some
6 ##- aspects of writing intricate Jabber, XML, and User Interface (wxPython, etc.)
7 ##- applications, while providing the flexibility to modularly change the
8 ##- architecture. Enjoy.
9 ##~
10 ##~ Copyright (C) 2002  TechGame Networks, LLC.
11 ##~
12 ##~ This library is free software; you can redistribute it and/or
13 ##~ modify it under the terms of the BSD style License as found in the
14 ##~ LICENSE file included with this distribution.
15 ##~
16 ##~ TechGame Networks, LLC can be reached at:
17 ##~ 3578 E. Hartsel Drive #211
18 ##~ Colorado Springs, Colorado, USA, 80920
19 ##~
20 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21
22 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 #~ Imports
24 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25
26 import warnings
27 from OpenGL import GL
28
29 from RBRapier.Tools.Geometry import Curves
30 from RBRapier.Tools.Geometry.gluPolygonTesselation import gluPolygonTesselator
31
32 import Abstract
33
34 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35 #~ Definitions
36 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37
38 class Base(object):
39     __slots__ = 'strokecolor', 'fillcolor'
40
41     def SetColors(self, stroke, fill):
42         self.strokecolor = stroke
43         self.fillcolor = fill
44
45 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46
47 class Text(Base):
48     __slots__ = ()
49
50     def __nonzero__(self):
51         return False
52
53     def __init__(self):
54         warnings.warn( "TODO: Text")
55
56     def Execute(self, context):
57         pass
58
59 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
60
61 class Line(Base):
62     __slots__ = 'v0', 'v1'
63
64     def __init__(self, v0, v1):
65         self.v0 = v0
66         self.v1 = v1
67
68     def Execute(self, context):
69         if self.strokecolor:
70             GL.glColor4ubv(self.strokecolor)
71
72         GL.glBegin(GL.GL_LINES)
73         GL.glVertex2fv(self.v0)
74         GL.glVertex2fv(self.v1)
75         GL.glEnd()
76
77 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
78
79 class Rect(Base):
80     __slots__ = 'w', 'h'
81
82     def __init__(self, w, h):
83         self.w = w
84         self.h = h
85
86     def Execute(self, context):
87         if self.fillcolor:
88             GL.glColor4ubv(self.fillcolor)
89         elif self.strokecolor:
90             GL.glColor4ubv(self.strokecolor)
91         GL.glRectf(0., 0., self.w, self.h)
92
93 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94
95 class Ellipse(Base):
96     __slots__ = 'cx', 'cy', 'rx', 'ry'
97
98     def __init__(self, cx, cy, rx, ry):
99         self.cx = cx
100         self.cy = cy
101         self.rx = rx
102         self.ry = ry
103         warnings.warn( "TODO: Ellipse")
104
105     def Execute(self, context):
106         if self.fillcolor:
107             GL.glColor4ubv(self.fillcolor)
108         elif self.strokecolor:
109             GL.glColor4ubv(self.strokecolor)
110
111         x0 = self.cx - self.rx
112         y0 = self.cy - self.ry
113         x1 = self.cx + self.rx
114         y1 = self.cy + self.ry
115         GL.glRectf(x0, y0, x1, y1)
116
117 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
118
119 class Circle(Ellipse):
120     def __init__(self, cx, cy, r):
121         warnings.warn( "TODO: Circle")
122         Ellipse.__init__(self, cx, cy, r, r)
123
124 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125
126 class Polygon(Base):
127     __slots__ = 'verticies', 'listid'
128
129     def __init__(self, verticies):
130         self.verticies = [(x,y,0) for x,y in verticies]
131         self.listid = None
132
133     def Execute(self, context):
134         if self.listid is None:
135             self.listid = GL.glGenLists(1)
136             GL.glNewList(self.listid, GL.GL_COMPILE)
137             gluPolygonTesselator([self.verticies])
138             GL.glEndList()
139             self.verticies = None
140
141         if self.fillcolor:
142             GL.glColor4ubv(self.fillcolor)
143         GL.glCallList(self.listid)
144
145         #if self.strokecolor:
146         #    GL.glColor4ubv(self.strokecolor)
147         #GL.glCallList(self.listid)
148
149 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
150
151 class Polyline(Polygon):
152     pass
153
154 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
155
156 class Path(Base):
157     __slots__ = 'paths', 'listid'
158
159     def __init__(self, paths):
160         self.paths = paths
161         self.listid = None
162
163     def Execute(self, context):
164         if self.listid is None:
165             self.listid = GL.glGenLists(1)
166             GL.glNewList(self.listid, GL.GL_COMPILE)
167             if self.fillcolor:
168                 gluPolygonTesselator(self.paths)
169             else:
170                 for verticies in self.paths:
171                     GL.glBegin(GL.GL_LINE_STRIP)
172                     for v in verticies:
173                         GL.glVertex2fv(v)
174                     GL.glEnd()
175             GL.glEndList()
176             self.paths = None
177
178         if self.fillcolor:
179             GL.glColor4ubv(self.fillcolor)
180         elif self.strokecolor:
181             GL.glColor4ubv(self.strokecolor)
182         GL.glCallList(self.listid)
183
184 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
185 #~ Paths
186 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187
188 class RapierPathFactory(Abstract.AbstractPathFactory):
189     steps = 32
190     pos = (0,0)
191     controlpoint = None
192
193     command_table = {
194         'M': 'move',
195         'm': 'move_rel',
196         'Z': 'closepath',
197         'z': 'closepath',
198         'L': 'line',
199         'l': 'line_rel',
200         'H': 'hline',
201         'h': 'hline_rel',
202         'V': 'vline',
203         'v': 'vline_rel',
204         'C': 'cubicbezier',
205         'c': 'cubicbezier_rel',
206         'S': 'smoothcurve',
207         's': 'smoothcurve_rel',
208         'Q': 'quadraticbezier',
209         'q': 'quadraticbezier_rel',
210         'T': 'smoothquadraticbezier',
211         't': 'smoothquadraticbezier_rel',
212         'A': 'ellipticarc',
213         'a': 'ellipticarc_rel'}
214
215     def BeginPath(self, pathstr):
216         self.pathlist = []
217
218     def AddPathElement(self, name, *args):
219         name = self.command_table.get(name, '')
220         transformfn = getattr(self, name, None)
221         if transformfn is not None:
222             transformfn(*args)
223
224     def EndPath(self, pathstr):
225         pass
226
227     def move_rel(self, x, y):
228         dx, dy = self.pos
229         return self.move(x+dx, y+dy)
230     def move(self, x, y):
231         if not self.pathlist or self.pathlist[-1]:
232             self.pathlist.append([(x,y)])
233         else:
234             self.pathlist[-1][:] = [(x,y)]
235         self._savepos((x,y))
236
237     def closepath(self):
238         if self.pathlist[-1]:
239             lastpath = self.pathlist[-1]
240             pos = lastpath[0]
241             lastpath.append(pos)
242             self.pathlist.append([])
243             self._savepos(pos)
244
245     def line_rel(self, x, y):
246         dx, dy = self.pos
247         return self.line(x+dx, y+dy)
248     def line(self, x, y):
249         self.pathlist[-1].append((x,y))
250         self._savepos((x,y))
251
252     def hline_rel(self, x):
253         dx = self.pos[0]
254         return self.hline(x+dx)
255     def hline(self, x):
256         y = self.pos[1]
257         self.pathlist[-1].append((x,y))
258         self._savepos((x,y))
259
260     def vline_rel(self, y):
261         dy = self.pos[1]
262         return self.vline(y+dy)
263     def vline(self, y):
264         x = self.pos[0]
265         self.pathlist[-1].append((x,y))
266         self._savepos((x,y))
267
268     def cubicbezier_rel(self, x1, y1, x2, y2, x, y):
269         dx, dy = self.pos
270         return self.cubicbezier(x1+dx, y1+dy, x2+dx, y2+dy, x+dx, y+dy)
271     def cubicbezier(self, x1, y1, x2, y2, x, y):
272         bezier = Curves.CubicBezier(self.pos, (x1, y1), (x2, y2), (x, y), steps=self.steps)
273         self.pathlist[-1].extend(list(bezier))
274         self._savepos((x,y), (x2, y2))
275
276     def smoothcurve_rel(self, x2, y2, x, y):
277         dx, dy = self.pos
278         return self.smoothcurve(x2+dx, y2+dy, x+dx, y+dy)
279     def smoothcurve(self, x2, y2, x, y):
280         x1r, y1r = self._getControlPoint()
281         return self.cubicbezier(x1r, y1r, x2, y2, x, y)
282
283     def quadraticbezier_rel(self, x1, y1, x, y):
284         dx, dy = self.pos
285         return self.quadraticbezier(x1+dx, y1+dy, x+dx, y+dy)
286     def quadraticbezier(self, x1, y1, x, y):
287         bezier = Curves.QuadraticBezier(self.pos, (x1, y1), (x, y), steps=self.steps)
288         self.pathlist[-1].extend(list(bezier))
289         self._savepos((x,y), (x1,y1))
290
291     def smoothquadraticbezier_rel(self, x, y):
292         dx, dy = self.pos
293         return self.smoothquadraticbezier(x+dx, y+dy)
294     def smoothquadraticbezier(self, x, y):
295         x1r, y1r = self._getControlPoint()
296         return self.quadraticbezier(x1r, y1r, x, y)
297
298     def ellipticarc_rel(self, rx, ry, xrotation, largeArcFlag, sweepFlag, x, y):
299         dx, dy = self.pos
300         return self.ellipticarc(rx, ry, xrotation, largeArcFlag, sweepFlag, x+dx, y+dy)
301     def ellipticarc(self, rx, ry, xrotation, largeArcFlag, sweepFlag, x, y):
302         startpt, endpt = self.pos, (x,y)
303         if startpt == endpt: return
304         if rx and ry:
305             ellipse = Curves.Ellipse.fromArc(startpt, endpt, abs(rx), abs(ry), xrotation % 360, bool(largeArcFlag), bool(sweepFlag), inDegrees=True, steps=self.steps)
306             self.pathlist[-1].extend(list(ellipse))
307         else:
308             # If rX = 0 or rY  = 0, then treat this as a straight line from (x1, y1) to (x2, y2) and stop
309             self.pathlist[-1].append(endpt)
310         self._savepos(endpt)
311
312     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
313
314     def _savepos(self, pos, controlpoint=None):
315         self.pos = pos
316         self.controlpoint = controlpoint
317
318     def _getControlPoint(self):
319         if self.controlpoint is None:
320             return self.pos
321         else:
322             x,y = self.pos
323             xcp, ycp = self.controlpoint
324             return 2*x-xcp, 2*y-ycp
325
326 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
327 #~ Transforms
328 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
329
330 class RapierTransformFactory(object):
331     def __init__(self, xform):
332         pass
333
334     def BeginTransform(self, transformstr):
335         pass
336     def AddTransformElement(self, name, *args):
337         pass
338     def EndTransform(self, transformstr):
339         pass
340
341     def translate(self, x=0.0, y=0.0):
342         warnings.warn( "TODO: transform.translate")
343     def scale(self, x=1., y=None):
344         warnings.warn( "TODO: transform.scale")
345     def rotate(self, angle=0.0, x=0, y=0):
346         warnings.warn( "TODO: transform.rotate")
347     def skewx(self, angle=0.0):
348         warnings.warn( "TODO: transform.skewx")
349     def skewy(self, angle=0.0):
350         warnings.warn( "TODO: transform.skewy")
351     def matrix(self, xx=1.0, xy=0.0, xh=0.0, yx=0.0, yy=1.0, yh=0.0, hx=0.0, hy=0.0, hh=1.0):
352         warnings.warn( "TODO: transform.matrix")
353
354 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
355
Note: See TracBrowser for help on using the browser.