SUMO - Simulation of Urban MObility

GUIColorScheme.h

Go to the documentation of this file.
00001 /****************************************************************************/
00007 //
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 #ifndef GUIColorScheme_h
00020 #define GUIColorScheme_h
00021 
00022 
00023 // ===========================================================================
00024 // included modules
00025 // ===========================================================================
00026 #ifdef _MSC_VER
00027 #include <windows_config.h>
00028 #else
00029 #include <config.h>
00030 #endif
00031 
00032 #include <cassert>
00033 #include <vector>
00034 #include <utils/common/RGBColor.h>
00035 #include <utils/iodevices/OutputDevice.h>
00036 
00037 
00038 // ===========================================================================
00039 // class definitions
00040 // ===========================================================================
00045 class GUIColorScheme {
00046 public:
00048     GUIColorScheme(const std::string& name, const RGBColor& baseColor,
00049                    const std::string& colName="", const bool isFixed=false)
00050         : myName(name), myIsInterpolated(!isFixed), myIsFixed(isFixed) {
00051         addColor(baseColor, 0, colName);
00052     }
00053 
00054     void setThreshold(const size_t pos, const SUMOReal threshold) {
00055         myThresholds[pos] = threshold;
00056     }
00057 
00058     void setColor(const size_t pos, const RGBColor& color) {
00059         myColors[pos] = color;
00060     }
00061 
00062     bool setColor(const std::string& name, const RGBColor& color) {
00063         std::vector<std::string>::iterator nameIt = myNames.begin();
00064         std::vector<RGBColor>::iterator colIt = myColors.begin();
00065         for (; nameIt != myNames.end(); ++nameIt, ++colIt) {
00066             if (*nameIt == name) {
00067                 (*colIt) = color;
00068                 return true;
00069             }
00070         }
00071         return false;
00072     }
00073 
00074     unsigned int addColor(const RGBColor& color, const SUMOReal threshold, const std::string& name="") {
00075         std::vector<RGBColor>::iterator colIt = myColors.begin();
00076         std::vector<SUMOReal>::iterator threshIt = myThresholds.begin();
00077         std::vector<std::string>::iterator nameIt = myNames.begin();
00078         unsigned int pos = 0;
00079         while (threshIt != myThresholds.end() && (*threshIt) < threshold) {
00080             ++threshIt;
00081             ++colIt;
00082             ++nameIt;
00083             pos++;
00084         }
00085         myColors.insert(colIt, color);
00086         myThresholds.insert(threshIt, threshold);
00087         myNames.insert(nameIt, name);
00088         return pos;
00089     }
00090 
00091     void removeColor(const size_t pos) {
00092         assert(pos < myColors.size());
00093         myColors.erase(myColors.begin()+pos);
00094         myThresholds.erase(myThresholds.begin()+pos);
00095         myNames.erase(myNames.begin()+pos);
00096     }
00097 
00098     void clear() {
00099         myColors.clear();
00100         myThresholds.clear();
00101         myNames.clear();
00102     }
00103 
00104     const RGBColor getColor(const SUMOReal value) const {
00105         if (myColors.size() == 1 || value < myThresholds.front()) {
00106             return myColors.front();
00107         }
00108         std::vector<RGBColor>::const_iterator colIt = myColors.begin()+1;
00109         std::vector<SUMOReal>::const_iterator threshIt = myThresholds.begin()+1;
00110         while (threshIt != myThresholds.end() && (*threshIt) <= value) {
00111             ++threshIt;
00112             ++colIt;
00113         }
00114         if (threshIt == myThresholds.end()) {
00115             return myColors.back();
00116         }
00117         if (!myIsInterpolated) {
00118             return *(colIt-1);
00119         }
00120         SUMOReal lowVal = *(threshIt-1);
00121         return RGBColor::interpolate(*(colIt-1), *colIt, (value-lowVal)/((*threshIt)-lowVal));
00122     }
00123 
00124     void setInterpolated(const bool interpolate, SUMOReal interpolationStart=0.f) {
00125         myIsInterpolated = interpolate;
00126         if (interpolate) {
00127             myThresholds[0] = interpolationStart;
00128         }
00129     }
00130 
00131     const std::string& getName() const {
00132         return myName;
00133     }
00134 
00135     const std::vector<RGBColor> &getColors() const {
00136         return myColors;
00137     }
00138 
00139     const std::vector<SUMOReal> &getThresholds() const {
00140         return myThresholds;
00141     }
00142 
00143     bool isInterpolated() const {
00144         return myIsInterpolated;
00145     }
00146 
00147     const std::vector<std::string> &getNames() const {
00148         return myNames;
00149     }
00150 
00151     bool isFixed() const {
00152         return myIsFixed;
00153     }
00154 
00155     bool allowsNegativeValues() const {
00156         return myAllowNegativeValues;
00157     }
00158 
00159     void setAllowsNegativeValues(bool value) {
00160         myAllowNegativeValues = value;
00161     }
00162 
00163     void save(OutputDevice& dev) const {
00164         dev << "            <colorScheme name=\"" << myName;
00165         if (!myIsFixed) {
00166             dev << "\" interpolated=\"" << myIsInterpolated;
00167         }
00168         dev << "\">\n";
00169         std::vector<RGBColor>::const_iterator colIt = myColors.begin();
00170         std::vector<SUMOReal>::const_iterator threshIt = myThresholds.begin();
00171         std::vector<std::string>::const_iterator nameIt = myNames.begin();
00172         while (threshIt != myThresholds.end()) {
00173             dev << "                <entry color=\"" << (*colIt);
00174             if (!myIsFixed) {
00175                 dev << "\" threshold=\"" << (*threshIt);
00176             }
00177             if ((*nameIt) != "") {
00178                 dev << "\" name=\"" << (*nameIt);
00179             }
00180             dev << "\"/>\n";
00181             ++threshIt;
00182             ++colIt;
00183             ++nameIt;
00184         }
00185         dev << "            </colorScheme>\n";
00186     }
00187 
00188     bool operator==(const GUIColorScheme& c) const {
00189         return myName == c.myName && myColors == c.myColors && myThresholds == c.myThresholds && myIsInterpolated == c.myIsInterpolated;
00190     }
00191 
00192 private:
00193     std::string myName;
00194     std::vector<RGBColor> myColors;
00195     std::vector<SUMOReal> myThresholds;
00196     bool myIsInterpolated;
00197     std::vector<std::string> myNames;
00198     bool myIsFixed;
00199     bool myAllowNegativeValues;
00200 
00201 };
00202 
00203 #endif
00204 
00205 /****************************************************************************/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines