color_object_map.cpp

00001
00002 /***************************************************************************
00003  *  color_object_map.cpp - Mapping between color and roi
00004  *
00005  *  Created: Mon May 16th 2008
00006  *  Copyright  2008 Christof Rath <c.rath@student.tugraz.at>
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023
00024 #include "color_object_map.h"
00025 
00026 /** @class ColorObjectMap color_object_map.h <fvutils/color/color_object_map.h>
00027  * Color mapping class.
00028  * This class defines a mapping between regions of interest and @see color_t
00029  * values. It also provides corresponding @see YUVColor values for a color_t.
00030  *
00031  * @author Christof Rath
00032  */
00033 
00034 /** @var static ColorObjectMap* ColorObjectMap::__singleton
00035  * A singelton instance of ColorObjectMap
00036  */
00037 /** @var std::map<hint_t, color_t> ColorObjectMap::__color_for_hint
00038  * A list of color_t with hint_t (ROI's) as index
00039  */
00040 /** @var std::map<color_t, hint_t> ColorObjectMap::__hint_for_color
00041  * A list of hint_t (ROI's) with color_t as index
00042  */
00043 /** @var color_t ColorObjectMap::__c_other
00044  * The default color
00045  */
00046 /** @var hint_t ColorObjectMap::__h_unknown
00047  * The default hint
00048  */
00049 
00050 /** @fn static const ColorObjectMap& ColorObjectMap::get_instance()
00051  * ColorObjectMap getter.
00052  * @return the one and only instance of ColorObjectMap
00053  */
00054 
00055 /** @fn const color_t& ColorObjectMap::get(hint_t hint) const
00056  * Inline color_t reference getter.
00057  * @param hint the ROI of interest
00058  * @return the matching color_t value
00059  */
00060 
00061 /** @fn const hint_t ColorObjectMap::get(color_t color) const
00062  * Inline hint_t(ROI) reference getter
00063  * @param color value of interest
00064  * @return corresponding ROI
00065  */
00066 
00067 /** Static initialzer */
00068 ColorObjectMap* ColorObjectMap::__singleton = new ColorObjectMap();
00069 
00070 /** Default Contructor.
00071  * The constructor is private to implement a singelton pattern
00072  */
00073 ColorObjectMap::ColorObjectMap()
00074 {
00075   __c_other   = C_OTHER;
00076   __h_unknown = H_UNKNOWN;
00077
00078   //Standard mapping:
00079   set_mapping(H_BALL, C_ORANGE);
00080   set_mapping(H_ROBOT, C_BLACK);
00081   set_mapping(H_ROBOT_OPP, C_RED);
00082   set_mapping(H_FIELD, C_GREEN);
00083   set_mapping(H_GOAL_YELLOW, C_YELLOW);
00084   set_mapping(H_GOAL_BLUE, C_CYAN);
00085   set_mapping(H_LINE, C_WHITE);
00086   set_mapping(H_BACKGROUND, C_BACKGROUND);
00087 }
00088 
00089 /** Destructor */
00090 ColorObjectMap::~ColorObjectMap()
00091 {
00092 }
00093 
00094 /** YUV_t getter.
00095  * @param color a color_t value (@see color_t enumeration)
00096  * @return a corresponding YUV color
00097  */
00098 YUV_t ColorObjectMap::get_color(color_t color)
00099 {
00100   switch (color) {
00101     case C_ORANGE:
00102       return YUV_t::orange();
00103
00104     case C_MAGENTA:
00105       return YUV_t::magenta();
00106
00107     case C_CYAN:
00108       return YUV_t::cyan();
00109
00110     case C_BLUE:
00111       return YUV_t::blue();
00112
00113     case C_YELLOW:
00114       return YUV_t::yellow();
00115
00116     case C_GREEN:
00117       return YUV_t::green();
00118
00119     case C_WHITE:
00120       return YUV_t::white();
00121
00122     case C_RED:
00123       return YUV_t::red();
00124
00125     case C_BLACK:
00126       return YUV_t::black();
00127
00128     default: //also C_BACKGROUND
00129       return YUV_t::gray();
00130   }
00131 }
00132 
00133 /** Mapping setter.
00134  * Sets the mapping between ROI and color_t values
00135  * @param roi region of interest (@see hint_t enumeration)
00136  * @param color matching color_t value (@see color_t enumeration)
00137  */
00138 void ColorObjectMap::set_mapping(hint_t roi, color_t color)
00139 {
00140   hint_t cur_roi = get(color);
00141   if (cur_roi != H_UNKNOWN) //There is a previous mapping -> unlink it
00142   {
00143     color_t cur_col = get(roi);
00144     __color_for_hint[cur_roi] = C_OTHER;
00145     __hint_for_color[cur_col] = H_UNKNOWN;
00146   }
00147
00148   __color_for_hint[roi] = color;
00149   __hint_for_color[color] = roi;
00150 }