Eclipse 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-2019 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>
33 
34 
35 // ===========================================================================
36 // class definitions
37 // ===========================================================================
45 template<class T>
47 public:
49  GUIPropertyScheme(const std::string& name, const T& baseColor,
50  const std::string& colName = "", const bool isFixed = false, double baseValue = 0,
51  RGBColor bgColor = RGBColor::INVISIBLE,
52  GUIIcon icon = ICON_EMPTY) :
55  myAllowNegativeValues(false),
56  myIcon(icon),
57  myBgColor(bgColor) {
58  addColor(baseColor, baseValue, colName);
59  }
60 
61  void setThreshold(const int pos, const double threshold) {
62  myThresholds[pos] = threshold;
63  }
64 
65  void setColor(const int pos, const T& color) {
66  myColors[pos] = color;
67  }
68 
69  bool setColor(const std::string& name, const T& color) {
70  std::vector<std::string>::iterator nameIt = myNames.begin();
71  typename std::vector<T>::iterator colIt = myColors.begin();
72  for (; nameIt != myNames.end(); ++nameIt, ++colIt) {
73  if (*nameIt == name) {
74  (*colIt) = color;
75  return true;
76  }
77  }
78  return false;
79  }
80 
81  int addColor(const T& color, const double threshold, const std::string& name = "") {
82  typename std::vector<T>::iterator colIt = myColors.begin();
83  std::vector<double>::iterator threshIt = myThresholds.begin();
84  std::vector<std::string>::iterator nameIt = myNames.begin();
85  int pos = 0;
86  while (threshIt != myThresholds.end() && (*threshIt) < threshold) {
87  ++threshIt;
88  ++colIt;
89  ++nameIt;
90  pos++;
91  }
92  myColors.insert(colIt, color);
93  myThresholds.insert(threshIt, threshold);
94  myNames.insert(nameIt, name);
95  return pos;
96  }
97 
98  void removeColor(const int pos) {
99  assert(pos < (int)myColors.size());
100  myColors.erase(myColors.begin() + pos);
101  myThresholds.erase(myThresholds.begin() + pos);
102  myNames.erase(myNames.begin() + pos);
103  }
104 
105  void clear() {
106  myColors.clear();
107  myThresholds.clear();
108  myNames.clear();
109  }
110 
111  const T getColor(const double value) const {
112  if (myColors.size() == 1 || value < myThresholds.front()) {
113  return myColors.front();
114  }
115  typename std::vector<T>::const_iterator colIt = myColors.begin() + 1;
116  std::vector<double>::const_iterator threshIt = myThresholds.begin() + 1;
117  while (threshIt != myThresholds.end() && (*threshIt) <= value) {
118  ++threshIt;
119  ++colIt;
120  }
121  if (threshIt == myThresholds.end()) {
122  return myColors.back();
123  }
124  if (!myIsInterpolated) {
125  return *(colIt - 1);
126  }
127  double lowVal = *(threshIt - 1);
128  return interpolate(*(colIt - 1), *colIt, (value - lowVal) / ((*threshIt) - lowVal));
129  }
130 
131  void setInterpolated(const bool interpolate, double interpolationStart = 0.f) {
133  if (interpolate) {
134  myThresholds[0] = interpolationStart;
135  }
136  }
137 
138  const std::string& getName() const {
139  return myName;
140  }
141 
142  const std::vector<T>& getColors() const {
143  return myColors;
144  }
145 
146  const std::vector<double>& getThresholds() const {
147  return myThresholds;
148  }
149 
150  bool isInterpolated() const {
151  return myIsInterpolated;
152  }
153 
154  const std::vector<std::string>& getNames() const {
155  return myNames;
156  }
157 
158  bool isFixed() const {
159  return myIsFixed;
160  }
161 
162  bool allowsNegativeValues() const {
163  return myAllowNegativeValues;
164  }
165 
166  void setAllowsNegativeValues(bool value) {
167  myAllowNegativeValues = value;
168  }
169 
170  GUIIcon getIcon() const {
171  return myIcon;
172  }
173 
174  const RGBColor& getBackgroundColor() const {
175  return myBgColor;
176  }
177 
178  void save(OutputDevice& dev) const {
179  const std::string tag = getTagName(myColors);
180 
181  dev.openTag(tag);
183  if (!myIsFixed) {
185  }
186  typename std::vector<T>::const_iterator colIt = myColors.begin();
187  std::vector<double>::const_iterator threshIt = myThresholds.begin();
188  std::vector<std::string>::const_iterator nameIt = myNames.begin();
189  while (threshIt != myThresholds.end()) {
190  dev.openTag(SUMO_TAG_ENTRY);
191  dev.writeAttr(SUMO_ATTR_COLOR, *colIt);
192  if (!myIsFixed) {
193  dev.writeAttr(SUMO_ATTR_THRESHOLD, *threshIt);
194  }
195  if ((*nameIt) != "") {
196  dev.writeAttr(SUMO_ATTR_NAME, *nameIt);
197  }
198  dev.closeTag();
199  ++threshIt;
200  ++colIt;
201  ++nameIt;
202  }
203  dev.closeTag();
204  }
205 
206  bool operator==(const GUIPropertyScheme& c) const {
208  }
209 
210 
212  RGBColor interpolate(const RGBColor& min, const RGBColor& max, double weight) const {
213  return RGBColor::interpolate(min, max, weight);
214  }
215 
216  std::string getTagName(std::vector<RGBColor>) const {
218  }
219 
220 
222  double interpolate(const double& min, const double& max, double weight) const {
223  return min + (max - min) * weight;
224  }
225 
226  std::string getTagName(std::vector<double>) const {
228  }
229 
230 
231 private:
232  std::string myName;
233  std::vector<T> myColors;
234  std::vector<double> myThresholds;
236  std::vector<std::string> myNames;
237  bool myIsFixed;
241 
242 };
243 
246 
247 #endif
248 
249 /****************************************************************************/
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< double > GUIScaleScheme
const std::string & getName() const
GUIIcon
An enumeration of icons used by the gui applications.
Definition: GUIIcons.h:36
void setAllowsNegativeValues(bool value)
const std::vector< std::string > & getNames() const
bool allowsNegativeValues() const
GUIIcon getIcon() 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:48
void save(OutputDevice &dev) const
const RGBColor & getBackgroundColor() const
GUIPropertyScheme(const std::string &name, const T &baseColor, const std::string &colName="", const bool isFixed=false, double baseValue=0, RGBColor bgColor=RGBColor::INVISIBLE, GUIIcon icon=ICON_EMPTY)
Constructor.
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
static const RGBColor INVISIBLE
Definition: RGBColor.h:200
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)