SUMO - Simulation of Urban MObility
GUIPropertyScheme.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
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, double baseValue = 0) :
58  myAllowNegativeValues(false) {
59  addColor(baseColor, baseValue, colName);
60  }
61 
62  void setThreshold(const int pos, const double threshold) {
63  myThresholds[pos] = threshold;
64  }
65 
66  void setColor(const int 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  int addColor(const T& color, const double threshold, const std::string& name = "") {
83  typename std::vector<T>::iterator colIt = myColors.begin();
84  std::vector<double>::iterator threshIt = myThresholds.begin();
85  std::vector<std::string>::iterator nameIt = myNames.begin();
86  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 int pos) {
100  assert(pos < (int)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 double 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<double>::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  double lowVal = *(threshIt - 1);
129  return interpolate(*(colIt - 1), *colIt, (value - lowVal) / ((*threshIt) - lowVal));
130  }
131 
132  void setInterpolated(const bool interpolate, double 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<double>& 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 
174  dev.openTag(tag);
176  if (!myIsFixed) {
178  }
179  typename std::vector<T>::const_iterator colIt = myColors.begin();
180  std::vector<double>::const_iterator threshIt = myThresholds.begin();
181  std::vector<std::string>::const_iterator nameIt = myNames.begin();
182  while (threshIt != myThresholds.end()) {
183  dev.openTag(SUMO_TAG_ENTRY);
184  dev.writeAttr(SUMO_ATTR_COLOR, *colIt);
185  if (!myIsFixed) {
186  dev.writeAttr(SUMO_ATTR_THRESHOLD, *threshIt);
187  }
188  if ((*nameIt) != "") {
189  dev.writeAttr(SUMO_ATTR_NAME, *nameIt);
190  }
191  dev.closeTag();
192  ++threshIt;
193  ++colIt;
194  ++nameIt;
195  }
196  dev.closeTag();
197  }
198 
199  bool operator==(const GUIPropertyScheme& c) const {
201  }
202 
203 
205  RGBColor interpolate(const RGBColor& min, const RGBColor& max, double weight) const {
206  return RGBColor::interpolate(min, max, weight);
207  }
208 
209  std::string getTagName(std::vector<RGBColor>) const {
211  }
212 
213 
215  double interpolate(const double& min, const double& max, double weight) const {
216  return min + (max - min) * weight;
217  }
218 
219  std::string getTagName(std::vector<double>) const {
221  }
222 
223 
224 private:
225  std::string myName;
226  std::vector<T> myColors;
227  std::vector<double> myThresholds;
229  std::vector<std::string> myNames;
230  bool myIsFixed;
232 
233 };
234 
237 
238 #endif
239 
240 /****************************************************************************/
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:260
GUIPropertyScheme(const std::string &name, const T &baseColor, const std::string &colName="", const bool isFixed=false, double baseValue=0)
Constructor.
GUIPropertyScheme< double > GUIScaleScheme
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
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:55
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:70
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:285
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void setInterpolated(const bool interpolate, double interpolationStart=0.f)