def contiguous(cls, source, typeCode=None):
        """Get contiguous array from source

        source -- numpy Python array (or compatible object)
            for use as the data source.  If this is not a contiguous
            array of the given typeCode, a copy will be made,
            otherwise will just be returned unchanged.
        typeCode -- optional 1-character typeCode specifier for
            the numpy.array function.

        All gl*Pointer calls should use contiguous arrays, as non-
        contiguous arrays will be re-copied on every rendering pass.
        Although this doesn"t raise an error, it does tend to slow
        down rendering.
        """
        typeCode = GL_TYPE_TO_ARRAY_MAPPING[typeCode]
        try:
            contiguous = source.flags.contiguous
        except AttributeError as err:
            if typeCode:
                return numpy.ascontiguousarray(source, typeCode)
            else:
                return numpy.ascontiguousarray(source)
        else:
            if contiguous and (typeCode is None
                               or typeCode == source.dtype.char):
                return source
            elif (contiguous and cls.ERROR_ON_COPY):
                from OpenGL import error
                raise error.CopyError(
                    """Array of type %r passed, required array of type %r""",
                    source.dtype.char,
                    typeCode,
                )
            else:
                # We have to do astype to avoid errors about unsafe conversions
                # XXX Confirm that this will *always* create a new contiguous array
                # XXX Guard against wacky conversion types like uint to float, where
                # we really don"t want to have the C-level conversion occur.
                # XXX ascontiguousarray is apparently now available in numpy!
                if cls.ERROR_ON_COPY:
                    from OpenGL import error
                    raise error.CopyError(
                        """Non-contiguous array passed""",
                        source,
                    )
                if typeCode is None:
                    typeCode = source.dtype.char
                return numpy.ascontiguousarray(source, typeCode)
Example #2
0
 def from_param(cls, value, typeCode=None):
     # TODO: raise CopyError if the flag is set!
     converted = _bytes.as_8_bit(value)
     result = StringHandler.from_param(converted)
     if converted is not value:
         if ERROR_ON_COPY:
             raise error.CopyError(
                 """Unicode string passed, cannot copy with ERROR_ON_COPY set, please use 8-bit strings"""
             )
         result._temporary_array_ = converted
     return result
Example #3
0
 def arrayByteCount(cls, value, typeCode=None):
     """Given a data-value, calculate number of bytes required to represent"""
     try:
         return value.nbytes
     except AttributeError, err:
         if cls.ERROR_ON_COPY:
             raise error.CopyError(
                 """Non-numpy array passed to numpy arrayByteCount: %s""",
                 type(value),
             )
         value = cls.asArray(value, typeCode)
         return value.nbytes
 def from_param( cls, instance, typeCode=None ):
     try:
         pointer = cls.dataPointer( instance )
     except TypeError as err:
         array = cls.asArray( instance, typeCode )
         pp = cls.dataPointer( array )
         pp._temporary_array_ = (array,)
         return pp
     else:
         if typeCode and instance.dtype != GL_TYPE_TO_ARRAY_MAPPING[ typeCode ]:
             raise error.CopyError(
                 """Array of type %r passed, required array of type %r""",
                 instance.dtype.char, typeCode,
             )
         return c_void_p( pointer )
Example #5
0
 def raiseErrorOnCopy(self, value, *args, **named):
     raise error.CopyError(
         """%s passed, cannot copy with ERROR_ON_COPY set, please use an array type which has native data-pointer support (e.g. numpy or ctypes arrays)"""
         % (value.__class__.__name__, ))
Example #6
0
 try:
     contiguous = source.flags.contiguous
 except AttributeError, err:
     if typeCode:
         return numpy.ascontiguousarray(source, typeCode)
     else:
         return numpy.ascontiguousarray(source)
 else:
     if contiguous and (typeCode is None
                        or typeCode == source.dtype.char):
         return source
     elif (contiguous and cls.ERROR_ON_COPY):
         from OpenGL import error
         raise error.CopyError(
             """Array of type %r passed, required array of type %r""",
             source.dtype.char,
             typeCode,
         )
     else:
         # We have to do astype to avoid errors about unsafe conversions
         # XXX Confirm that this will *always* create a new contiguous array
         # XXX Guard against wacky conversion types like uint to float, where
         # we really don't want to have the C-level conversion occur.
         # XXX ascontiguousarray is apparently now available in numpy!
         if cls.ERROR_ON_COPY:
             from OpenGL import error
             raise error.CopyError(
                 """Non-contiguous array passed""",
                 source,
             )
         if typeCode is None: