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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 //
18 /****************************************************************************/
19 #ifndef GUIPropertyScheme_h
20 #define GUIPropertyScheme_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <cassert>
29 #include <vector>
30 #include <utils/common/RGBColor.h>
32 
33 
34 // ===========================================================================
35 // class definitions
36 // ===========================================================================
44 template<class T>
46 public:
48  GUIPropertyScheme(const std::string& name, const T& baseColor,
49  const std::string& colName = "", const bool isFixed = false, double baseValue = 0) :
52  myAllowNegativeValues(false) {
53  addColor(baseColor, baseValue, colName);
54  }
55 
56  void setThreshold(const int pos, const double threshold) {
57  myThresholds[pos] = threshold;
58  }
59 
60  void setColor(const int pos, const T& color) {
61  myColors[pos] = color;
62  }
63 
64  bool setColor(const std::string& name, const T& color) {
65  std::vector<std::string>::iterator nameIt = myNames.begin();
66  typename std::vector<T>::iterator colIt = myColors.begin();
67  for (; nameIt != myNames.end(); ++nameIt, ++colIt) {
68  if (*nameIt == name) {
69  (*colIt) = color;
70  return true;
71  }
72  }
73  return false;
74  }
75 
76  int addColor(const T& color, const double threshold, const std::string& name = "") {
77  typename std::vector<T>::iterator colIt = myColors.begin();
78  std::vector<double>::iterator threshIt = myThresholds.begin();
79  std::vector<std::string>::iterator nameIt = myNames.begin();
80  int pos = 0;
81  while (threshIt != myThresholds.end() && (*threshIt) < threshold) {
82  ++threshIt;
83  ++colIt;
84  ++nameIt;
85  pos++;
86  }
87  myColors.insert(colIt, color);
88  myThresholds.insert(threshIt, threshold);
89  myNames.insert(nameIt, name);
90  return pos;
91  }
92 
93  void removeColor(const int pos) {
94  assert(pos < (int)myColors.size());
95  myColors.erase(myColors.begin() + pos);
96  myThresholds.erase(myThresholds.begin() + pos);
97  myNames.erase(myNames.begin() + pos);
98  }
99 
100  void clear() {
101  myColors.clear();
102  myThresholds.clear();
103  myNames.clear();
104  }
105 
106  const T getColor(const double value) const {
107  if (myColors.size() == 1 || value < myThresholds.front()) {
108  return myColors.front();
109  }
110  typename std::vector<T>::const_iterator colIt = myColors.begin() + 1;
111  std::vector<double>::const_iterator threshIt = myThresholds.begin() + 1;
112  while (threshIt != myThresholds.end() && (*threshIt) <= value) {
113  ++threshIt;
114  ++colIt;
115  }
116  if (threshIt == myThresholds.end()) {
117  return myColors.back();
118  }
119  if (!myIsInterpolated) {
120  return *(colIt - 1);
121  }
122  double lowVal = *(threshIt - 1);
123  return interpolate(*(colIt - 1), *colIt, (value - lowVal) / ((*threshIt) - lowVal));
124  }
125 
126  void setInterpolated(const bool interpolate, double interpolationStart = 0.f) {
128  if (interpolate) {
129  myThresholds[0] = interpolationStart;
130  }
131  }
132 
133  const std::string& getName() const {
134  return myName;
135  }
136 
137  const std::vector<T>& getColors() const {
138  return myColors;
139  }
140 
141  const std::vector<double>& getThresholds() const {
142  return myThresholds;
143  }
144 
145  bool isInterpolated() const {
146  return myIsInterpolated;
147  }
148 
149  const std::vector<std::string>& getNames() const {
150  return myNames;
151  }
152 
153  bool isFixed() const {
154  return myIsFixed;
155  }
156 
157  bool allowsNegativeValues() const {
158  return myAllowNegativeValues;
159  }
160 
161  void setAllowsNegativeValues(bool value) {
162  myAllowNegativeValues = value;
163  }
164 
165  void save(OutputDevice& dev) const {
166  const std::string tag = getTagName(myColors);
167 
168  dev.openTag(tag);
170  if (!myIsFixed) {
172  }
173  typename std::vector<T>::const_iterator colIt = myColors.begin();
174  std::vector<double>::const_iterator threshIt = myThresholds.begin();
175  std::vector<std::string>::const_iterator nameIt = myNames.begin();
176  while (threshIt != myThresholds.end()) {
177  dev.openTag(SUMO_TAG_ENTRY);
178  dev.writeAttr(SUMO_ATTR_COLOR, *colIt);
179  if (!myIsFixed) {
180  dev.writeAttr(SUMO_ATTR_THRESHOLD, *threshIt);
181  }
182  if ((*nameIt) != "") {
183  dev.writeAttr(SUMO_ATTR_NAME, *nameIt);
184  }
185  dev.closeTag();
186  ++threshIt;
187  ++colIt;
188  ++nameIt;
189  }
190  dev.closeTag();
191  }
192 
193  bool operator==(const GUIPropertyScheme& c) const {
195  }
196 
197 
199  RGBColor interpolate(const RGBColor& min, const RGBColor& max, double weight) const {
200  return RGBColor::interpolate(min, max, weight);
201  }
202 
203  std::string getTagName(std::vector<RGBColor>) const {
205  }
206 
207 
209  double interpolate(const double& min, const double& max, double weight) const {
210  return min + (max - min) * weight;
211  }
212 
213  std::string getTagName(std::vector<double>) const {
215  }
216 
217 
218 private:
219  std::string myName;
220  std::vector<T> myColors;
221  std::vector<double> myThresholds;
223  std::vector<std::string> myNames;
224  bool myIsFixed;
226 
227 };
228 
231 
232 #endif
233 
234 /****************************************************************************/
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
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:49
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:64
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
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:283
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void setInterpolated(const bool interpolate, double interpolationStart=0.f)