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 / numarrays.py < prev    next >
Encoding:
Python Source  |  2008-12-20  |  4.3 KB  |  126 lines

  1. """NumArray implementation of the OpenGL-ctypes array interfaces
  2. """
  3. REGISTRY_NAME = 'numarray'
  4. try:
  5.     import numarray
  6. except ImportError, err:
  7.     raise ImportError( """No numarray module present: %s"""%(err))
  8. import operator
  9.  
  10. from OpenGL.arrays import numericnames
  11. implementationModuleName = numericnames.moduleName( numarray )
  12. try:
  13.     implementationModule = numericnames.loadModule( implementationModuleName )
  14. except ImportError, err:
  15.     raise ImportError( """Do not have implementation module %s for current numpy module"""%(
  16.         implementationModuleName,
  17.     ))
  18. dataPointer = implementationModule.dataPointer
  19.  
  20. from OpenGL import constants, constant
  21. from OpenGL.arrays import formathandler
  22.  
  23. class NumarrayHandler( formathandler.FormatHandler ):
  24.     """Numarray-specific data-type handler for OpenGL"""
  25.     @classmethod
  26.     def from_param( cls, value, typeCode=None ):
  27.         return dataPointer( value )
  28.     dataPointer = staticmethod( dataPointer )
  29.     HANDLED_TYPES = (numarray.ArrayType, )
  30.     isOutput = True
  31.  
  32.     def voidDataPointer( cls, value ):
  33.         """Given value in a known data-pointer type, return void_p for pointer"""
  34.         return ctypes.c_void_p( self.dataPointer( value ))
  35.     def zeros( self, dims, typeCode ):
  36.         """Return Numpy array of zeros in given size"""
  37.         return numarray.zeros( dims, GL_TYPE_TO_ARRAY_MAPPING.get(typeCode) or typeCode )
  38.     def arrayToGLType( self, value ):
  39.         """Given a value, guess OpenGL type of the corresponding pointer"""
  40.         typeCode = value.typecode()
  41.         constant = ARRAY_TO_GL_TYPE_MAPPING.get( typeCode )
  42.         if constant is None:
  43.             raise TypeError(
  44.                 """Don't know GL type for array of type %r, known types: %s\nvalue:%s"""%(
  45.                     typeCode, ARRAY_TO_GL_TYPE_MAPPING.keys(), value,
  46.                 )
  47.             )
  48.         return constant
  49.     def arraySize( self, value, typeCode = None ):
  50.         """Given a data-value, calculate dimensions for the array"""
  51.         try:
  52.             dimValue = value.shape
  53.         except AttributeError, err:
  54.             # XXX it's a list or a tuple, how do we determine dimensions there???
  55.             # for now we'll just punt and convert to an array first...
  56.             value = self.asArray( value, typeCode )
  57.             dimValue = value.shape 
  58.         dims = 1
  59.         for dim in dimValue:
  60.             dims *= dim 
  61.         return dims 
  62.     def asArray( self, value, typeCode=None ):
  63.         """Convert given value to an array value of given typeCode"""
  64.         if value is None:
  65.             return value
  66.         else:
  67.             return self.contiguous( value, typeCode )
  68.  
  69.     def contiguous( self, source, typeCode=None ):
  70.         """Get contiguous array from source
  71.         
  72.         source -- numarray Python array (or compatible object)
  73.             for use as the data source.  If this is not a contiguous
  74.             array of the given typeCode, a copy will be made, 
  75.             otherwise will just be returned unchanged.
  76.         typeCode -- optional 1-character typeCode specifier for
  77.             the numarray.array function.
  78.             
  79.         All gl*Pointer calls should use contiguous arrays, as non-
  80.         contiguous arrays will be re-copied on every rendering pass.
  81.         Although this doesn't raise an error, it does tend to slow
  82.         down rendering.
  83.         """
  84.         typeCode = GL_TYPE_TO_ARRAY_MAPPING[ typeCode ]
  85.         if isinstance( source, numarray.ArrayType):
  86.             if source.iscontiguous() and (typeCode is None or typeCode==source.typecode()):
  87.                 return source
  88.             else:
  89.                 # We have to do astype to avoid errors about unsafe conversions
  90.                 # XXX Confirm that this will *always* create a new contiguous array 
  91.                 # XXX Allow a way to make this raise an error for performance reasons
  92.                 # XXX Guard against wacky conversion types like uint to float, where
  93.                 # we really don't want to have the C-level conversion occur.
  94.                 return numarray.array( source.astype( typeCode ), typeCode )
  95.         elif typeCode:
  96.             return numarray.array( source, typeCode )
  97.         else:
  98.             return numarray.array( source )
  99.     def unitSize( self, value, typeCode=None ):
  100.         """Determine unit size of an array (if possible)"""
  101.         return value.shape[-1]
  102.     def dimensions( self, value, typeCode=None ):
  103.         """Determine dimensions of the passed array value (if possible)"""
  104.         return value.shape
  105.  
  106.  
  107. ARRAY_TO_GL_TYPE_MAPPING = {
  108.     'd': constants.GL_DOUBLE,
  109.     'f': constants.GL_FLOAT,
  110.     'i': constants.GL_INT,
  111.     's': constants.GL_SHORT,
  112.     'c': constants.GL_UNSIGNED_BYTE,
  113.     'b': constants.GL_BYTE,
  114.     'I': constants.GL_UNSIGNED_INT,
  115. }
  116. GL_TYPE_TO_ARRAY_MAPPING = {
  117.     constants.GL_DOUBLE: 'd',
  118.     constants.GL_FLOAT:'f',
  119.     constants.GL_INT: 'i',
  120.     constants.GL_UNSIGNED_INT: 'i',
  121.     constants.GL_UNSIGNED_BYTE: 'c',
  122.     constants.GL_SHORT: 's',
  123.     constants.GL_UNSIGNED_SHORT: 's',
  124.     constants.GL_BYTE: 'b',
  125. }
  126.