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-2022 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 T& baseColor,
47  const std::string& colName = "", const bool isFixed = false, double baseValue = 0,
48  RGBColor bgColor = RGBColor::WHITE,
49  GUIIcon icon = GUIIcon::EMPTY) :
52  myAllowNegativeValues(false),
53  myIcon(icon),
54  myBgColor(bgColor) {
55  addColor(baseColor, baseValue, colName);
56  }
57 
58  void setThreshold(const int pos, const double threshold) {
59  myThresholds[pos] = threshold;
60  }
61 
62  void setColor(const int pos, const T& color) {
63  myColors[pos] = color;
64  }
65 
66  bool setColor(const std::string& name, const T& color) {
67  std::vector<std::string>::iterator nameIt = myNames.begin();
68  typename std::vector<T>::iterator colIt = myColors.begin();
69  for (; nameIt != myNames.end(); ++nameIt, ++colIt) {
70  if (*nameIt == name) {
71  (*colIt) = color;
72  return true;
73  }
74  }
75  return false;
76  }
77 
78  int addColor(const T& color, const double threshold, const std::string& name = "") {
79  typename std::vector<T>::iterator colIt = myColors.begin();
80  std::vector<double>::iterator threshIt = myThresholds.begin();
81  std::vector<std::string>::iterator nameIt = myNames.begin();
82  int pos = 0;
83  while (threshIt != myThresholds.end() && (*threshIt) < threshold) {
84  ++threshIt;
85  ++colIt;
86  ++nameIt;
87  pos++;
88  }
89  myColors.insert(colIt, color);
90  myThresholds.insert(threshIt, threshold);
91  myNames.insert(nameIt, name);
92  return pos;
93  }
94 
95  void removeColor(const int pos) {
96  assert(pos < (int)myColors.size());
97  myColors.erase(myColors.begin() + pos);
98  myThresholds.erase(myThresholds.begin() + pos);
99  myNames.erase(myNames.begin() + pos);
100  }
101 
102  void clear() {
103  myColors.clear();
104  myThresholds.clear();
105  myNames.clear();
106  }
107 
108  const T getColor(const double value) const {
109  if (myColors.size() == 1 || value < myThresholds.front()) {
110  return myColors.front();
111  }
112  typename std::vector<T>::const_iterator colIt = myColors.begin() + 1;
113  std::vector<double>::const_iterator threshIt = myThresholds.begin() + 1;
114  while (threshIt != myThresholds.end() && (*threshIt) <= value) {
115  ++threshIt;
116  ++colIt;
117  }
118  if (threshIt == myThresholds.end()) {
119  return myColors.back();
120  }
121  if (!myIsInterpolated) {
122  return *(colIt - 1);
123  }
124  double lowVal = *(threshIt - 1);
125  return interpolate(*(colIt - 1), *colIt, (value - lowVal) / ((*threshIt) - lowVal));
126  }
127 
128  void setInterpolated(const bool interpolate, double interpolationStart = 0.f) {
130  if (interpolate) {
131  myThresholds[0] = interpolationStart;
132  }
133  }
134 
135  const std::string& getName() const {
136  return myName;
137  }
138 
139  const std::vector<T>& getColors() const {
140  return myColors;
141  }
142 
143  const std::vector<double>& getThresholds() const {
144  return myThresholds;
145  }
146 
147  bool isInterpolated() const {
148  return myIsInterpolated;
149  }
150 
151  const std::vector<std::string>& getNames() const {
152  return myNames;
153  }
154 
155  bool isFixed() const {
156  return myIsFixed;
157  }
158 
159  bool allowsNegativeValues() const {
160  return myAllowNegativeValues;
161  }
162 
163  void setAllowsNegativeValues(bool value) {
164  myAllowNegativeValues = value;
165  }
166 
167  GUIIcon getIcon() const {
168  return myIcon;
169  }
170 
171  const RGBColor& getBackgroundColor() const {
172  return myBgColor;
173  }
174 
175  void save(OutputDevice& dev, const std::string& prefix = "") const {
176  const std::string tag = getTagName(myColors);
177 
178  dev.openTag(tag);
179  dev.writeAttr(SUMO_ATTR_NAME, prefix + myName);
180  if (!myIsFixed) {
182  }
183  typename std::vector<T>::const_iterator colIt = myColors.begin();
184  std::vector<double>::const_iterator threshIt = myThresholds.begin();
185  std::vector<std::string>::const_iterator nameIt = myNames.begin();
186  while (threshIt != myThresholds.end()) {
187  dev.openTag(SUMO_TAG_ENTRY);
188  dev.writeAttr(SUMO_ATTR_COLOR, *colIt);
189  if (!myIsFixed && (*threshIt) != std::numeric_limits<double>::max()) {
190  dev.writeAttr(SUMO_ATTR_THRESHOLD, *threshIt);
191  }
192  if ((*nameIt) != "") {
193  dev.writeAttr(SUMO_ATTR_NAME, *nameIt);
194  }
195  dev.closeTag();
196  ++threshIt;
197  ++colIt;
198  ++nameIt;
199  }
200  dev.closeTag();
201  }
202 
203  bool operator==(const GUIPropertyScheme& c) const {
205  }
206 
207 
209  RGBColor interpolate(const RGBColor& min, const RGBColor& max, double weight) const {
210  return RGBColor::interpolate(min, max, weight);
211  }
212 
213  std::string getTagName(std::vector<RGBColor>) const {
215  }
216 
217 
219  double interpolate(const double& min, const double& max, double weight) const {
220  return min + (max - min) * weight;
221  }
222 
223  std::string getTagName(std::vector<double>) const {
225  }
226 
227 
228 private:
229  std::string myName;
230  std::vector<T> myColors;
231  std::vector<double> myThresholds;
233  std::vector<std::string> myNames;
234  bool myIsFixed;
238 
239 };
240 
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 T getColor(const double value) const
std::string getTagName(std::vector< double >) const
GUIIcon getIcon() 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)
Constructor.
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
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
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:248
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
static RGBColor interpolate(const RGBColor &minColor, const RGBColor &maxColor, double weight)
Interpolates between two colors.
Definition: RGBColor.cpp:352
static const RGBColor WHITE
Definition: RGBColor.h:192