Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/gui/settings/GUIPropertyScheme.h
169685 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 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
/****************************************************************************/
14
/// @file GUIPropertyScheme.h
15
/// @author Michael Behrisch
16
/// @author Daniel Krajzewicz
17
/// @author Jakob Erdmann
18
/// @date Mon, 20.07.2009
19
///
20
//
21
/****************************************************************************/
22
#pragma once
23
#include <config.h>
24
25
#include <cassert>
26
#include <vector>
27
#include <utils/common/RGBColor.h>
28
#include <utils/iodevices/OutputDevice.h>
29
#include <utils/gui/images/GUIIcons.h>
30
31
32
// ===========================================================================
33
// class definitions
34
// ===========================================================================
35
/**
36
* @class GUIPropertyScheme
37
* This class provides a mapping from real values to properties (mainly colors).
38
* Each color is stored along with a threshold value.
39
* Color values between thresholds are obtained by interpolation
40
*/
41
42
template<class T>
43
class GUIPropertyScheme {
44
public:
45
/// Constructor
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),
52
myIsInterpolated(!isFixed),
53
myIsFixed(isFixed),
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),
66
myIsInterpolated(!isFixed),
67
myIsFixed(isFixed),
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) {
145
myIsInterpolated = interpolate;
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) {
203
dev.writeAttr(SUMO_ATTR_INTERPOLATED, myIsInterpolated);
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
}
216
dev.writeAttr(SUMO_ATTR_THRESHOLD, t);
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 {
231
return myName == c.myName && myColors == c.myColors && myThresholds == c.myThresholds && myIsInterpolated == c.myIsInterpolated;
232
}
233
234
235
/// @brief specializations for GUIColorScheme
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 {
241
return toString(SUMO_TAG_COLORSCHEME);
242
}
243
244
245
/// @brief specializations for GUIScaleScheme
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 {
251
return toString(SUMO_TAG_SCALINGSCHEME);
252
}
253
254
255
private:
256
std::string myName;
257
std::string myTranslatedName;
258
std::vector<T> myColors;
259
std::vector<double> myThresholds;
260
bool myIsInterpolated;
261
std::vector<std::string> myNames;
262
bool myIsFixed;
263
bool myAllowNegativeValues;
264
GUIIcon myIcon;
265
RGBColor myBgColor;
266
267
};
268
269
typedef GUIPropertyScheme<RGBColor> GUIColorScheme;
270
typedef GUIPropertyScheme<double> GUIScaleScheme;
271
272