Commit c166c157 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

RF: decouple tensor, rgbvolume, rgbvector, linevector from canvas

parent 0b018517
......@@ -147,13 +147,12 @@ def preDraw(self):
self.shader.load()
def draw2D(self, zpos, axes, xform=None, bbox=True):
def draw2D(self, canvas, zpos, axes, xform=None, bbox=True):
"""Draws the line vertices corresponding to a 2D plane located
at the specified Z location.
"""
opts = self.opts
canvas = self.canvas
shader = self.shader
mvpmat = canvas.mvpMatrix
......@@ -161,7 +160,7 @@ def draw2D(self, zpos, axes, xform=None, bbox=True):
else: bbox = None
vertices, indices, voxCoords = self.lineVertices.getVertices2D(
self, zpos, axes, bbox=bbox)
self, canvas, zpos, axes, bbox=bbox)
if vertices.size == 0:
return
......@@ -180,10 +179,10 @@ def draw2D(self, zpos, axes, xform=None, bbox=True):
shader.draw(gl.GL_TRIANGLES)
def drawAll(self, axes, zposes, xforms):
def drawAll(self, canvas, axes, zposes, xforms):
"""Draws line vertices corresponding to each Z location. """
for zpos, xform in zip(zposes, xforms):
draw2D(self, zpos, axes, xform, bbox=False)
draw2D(self, canvas, zpos, axes, xform, bbox=False)
def postDraw(self):
......
......@@ -84,18 +84,17 @@ def updateShaderState(self):
return changed
def draw2D(self, zpos, axes, xform=None):
def draw2D(self, *args, **kawrgs):
"""Draws a 2D slice at the given ``zpos``. Uses the
:func:`.gl14.glvolume_funcs.draw2D` function.
"""
with self.shader.loaded():
glvolume_funcs.draw2D(self, zpos, axes, xform)
glvolume_funcs.draw2D(self, *args, **kawrgs)
def drawAll(self, axes, zposes, xforms):
def drawAll(self, *args, **kawrgs):
"""Draws all specified slices. Uses the
:func:`.gl14.glvolume_funcs.drawAll` function.
"""
with self.shader.loaded():
glvolume_funcs.drawAll(self, axes, zposes, xforms)
glvolume_funcs.drawAll(self, *args, **kawrgs)
......@@ -149,7 +149,7 @@ def preDraw(self):
self.shader.load()
def draw2D(self, zpos, axes, xform=None, bbox=True):
def draw2D(self, canvas, zpos, axes, xform=None, bbox=True):
"""Draws the line vectors at a plane at the specified Z location.
Voxel coordinates are passed to the vertex shader, which calculates
the corresponding line vertex locations.
......@@ -157,8 +157,7 @@ def draw2D(self, zpos, axes, xform=None, bbox=True):
opts = self.opts
shader = self.shader
canvas = self.canvas
lineWidth = self.normalisedLineWidth
lineWidth = self.normalisedLineWidth(canvas)
mvpmat = canvas.mvpMatrix
v2dMat = opts.getTransform('voxel', 'display')
......@@ -213,10 +212,10 @@ def draw2D(self, zpos, axes, xform=None, bbox=True):
def drawAll(self, axes, zposes, xforms):
def drawAll(self, canvas, axes, zposes, xforms):
"""Draws line vertices corresponding to each Z location. """
for zpos, xform in zip(zposes, xforms):
draw2D(self, zpos, axes, xform, bbox=False)
draw2D(self, canvas, zpos, axes, xform, bbox=False)
def postDraw(self):
......
......@@ -83,17 +83,17 @@ def updateShaderState(self):
return changed
def draw2D(self, zpos, axes, xform=None):
def draw2D(self, *args, **kawrgs):
"""Draws a 2D slice at the given ``zpos``. Uses the
:func:`.gl21.glvolume_funcs.draw2D` function.
"""
with self.shader.loaded():
glvolume_funcs.draw2D(self, zpos, axes, xform)
glvolume_funcs.draw2D(self, *args, **kawrgs)
def drawAll(self, axes, zposes, xforms):
def drawAll(self, *args, **kawrgs):
"""Draws all specified slices. Uses the
:func:`.gl21.glvolume_funcs.drawAll` function.
"""
with self.shader.loaded():
glvolume_funcs.drawAll(self, axes, zposes, xforms)
glvolume_funcs.drawAll(self, *args, **kawrgs)
......@@ -145,18 +145,7 @@ def preDraw(self):
does some shader state configuration.
"""
shader = self.shader
shader.load()
# Calculate a transformation matrix for
# normal vectors - T(I(MV matrix))
mvMat = self.canvas.viewMatrix[:3, :3]
v2dMat = self.opts.getTransform('voxel', 'display')[:3, :3]
normalMatrix = affine.concat(mvMat, v2dMat)
normalMatrix = npla.inv(normalMatrix).T
shader.set('normalMatrix', normalMatrix)
self.shader.load()
gl.glEnable(gl.GL_CULL_FACE)
gl.glEnable(gl.GL_DEPTH_TEST)
......@@ -164,17 +153,22 @@ def preDraw(self):
gl.glCullFace(gl.GL_BACK)
def draw2D(self, zpos, axes, xform=None, applyBbox=True):
def draw2D(self, canvas, zpos, axes, xform=None, applyBbox=True):
"""Generates voxel coordinates for each tensor to be drawn, does some
final shader state configuration, and draws the tensors.
"""
opts = self.opts
shader = self.shader
canvas = self.canvas
mvp = canvas.mvpMatrix
v2dMat = opts.getTransform('voxel', 'display')
# Calculate a transformation matrix for
# normal vectors - T(I(MV matrix))
mvMat = canvas.viewMatrix[:3, :3]
normalMatrix = affine.concat(mvMat, v2dMat[:3, :3])
normalMatrix = npla.inv(normalMatrix).T
if applyBbox: bbox = canvas.viewport
else: bbox = None
......@@ -188,6 +182,7 @@ def draw2D(self, zpos, axes, xform=None, applyBbox=True):
# voxel coordinates for every sphere drawn
shader.setAtt('voxel', voxels, divisor=1)
shader.set( 'voxToDisplayMat', xform)
shader.set( 'normalMatrix', normalMatrix)
with shader.loadedAtts():
glexts.glDrawElementsInstanced(gl.GL_TRIANGLES,
......@@ -197,10 +192,10 @@ def draw2D(self, zpos, axes, xform=None, applyBbox=True):
nVoxels)
def drawAll(self, axes, zposes, xforms):
def drawAll(self, canvas, axes, zposes, xforms):
"""Draws all of the specified slices. """
for zpos, xform in zip(zposes, xforms):
draw2D(self, zpos, axes, xform, applyBbox=False)
draw2D(self, canvas, zpos, axes, xform, applyBbox=False)
def postDraw(self):
......
......@@ -55,14 +55,13 @@ class GLLineVector(glvector.GLVector):
"""
def __init__(self, image, overlayList, displayCtx, canvas, threedee):
def __init__(self, image, overlayList, displayCtx, threedee):
"""Create a ``GLLineVector`` instance.
:arg image: An :class:`.Image` or :class:`.DTIFitTensor`
instance.
:arg overlayList: The :class:`.OverlayList`
:arg displayCtx: The :class:`.DisplayContext` managing the scene.
:arg canvas: The canvas doing the drawing.
:arg threedee: 2D or 3D rendering.
"""
......@@ -111,7 +110,6 @@ class GLLineVector(glvector.GLVector):
image,
overlayList,
displayCtx,
canvas,
threedee,
prefilter=prefilter,
prefilterRange=prefilterRange,
......@@ -136,15 +134,13 @@ class GLLineVector(glvector.GLVector):
glvector.GLVector.destroy(self)
@property
def normalisedLineWidth(self):
def normalisedLineWidth(self, canvas):
"""Returns the :attr:`.LineVectorOpts.lineWidth`, scaled to be
proportional to the same coordinate system that the vector values
are defined in (assumed to be scaled voxels, e.g. the FSL coordinate
system).
"""
opts = self.opts
canvas = self.canvas
d2p = opts.getTransform('display', 'pixdim')
lineWidth = opts.lineWidth * canvas.pixelSize()[0]
lineWidth = affine.transform([lineWidth] * 3, d2p, vector=True)[0]
......@@ -387,7 +383,7 @@ class GLLineVertices:
self.__hash = self.calculateHash(glvec)
def getVertices2D(self, glvec, zpos, axes, bbox=None):
def getVertices2D(self, glvec, canvas, zpos, axes, bbox=None):
"""Extracts and returns a slice of line vertices, and the associated
voxel coordinates, which are in a plane located at the given Z
position (in display coordinates).
......@@ -401,7 +397,6 @@ class GLLineVertices:
opts = glvec.opts
image = glvec.image
canvas = glvec.canvas
shape = image.shape[:3]
zax = axes[2]
......@@ -426,7 +421,7 @@ class GLLineVertices:
# Convert line segments into rectangles so
# we can draw lines at arbitrary widths.
lineWidth = glvec.normalisedLineWidth
lineWidth = glvec.normalisedLineWidth(canvas)
vertices, indices = glroutines.lineAsPolygon(vertices,
lineWidth,
zax,
......
......@@ -63,14 +63,13 @@ class GLRGBVector(glvector.GLVector):
"""
def __init__(self, image, overlayList, displayCtx, canvas, threedee):
def __init__(self, image, overlayList, displayCtx, threedee):
"""Create a ``GLRGBVector``.
:arg image: An :class:`.Image` or :class:`.DTIFitTensor`
instance.
:arg overlayList: The :class:`.OverlayList`
:arg displayCtx: The :class:`.DisplayContext` managing the scene.
:arg canvas: The canvas doing the drawing.
:arg threedee: 2D or 3D rendering
"""
......@@ -115,7 +114,6 @@ class GLRGBVector(glvector.GLVector):
image,
overlayList,
displayCtx,
canvas,
threedee,
prefilter=prefilter,
prefilterRange=prefilterRange,
......@@ -131,7 +129,6 @@ class GLRGBVector(glvector.GLVector):
self.__unitLengthChanged)
def destroy(self):
"""Must be called when this ``GLRGBVector`` is no longer needed.
Removes some property listeners from the :class:`.RGBVectorOpts`
......@@ -214,15 +211,15 @@ class GLRGBVector(glvector.GLVector):
fslgl.glrgbvector_funcs.preDraw(self)
def draw2D(self, *args, **kwargs):
def draw2D(self, canvas, *args, **kwargs):
"""Overrides :meth:`.GLVector.draw2D`. Calls the OpenGL
version-specific ``draw2D`` function.
"""
with glroutines.enabled((gl.GL_CULL_FACE)):
gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL)
gl.glCullFace(gl.GL_BACK)
gl.glFrontFace(self.frontFace())
fslgl.glrgbvector_funcs.draw2D(self, *args, **kwargs)
gl.glFrontFace(self.frontFace(canvas))
fslgl.glrgbvector_funcs.draw2D(self, canvas, *args, **kwargs)
def draw3D(self, *args, **kwargs):
......
......@@ -29,20 +29,18 @@ class GLRGBVolume(glimageobject.GLImageObject):
"""
def __init__(self, image, overlayList, displayCtx, canvas, threedee):
def __init__(self, image, overlayList, displayCtx, threedee):
"""Create a ``GLRGBVolume``.
:arg image: The :class:`.Image` instance.
:arg overlayList: The :class:`.OverlayList`
:arg displayCtx: The :class:`.DisplayContext` managing the scene.
:arg canvas: The canvas doing the drawing.
:arg threedee: 2D or 3D rendering
"""
glimageobject.GLImageObject.__init__(self,
image,
overlayList,
displayCtx,
canvas,
threedee)
self.shader = None
......@@ -239,21 +237,21 @@ class GLRGBVolume(glimageobject.GLImageObject):
self.imageTexture.bindTexture(gl.GL_TEXTURE0)
def draw2D(self, zpos, axes, xform=None):
def draw2D(self, canvas, *args, **kawrgs):
"""Calls :func:`.glrgbvolume_funcs.draw2D`. """
with glroutines.enabled((gl.GL_CULL_FACE)):
gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL)
gl.glCullFace(gl.GL_BACK)
gl.glFrontFace(self.frontFace())
fslgl.glrgbvolume_funcs.draw2D(self, zpos, axes, xform)
gl.glFrontFace(self.frontFace(canvas))
fslgl.glrgbvolume_funcs.draw2D(self, canvas, *args, **kawrgs)
def drawAll(self, axes, zposes, xforms):
def drawAll(self, *args, **kawrgs):
"""Calls :func:`.glrgbvolume_funcs.drawAll`. """
fslgl.glrgbvolume_funcs.drawAll(self, axes, zposes, xforms)
fslgl.glrgbvolume_funcs.drawAll(self, *args, **kawrgs)
def draw3D(self, xform=None):
def draw3D(self, *args, **kawrgs):
"""Does nothing. """
......
......@@ -53,7 +53,7 @@ class GLTensor(glvector.GLVector):
"""
def __init__(self, image, overlayList, displayCtx, canvas, threedee):
def __init__(self, image, overlayList, displayCtx, threedee):
"""Create a ``GLTensor``. Prepares the eigenvalue and eigenvector
textures, and calls the :func:`.gl21.gltensor_funcs.init` function.
......@@ -64,8 +64,6 @@ class GLTensor(glvector.GLVector):
:arg displayCtx: The :class:`.DisplayContext` managing the scene.
:arg canvas: The canvas doing the drawing.
:arg threedee: 2D or 3D rendering.
"""
......@@ -130,7 +128,6 @@ class GLTensor(glvector.GLVector):
image,
overlayList,
displayCtx,
canvas,
threedee,
prefilter=prefilter,
prefilterRange=prefilterRange,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment