[visionegg] Arrow and fixation point stimulus

Hi all,

I needed an arrow and also a fixation point stimulus in Vision
Egg. Please look at the attached files and let me know what you
think about it. The fixation point stimulus is realized thru a
circle and not thru a rectangle as in the FixationSpot class.
Maybe both stimuli classes fits into the Core.py file in the
VisionEgg library.

Best

        Hubertus


================================================================
                            Arrow.py
================================================================

# The Vision Egg: Arrow
#
# Author(s): Hubertus Becker <hubertus.becker@xxxxxxxxxxxxxxxx>
# Copyright: (C) 2005 by Hertie Institute for Clinical Brain Research,
#            Department of Cognitive Neurology, University of Tuebingen
# URL:       http://www.hubertus-becker.de/resources/visionegg/
#
# This library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.
#
# $Revision: 1.4 $  $Date: 2005/04/26 06:42:32 $

"""
Arrow stimulus.

"""

####################################################################
#
#        Import all the necessary packages
#
####################################################################

try:
    import logging
except ImportError:
    import VisionEgg.py_logging as logging

import VisionEgg
import VisionEgg.Core
import VisionEgg.ParameterTypes as ve_types

import Numeric, RandomArray
import math, types, string

import VisionEgg.GL as gl # get all OpenGL stuff in one namespace

__version__ = VisionEgg.release_name
__cvs__ = '$Revision: 1.4 $'.split()[1]
__date__ = ' '.join('$Date: 2005/04/26 06:42:32 $'.split()[1:3])
__author__ = 'Hubertus Becker <hubertus.becker@xxxxxxxxxxxxxxxx>'

# Use Python's bool constants if available, make aliases if not
try:
    True
except NameError:
    True = 1==1
    False = 1==0

class Arrow(VisionEgg.Core.Stimulus):
    """Arrow stimulus.

    Parameters
    ==========
    anchor        -- (String)
                     Default: center
    anti_aliasing -- (Boolean)
                     Default: True
    center        -- (Sequence2 of Real)
                     Default: (determined at instantiation)
    color         -- (AnyOf(Sequence3 of Real or Sequence4 of Real))
                     Default: (1.0, 1.0, 1.0)
    on            -- (Boolean)
                     Default: True
    orientation   -- (Real)
                     Default: 0.0
    position      -- units: eye coordinates (AnyOf(Sequence2 of Real or 
Sequence3 of Real or Sequence4 of Real))
                     Default: (320.0, 240.0)
    size          -- (Sequence2 of Real)
                     Default: (64.0, 16.0)
    """

    parameters_and_defaults = {
        'on':(True,
              ve_types.Boolean),
        'color':((1.0,1.0,1.0),
                 ve_types.AnyOf(ve_types.Sequence3(ve_types.Real),
                                ve_types.Sequence4(ve_types.Real))),
        'anti_aliasing':(True,
                         ve_types.Boolean),
        'orientation':(0.0, # 0.0 degrees = right, 90.0 degrees = up
                       ve_types.Real),
        'position' : ( ( 320.0, 240.0 ), # In eye coordinates
                       ve_types.AnyOf(ve_types.Sequence2(ve_types.Real),
                                      ve_types.Sequence3(ve_types.Real),
                                      ve_types.Sequence4(ve_types.Real)),
                       "units: eye coordinates"),
        'anchor' : ('center',
                    ve_types.String),
        'size':((64.0,16.0), # In eye coordinates
                ve_types.Sequence2(ve_types.Real)),
        'center' : (None,  # DEPRECATED -- don't use
                    ve_types.Sequence2(ve_types.Real)),  
        }

    __slots__ = VisionEgg.Core.Stimulus.__slots__ + (
        '_gave_alpha_warning',
        )

    def __init__(self,**kw):
        VisionEgg.Core.Stimulus.__init__(self,**kw)
        self._gave_alpha_warning = 0

    def draw(self):
        p = self.parameters # Shorthand
        if p.center is not None:
            if not hasattr(VisionEgg.config,"_GAVE_CENTER_DEPRECATION"):
                logger = logging.getLogger('VisionEgg.Arrow')
                logger.warning("Specifying Arrow by deprecated "
                               "'center' parameter deprecated. Use "
                               "'position' parameter instead. (Allows "
                               "use of 'anchor' parameter to set to "
                               "other values.)")
                VisionEgg.config._GAVE_CENTER_DEPRECATION = 1
            p.anchor = 'center'
            p.position = p.center[0], p.center[1] # Copy values (don't copy 
