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