SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GUIColorScheme.h
Go to the documentation of this file.
1 /****************************************************************************/
8 //
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 #ifndef GUIPropertyScheme_h
22 #define GUIPropertyScheme_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <cassert>
35 #include <vector>
36 #include <utils/common/RGBColor.h>
38 
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
50 template<class T>
52 public:
54  GUIPropertyScheme(const std::string& name, const T& baseColor,
55  const std::string& colName = "", const bool isFixed = false, SUMOReal baseValue = 0) :
58  myAllowNegativeValues(false) {
59  addColor(baseColor, baseValue, colName);
60  }
61 
62  void setThreshold(const size_t pos, const SUMOReal threshold) {
63  myThresholds[pos] = threshold;
64  }
65 
66  void setColor(const size_t pos, const T& color) {
67  myColors[pos] = color;
68  }
69 
70  bool setColor(const std::string& name, const T& color) {
71  std::vector<std::string>::iterator nameIt = myNames.begin();
72  typename std::vector<T>::iterator colIt = myColors.begin();
73  for (; nameIt != myNames.end(); ++nameIt, ++colIt) {
74  if (*nameIt == name) {
75  (*colIt) = color;
76  return true;
77  }
78  }
79  return false;
80  }
81 
82  unsigned int addColor(const T& color, const SUMOReal threshold, const std::string& name = "") {
83  typename std::vector<T>::iterator colIt = myColors.begin();
84  std::vector<SUMOReal>::iterator threshIt = myThresholds.begin();
85  std::vector<std::string>::iterator nameIt = myNames.begin();
86  unsigned int pos = 0;
87  while (threshIt != myThresholds.end() && (*threshIt) < threshold) {
88  ++threshIt;
89  ++colIt;
90  ++nameIt;
91  pos++;
92  }
93  myColors.insert(colIt, color);
94  myThresholds.insert(threshIt, threshold);
95  myNames.insert(nameIt, name);
96  return pos;
97  }
98 
99  void removeColor(const size_t pos) {
100  assert(pos < myColors.size());
101  myColors.erase(myColors.begin() + pos);
102  myThresholds.erase(myThresholds.begin() + pos);
103  myNames.erase(myNames.begin() + pos);
104  }
105 
106  void clear() {
107  myColors.clear();
108  myThresholds.clear();
109  myNames.clear();
110  }
111 
112  const T getColor(const SUMOReal value) const {
113  if (myColors.size() == 1 || value < myThresholds.front()) {
114  return myColors.front();
115  }
116  typename std::vector<T>::const_iterator colIt = myColors.begin() + 1;
117  std::vector<SUMOReal>::const_iterator threshIt = myThresholds.begin() + 1;
118  while (threshIt != myThresholds.end() && (*threshIt) <= value) {
119  ++threshIt;
120  ++colIt;
121  }
122  if (threshIt == myThresholds.end()) {
123  return myColors.back();
124  }
125  if (!myIsInterpolated) {
126  return *(colIt - 1);
127  }
128  SUMOReal lowVal = *(threshIt - 1);
129  return interpolate(*(colIt - 1), *colIt, (value - lowVal) / ((*threshIt) - lowVal));
130  }
131 
132  void setInterpolated(const bool interpolate, SUMOReal interpolationStart = 0.f) {
134  if (interpolate) {
135  myThresholds[0] = interpolationStart;
136  }
137  }
138 
139  const std::string& getName() const {
140  return myName;
141  }
142 
143  const std::vector<T>& getColors() const {
144  return myColors;
145  }
146 
147  const std::vector<SUMOReal>& getThresholds() const {
148  return myThresholds;
149  }
150 
151  bool isInterpolated() const {
152  return myIsInterpolated;
153  }
154 
155  const std::vector<std::string>& getNames() const {
156  return myNames;
157  }
158 
159  bool isFixed() const {
160  return myIsFixed;
161  }
162 
163  bool allowsNegativeValues() const {
164  return myAllowNegativeValues;
165  }
166 
167  void setAllowsNegativeValues(bool value) {
168  myAllowNegativeValues = value;
169  }
170 
171  void save(OutputDevice& dev) const {
172  const std::string tag = getTagName(myColors);
173  dev << " <" << tag << " name=\"" << myName;
174  if (!myIsFixed) {
175  dev << "\" interpolated=\"" << myIsInterpolated;
176  }
177  dev << "\">\n";
178  typename std::vector<T>::const_iterator colIt = myColors.begin();
179  std::vector<SUMOReal>::const_iterator threshIt = myThresholds.begin();
180  std::vector<std::string>::const_iterator nameIt = myNames.begin();
181  while (threshIt != myThresholds.end()) {
182  dev << " <entry color=\"" << (*colIt);
183  if (!myIsFixed) {
184  dev << "\" threshold=\"" << (*threshIt);
185  }
186  if ((*nameIt) != "") {
187  dev << "\" name=\"" << (*nameIt);
188  }
189  dev << "\"/>\n";
190  ++threshIt;
191  ++colIt;
192  ++nameIt;
193  }
194  dev << " </" << tag << ">\n";
195  }
196 
197  bool operator==(const GUIPropertyScheme& c) const {
199  }
200 
201 
203  RGBColor interpolate(const RGBColor& min, const RGBColor& max, SUMOReal weight) const {
204  return RGBColor::interpolate(min, max, weight);
205  }
206 
207  std::string getTagName(std::vector<RGBColor>) const {
209  }
210 
211 
213  SUMOReal interpolate(const SUMOReal& min, const SUMOReal& max, SUMOReal weight) const {
214  return min + (max - min) * weight;
215  }
216 
217  std::string getTagName(std::vector<SUMOReal>) const {
219  }
220 
221 
222 private:
223  std::string myName;
224  std::vector<T> myColors;
225  std::vector<SUMOReal> myThresholds;
227  std::vector<std::string> myNames;
228  bool myIsFixed;
230 
231 };
232 
235 
236 #endif
237 
238 /****************************************************************************/
const std::vector< T > & getColors() const
bool isInterpolated() const
GUIPropertyScheme< SUMOReal > GUIScaleScheme
std::string getTagName(std::vector< RGBColor >) const
#define min(a, b)
Definition: polyfonts.c:66
SUMOReal interpolate(const SUMOReal &min, const SUMOReal &max, SUMOReal weight) const
specializations for GUIScaleScheme
void removeColor(const size_t pos)
void setAllowsNegativeValues(bool value)
void setColor(const size_t pos, const T &color)
static RGBColor interpolate(const RGBColor &minColor, const RGBColor &maxColor, SUMOReal weight)
Interpolates between two colors.
Definition: RGBColor.cpp:271
std::string myName
bool operator==(const GUIPropertyScheme &c) const
void setThreshold(const size_t pos, const SUMOReal threshold)
std::string getTagName(std::vector< SUMOReal >) const
const std::string & getName() const
bool isFixed() const
void setInterpolated(const bool interpolate, SUMOReal interpolationStart=0.f)
#define max(a, b)
Definition: polyfonts.c:65
bool allowsNegativeValues() const
const T getColor(const SUMOReal value) const
std::vector< std::string > myNames
const std::vector< std::string > & getNames() const
GUIPropertyScheme< RGBColor > GUIColorScheme
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
unsigned int addColor(const T &color, const SUMOReal threshold, const std::string &name="")
std::vector< SUMOReal > myThresholds
bool setColor(const std::string &name, const T &color)
std::vector< T > myColors
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
#define SUMOReal
Definition: config.h:218
void save(OutputDevice &dev) const
GUIPropertyScheme(const std::string &name, const T &baseColor, const std::string &colName="", const bool isFixed=false, SUMOReal baseValue=0)
Constructor.
RGBColor interpolate(const RGBColor &min, const RGBColor &max, SUMOReal weight) const
specializations for GUIColorScheme
const std::vector< SUMOReal > & getThresholds() const