ref to tuple)
        if p.on:
            # Calculate center
            center = VisionEgg._get_center(p.position,p.anchor,p.size)
            gl.glMatrixMode(gl.GL_MODELVIEW)
            gl.glLoadIdentity()
            gl.glTranslate(center[0],center[1],0.0)
            gl.glRotate(-p.orientation,0.0,0.0,1.0)

            gl.glColorf(*p.color)
            gl.glDisable(gl.GL_DEPTH_TEST)
            gl.glDisable(gl.GL_TEXTURE_2D)
            gl.glBlendFunc(gl.GL_SRC_ALPHA,gl.GL_ONE_MINUS_SRC_ALPHA)
            gl.glEnable(gl.GL_BLEND)

            w = p.size[0]/2.0
            h = p.size[1]/2.0

            gl.glBegin(gl.GL_QUADS) # Draw Rectangle
            gl.glVertex3f( 0.25*w, h, 0.0);
            gl.glVertex3f(-w, h, 0.0);
            gl.glVertex3f(-w,-h, 0.0);
            gl.glVertex3f( 0.25*w, -h, 0.0);
            gl.glEnd() # GL_QUADS

            gl.glBegin(gl.GL_TRIANGLES) # Draw Triangle
            gl.glVertex3f( 1.00*w, 0.0*h, 0.0); # Top
            gl.glVertex3f( 0.25*w,-3.0*h, 0.0);
            gl.glVertex3f( 0.25*w, 3.0*h, 0.0);
            gl.glEnd() # GL_QUADS

            if p.anti_aliasing:
                if not self._gave_alpha_warning:
                    if len(p.color) > 3 and p.color[3] != 1.0:
                        logger = logging.getLogger('VisionEgg.Arrow')
                        logger.warning("The parameter anti_aliasing is "
                                       "set to true in the Arrow "
                                       "stimulus class, but the color "
                                       "parameter specifies an alpha "
                                       "value other than 1.0.  To "
                                       "acheive anti-aliasing, ensure "
                                       "that the alpha value for the "
                                       "color parameter is 1.0.")
                        self._gave_alpha_warning = 1

                # We've already drawn a filled polygon (aliased), now redraw
                # the outline of the polygon (with anti-aliasing). (Using
                # GL_POLYGON_SMOOTH results in artifactual lines where
                # triangles were joined to create quad, at least on some 
OpenGL
                # implementations.)

                # Calculate coverage value for each pixel of outline
                # and store as alpha
                gl.glEnable(gl.GL_LINE_SMOOTH)
                # Now specify how to use the alpha value
                gl.glBlendFunc(gl.GL_SRC_ALPHA,gl.GL_ONE_MINUS_SRC_ALPHA)
                gl.glEnable(gl.GL_BLEND)

                # Draw a second polygon in line mode, so the edges are 
anti-aliased
                gl.glPolygonMode(gl.GL_FRONT_AND_BACK,gl.GL_LINE)
                gl.glBegin(gl.GL_QUADS)

                gl.glVertex3f( 0.25*w, h, 0.0); # Draw Rectangle
                gl.glVertex3f(-w, h, 0.0);
                gl.glVertex3f(-w,-h, 0.0);
                gl.glVertex3f( 0.25*w, -h, 0.0);
                gl.glVertex3f( 1.00*w, 0.0*h, 0.0); # Draw Triangle
                gl.glVertex3f( 0.25*w,-3.0*h, 0.0);
                gl.glVertex3f( 0.25*w, 3.0*h, 0.0);
                gl.glEnd() # GL_QUADS

                # Set the polygon mode back to fill mode
                gl.glPolygonMode(gl.GL_FRONT_AND_BACK,gl.GL_FILL)
                gl.glDisable(gl.GL_LINE_SMOOTH)


================================================================
                        FixationPoint.py
================================================================

# The Vision Egg: FixationPoint
#
# Author(s): Hubertus Becker <hubertus.becker@xxxxxxxxxxxxxxxx>
# Copyright: (C) 2005 by Hertie Institute for Clinical Brain Research,
#            Department of Cognitive Neurology, University of Tuebingen
# URL:       http://www.hubertus-becker.de/resources/visionegg/
#
# This library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.
#
# $Revision: 1.6 $  $Date: 2005/04/26 06:42:32 $


"""
Fixation point stimulus.

"""

####################################################################
#
#        Import all the necessary packages
#
####################################################################

try:
    import logging
except ImportError:
    import VisionEgg.py_logging as logging

import VisionEgg
import VisionEgg.Core
import VisionEgg.ParameterTypes as ve_types

import math, types, string

import VisionEgg.GL as gl # get all OpenGL stuff in one namespace

