LoLaserWindowMarkings.C

Go to the documentation of this file.
00001 /**
00002    \file Robots/LoBot/ui/LoLaserWindowMarkings.C
00003 
00004    \brief This file defines the non-inline member functions of the
00005    lobot::LaserWindowMarkings class, which is used to draw markers at
00006    regular intervals to help indicate distance from the laser range
00007    finder.
00008 */
00009 
00010 // //////////////////////////////////////////////////////////////////// //
00011 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00012 // by the University of Southern California (USC) and the iLab at USC.  //
00013 // See http://iLab.usc.edu for information about this project.          //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00016 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00017 // in Visual Environments, and Applications'' by Christof Koch and      //
00018 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00019 // pending; application number 09/912,225 filed July 23, 2001; see      //
00020 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00021 // //////////////////////////////////////////////////////////////////// //
00022 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00023 //                                                                      //
00024 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00025 // redistribute it and/or modify it under the terms of the GNU General  //
00026 // Public License as published by the Free Software Foundation; either  //
00027 // version 2 of the License, or (at your option) any later version.     //
00028 //                                                                      //
00029 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00030 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00031 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00032 // PURPOSE.  See the GNU General Public License for more details.       //
00033 //                                                                      //
00034 // You should have received a copy of the GNU General Public License    //
00035 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00036 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00037 // Boston, MA 02111-1307 USA.                                           //
00038 // //////////////////////////////////////////////////////////////////// //
00039 //
00040 // Primary maintainer for this file: mviswana usc edu
00041 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/LoBot/ui/LoLaserWindowMarkings.C $
00042 // $Id: LoLaserWindowMarkings.C 13905 2010-09-09 21:38:44Z mviswana $
00043 //
00044 
00045 //---------------------- ALTERNATIVE DEFINITION -------------------------
00046 
00047 // In case OpenGL is missing...
00048 #ifndef INVT_HAVE_LIBGL
00049 
00050 #include "Robots/LoBot/ui/LoLaserWindowMarkings.H"
00051 #include "Robots/LoBot/misc/LoExcept.H"
00052 
00053 namespace lobot {
00054 
00055 LaserWindowMarkings::LaserWindowMarkings()
00056 {
00057    throw missing_libs(MISSING_OPENGL) ;
00058 }
00059 
00060 LaserWindowMarkings::~LaserWindowMarkings(){}
00061 
00062 // Empty API
00063 void LaserWindowMarkings::draw_main_axes() const {}
00064 
00065 }
00066 
00067 #else // OpenGL available ==> the real McCoy
00068 
00069 //------------------------------ HEADERS --------------------------------
00070 
00071 // DEVNOTE: The different types of distance markings are created with an
00072 // object factory. Usually, we would register all the subfactories
00073 // together in a separate registry module. However, for the laser range
00074 // finder test program, we don't go to the trouble of doing all that as
00075 // there aren't very many such factory-created objects.
00076 //
00077 // Instead, we put each class's subfactory into its own translation unit.
00078 // That is, some markings type implemented in foo.H and foo.C will have
00079 // its subfactory registration static data member defined in foo.C
00080 // instead of some central registry.C.
00081 //
00082 // Since the INVT build system automatically deduces the dependencies for
00083 // each target, we need to ensure that this module's dependencies list
00084 // includes foo.C. Otherwise, when the program is run and the user
00085 // chooses to use the foo markings type, it will crash because the linker
00086 // would not have linked with foo.o/foo.so.
00087 //
00088 // If all the marking types were in a registry.o/so, we could have
00089 // included registry.H where it was needed and ensured proper linkage.
00090 // However, since we decided not go with a central registry in this case,
00091 // we need to force/trick INVT's dependency calculator into including the
00092 // foo and all other marking modules into this module's dependency list.
00093 // That way, the modules depending on this abstract marker class will
00094 // automatically get linked with the modules containing the subclasses
00095 // and corresponding subfactories.
00096 //
00097 // We force the above-mentioned linkage by simply including all the
00098 // headers for the different marking types over here. It's a little ugly
00099 // w.r.t. how a polymorphic factory should work (i.e., the base class
00100 // shouldn't know about its subclasses), but it gets the job done by
00101 // getting the dependencies calculator to perform the necessary magic.
00102 #include "Robots/LoBot/ui/LoLaserWindowGrid.H"
00103 #include "Robots/LoBot/ui/LoLaserWindowRings.H"
00104 
00105 // lobot headers
00106 #include "Robots/LoBot/ui/LoLaserWindowMarkings.H"
00107 #include "Robots/LoBot/config/LoConfigHelpers.H"
00108 #include "Robots/LoBot/misc/LoTypes.H"
00109 #include "Robots/LoBot/util/LoString.H"
00110 
00111 // OpenGL headers
00112 #include <GL/gl.h>
00113 
00114 // Standard C++ headers
00115 #include <algorithm>
00116 #include <vector>
00117 
00118 //----------------------------- NAMESPACE -------------------------------
00119 
00120 namespace lobot {
00121 
00122 //-------------------------- INITIALIZATION -----------------------------
00123 
00124 // This function returns a triple consisting of the given interval, its
00125 // corresponding color and zoom range.
00126 static LaserWindowMarkings::Marking make_marking(const float& interval)
00127 {
00128    std::string interval_str = to_string(interval) ;
00129 
00130    // Retrieve color setting for interval (e.g., color_500 for interval 500)
00131    PixelType C(get_conf<PixelType>("markings",
00132                                    std::string("color_") + interval_str,
00133                                    PixelType(32, 32, 32))) ;
00134    GLColor color(C.red(), C.green(), C.blue()) ;
00135 
00136    // Retrieve zoom setting for interval (e.g., zoom_500 for interval 500)
00137    std::vector<float> zoom = string_to_vector<float>(
00138       get_conf<std::string>("markings",
00139                             std::string("zoom_") + interval_str,
00140                             "0.05 100")) ;
00141    LaserWindowMarkings::ZoomRange zoom_range(zoom[0], zoom[1]) ;
00142 
00143    // Package the interval and corresponding settings together
00144    return make_triple(interval, color, zoom_range) ;
00145 }
00146 
00147 bool compare_markings(const LaserWindowMarkings::Marking& a,
00148                       const LaserWindowMarkings::Marking& b)
00149 {
00150    return a.first < b.first ;
00151 }
00152 
00153 // On instantiation, we retrieve the intervals specified in the markings
00154 // section of the config file and create marking specs corresponding to
00155 // each of these intervals.
00156 LaserWindowMarkings::LaserWindowMarkings()
00157    : m_canvas(0), m_max(0)
00158 {
00159    std::vector<float> intervals = string_to_vector<float>(
00160       get_conf<std::string>("markings", "intervals", "100 500 1000 5000")) ;
00161 
00162    m_markings.reserve(intervals.size()) ;
00163    std::transform(intervals.begin(), intervals.end(),
00164                   std::back_inserter(m_markings), make_marking) ;
00165 
00166    std::sort(m_markings.begin(), m_markings.end(), compare_markings) ;
00167 }
00168 
00169 //----------------------------- CLEAN-UP --------------------------------
00170 
00171 LaserWindowMarkings::~LaserWindowMarkings()
00172 {}
00173 
00174 //------------------------------ HELPERS --------------------------------
00175 
00176 void LaserWindowMarkings::draw_main_axes() const
00177 {
00178    if (Params::main_axes_disabled())
00179       return ;
00180 
00181    glColor3fv(Params::main_axes_color().rgb()) ;
00182    glBegin(GL_LINES) ;
00183       glVertex2f(-m_max, 0) ;
00184       glVertex2f( m_max, 0) ;
00185       glVertex2f(0, -m_max) ;
00186       glVertex2f(0,  m_max) ;
00187    glEnd() ;
00188 }
00189 
00190 //-------------------------- KNOB TWIDDLING -----------------------------
00191 
00192 // Parameters initialization
00193 LaserWindowMarkings::Params::Params()
00194 {
00195    PixelType c(get_conf("markings", "main_axes_color", PixelType())) ;
00196    if (c.red() == 0 && c.green() == 0 && c.blue() == 0)
00197       m_draw_main_axes = false ;
00198    else
00199    {
00200       m_draw_main_axes = true ;
00201       m_main_axes_color = GLColor(c.red(), c.green(), c.blue()) ;
00202    }
00203 }
00204 
00205 // Parameters clean-up
00206 LaserWindowMarkings::Params::~Params(){}
00207 
00208 // Parameters access
00209 bool LaserWindowMarkings::Params::main_axes_enabled()
00210 {
00211    return instance().m_draw_main_axes ;
00212 }
00213 
00214 const GLColor& LaserWindowMarkings::Params::main_axes_color()
00215 {
00216    return instance().m_main_axes_color ;
00217 }
00218 
00219 //-----------------------------------------------------------------------
00220 
00221 } // end of namespace encapsulating this file's definitions
00222 
00223 #endif // INVT_HAVE_LIBGL
00224 
00225 /* So things look consistent in everyone's emacs... */
00226 /* Local Variables: */
00227 /* indent-tabs-mode: nil */
00228 /* End: */
Generated on Sun May 8 08:41:31 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3