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 / numeric.py < prev    next >
Encoding:
Python Source  |  2008-12-20  |  4.5 KB  |  129 lines

  1. """Original Numeric module implementation of the OpenGL-ctypes array interfaces
  2.  
  3. Eventual Goals:
  4.     * Be able to register handlers for a given data-storage mechanism at
  5.         run-time
  6.     * Be able to choose what data-type to use for any given operation where 
  7.         we are getting return values and/or register a default format for
  8.         return values (i.e. tell OpenGL to return ctypes pointers or Numeric 
  9.         arrays or Numarray arrays for glGet* calls)
  10. """
  11. REGISTRY_NAME = 'numeric'
  12. try:
  13.     import Numeric
  14. except ImportError, err:
  15.     raise ImportError( """No Numeric module present: %s"""%(err))
  16. import operator
  17. from OpenGL.arrays import _numeric
  18.  
  19. dataPointer = _numeric.dataPointer
  20.  
  21. from OpenGL import constants, constant
  22. from OpenGL.arrays import formathandler
  23.  
  24. class NumericHandler( formathandler.FormatHandler ):
  25.     """Numeric-specific data-type handler for OpenGL"""
  26.     HANDLED_TYPES = (Numeric.ArrayType, )
  27.     isOutput = True
  28.     @classmethod
  29.     def from_param( cls, value, typeCode=None ):
  30.         return dataPointer( value )
  31.     dataPointer = staticmethod( dataPointer )
  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 Numeric array of zeros in given size"""
  37.         return Numeric.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 -- Numeric 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 Numeric.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.get(  typeCode )
  85.         if isinstance( source, Numeric.ArrayType):
  86.             if source.iscontiguous() and (typeCode is None or typeCode==source.typecode()):
  87.                 return source
  88.             else:
  89.                 if typeCode is None:
  90.                     typeCode = source.typecode()
  91.                 # We have to do astype to avoid errors about unsafe conversions
  92.                 # XXX Confirm that this will *always* create a new contiguous array 
  93.                 # XXX Allow a way to make this raise an error for performance reasons
  94.                 # XXX Guard against wacky conversion types like uint to float, where
  95.                 # we really don't want to have the C-level conversion occur.
  96.                 return Numeric.array( source.astype( typeCode ), typeCode )
  97.         elif typeCode:
  98.             return Numeric.array( source, typeCode )
  99.         else:
  100.             return Numeric.array( source )
  101.     def unitSize( self, value, typeCode=None ):
  102.         """Determine unit size of an array (if possible)"""
  103.         return value.shape[-1]
  104.     def dimensions( self, value, typeCode=None ):
  105.         """Determine dimensions of the passed array value (if possible)"""
  106.         return value.shape
  107.  
  108.  
  109. ARRAY_TO_GL_TYPE_MAPPING = {
  110.     'd': constants.GL_DOUBLE,
  111.     'f': constants.GL_FLOAT,
  112.     'i': constants.GL_INT,
  113.     's': constants.GL_SHORT,
  114.     'c': constants.GL_UNSIGNED_BYTE,
  115.     'b': constants.GL_BYTE,
  116.     'I': constants.GL_UNSIGNED_INT,
  117. }
  118. GL_TYPE_TO_ARRAY_MAPPING = {
  119.     constants.GL_DOUBLE: 'd',
  120.     constants.GL_FLOAT:'f',
  121.     constants.GL_INT: 'i',
  122.     constants.GL_UNSIGNED_INT: 'i',
  123.     constants.GL_UNSIGNED_BYTE: 'c',
  124.     constants.GL_SHORT: 's',
  125.     constants.GL_UNSIGNED_SHORT: 's',
  126.     constants.GL_BYTE: 'b',
  127. }
  128.  
  129.