fsleyes.gl.textures.texture3d¶
This module provides the Texture3D class, which represents a
3D OpenGL texture.
-
class
fsleyes.gl.textures.texture3d.Texture3D(name, nvals=1, notify=True, threaded=None, **kwargs)¶ Bases:
fsleyes.gl.textures.texture.Texture,__main__.MockClassThe
Texture3Dclass contains the logic required to create and manage a 3D texture.A number of texture settings can be configured through the following methods:
setSet any parameters on this Texture3D.refresh(Re-)configures the OpenGL texture. setDataSets the texture data - assumed to be a numpyarray.setInterpSets the texture interpolation - either GL_NEARESTorGL_LINEAR.setResolutionSets the texture data resolution - this value is passed to the routines.subsample()function, in the__prepareTextureData()method.setScalesSets scaling factors for each axis of the texture data. setPrefilterSets the prefilter function - texture data is passed through this function before being uploaded to the GPU. setPrefilterRangeSets the prefilter range function - if the prefilterfunction changes the data range, this function must be provided.setNormaliseEnable/disable normalisation. readyReturns Trueif thisTexture3Dis ready to be used,Falseotherwise.textureShapeReturn a tuple containing the texture data shape. voxValXformReturn a transformation matrix that can be used to transform values read from the texture back to the original data range. invVoxValXformReturn a transformation matrix that can be used to transform values in the original data range to values as read from the texture. When a
Texture3Dis created, and when its settings are changed, it may need to prepare the data to be passed to OpenGL - for large textures, this can be a time consuming process, so this is performed on a separate thread using theidlemodule (unless thethreadedparameter to__init__()is set toFalse). Theready()method returnsTrueorFalseto indicate whether theTexture3Dcan be used.Furthermore, the
Texture3Dclass derives fromNotifier, so listeners can register to be notified when anTexture3Dis ready to be used.-
__init__(name, nvals=1, notify=True, threaded=None, **kwargs)¶ Create a
Texture3D.Parameters: - name – A unique name for the texture.
- nvals – Number of values per voxel. Currently must be either
1or3. - notify – Passed to the initial call to
refresh(). - threaded – If
True, the texture data will be prepared on a separate thread (on calls torefresh()). IfFalse, the texture data is prepared on the calling thread, and therefresh()call will block until it has been prepared.
All other keyword arguments are passed through to the
set()method, and thus used as initial texture settings.Note
The default value of the
threadedparameter is set to the value offsl.utils.platform.Platform.haveGui.
-
destroy()¶ Must be called when this
Texture3Dis no longer needed. Deletes the texture handle.
-
classmethod
canUseFloatTextures(*args, **kwargs)¶ MagicMock is a subclass of Mock with default implementations of most of the magic methods. You can use MagicMock without having to configure the magic methods yourself.
If you use the spec or spec_set arguments then only magic methods that exist in the spec will be created.
Attributes and the return value of a MagicMock will also be MagicMocks.
-
classmethod
oneChannelFormat(*args, **kwargs)¶ MagicMock is a subclass of Mock with default implementations of most of the magic methods. You can use MagicMock without having to configure the magic methods yourself.
If you use the spec or spec_set arguments then only magic methods that exist in the spec will be created.
Attributes and the return value of a MagicMock will also be MagicMocks.
-
classmethod
getTextureType(*args, **kwargs)¶ MagicMock is a subclass of Mock with default implementations of most of the magic methods. You can use MagicMock without having to configure the magic methods yourself.
If you use the spec or spec_set arguments then only magic methods that exist in the spec will be created.
Attributes and the return value of a MagicMock will also be MagicMocks.
-
ready()¶ Returns
Trueif thisTexture3Dis ready to be used,Falseotherwise.
-
setInterp(interp)¶ Sets the texture interpolation - either
GL_NEARESTorGL_LINEAR.
-
setData(data)¶ Sets the texture data - assumed to be a
numpyarray.
-
setPrefilter(prefilter)¶ Sets the prefilter function - texture data is passed through this function before being uploaded to the GPU.
If this function changes the range of the data, you must also provide a
prefilterRangefunction - seesetPrefilterRange().
-
setPrefilterRange(prefilterRange)¶ Sets the prefilter range function - if the
prefilterfunction changes the data range, this function must be provided. It is passed two parameters - the known data minimum and maximum, and must adjust these values so that they reflect the adjusted range of the data that was passed to theprefilterfunction.
-
setResolution(resolution)¶ Sets the texture data resolution - this value is passed to the
routines.subsample()function, in the__prepareTextureData()method.
-
setScales(scales)¶ Sets scaling factors for each axis of the texture data. These values are solely used to calculate the sub-sampling rate if the resolution (as set by
setResolution()) is in terms of something other than data indices (e.g.Imagepixdims).
-
setNormalise(normalise)¶ Enable/disable normalisation.
If
normalise=True, the data is normalised to lie in the range[0, 1](or normalised to the full range, if being stored as integers) before being stored. The data is normalised according to the minimum/maximum of the data, or to a normalise range set viasetNormaliseRange().Set to
Falseto disable normalisation.Note
If the data is not of a type that can be stored natively as a texture, the data is automatically normalised, regardless of the value specified here.
-
setNormaliseRange(normaliseRange)¶ Enable/disable normalisation.
If normalisation is enabled (see
setNormalise()), or necessary, the data is normalised according to either its minimum/maximum, or to the range specified via this method.This parameter must be a sequence of two values, containing the
(min, max)normalisation range. The data is then normalised to lie in the range[0, 1](or normalised to the full range, if being stored as integers) before being stored.If
None, the data minimum/maximum are calculated and used.
-
voxValXform¶ Return a transformation matrix that can be used to transform values read from the texture back to the original data range.
-
invVoxValXform¶ Return a transformation matrix that can be used to transform values in the original data range to values as read from the texture.
-
textureShape¶ Return a tuple containing the texture data shape.
-
patchData(data, offset)¶ This is a shortcut method which can be used to replace part of the image texture data without having to regenerate the entire texture.
The
set()andrefresh()methods are quite heavyweight, and are written in such a way that partial texture updates are not possible. This method was added as a hacky workaround to allow small parts of the image texture to be quickly updated.Note
Hopefully, at some stage, I will refactor the
Texture3Dclass to be more flexible. Therefore, this method might disappear in the future.
-
set(**kwargs)¶ Set any parameters on this
Texture3D. Valid keyword arguments are:interpSee setInterp().dataSee setData().prefilterSee setPrefilter().prefilterRangeSee setPrefilterRange()resolutionSee setResolution().scalesSee setScales().normaliseSee setNormalise.()normaliseRangeSee setNormaliseRange().refreshIf True(the default), therefresh()function is called (but only if a setting has changed).callbackOptional function which will be called (via idle.idle()) when the texture has been refreshed. Only called ifrefreshisTrue, and a setting has changed.notifyPassed through to the refresh()method.Returns: Trueif any settings have changed and theTexture3Dis to be refreshed,Falseotherwise.
-
refresh(*args, **kwargs)¶ (Re-)configures the OpenGL texture.
Note
This method is a wrapper around the
__refresh()method, which does the real work, and which is not intended to be called from outside theTexture3Dclass.
-
_Texture3D__determineTextureType()¶ Figures out how the texture data should be stored as an OpenGL 3D texture. This method just figures out the data types which should be used to store the data as a texture. Any necessary data conversion/ transformation is performed by the
__prepareTextureData()method.The data can be stored as a GL texture as-is if:
- it is of type
uint8oruint16 - it is of type
float32, and this GL environment has support for floating point textures. Support for floating point textures is determined by testing for the availability of theARB_texture_floatextension.
In all other cases, the data needs to be converted to a supported data type, and potentially normalised, before it can be used as texture data. If floating point textures are available, the data is converted to float32. Otherwise, the data is converted to
uint16, and normalised to take the fulluint16data range (0-65535), and a transformation matrix is saved, allowing transformation of this normalised data back to its original data range.Note
OpenGL does different things to 3D texture data depending on its type: unsigned integer types are normalised from
[0, INT_MAX]to[0, 1].Floating point texture data types are, by default, clamped (not normalised), to the range
[0, 1]! We can overcome by this by using a true floating point texture, which is accomplished by using one of the data types provided by theARB_texture_floatextension. If this extension is not available, we have no choice but to normalise the data.This method sets the following attributes on this
Texture3Dinstance:__texFmtThe texture format (e.g. GL_RGB,GL_LUMINANCE, etc).__texIntFmtThe internal texture format used by OpenGL for storage (e.g. GL_RGB16,GL_LUMINANCE8, etc).__texDtypeThe raw type of the texture data (e.g. GL_UNSIGNED_SHORT)- it is of type
-
_Texture3D__prepareTextureData()¶ This method is a wrapper around the
__realPrepareTextureData()method.This method passes the stored image data to
realPrepareTextureData, and then stores references to its return valuesa as attributes on thisImageTextureinstance:__preparedataA numpyarray containing the image data, ready to be copied to the GPU.__voxValXformAn affine transformation matrix which encodes an offset and a scale, which may be used to transform the texture data from the range [0.0, 1.0]to its raw data range.__invVoxValXformInverse of voxValXform.
-
_Texture3D__realPrepareTextureData(data)¶ This method prepares and returns the given
data, ready to be used as GL texture data.This process potentially involves:
- Resampling to a different resolution (see the
routines.subsample()function). - Pre-filtering (see the
prefilterparameter to__init__()). - Normalising (if the
normaliseparameter to__init__()wasTrue, or if the data type cannot be used as-is). - Casting to a different data type (if the data type cannot be used as-is).
Returns: A tuple containing: - A
numpyarray containing the image data, ready to be - copied to the GPU.
- A
- An affine transformation matrix which encodes an offset
and a scale, which may be used to transform the texture
data from the range
[0.0, 1.0]to its raw data range. - Inverse of
voxValXform.
- Resampling to a different resolution (see the
-
_Texture3D__refresh(*args, **kwargs)¶ (Re-)configures the OpenGL texture.
Parameters: - refreshData – If
True(the default), the texture data is refreshed. - notify – If
True(the default), a notification is triggered via theNotifierbase-class, when thisTexture3Dhas been refreshed, and is ready to use. Otherwise, the notification is suppressed. - callback – Optional function which will be called (via
idle.idle()) when the texture has been refreshed. Only called ifrefreshisTrue, and a setting has changed.
This method sets an attribute
__textureShapeon thisTexture3Dinstance, containing the shape of the texture data.Note
The texture data is generated on a separate thread, using the
idle.run()function.- refreshData – If
-
__module__= 'fsleyes.gl.textures.texture3d'¶
-