Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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-2023 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>
30
31
32// ===========================================================================
33// class definitions
34// ===========================================================================
42template<class T>
44public:
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) :
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 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 {
161 }
162
163 void setAllowsNegativeValues(bool value) {
164 myAllowNegativeValues = value;
165 }
166
167 GUIIcon getIcon() const {
168 return myIcon;
169 }
170
172 return myBgColor;
173 }
174
175 void save(OutputDevice& dev, const std::string& prefix = "") const {
176 const int precision = dev.getPrecision();
177 const bool checkPrecision = precision <= 2; // 2 is default precision (see SystemFrame)
178 const std::string tag = getTagName(myColors);
179
180 dev.openTag(tag);
181 dev.writeAttr(SUMO_ATTR_NAME, prefix + myName);
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()) {
190 dev.writeAttr(SUMO_ATTR_COLOR, *colIt);
191 if (!myIsFixed && (*threshIt) != std::numeric_limits<double>::max()) {
192 const double t = *threshIt;
193 if (checkPrecision && t != 0 && fabs(t) < 0.01) {
194 dev.setPrecision(8);
195 }
197 dev.setPrecision(precision);
198 }
199 if ((*nameIt) != "") {
200 dev.writeAttr(SUMO_ATTR_NAME, *nameIt);
201 }
202 dev.closeTag();
203 ++threshIt;
204 ++colIt;
205 ++nameIt;
206 }
207 dev.closeTag();
208 }
209
210 bool operator==(const GUIPropertyScheme& c) const {
212 }
213
214
216 RGBColor interpolate(const RGBColor& min, const RGBColor& max, double weight) const {
217 return RGBColor::interpolate(min, max, weight);
218 }
219
220 std::string getTagName(std::vector<RGBColor>) const {
222 }
223
224
226 double interpolate(const double& min, const double& max, double weight) const {
227 return min + (max - min) * weight;
228 }
229
230 std::string getTagName(std::vector<double>) const {
232 }
233
234
235private:
236 std::string myName;
237 std::vector<T> myColors;
238 std::vector<double> myThresholds;
240 std::vector<std::string> myNames;
245
246};
247
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
std::string getTagName(std::vector< double >) const
GUIIcon getIcon() const
T getColor(const double value) const
void setAllowsNegativeValues(bool value)
const std::vector< double > & getThresholds() const
std::string getTagName(std::vector< RGBColor >) const
const RGBColor & getBackgroundColor() 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)
const std::vector< std::string > & getNames() const
void removeColor(const int pos)
std::vector< std::string > myNames
void save(OutputDevice &dev, const std::string &prefix="") const
const std::string & getName() const
const std::vector< T > & getColors() 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)
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.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
int getPrecision()
Returns the precision of the underlying stream.
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