home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / lib / site-packages / OpenGL / arrays / arrayhelpers.py < prev    next >
Encoding:
Python Source  |  2008-12-07  |  4.7 KB  |  139 lines

  1. """Helper functions for wrapping array-using operations
  2.  
  3. These are functions intended to be used in wrapping
  4. GL functions that deal with OpenGL array data-types.
  5. """
  6. from OpenGL import contextdata, error, wrapper, constants, converters
  7. from OpenGL.arrays import arraydatatype
  8.  
  9. def asArrayType( typ ):
  10.     """Create PyConverter to get first argument as array of type"""
  11.     return converters.CallFuncPyConverter( typ.asArray )
  12.  
  13. def typedPointer( typ, ctyp ):
  14.     """Create a CResolver to get argument as a pointer to the ctypes type ctyp
  15.     
  16.     i.e. typedPointer( arrays.UIntArray, ctypes.c_uint )
  17.     """
  18.     import ctypes
  19.     ctyp = ctypes.POINTER( ctyp )
  20.     def typedPointerConverter(  value ):
  21.         """Get the arg as an array of the appropriate type"""
  22.         return ctypes.cast( typ.dataPointer(value), ctyp)
  23.     return typedPointerConverter
  24.     
  25. def asArrayTypeSize( typ, size ):
  26.     """Create PyConverter function to get array as type and check size
  27.     
  28.     Produces a raw function, not a PyConverter instance
  29.     """
  30.     asArray = typ.asArray
  31.     arraySize = typ.arraySize
  32.     def asArraySize( incoming, function, args ):
  33.         result = asArray( incoming )
  34.         actualSize = arraySize(result)
  35.         if actualSize != size:
  36.             raise ValueError(
  37.                 """Expected %r item array, got %r item array"""%(
  38.                     size,
  39.                     actualSize,
  40.                 ),
  41.                 incoming,
  42.             )
  43.         return result
  44.     return asArraySize
  45.  
  46. def asVoidArray( ):
  47.     """Create PyConverter returning incoming as an array of any type"""
  48.     from OpenGL.arrays import ArrayDatatype
  49.     return converters.CallFuncPyConverter( ArrayDatatype.asArray )
  50.  
  51. class storePointerType( object ):
  52.     """Store named pointer value in context indexed by constant
  53.     
  54.     pointerName -- named pointer argument 
  55.     constant -- constant used to index in the context storage
  56.     
  57.     Stores the pyArgs (i.e. result of pyConverters) for the named
  58.     pointer argument...
  59.     """
  60.     def __init__( self, pointerName, constant ):
  61.         self.pointerName = pointerName
  62.         self.constant = constant 
  63.     def finalise( self, wrapper ):
  64.         self.pointerIndex = wrapper.pyArgIndex( self.pointerName )
  65.     def __call__( self, result, baseOperation, pyArgs, cArgs ):
  66.         contextdata.setValue( self.constant, pyArgs[self.pointerIndex] )
  67.  
  68. def returnPointer( result,baseOperation,pyArgs,cArgs, ):
  69.     """Return the converted object as result of function
  70.     
  71.     Note: this is a hack that always returns pyArgs[0]!
  72.     """
  73.     return pyArgs[0]
  74.  
  75. def setInputArraySizeType( baseOperation, size, type, argName=0 ):
  76.     """Decorate function with vector-handling code for a single argument
  77.     
  78.     This assumes *no* argument expansion, i.e. a 1:1 correspondence...
  79.     """
  80.     function = wrapper.wrapper( baseOperation )
  81.     if not hasattr( function, 'returnValues' ):
  82.         function.setReturnValues( returnPointer )
  83.     if size is not None:
  84.         function.setPyConverter( argName, asArrayTypeSize(type, size) )
  85.     else:
  86.         function.setPyConverter( argName, asArrayType(type) )
  87.     function.setCConverter( argName, converters.getPyArgsName( argName ) )
  88.     return function
  89.  
  90. def arraySizeOfFirstType( typ, default ):
  91.     def arraySizeOfFirst( pyArgs, index, baseOperation ):
  92.         """Return the array size of the first argument"""
  93.         array = pyArgs[0]
  94.         if array is None:
  95.             return default
  96.         else:
  97.             return typ.unitSize( array )
  98.     return arraySizeOfFirst
  99.  
  100. class AsArrayOfType( converters.PyConverter ):
  101.     """Given arrayName and typeName coerce arrayName to array of type typeName"""
  102.     argNames = ( 'arrayName','typeName' )
  103.     indexLookups = ( 
  104.         ('arrayIndex', 'arrayName','pyArgIndex'),
  105.         ('typeIndex', 'typeName','pyArgIndex'),
  106.     )
  107.     def __init__( self, arrayName='pointer', typeName='type' ):
  108.         self.arrayName = arrayName
  109.         self.typeName = typeName 
  110.     def __call__( self, arg, wrappedOperation, args):
  111.         """Get the arg as an array of the appropriate type"""
  112.         type = args[ self.typeIndex ]
  113.         arrayType = arraydatatype.GL_CONSTANT_TO_ARRAY_TYPE[ type ]
  114.         return arrayType.asArray( arg )
  115. class AsArrayTyped( converters.PyConverter ):
  116.     """Given arrayName and arrayType, convert arrayName to array of type"""
  117.     argNames = ( 'arrayName','arrayType' )
  118.     indexLookups = ( 
  119.         ('arrayIndex', 'arrayName','pyArgIndex'),
  120.     )
  121.     def __init__( self, arrayName='pointer', arrayType=None ):
  122.         self.arrayName = arrayName
  123.         self.arrayType = arrayType
  124.     def __call__( self, arg, wrappedOperation, args):
  125.         """Get the arg as an array of the appropriate type"""
  126.         return self.arrayType.asArray( arg )
  127. class AsArrayTypedSize( converters.CConverter ):
  128.     """Given arrayName and arrayType, determine size of arrayName"""
  129.     argNames = ( 'arrayName','arrayType' )
  130.     indexLookups = ( 
  131.         ('arrayIndex', 'arrayName','pyArgIndex'),
  132.     )
  133.     def __init__( self, arrayName='pointer', arrayType=None ):
  134.         self.arrayName = arrayName
  135.         self.arrayType = arrayType
  136.     def __call__( self, pyArgs, index, wrappedOperation ):
  137.         """Get the arg as an array of the appropriate type"""
  138.         return self.arrayType.arraySize( pyArgs[self.arrayIndex ] )
  139.