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 /****************************************************************************/
16 //
17 /****************************************************************************/
18 #ifndef GUIPropertyScheme_h
19 #define GUIPropertyScheme_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <cassert>
28 #include <vector>
29 #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,
50  RGBColor bgColor = RGBColor::INVISIBLE,
51  GUIIcon icon = ICON_EMPTY) :
54  myAllowNegativeValues(false),
55  myIcon(icon),
56  myBgColor(bgColor) {
57  addColor(baseColor, baseValue, colName);
58  }
59 
60  void setThreshold(const int pos, const double threshold) {
61  myThresholds[pos] = threshold;
62  }
63 
64  void setColor(const int pos, const T& color) {
65  myColors[pos] = color;
66  }
67 
68  bool setColor(const std::string& name, const T& color) {
69  std::vector<std::string>::iterator nameIt = myNames.begin();
70  typename std::vector<T>::iterator colIt = myColors.begin();
71  for (; nameIt != myNames.end(); ++nameIt, ++colIt) {
72  if (*nameIt == name) {
73  (*colIt) = color;
74  return true;
75  }
76  }
77  return false;
78  }
79 
80  int addColor(const T& color, const double threshold, const std::string& name = "") {
81  typename std::vector<T>::iterator colIt = myColors.begin();
82  std::vector<double>::iterator threshIt = myThresholds.begin();
83  std::vector<std::string>::iterator nameIt = myNames.begin();
84  int pos = 0;
85  while (threshIt != myThresholds.end() && (*threshIt) < threshold) {
86  ++threshIt;
87  ++colIt;
88  ++nameIt;
89  pos++;
90  }
91  myColors.insert(colIt, color);
92  myThresholds.insert(threshIt, threshold);
93  myNames.insert(nameIt, name);
94  return pos;
95  }
96 
97  void removeColor(const int pos) {
98  assert(pos < (int)myColors.size());
99  myColors.erase(myColors.begin() + pos);
100  myThresholds.erase(myThresholds.begin() + pos);
101  myNames.erase(myNames.begin() + pos);
102  }
103 
104  void clear() {
105  myColors.clear();
106  myThresholds.clear();
107  myNames.clear();
108  }
109 
110  const T getColor(const double value) const {
111  if (myColors.size() == 1 || value < myThresholds.front()) {
112  return myColors.front();
113  }
114  typename std::vector<T>::const_iterator colIt = myColors.begin() + 1;
115  std::vector<double>::const_iterator threshIt = myThresholds.begin() + 1;
116  while (threshIt != myThresholds.end() && (*threshIt) <= value) {
117  ++threshIt;
118  ++colIt;
119  }
120  if (threshIt == myThresholds.end()) {
121  return myColors.back();
122  }
123  if (!myIsInterpolated) {
124  return *(colIt - 1);
125  }
126  double lowVal = *(threshIt - 1);
127  return interpolate(*(colIt - 1), *colIt, (value - lowVal) / ((*threshIt) - lowVal));
128  }
129 
130  void setInterpolated(const bool interpolate, double interpolationStart = 0.f) {
132  if (interpolate) {
133  myThresholds[0] = interpolationStart;
134  }
135  }
136 
137  const std::string& getName() const {
138  return myName;
139  }
140 
141  const std::vector<T>& getColors() const {
142  return myColors;
143  }
144 
145  const std::vector<double>& getThresholds() const {
146  return myThresholds;
147  }
148 
149  bool isInterpolated() const {
150  return myIsInterpolated;
151  }
152 
153  const std::vector<std::string>& getNames() const {
154  return myNames;
155  }
156 
157  bool isFixed() const {
158  return myIsFixed;
159  }
160 
161  bool allowsNegativeValues() const {
162  return myAllowNegativeValues;
163  }
164 
165  void setAllowsNegativeValues(bool value) {
166  myAllowNegativeValues = value;
167  }
168 
169  GUIIcon getIcon() const {
170  return myIcon;
171  }
172 
173  const RGBColor& getBackgroundColor() const {
174  return myBgColor;
175  }
176 
177  void save(OutputDevice& dev) const {
178  const std::string tag = getTagName(myColors);
179 
180  dev.openTag(tag);
182  if (!myIsFixed) {
184  }
185  typename std::vector<T>::const_iterator colIt = myColors.begin();
186  std::vector<double>::const_iterator threshIt = myThresholds.begin();
187  std::vector<std::string>::const_iterator nameIt = myNames.begin();
188  while (threshIt != myThresholds.end()) {
189  dev.openTag(SUMO_TAG_ENTRY);
190  dev.writeAttr(SUMO_ATTR_COLOR, *colIt);
191  if (!myIsFixed) {
192  dev.writeAttr(SUMO_ATTR_THRESHOLD, *threshIt);
193  }
194  if ((*nameIt) != "") {
195  dev.writeAttr(SUMO_ATTR_NAME, *nameIt);
196  }
197  dev.closeTag();
198  ++threshIt;
199  ++colIt;
200  ++nameIt;
201  }
202  dev.closeTag();
203  }
204 
205  bool operator==(const GUIPropertyScheme& c) const {
207  }
208 
209 
211  RGBColor interpolate(const RGBColor& min, const RGBColor& max, double weight) const {
212  return RGBColor::interpolate(min, max, weight);
213  }
214 
215  std::string getTagName(std::vector<RGBColor>) const {
217  }
218 
219 
221  double interpolate(const double& min, const double& max, double weight) const {
222  return min + (max - min) * weight;
223  }
224 
225  std::string getTagName(std::vector<double>) const {
227  }
228 
229 
230 private:
231  std::string myName;
232  std::vector<T> myColors;
233  std::vector<double> myThresholds;
235  std::vector<std::string> myNames;
236  bool myIsFixed;
240 
241 };
242 
245 
246 #endif
247 
248 /****************************************************************************/
GUIPropertyScheme::myNames
std::vector< std::string > myNames
Definition: GUIPropertyScheme.h:235
GUIPropertyScheme::interpolate
double interpolate(const double &min, const double &max, double weight) const
specializations for GUIScaleScheme
Definition: GUIPropertyScheme.h:221
SUMO_ATTR_INTERPOLATED
Definition: SUMOXMLDefinitions.h:824
RGBColor::INVISIBLE
static const RGBColor INVISIBLE
Definition: RGBColor.h:199
SUMO_TAG_SCALINGSCHEME
Definition: SUMOXMLDefinitions.h:265
GUIPropertyScheme::GUIPropertyScheme
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.
Definition: GUIPropertyScheme.h:48
GUIPropertyScheme::setColor
void setColor(const int pos, const T &color)
Definition: GUIPropertyScheme.h:64
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
SUMO_ATTR_THRESHOLD
Definition: SUMOXMLDefinitions.h:825
ICON_EMPTY
Definition: GUIIcons.h:41
GUIScaleScheme
GUIPropertyScheme< double > GUIScaleScheme
Definition: GUIPropertyScheme.h:244
GUIColorScheme
GUIPropertyScheme< RGBColor > GUIColorScheme
Definition: GUIPropertyScheme.h:243
GUIPropertyScheme::myColors
std::vector< T > myColors
Definition: GUIPropertyScheme.h:232
GUIPropertyScheme::removeColor
void removeColor(const int pos)
Definition: GUIPropertyScheme.h:97
SUMO_TAG_COLORSCHEME
Definition: SUMOXMLDefinitions.h:264
SUMO_ATTR_COLOR
A color information.
Definition: SUMOXMLDefinitions.h:704
GUIPropertyScheme::myIsInterpolated
bool myIsInterpolated
Definition: GUIPropertyScheme.h:234
GUIPropertyScheme::operator==
bool operator==(const GUIPropertyScheme &c) const
Definition: GUIPropertyScheme.h:205
GUIPropertyScheme::myIsFixed
bool myIsFixed
Definition: GUIPropertyScheme.h:236
RGBColor.h
OutputDevice::closeTag
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Definition: OutputDevice.cpp:253
GUIPropertyScheme::getThresholds
const std::vector< double > & getThresholds() const
Definition: GUIPropertyScheme.h:145
GUIPropertyScheme::interpolate
RGBColor interpolate(const RGBColor &min, const RGBColor &max, double weight) const
specializations for GUIColorScheme
Definition: GUIPropertyScheme.h:211
RGBColor::interpolate
static RGBColor interpolate(const RGBColor &minColor, const RGBColor &maxColor, double weight)
Interpolates between two colors.
Definition: RGBColor.cpp:282
OutputDevice::writeAttr
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:255
RGBColor
Definition: RGBColor.h:39
GUIPropertyScheme::getColors
const std::vector< T > & getColors() const
Definition: GUIPropertyScheme.h:141
GUIPropertyScheme::getTagName
std::string getTagName(std::vector< RGBColor >) const
Definition: GUIPropertyScheme.h:215
GUIPropertyScheme::setColor
bool setColor(const std::string &name, const T &color)
Definition: GUIPropertyScheme.h:68
GUIPropertyScheme::clear
void clear()
Definition: GUIPropertyScheme.h:104
GUIPropertyScheme::isFixed
bool isFixed() const
Definition: GUIPropertyScheme.h:157
GUIPropertyScheme::myIcon
GUIIcon myIcon
Definition: GUIPropertyScheme.h:238
OutputDevice.h
GUIPropertyScheme::setInterpolated
void setInterpolated(const bool interpolate, double interpolationStart=0.f)
Definition: GUIPropertyScheme.h:130
GUIIcons.h
GUIPropertyScheme::myAllowNegativeValues
bool myAllowNegativeValues
Definition: GUIPropertyScheme.h:237
SUMO_TAG_ENTRY
Definition: SUMOXMLDefinitions.h:266
GUIPropertyScheme::allowsNegativeValues
bool allowsNegativeValues() const
Definition: GUIPropertyScheme.h:161
GUIIcon
GUIIcon
An enumeration of icons used by the gui applications.
Definition: GUIIcons.h:35
OutputDevice::openTag
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Definition: OutputDevice.cpp:239
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
GUIPropertyScheme::myName
std::string myName
Definition: GUIPropertyScheme.h:231
GUIPropertyScheme::myBgColor
RGBColor myBgColor
Definition: GUIPropertyScheme.h:239
GUIPropertyScheme::getName
const std::string & getName() const
Definition: GUIPropertyScheme.h:137
GUIPropertyScheme::setAllowsNegativeValues
void setAllowsNegativeValues(bool value)
Definition: GUIPropertyScheme.h:165
GUIPropertyScheme::addColor
int addColor(const T &color, const double threshold, const std::string &name="")
Definition: GUIPropertyScheme.h:80
GUIPropertyScheme::getIcon
GUIIcon getIcon() const
Definition: GUIPropertyScheme.h:169
GUIPropertyScheme::getNames
const std::vector< std::string > & getNames() const
Definition: GUIPropertyScheme.h:153
config.h
SUMO_ATTR_NAME
Definition: SUMOXMLDefinitions.h:380
GUIPropertyScheme::myThresholds
std::vector< double > myThresholds
Definition: GUIPropertyScheme.h:233
GUIPropertyScheme::setThreshold
void setThreshold(const int pos, const double threshold)
Definition: GUIPropertyScheme.h:60
GUIPropertyScheme::isInterpolated
bool isInterpolated() const
Definition: GUIPropertyScheme.h:149
GUIPropertyScheme::getColor
const T getColor(const double value) const
Definition: GUIPropertyScheme.h:110
GUIPropertyScheme::save
void save(OutputDevice &dev) const
Definition: GUIPropertyScheme.h:177
GUIPropertyScheme::getTagName
std::string getTagName(std::vector< double >) const
Definition: GUIPropertyScheme.h:225
GUIPropertyScheme
Definition: GUIPropertyScheme.h:45
GUIPropertyScheme::getBackgroundColor
const RGBColor & getBackgroundColor() const
Definition: GUIPropertyScheme.h:173