root/trunk/RBRapier/RBRapier/Tools/Geometry/gluPolygonTesselation.py

Revision 680, 5.5 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 from OpenGL import GL, GLU
27
28 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 #~ Definitions
30 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31
32 class _gluTessProperty(object):
33     def __init__(self, key):
34         self.key = key
35
36     def __get__(self, obj, klass):
37         if obj is None:
38             return self
39         else:
40             tessobj = obj._getTessObj()
41             return GLU.gluGetTessProperty(tessobj, self.key)
42
43     def __set__(self, obj, value):
44         tessobj = obj._getTessObj()
45         GLU.gluTessProperty(tessobj, self.key, value)
46
47 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48
49 class gluPolygonTesselator(object):
50     """Modeled after Michael Fletcher's polygon tesselation object in OpenGLContext from pyopengl.sf.net (BSD style license)"""
51
52     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53     #~ Constants / Variables / Etc.
54     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55
56     tessobj = None
57
58     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59     #~ Public Methods
60     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61
62     def __init__(self, *args, **settings):
63         if settings:
64             self.open(**settings)
65
66         if args:
67             self.tessellate(*args)
68
69     def __del__(self):
70         self.close()
71
72     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73
74     def open(self, **settings):
75         tessobj = self.tessobj = GLU.gluNewTess()
76         GLU.gluTessCallback(tessobj, GLU.GLU_TESS_BEGIN, self._OnBegin)
77         GLU.gluTessCallback(tessobj, GLU.GLU_TESS_VERTEX, self._OnVertex)
78         GLU.gluTessCallback(tessobj, GLU.GLU_TESS_COMBINE, self._OnCombine)
79         GLU.gluTessCallback(tessobj, GLU.GLU_TESS_END, self._OnEnd)
80
81         for n, v in settings.iteritems():
82             setattr(self, n, v)
83
84     def close(self):
85         if self.tessobj is not None:
86             GLU.gluDeleteTess(self.tessobj)
87         self.tessobj = None
88
89     def tessellate(self, contours, **settings):
90         tessobj = self._getTessObj()
91         for n, v in settings.iteritems(): setattr(self, n, v)
92
93         if len(contours[0][0]) == 2:
94             vertexcall = lambda (x,y): GLU.gluTessVertex(tessobj, (x,y, 0), (x,y))
95         else:
96             vertexcall = lambda each: GLU.gluTessVertex(tessobj, each[:3], each)
97         GLU.gluTessBeginPolygon(tessobj, None)
98         for contour in contours:
99             GLU.gluTessBeginContour(tessobj)
100             map(vertexcall, contour)
101             GLU.gluTessEndContour(tessobj)
102         GLU.gluTessEndPolygon(tessobj)
103
104     def GetUseEdgeFlag(self):
105         try: return self._useedge
106         except AttributeError: return False
107     def SetUseEdgeFlag(self, value):
108         if value and not callable(value):
109             value = self._OnEdgeFlag
110         GLU.gluTessCallback(self._getTessObj(), GLU.GLU_TESS_EDGE_FLAG, value or None)
111         self._useedge = value
112     useedge = property(GetUseEdgeFlag, SetUseEdgeFlag)
113
114     def GetNormal(self):
115         try: return self._normal
116         except AttributeError: return (0., 0., 0.)
117     def SetNormal(self, normal):
118         if normal is None:
119             normal = (0., 0., 0.)
120         GLU.gluTessNormal(self._getTessObj(), *normal)
121         self._normal = normal
122     normal = property(GetNormal, SetNormal)
123
124     windingrule = _gluTessProperty(GLU.GLU_TESS_WINDING_RULE)
125     tolerance = _gluTessProperty(GLU.GLU_TESS_TOLERANCE)
126     boundary = _gluTessProperty(GLU.GLU_TESS_BOUNDARY_ONLY)
127
128     #~ protected ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129
130     def _getTessObj(self):
131         if self.tessobj is None:
132             self.open()
133         return self.tessobj
134
135     def _OnBegin(self, mode):
136         """
137         if self._OnEdgeFlag is not None:
138             if bool(self.boundary):
139                 assert mode == GL.GL_LINE_LOOP
140             else:
141                 assert mode == GL.GL_TRIANGLES
142         """
143         GL.glBegin(mode)
144
145     def _OnVertex(self, vertexdata):
146         GL.glVertex3fv(vertexdata)
147
148     def _OnCombine(self, newcoord, verticies, weights):
149         return newcoord
150
151     def _OnEdgeFlag(self, edgeFlag):
152         GL.glEdgeFlag(edgeFlag)
153
154     def _OnEnd(self):
155         GL.glEnd()
156
157 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
158
159 class PolygonTesselator(gluPolygonTesselator):
160     def tessellate(self, *args, **kw):
161         self.results = []
162         gluPolygonTesselator.tessellate(self, *args, **kw)
163         return self.results
164
165     def _OnBegin(self, mode):
166         self.mode = mode
167         self.vertexdata = []
168     def _OnVertex(self, vertexdata):
169         self.vertexdata.append(vertexdata)
170     def _OnEnd(self):
171         self.results.append((self.mode, self.vertexdata))
172         del self.mode, self.vertexdata
173
Note: See TracBrowser for help on using the browser.