-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.py
308 lines (262 loc) · 11 KB
/
node.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# Stubs for a scenegraph
from OpenGL.GL import *
from matrix import *
from material import *
from Select import Selectable, SelectState
from XFormMatrix import *
# NOTE:
# I've mushed three concepts together
# 1) A generic graph node
# 2) A DagNode
# 3) Transform
# Generic graph nodes are simply the minimum primitive for existing in the graph
# The DagNode is one that can take children.
# The Transform is a DagNode that has a transformation.
#
# If these were unique, a geometry node would simply be a node.
# positioning it in space would require placing a transform Node above it. (a la Maya)
# In this light, every node has a transform.
def unionBB( min0, max0, min1, max1 ):
'''Creates a bounding box which bounds both bounding boxes.
@param: min0 The minimum point of the first bounding box.
@param: min1 The maximum point of the first bounding box.
@param: min0 The minimum point of the first bounding box.
@param: min1 The maximum point of the first bounding box.
@returns: Two 2-tuples of floats. The minimum and maximum points of the
overall bounding box.
'''
minPt = min0.copy()
maxPt = max0.copy()
for i in xrange( 3 ):
if ( min1[i] < minPt[i] ):
minPt[i] = min1[i]
if ( max1[i] > maxPt[i] ):
maxPt[i] = max1[i]
return minPt,maxPt
def bbCorners( minPt, maxPt ):
'''Given the minimum and maximum points of a bounding box, prodcues a list of vectors
containing all points on the bounding box.
@returns: A list of 8 Vector3 instances. The 8 points of the bounding box.
'''
return [ minPt,
maxPt,
Vector3( minPt.x, minPt.y, maxPt.z ),
Vector3( minPt.x, maxPt.y, maxPt.z ),
Vector3( minPt.x, maxPt.y, minPt.z ),
Vector3( maxPt.x, maxPt.y, maxPt.z ),
Vector3( maxPt.x, minPt.y, maxPt.z ),
Vector3( maxPt.x, maxPt.y, minPt.z )
]
class Node( object ):
'''Basic node in the scene graph.
It includes a transform and the ability to have children.
'''
def __init__( self, xform=IDENTITY4x4, parent=None ):
'''Constructor.
@param: drawable The drawable associated with this node.
'''
self.xform = BaseXformMatrix()
self.visible = True
self.children = []
self._parent = parent
if ( parent ):
parent.children.append( self )
self.cleared = False
self.clearMatrices()
def _setParent( self, parent ):
'''Sets the parent of this node.'''
if ( self._parent != parent ):
# remove from previous parent
if ( self._parent ):
self._parent.removeChild( self )
# add to new parent
if ( parent ):
parent.addChild( self )
parent = property( lambda self: self._parent, lambda self, p: self._setParent( p ) )
def clearMatrices( self ):
"""Clears all of the cached matrices: local, parent and world"""
if ( not self.cleared ):
self.matrix = None
self.invMatrix = None
self.worldMatrix = None
self.worldInverseMatrix = None
self.parentMatrix = None
self.parentInverseMatrix = None
for child in self.children:
child.clearMatrices()
self.cleared = True
def setTranslation( self, vec ):
self.xform.setTranslation( vec )
self.clearMatrices()
def addTranslation( self, vec ):
self.xform.addTranslation( vec )
self.clearMatrices()
def setScale( self, vec ):
self.xform.setScale( vec )
self.clearMatrices()
def setRotationDeg( self, vec ):
self.xform.setRotationDeg( vec )
self.clearMatrices()
def setRotationRad( self, vec ):
self.xform.setRotationRad( vec )
self.clearMatrices()
def addRotationDeg( self, vec ):
self.xform.addRotationDeg( vec )
self.clearMatrices()
def addRotationRad( self, vec ):
self.xform.addRotationRad( vec )
self.clearMatrices()
def setRotationOrder( self, order ):
self.xform.setRotationOrder( order )
self.clearMatrices()
def getMatrix( self ):
"""Returns the local transformation matrix"""
if ( not self.matrix ):
self.matrix = self.xform.getMatrix()
self.cleared = False
return self.matrix
def getInverseMatrix( self ):
"""Returns the local inverse transformation matrix"""
if ( not self.invMatrix ):
self.invMatrix = self.xform.getInverseMatrix()
self.cleared = False
return self.invMatrix
def getWorldMatrix( self ):
"""Returns the matrix which transforms this space to the world space"""
if ( not self.worldMatrix ):
if ( self.parent ):
self.worldMatrix = self.getMatrix() * self.parent.getWorldMatrix()
else:
self.worldMatrix = self.getMatrix()
self.cleared = False
return self.worldMatrix
def getWorldInverseMatrix( self ):
"""Returns the matrix which transforms world coordiantes into this space"""
if ( not self.worldInverseMatrix ):
if ( self.parent ):
self.worldInverseMatrix = self.parent.getWorldInverseMatrix() * self.getInverseMatrix()
else:
self.worldInverseMatrix = self.getInverseMatrix().copy()
self.cleared = False
return self.worldInverseMatrix
def getParentMatrix( self ):
"""Returns the parent's world matrix"""
if ( not self.parentMatrix ):
if ( self.parent ):
self.parentMatrix = self.parent.getWorldMatrix()
else:
self.parentMatrix = IDENTITY4x4
self.cleared = False
return self.parentMatrix
def getParentInverseMatrix( self ):
"""Returns the parent's world inverse matrix"""
if ( not self.parentInverseMatrix ):
self.parentMatrix = self.parent.getWorldInverseMatrix()
self.cleared = False
return self.parentMatrix
# query portions of the transformation matrix
def getTranslation( self, space=Space.WORLD ):
"""Returns the translation value of this transform node -- either in world or local space"""
# returns a Vector3
if ( space == Space.WORLD ):
return self.xform.getTranslation( self.getWorldMatrix() )
elif ( space == Space.LOCAL ):
return self.xform.trans
def drawGL( self, selectState=SelectState.DRAW, forceVisible=False ):
'''Draws this node to the current OpenGL context.
@param: selectState Indicates if the drawing is being done for
selection or visualization.
'''
if ( self.visible or forceVisible ):
glPushMatrix()
mat = self.xform.getMatrix()
glMultMatrixf( mat.getFlattened() )
self.drawCommands( selectState )
for child in self.children:
child.drawGL( selectState )
glPopMatrix()
## self.drawBB()
def drawCommands( self, selectState ):
'''The draw commands for this actual node's contents'''
pass
def getBB( self ):
'''Computes the axis-aligned bounding box of this node.
@returns: A 2-tuple of Vector3s. The (min, max) points of the BB.
'''
minPt = Vector3( 1e6, 1e6, 1e6 )
maxPt = -minPt
for n in self.children:
nMin, nMax = n.getBB()
minPt, maxPt = unionBB( minPt, maxPt, nMin, nMax )
return minPt, maxPt
def drawBB( self ):
'''Draws the bounding box on this node.'''
minPt, maxPt = self.getBB()
glPushAttrib( GL_COLOR_BUFFER_BIT | GL_LINE_BIT | GL_ENABLE_BIT )
glDisable( GL_LIGHTING )
glColor3f( 0.2, 0.9, 0.9 )
glLineWidth( 2.0 )
glBegin( GL_LINE_STRIP )
glVertex3f( minPt[0], minPt[1], minPt[2] )
glVertex3f( maxPt[0], minPt[1], minPt[2] )
glVertex3f( maxPt[0], maxPt[1], minPt[2] )
glVertex3f( minPt[0], maxPt[1], minPt[2] )
glVertex3f( minPt[0], minPt[1], minPt[2] )
glVertex3f( minPt[0], minPt[1], maxPt[2] )
glVertex3f( maxPt[0], minPt[1], maxPt[2] )
glVertex3f( maxPt[0], maxPt[1], maxPt[2] )
glVertex3f( minPt[0], maxPt[1], maxPt[2] )
glVertex3f( minPt[0], minPt[1], maxPt[2] )
glEnd()
glBegin( GL_LINES )
glVertex3f( minPt[0], maxPt[1], minPt[2] )
glVertex3f( minPt[0], maxPt[1], maxPt[2] )
glVertex3f( maxPt[0], maxPt[1], minPt[2] )
glVertex3f( maxPt[0], maxPt[1], maxPt[2] )
glVertex3f( maxPt[0], minPt[1], minPt[2] )
glVertex3f( maxPt[0], minPt[1], maxPt[2] )
glEnd()
glPopAttrib()
class DrawableNode( Node ):
'''A scenegraph node which actually has something to draw'''
def __init__( self, drawable, xform=IDENTITY4x4, parent=None ):
Node.__init__( self, xform, parent )
self.drawable = drawable
def getBB( self ):
'''Computes the axis-aligned bounding box of this node.
@returns: A 2-tuple of Vector3s. The (min, max) points of the BB.
'''
minPt, maxPt = self.drawable.getBB( self.getWorldMatrix() )
childMin, childMax = Node.getBB( self )
return unionBB( minPt, maxPt, childMin, childMax )
def drawCommands( self, selectState ):
self.drawable.drawGL()
class GeoNode( DrawableNode ):
'''A scenegraph node which draws geometry with materials'''
def __init__( self, drawable, xform=IDENTITY4x4, parent=None ):
DrawableNode.__init__( self, drawable, xform, parent )
self.material = Material()
def drawCommands( self, selectState ):
self.material.setGL( selectState )
DrawableNode.drawCommands( self, selectState )
def setMaterial( self, material ):
'''Sets the node's material.
@param: material The material to use for this node.
'''
self.material = material
class SelectableGeoNode( GeoNode, Selectable ):
'''A scene graph node for a selectable geometry node'''
def __init__( self, drawable, xform=IDENTITY4x4, parent=None ):
GeoNode.__init__( self, drawable, xform, parent )
Selectable.__init__( self )
def drawCommands( self, selectState=SelectState.DRAW ):
'''Draws the node into the current OpenGL context.
@param: selectState Indicates if the drawing is being done for
selection or visualization.
'''
if ( selectState == SelectState.SELECT ):
self.glName()
self.drawable.drawGL( selectState )
else:
self.material.setGL( self.selected )
self.drawable.drawGL( selectState )