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