fsleyes.gl.glvolume¶
This module provides the GLVolume class, which creates and
encapsulates the data and logic required to render 2D slice of an
Image instance.
-
class
fsleyes.gl.glvolume.GLVolume(image, overlayList, displayCtx, canvas, threedee)[source]¶ Bases:
fsleyes.gl.glimageobject.GLImageObjectThe
GLVolumeclass is aGLImageObjectwhich encapsulates the data and logic required to renderImageoverlays in 2D and 3D.A
GLVolumeinstance may be used to render anImageinstance which has aDisplay.overlayTypeequal tovolume. It is assumed that thisImageinstance is associated with aDisplayinstance which, in turn, contains aVolumeOptsinstance, containing display options specific to volume rendering.Version dependent modules
The
GLVolumeclass makes use of the functions defined in thegl14.glvolume_funcsor thegl21.glvolume_funcsmodules, which provide OpenGL version specific details for creation/storage of vertex data, and for rendering.These version dependent modules must provide the following functions:
init(GLVolume)Perform any necessary initialisation.
destroy(GLVolume)Perform any necessary clean up.
compileShaders(GLVolume)(Re-)Compile the shader programs.
updateShaderState(GLVolume)Updates the shader program states when display parameters are changed.
preDraw(GLVolume, xform, bbox)Initialise the GL state, ready for drawing.
draw2D(GLVolume, zpos, xform)Draw a slice of the image at the given
zpos. Ifxformis notNone, it must be applied as a transformation on the vertex coordinates.draw3D(GLVolume, xform, bbox)Draw the image in 3D. If
xformis notNone, it must be applied as a transformation on the vertex coordinates.drawAll(Glvolume, zposes, xforms)Draws slices at each of the specified
zposes, applying the correspondingxformsto each.postDraw(GLVolume, xform, bbox)Clear the GL state after drawing.
2D rendering
Images are rendered in essentially the same way, regardless of which OpenGL version-specific module is used. The image data itself is stored as an
ImageTexture. ThisImageTextureis managed by theresourcesmodule, so may be shared by manyGLVolumeinstances. The current colour maps (defined by theVolumeOpts.cmapandVolumeOpts.negativeCmapproperties) are stored asColourMapTextureinstances. A slice through the texture is rendered using six vertices, located at the respective corners of the image bounds.Image voxels may be clipped according to the
VolumeOpts.clippingRangeproperty. By default, the voxel value is compared against the clipping range, but theVolumeOpts.clipImageproperty allows the data from a different image (of the same dimensions) to be used for clipping. If specified, this clipping image is stored as anotherImageTexture.If the
ColourMapOpts.modulateAlphasetting is active, the opacity of rendered voxels is modulated by the voxel intensity. IfVolumeOpts.modulateImageis also set, the opacity is modulated by the values in a different image - in this case, this image is stored in anotherImageTexture.An
AuxImageTextureManageris used to manage the clip and modulate textures.3D rendering
In 3D, images are rendered using a ray-casting approach. The image bounding box is drawn as a cuboid. Then for each pixel, a ray is cast from the ‘camera’ through the image texture. The resulting colour is generated from sampling points along the ray.
The
glvolume_funcsmodules are expected to perform this rendering off-screen, using twoRenderTextureinstances, available as attributesrenderTexture1and``renderTexture2``. After a call todraw3D, the final result is assuemd to be contained inrenderTexture1.Textures
The
GLVolumeclass uses the following textures:An
ImageTexture, a 3D texture which contains image data. This is bound to texture unit 0.A
ColourMapTexture, a 1D texture which contains the colour map defined by theVolumeOpts.cmapproperty. This is bound to texture unit 1.A
ColourMapTexture, a 1D texture which contains the colour map defined by theVolumeOpts.negativeCmapproperty. This is bound to texture unit 2.An
ImageTexturewhich contains the clippimg image data. This is bound to texture unit 3. If theVolumeOpts.clipImageproperty is not specified (i.e. it has a value ofNone), this texture will not be bound - in this case, the image texture is used for clipping.Two
RenderTextureinstances which are used for 3D rendering. Both of these textures have depth buffers. When one of these textures is being drawn it is bound to texture units 4 (for RGBA) and 5 (for depth).
Attributes
The following attributes are available on a
GLVolumeinstance:imageTextureThe
ImageTexturewhich stores the image data.clipTextureThe
ImageTexturewhich stores the clip image data.modulateTextureThe
ImageTexturewhich stores the modulate image data.colourTextureThe
ColourMapTextureused to store the colour map.negColourTextureThe
ColourMapTextureused to store the negative colour map.renderTexture1The first
RenderTextureused for 3D rendering.renderTexture2The first
RenderTextureused for 3D rendering.texNameA name used for the
imageTexture,colourTexture, andnegColourTexture`. The name for the latter is suffixed with ``'_neg'.-
__init__(image, overlayList, displayCtx, canvas, threedee)[source]¶ Create a
GLVolumeobject.- Parameters
image – An
Imageobject.overlayList – The
OverlayListdisplayCtx – The
DisplayContextobject managing the scene.canvas – The canvas doing the drawing.
threedee – Set up for 2D or 3D rendering.
-
destroy()[source]¶ This must be called when this
GLVolumeobject is no longer needed. It performs any needed clean up of OpenGL data (e.g. deleting texture handles), callsremoveDisplayListeners(), and callsGLImageObject.destroy().
-
texturesReady()[source]¶ Returns
Trueif theimageTextureandclipTexture(if applicable) are both ready to be used,Falseotherwise.
-
property
clipTexture¶ Returns the
ImageTextureassociated with theVolumeOpts.clipImage.
-
property
modulateTexture¶ Returns the
ImageTextureassociated with theVolumeOpts.modulateImage.
-
addDisplayListeners()[source]¶ Called by
__init__().Adds a bunch of listeners to the
Displayobject, and the associatedVolumeOptsinstance, which define how the image should be displayed.This is done so we can update the colour, vertex, and image data when display properties are changed.
-
removeDisplayListeners()[source]¶ Called by
destroy(). Removes all the property listeners that were added byaddDisplayListeners().
-
testUnsynced()[source]¶ Used by the
refreshImageTexture()method.Returns
Trueif certain criticalVolumeOptsproperties have been unsynced from the parent instance, meaning that thisGLVolumeinstance needs to create its own image texture; returnsFalseotherwise.
-
updateShaderState(*args, **kwargs)[source]¶ Calls
gl14.glvolume_funcs.updateShaderState()orgl21.glvolume_funcs.updateShaderStatea(), thenNotifier.notify(). Uses theidle.idleWhen()function to make sure that it is not called untilready()returnsTrue.- Parameters
alwaysNotify – Must be passed as a keyword argument. If
False(the default),notifyis only called ifglvolume_funcs.updateShaderStatereturnsTrue. Otherwise,notifyis always called.
-
refreshImageTexture(**kwargs)[source]¶ Refreshes the
ImageTextureused to store theImagedata. This is performed through theresourcesmodule, so the image texture can be shared between multipleGLVolumeinstances.All keyword arguments are passed through to the
ImageTextureconstructor.
-
registerAuxImage(which, image, onReady=None)[source]¶ Calls
AuxImageTextureManager.registerAuxImage(), making sure that the texture interpolation is set appropriately.
-
refreshColourTextures()[source]¶ Refreshes the
ColourMapTextureinstances used to colour image voxels.
-
preDraw(*args, **kwargs)[source]¶ Binds the
ImageTexturetoGL_TEXTURE0and theColourMapTexturetoGL_TEXTURE1, and calls the version-dependent ``preDrawfunction.
-
postDraw(*args, **kwargs)[source]¶ Unbinds the
ImageTextureandColourMapTexture, and calls the version-dependentpostDrawfunction.
-
getAuxTextureXform(which)[source]¶ Calculates a transformation matrix which will transform from the image coordinate system into the
VolumeOpts.clipImageorVolumeOpts.modulateImagecoordinate system. If the property isNone, it will be an identity transform.This transform is used by shader programs to find the auxillary image coordinates that correspond with specific image coordinates.
-
getModulateValueXform()[source]¶ Returns an affine transform to normalise alpha modulation values.
The GL volume shaders need to normalise the modulate value by the modulation range to generate an opacity value. We calculate a suitable scale and offset by buildin an affine transform which transforms voxel values from the image/modulate image texture range to 0/1, where 0 corresponds to the low modulate range bound, and 1 to the high modulate range bound. The resulting scale/offset can be used by the shader to convert a modulate value directly into an opacity value.
-
generateVertices2D(zpos, axes, bbox=None)[source]¶ Overrides
GLImageObject.generateVertices2D().Appliies the
ImageTextureBase.texCoordXform()to the texture coordinates - this is performed to support 2D images/textures.
-
generateVertices3D(bbox=None)[source]¶ Overrides
GLImageObject.generateVertices3D().Appliies the
ImageTextureBase.texCoordXform()to the texture coordinates - this is performed to support 2D images/textures.
-
_alphaChanged(*a)[source]¶ Called when the
Display.alphaproperty changes.
-
_lowClippingRangeChanged(*a)[source]¶ Called when the low
VolumeOpts.clippingRangeproperty changes. Separate listeners are used for the low and high clipping values to avoid unnecessary duplicate refreshes in the event that theVolumeOpts.linkLowRangesorVolumeOpts.linkHighRangesflags are set.
-
_highClippingRangeChanged(*a)[source]¶ Called when the high
VolumeOpts.clippingRangeproperty changes (see_lowClippingRangeChanged()).
-
_clipImageChanged(*a)[source]¶ Called when the
VolumeOpts.clipImageproperty changes.
-
_modulateImageChanged(*a)[source]¶ Called when the
VolumeOpts.modulateImageproperty changes.
-
_cmapChanged(*a)[source]¶ Called when the
VolumeOpts.cmaporVolumeOpts.negativeCmapproperties change.
-
_modulateAlphaChanged(*a)[source]¶ Called when the
VolumeOpts.modulateAlphaproperty changes. CallsupdateShaderState().
-
_enableOverrideDataRangeChanged(*a)[source]¶ Called when the
VolumeOpts.enableOverrideDataRangeproperty changes. Calls_volumeChanged().
-
_overrideDataRangeChanged(*a)[source]¶ Called when the
VolumeOpts.overrideDataRangeproperty changes. Calls_volumeChanged(), but only ifVolumeOpts.enableOverrideDataRangeisTrue.
-
_volumeChanged(*a, **kwa)[source]¶ Called when the
NiftiOpts.volumeproperty changes Also called when other properties, which require a texture refresh, change.
-
_channelChanged(*a, **kwa)[source]¶ Called when the
NiftiOpts.channelchanges. Refreshes the texture.
-
_transformChanged(*a)[source]¶ Called when the
NiftiOpts.transformproperty changes.
-
_displayXformChanged(*a)[source]¶ Called when the
NiftiOpts.displayXformproperty changes.
-
_numStepsChanged(*a)[source]¶ Called when the
Volume3DOpts.numStepsproperty changes.
-
_numInnerStepsChanged(*a)[source]¶ Called when the
Volume3DOpts.numInnerStepsproperty changes.
-
_resolutionChanged(*a)[source]¶ Called when the
Volume3DOpts.resolutionproperty changes.
-
_numClipPlanesChanged(*a)[source]¶ Called when the
Volume3DOpts.numClipPlanesproperty changes.
-
_clipModeChanged(*a)[source]¶ Called when the
Volume3DOpts.clipModeproperty changes.
-
_clipping3DChanged(*a)[source]¶ Called when any of the
Volume3DOpts.clipPosition,Volume3DOpts.clipAzimuth, orVolume3DOpts.clipInclinationproperties change.
-
_showClipPlanesChanged(*a)[source]¶ Called when the
Volume3DOpts.showClipPlanesproperty changes.
-
_blendPropertiesChanged(*a)[source]¶ Called when the
Volume3DOpts.showClipPlanesproperty changes.
-
_smoothingChanged(*a)[source]¶ Called when the
Volume3DOpts.smoothingproperty changes.
-
_imageSyncChanged(*a)[source]¶ Called when the synchronisation state of the
NiftiOpts.volumeorVolumeOpts.interpolationproperties change.
-
__texturesChanged(*a)¶ Called when either the
imageTextureor theclipTexturechanges. CallsupdateShaderState().
-
__module__= 'fsleyes.gl.glvolume'¶