__version__ = VisionEgg.release_name
__cvs__ = '$Revision: 1.6 $'.split()[1]
__date__ = ' '.join('$Date: 2005/04/26 06:42:32 $'.split()[1:3])
__author__ = 'Hubertus Becker <hubertus.becker@xxxxxxxxxxxxxxxx>'

# Use Python's bool constants if available, make aliases if not
try:
    True
except NameError:
    True = 1==1
    False = 1==0


class FixationPoint(VisionEgg.Core.Stimulus):
    """A circular stimulus, typically used as a fixation point.

    Parameters
    ==========
    anchor   -- how position parameter is used (String)
                Default: center
    color    -- color (AnyOf(Sequence3 of Real or Sequence4 of Real))
                Default: (1.0, 1.0, 1.0)
    on       -- draw? (Boolean)
                Default: True
    position -- position in eye coordinates (AnyOf(Sequence2 of Real or 
Sequence3 of Real or Sequence4 of Real))
                Default: (320.0, 240.0)
    radius   -- size in eye coordinates (Sequence2 of Real)
                Default: (2.0)
    """

    parameters_and_defaults = VisionEgg.ParameterDefinition({
        'on':(True,
              ve_types.Boolean,
              'draw?'),
        'color':((1.0,1.0,1.0),
                 ve_types.AnyOf(ve_types.Sequence3(ve_types.Real),
                                ve_types.Sequence4(ve_types.Real)),
                 'color'),
        'position' : ( ( 320.0, 240.0 ), # in eye coordinates
                       ve_types.AnyOf(ve_types.Sequence2(ve_types.Real),
                                      ve_types.Sequence3(ve_types.Real),
                                      ve_types.Sequence4(ve_types.Real)),
                       'position in eye coordinates'),
        'anchor' : ('center',
                    ve_types.String,
                    'how position parameter is used'),
        'radius':((2.0),
                ve_types.Real,
                'radius in eye coordinates'),
        'center' : (None,  # DEPRECATED -- don't use
                    ve_types.Sequence2(ve_types.Real),
                    'position in eye coordinates',
                    VisionEgg.ParameterDefinition.DEPRECATED),
        })

    def __init__(self,**kw):
        VisionEgg.Core.Stimulus.__init__(self,**kw)

    def draw(self):
        p = self.parameters # shorthand
        if p.center is not None:
            if not hasattr(VisionEgg.config,"_GAVE_CENTER_DEPRECATION"):
                logger = logging.getLogger('VisionEgg.Core')
                logger.warning("Specifying FixationSpot by deprecated "
                               "'center' parameter deprecated.  Use "
                               "'position' parameter instead.  (Allows "
                               "use of 'anchor' parameter to set to "
                               "other values.)")
                VisionEgg.config._GAVE_CENTER_DEPRECATION = 1
            p.anchor = 'center'
            p.position = p.center[0], p.center[1] # copy values (don't copy 
ref to tuple)

        if p.on:
            # calculate center
            center = VisionEgg._get_center(p.position,p.anchor,(p.radius, 
p.radius))
            gl.glDisable(gl.GL_DEPTH_TEST)
            gl.glDisable(gl.GL_TEXTURE_2D)
            gl.glDisable(gl.GL_BLEND)

            gl.glMatrixMode(gl.GL_MODELVIEW)
            gl.glLoadIdentity()

            gl.glColorf(*p.color)

            # Build filled circle from points
#           gl.glBegin(gl.GL_POINTS);
#           radius = int(math.ceil(p.radius))
#           for i in range(-radius, radius):
#               for j in range(-radius, radius):
#                   if(i * i + j * j < radius * radius):
#                       gl.glVertex3f(p.position[0] + i, p.position[1] + j, 
0.0)
#           gl.glEnd(); # GL_POINTS

            # Build filled circle from 51 triangles (this is typically faster
            # then the commended code above with the points)
            gl.glBegin(gl.GL_TRIANGLE_FAN);
            gl.glVertex3f(p.position[0], p.position[1], 0.0);
            angle = 0.0;
            for i in range(51):
                angle = angle + (math.pi / 24.0);
                gl.glVertex3f(p.position[0] + p.radius * math.cos(angle),
                              p.position[1] + p.radius * math.sin(angle), 
0.0);
            gl.glEnd(); # GL_TRIANGLE_FAN


-- 
Hubertus Becker, Hertie Institute for Clinical Brain Research
Department of Cognitive Neurology, Visual Perception Laboratory
University of Tuebingen, Hoppe-Seyler-Str. 3, D-72076 Tuebingen
Homepage: http://www.hubertus-becker.de (GnuPG-Key available)

Other related posts: