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.dev/sumo
3 // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 //
21 /****************************************************************************/
22 #pragma once
23 #include <config.h>
24 
25 #include <cassert>
26 #include <vector>
27 #include <utils/common/RGBColor.h>
30 
31 
32 // ===========================================================================
33 // class definitions
34 // ===========================================================================
42 template<class T>
44 public:
46  GUIPropertyScheme(const std::string& name, const std::string& translatedName, const T& baseColor,
47  const std::string& colName = "", const bool isFixed = false, double baseValue = 0,
48  RGBColor bgColor = RGBColor::WHITE,
49  GUIIcon icon = GUIIcon::EMPTY) :
50  myName(name),
51  myTranslatedName(translatedName),
54  myAllowNegativeValues(false),
55  myIcon(icon),
56  myBgColor(bgColor) {
57  addColor(baseColor, baseValue, colName);
58  }
59 
60  GUIPropertyScheme(const std::string& name, const T& baseColor,
61  const std::string& colName = "", const bool isFixed = false, double baseValue = 0,
62  RGBColor bgColor = RGBColor::WHITE,
63  GUIIcon icon = GUIIcon::EMPTY) :
64  myName(name),
65  myTranslatedName(name),
68  myAllowNegativeValues(false),
69  myIcon(icon),
70  myBgColor(bgColor) {
71  addColor(baseColor, baseValue, colName);
72  }
73 
74  void setThreshold(const int pos, const double threshold) {
75  myThresholds[pos] = threshold;
76  }
77 
78  void setColor(const int pos, const T& color) {
79  myColors[pos] = color;
80  }
81 
82  bool setColor(const std::string& name, const T& color) {
83  std::vector<std::string>::iterator nameIt = myNames.begin();
84  typename std::vector<T>::iterator colIt = myColors.begin();
85  for (; nameIt != myNames.end(); ++nameIt, ++colIt) {
86  if (*nameIt == name) {
87  (*colIt) = color;
88  return true;
89  }
90  }
91  return false;
92  }
93 
94  int addColor(const T& color, const double threshold, const std::string& name = "") {
95  typename std::vector<T>::iterator colIt = myColors.begin();
96  std::vector<double>::iterator threshIt = myThresholds.begin();
97  std::vector<std::string>::iterator nameIt = myNames.begin();
98  int pos = 0;
99  while (threshIt != myThresholds.end() && (*threshIt) < threshold) {
100  ++threshIt;
101  ++colIt;
102  ++nameIt;
103  pos++;
104  }
105  myColors.insert(colIt, color);
106  myThresholds.insert(threshIt, threshold);
107  myNames.insert(nameIt, name);
108  return pos;
109  }
110 
111  void removeColor(const int pos) {
112  assert(pos < (int)myColors.size());
113  myColors.erase(myColors.begin() + pos);
114  myThresholds.erase(myThresholds.begin() + pos);
115  myNames.erase(myNames.begin() + pos);
116  }
117 
118  void clear() {
119  myColors.clear();
120  myThresholds.clear();
121  myNames.clear();
122  }
123 
124  T getColor(const double value) const {
125  if (myColors.size() == 1 || value < myThresholds.front()) {
126  return myColors.front();
127  }
128  typename std::vector<T>::const_iterator colIt = myColors.begin() + 1;
129  std::vector<double>::const_iterator threshIt = myThresholds.begin() + 1;
130  while (threshIt != myThresholds.end() && (*threshIt) <= value) {
131  ++threshIt;
132  ++colIt;
133  }
134  if (threshIt == myThresholds.end()) {
135  return myColors.back();
136  }
137  if (!myIsInterpolated) {
138  return *(colIt - 1);
139  }
140  double lowVal = *(threshIt - 1);
141  return interpolate(*(colIt - 1), *colIt, (value - lowVal) / ((*threshIt) - lowVal));
142  }
143 
144  void setInterpolated(const bool interpolate, double interpolationStart = 0.f) {
146  if (interpolate) {
147  myThresholds[0] = interpolationStart;
148  }
149  }
150 
151  const std::string& getName() const {
152  return myName;
153  }
154 
155  const std::string& getTranslatedName() const {
156  return myTranslatedName;
157  }
158 
159  const std::vector<T>& getColors() const {
160  return myColors;
161  }
162 
163  const std::vector<double>& getThresholds() const {
164  return myThresholds;
165  }
166 
167  bool isInterpolated() const {
168  return myIsInterpolated;
169  }
170 
171  const std::vector<std::string>& getNames() const {
172  return myNames;
173  }
174 
175  bool isFixed() const {
176  return myIsFixed;
177  }
178 
179  bool allowsNegativeValues() const {
180  return myAllowNegativeValues;
181  }
182 
183  void setAllowsNegativeValues(bool value) {
184  myAllowNegativeValues = value;
185  }
186 
187  GUIIcon getIcon() const {
188  return myIcon;
189  }
190 
191  const RGBColor& getBackgroundColor() const {
192  return myBgColor;
193  }
194 
195  void save(OutputDevice& dev, const std::string& prefix = "") const {
196  const int precision = dev.getPrecision();
197  const bool checkPrecision = precision <= 2; // 2 is default precision (see SystemFrame)
198  const std::string tag = getTagName(myColors);
199 
200  dev.openTag(tag);
201  dev.writeAttr(SUMO_ATTR_NAME, prefix + myName);
202  if (!myIsFixed) {
204  }
205  typename std::vector<T>::const_iterator colIt = myColors.begin();
206  std::vector<double>::const_iterator threshIt = myThresholds.begin();
207  std::vector<std::string>::const_iterator nameIt = myNames.begin();
208  while (threshIt != myThresholds.end()) {
209  dev.openTag(SUMO_TAG_ENTRY);
210  dev.writeAttr(SUMO_ATTR_COLOR, *colIt);
211  if (!myIsFixed && (*threshIt) != std::numeric_limits<double>::max()) {
212  const double t = *threshIt;
213  if (checkPrecision && t != 0 && fabs(t) < 0.01) {
214  dev.setPrecision(8);
215  }
217  dev.setPrecision(precision);
218  }
219  if ((*nameIt) != "") {
220  dev.writeAttr(SUMO_ATTR_NAME, *nameIt);
221  }
222  dev.closeTag();
223  ++threshIt;
224  ++colIt;
225  ++nameIt;
226  }
227  dev.closeTag();
228  }
229 
230  bool operator==(const GUIPropertyScheme& c) const {
232  }
233 
234 
236  RGBColor interpolate(const RGBColor& min, const RGBColor& max, double weight) const {
237  return RGBColor::interpolate(min, max, weight);
238  }
239 
240  std::string getTagName(std::vector<RGBColor>) const {
242  }
243 
244 
246  double interpolate(const double& min, const double& max, double weight) const {
247  return min + (max - min) * weight;
248  }
249 
250  std::string getTagName(std::vector<double>) const {
252  }
253 
254 
255 private:
256  std::string myName;
257  std::string myTranslatedName;
258  std::vector<T> myColors;
259  std::vector<double> myThresholds;
261  std::vector<std::string> myNames;
262  bool myIsFixed;
266 
267 };
268 
GUIIcon
An enumeration of icons used by the gui applications.
Definition: GUIIcons.h:33
GUIPropertyScheme< RGBColor > GUIColorScheme
GUIPropertyScheme< double > GUIScaleScheme
@ SUMO_TAG_COLORSCHEME
@ SUMO_TAG_ENTRY
@ SUMO_TAG_SCALINGSCHEME
@ SUMO_ATTR_THRESHOLD
@ SUMO_ATTR_NAME
@ SUMO_ATTR_COLOR
A color information.
@ SUMO_ATTR_INTERPOLATED
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
const std::string & getTranslatedName() const
std::string getTagName(std::vector< double >) const
GUIIcon getIcon() const
T getColor(const double value) const
void setAllowsNegativeValues(bool value)
const std::string & getName() const
const std::vector< double > & getThresholds() const
std::string getTagName(std::vector< RGBColor >) const
void setColor(const int pos, const T &color)
GUIPropertyScheme(const std::string &name, const T &baseColor, const std::string &colName="", const bool isFixed=false, double baseValue=0, RGBColor bgColor=RGBColor::WHITE, GUIIcon icon=GUIIcon::EMPTY)
std::vector< double > myThresholds
void setThreshold(const int pos, const double threshold)
void removeColor(const int pos)
const std::vector< T > & getColors() const
const RGBColor & getBackgroundColor() const
GUIPropertyScheme(const std::string &name, const std::string &translatedName, const T &baseColor, const std::string &colName="", const bool isFixed=false, double baseValue=0, RGBColor bgColor=RGBColor::WHITE, GUIIcon icon=GUIIcon::EMPTY)
Constructor.
std::vector< std::string > myNames
void save(OutputDevice &dev, const std::string &prefix="") const
int addColor(const T &color, const double threshold, const std::string &name="")
void setInterpolated(const bool interpolate, double interpolationStart=0.f)
double interpolate(const double &min, const double &max, double weight) const
specializations for GUIScaleScheme
bool isInterpolated() const
bool operator==(const GUIPropertyScheme &c) const
std::vector< T > myColors
std::string myTranslatedName
bool setColor(const std::string &name, const T &color)
const std::vector< std::string > & getNames() const
RGBColor interpolate(const RGBColor &min, const RGBColor &max, double weight) const
specializations for GUIColorScheme
bool allowsNegativeValues() const
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
int getPrecision()
Returns the precision of the underlying stream.
Definition: OutputDevice.h:177
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
void setPrecision(int precision=gPrecision)
Sets the precision or resets it to default.
static RGBColor interpolate(const RGBColor &minColor, const RGBColor &maxColor, double weight)
Interpolates between two colors.
Definition: RGBColor.cpp:355
static const RGBColor WHITE
Definition: RGBColor.h:192