LoLaserWindowRings.C

Go to the documentation of this file.
00001 /**
00002    \file Robots/LoBot/ui/LoLaserWindowRings.C
00003 
00004    \brief This file defines the non-inline member functions of the
00005    lobot::LaserWindowRings class, which is used to draw contours
00006    indicating distance from the laser range finder.
00007 */
00008 
00009 // //////////////////////////////////////////////////////////////////// //
00010 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00011 // by the University of Southern California (USC) and the iLab at USC.  //
00012 // See http://iLab.usc.edu for information about this project.          //
00013 // //////////////////////////////////////////////////////////////////// //
00014 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00015 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00016 // in Visual Environments, and Applications'' by Christof Koch and      //
00017 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00018 // pending; application number 09/912,225 filed July 23, 2001; see      //
00019 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00020 // //////////////////////////////////////////////////////////////////// //
00021 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00022 //                                                                      //
00023 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00024 // redistribute it and/or modify it under the terms of the GNU General  //
00025 // Public License as published by the Free Software Foundation; either  //
00026 // version 2 of the License, or (at your option) any later version.     //
00027 //                                                                      //
00028 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00029 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00030 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00031 // PURPOSE.  See the GNU General Public License for more details.       //
00032 //                                                                      //
00033 // You should have received a copy of the GNU General Public License    //
00034 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00035 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00036 // Boston, MA 02111-1307 USA.                                           //
00037 // //////////////////////////////////////////////////////////////////// //
00038 //
00039 // Primary maintainer for this file: mviswana usc edu
00040 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/LoBot/ui/LoLaserWindowRings.C $
00041 // $Id: LoLaserWindowRings.C 13037 2010-03-23 01:00:53Z mviswana $
00042 //
00043 
00044 //---------------------- ALTERNATIVE DEFINITION -------------------------
00045 
00046 // In case OpenGL is missing...
00047 #ifndef INVT_HAVE_LIBGL
00048 
00049 #include "Robots/LoBot/ui/LoLaserWindowRings.H"
00050 #include "Robots/LoBot/misc/LoExcept.H"
00051 
00052 namespace lobot {
00053 
00054 LaserWindowRings::my_factory LaserWindowRings::register_me("rings") ;
00055 
00056 LaserWindowRings::LaserWindowRings()
00057    : base(), m_display_list(0)
00058 {
00059    throw missing_libs(MISSING_OPENGL) ;
00060 }
00061 
00062 // Empty API
00063 void LaserWindowRings::render() const {}
00064 
00065 LaserWindowRings::~LaserWindowRings(){}
00066 
00067 }
00068 
00069 #else // OpenGL available ==> the real McCoy
00070 
00071 //------------------------------ HEADERS --------------------------------
00072 
00073 // lobot headers
00074 #include "Robots/LoBot/ui/LoLaserWindowRings.H"
00075 #include "Robots/LoBot/util/LoMath.H"
00076 
00077 // OpenGL headers
00078 #include <GL/gl.h>
00079 
00080 //----------------------------- NAMESPACE -------------------------------
00081 
00082 namespace lobot {
00083 
00084 //-------------------------- INITIALIZATION -----------------------------
00085 
00086 // Factory registration
00087 LaserWindowRings::my_factory LaserWindowRings::register_me("rings") ;
00088 
00089 LaserWindowRings::LaserWindowRings()
00090    : base(), m_display_list(0)
00091 {}
00092 
00093 //------------------------- DRAWING THE RINGS ---------------------------
00094 
00095 // This function object draws the concentric circular distance contours
00096 // corresponding to a given marking interval, taking care to only draw
00097 // the contours when they are actually active (as determined by the
00098 // current zoom level and the marking's zoom range).
00099 class draw_contours {
00100    float zoom_level, max ;
00101    int   display_list ;
00102 public:
00103    draw_contours(float zoom_level, float max, int display_list) ;
00104    void operator()(const LaserWindowMarkings::Marking&) const ;
00105 } ;
00106 
00107 draw_contours::draw_contours(float Z, float M, int L)
00108    : zoom_level(Z), max(M), display_list(L)
00109 {}
00110 
00111 void draw_contours::operator()(const LaserWindowRings::Marking& M) const
00112 {
00113    if (! M.third.in(zoom_level)) // current zoom level out of marking's
00114       return ;                   // zoom range
00115 
00116    glColor3fv(M.second.rgb()) ;
00117    glPushMatrix() ;
00118    for (float radius = M.first; radius <= max; radius += M.first) {
00119       glLoadIdentity() ;
00120       glScalef(radius, radius, 1) ;
00121       glCallList(display_list) ;
00122    }
00123    glPopMatrix() ;
00124 }
00125 
00126 // Draw the contours using the marking specifications
00127 void LaserWindowRings::render() const
00128 {
00129    if (! m_canvas)
00130       return ;
00131 
00132    // For the rings, we use an OpenGL display list that draws a unit
00133    // circle. To draw bigger circles, we apply a scaling transformation.
00134    //
00135    // DEVNOTE: It'd be nice to create the display list in the constructor
00136    // and then not bother performing this check each and every time.
00137    // Unfortunately, display lists are associated with the GL rendering
00138    // contexts for which they're created. And GL rendering contexts are
00139    // associated with the particular thread that created them.
00140    //
00141    // Putting this display list creation in the constructor will end up
00142    // associating it with the Robolocust main thread because that's the
00143    // beast that creates the laser visualizer object that creates and
00144    // uses this markings object. Since this application is setup so that
00145    // only the UI/visualization thread makes GL related rendering calls,
00146    // the display list, being associated with the main thread, won't work
00147    // in the context of the UI thread, which is why we need to create the
00148    // display list in the render function here, thus, ensuring that it is
00149    // created within the correct GL and thread contexts.
00150    if (! m_display_list)
00151    {
00152       m_display_list = glGenLists(1) ;
00153       glNewList(m_display_list, GL_COMPILE) ;
00154       glBegin(GL_LINE_LOOP) ;
00155          for (float angle = 0; angle < 360; ++angle)
00156             glVertex2f(cos(angle), sin(angle)) ;
00157       glEnd() ;
00158       glEndList() ;
00159    }
00160 
00161    const float Z = m_canvas->zoom_level() ;
00162    glPushAttrib(GL_COLOR_BUFFER_BIT) ;
00163       std::for_each(m_markings.begin(), m_markings.end(),
00164                     draw_contours(Z, m_max, m_display_list)) ;
00165       base::draw_main_axes() ;
00166    glPopAttrib() ;
00167 }
00168 
00169 //----------------------------- CLEAN-UP --------------------------------
00170 
00171 LaserWindowRings::~LaserWindowRings()
00172 {
00173    glDeleteLists(m_display_list, 1) ;
00174 }
00175 
00176 //-----------------------------------------------------------------------
00177 
00178 } // end of namespace encapsulating this file's definitions
00179 
00180 #endif // INVT_HAVE_LIBGL
00181 
00182 /* So things look consistent in everyone's emacs... */
00183 /* Local Variables: */
00184 /* indent-tabs-mode: nil */
00185 /* End: */
Generated on Sun May 8 08:41:31 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3