Eclipse SUMO - Simulation of Urban MObility
TrackerValueDesc.cpp
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 /****************************************************************************/
17 // Storage for a tracked value
18 /****************************************************************************/
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <string>
25 #include <vector>
26 #include <utils/common/RGBColor.h>
28 #include "TrackerValueDesc.h"
29 
30 
31 // ===========================================================================
32 // method definitions
33 // ===========================================================================
34 TrackerValueDesc::TrackerValueDesc(const std::string& name,
35  const RGBColor& col,
36  SUMOTime recordBegin,
37  double aggregationSeconds)
38  : myName(name), myActiveCol(col), myInactiveCol(col),
39  myMin(0), myMax(0),
40  myAggregationInterval(MAX2(1, (int)(TIME2STEPS(aggregationSeconds) / DELTA_T))),
41  myInvalidValue(INVALID_DOUBLE),
42  myValidNo(0),
43  myRecordingBegin(recordBegin), myTmpLastAggValue(0) {}
44 
45 
47  // just to quit cleanly on a failure
48  if (myLock.locked()) {
49  myLock.unlock();
50  }
51 }
52 
53 
54 void
56  if (myValues.size() == 0) {
57  myMin = value;
58  myMax = value;
59  } else {
60  myMin = value < myMin ? value : myMin;
61  myMax = value > myMax ? value : myMax;
62  }
63  FXMutexLock locker(myLock);
64  myValues.push_back(value);
65  if (value != myInvalidValue) {
66  myTmpLastAggValue += value;
67  myValidNo++;
68  }
69  const double avg = myValidNo == 0 ? static_cast<double>(0) : myTmpLastAggValue / static_cast<double>(myValidNo);
70  if (myAggregationInterval == 1 || myValues.size() % myAggregationInterval == 1) {
71  myAggregatedValues.push_back(avg);
72  } else {
73  myAggregatedValues.back() = avg;
74  }
75  if (myValues.size() % myAggregationInterval == 0) {
77  myValidNo = 0;
78  }
79 }
80 
81 
82 double
84  return myMax - myMin;
85 }
86 
87 
88 double
90  return myMin;
91 }
92 
93 
94 double
96  return myMax;
97 }
98 
99 
100 double
102  return (myMin + myMax) / 2.0f;
103 }
104 
105 
106 const RGBColor&
108  return myActiveCol;
109 }
110 
111 
112 const std::vector<double>&
114  myLock.lock();
115  return myValues;
116 }
117 
118 
119 const std::vector<double>&
121  myLock.lock();
122  return myAggregatedValues;
123 }
124 
125 
126 const std::string&
128  return myName;
129 }
130 
131 void
133  myLock.unlock();
134 }
135 
136 
137 void
139  FXMutexLock locker(myLock);
140  if (myAggregationInterval != as / DELTA_T) {
141  myAggregationInterval = (int)(as / DELTA_T);
142  // ok, the aggregation has changed,
143  // let's recompute the list of aggregated values
144  myAggregatedValues.clear();
145  std::vector<double>::const_iterator i = myValues.begin();
146  while (i != myValues.end()) {
147  myTmpLastAggValue = 0;
148  myValidNo = 0;
149  for (int j = 0; j < myAggregationInterval && i != myValues.end(); j++, ++i) {
150  if ((*i) != myInvalidValue) {
151  myTmpLastAggValue += (*i);
152  myValidNo++;
153  }
154  }
155  if (myValidNo == 0) {
156  myAggregatedValues.push_back(0);
157  } else {
158  myAggregatedValues.push_back(myTmpLastAggValue / static_cast<double>(myValidNo));
159  }
160  }
161  }
162 }
163 
164 
165 SUMOTime
168 }
169 
170 
171 SUMOTime
173  return myRecordingBegin;
174 }
175 
176 
177 
178 /****************************************************************************/
179 
GUIGlObject.h
TrackerValueDesc::myValues
std::vector< double > myValues
Values collected.
Definition: TrackerValueDesc.h:107
TrackerValueDesc::getMin
double getMin() const
Returns the values minimum.
Definition: TrackerValueDesc.cpp:89
DELTA_T
SUMOTime DELTA_T
Definition: SUMOTime.cpp:36
TrackerValueDesc::myValidNo
int myValidNo
Counter for valid numbers within the current aggregation interval.
Definition: TrackerValueDesc.h:125
TrackerValueDesc::getColor
const RGBColor & getColor() const
Returns the color to use to display the value.
Definition: TrackerValueDesc.cpp:107
TrackerValueDesc::myName
std::string myName
The name of the value.
Definition: TrackerValueDesc.h:98
TrackerValueDesc::addValue
void addValue(double value)
Adds a new value to the list.
Definition: TrackerValueDesc.cpp:55
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
TrackerValueDesc::getMax
double getMax() const
Returns the values maximum.
Definition: TrackerValueDesc.cpp:95
TrackerValueDesc::myLock
FXMutex myLock
Definition: TrackerValueDesc.h:116
TrackerValueDesc::unlockValues
void unlockValues()
Releases the locking after the values have been drawn.
Definition: TrackerValueDesc.cpp:132
TrackerValueDesc::~TrackerValueDesc
~TrackerValueDesc()
Destructor.
Definition: TrackerValueDesc.cpp:46
RGBColor.h
TrackerValueDesc::getYCenter
double getYCenter() const
Returns the center of the value.
Definition: TrackerValueDesc.cpp:101
MAX2
T MAX2(T a, T b)
Definition: StdDefs.h:79
RGBColor
Definition: RGBColor.h:39
TrackerValueDesc::setAggregationSpan
void setAggregationSpan(SUMOTime as)
set the aggregation amount
Definition: TrackerValueDesc.cpp:138
TIME2STEPS
#define TIME2STEPS(x)
Definition: SUMOTime.h:58
TrackerValueDesc::getValues
const std::vector< double > & getValues()
returns the vector of collected values The values will be locked - no further addition will be perfom...
Definition: TrackerValueDesc.cpp:113
TrackerValueDesc::myTmpLastAggValue
double myTmpLastAggValue
Temporary storage for the last aggregation interval.
Definition: TrackerValueDesc.h:131
TrackerValueDesc::getName
const std::string & getName() const
Returns the name of the value.
Definition: TrackerValueDesc.cpp:127
TrackerValueDesc::myMax
double myMax
Definition: TrackerValueDesc.h:113
TrackerValueDesc::myAggregationInterval
int myAggregationInterval
The aggregation interval in simulation steps.
Definition: TrackerValueDesc.h:119
TrackerValueDesc::myAggregatedValues
std::vector< double > myAggregatedValues
Collected values in their aggregated form.
Definition: TrackerValueDesc.h:110
TrackerValueDesc::myMin
double myMin
The minimum and the maximum of the value.
Definition: TrackerValueDesc.h:113
TrackerValueDesc.h
TrackerValueDesc::myRecordingBegin
SUMOTime myRecordingBegin
The time step the values are added from.
Definition: TrackerValueDesc.h:128
INVALID_DOUBLE
const double INVALID_DOUBLE
Definition: StdDefs.h:62
TrackerValueDesc::myActiveCol
RGBColor myActiveCol
The color to use when the value is set as "active".
Definition: TrackerValueDesc.h:101
config.h
TrackerValueDesc::getRange
double getRange() const
returns the maximum value range
Definition: TrackerValueDesc.cpp:83
TrackerValueDesc::getAggregatedValues
const std::vector< double > & getAggregatedValues()
returns the vector of aggregated values The values will be locked - no further addition will be perfo...
Definition: TrackerValueDesc.cpp:120
TrackerValueDesc::getRecordingBegin
SUMOTime getRecordingBegin() const
Returns the timestep the recording started.
Definition: TrackerValueDesc.cpp:172
TrackerValueDesc::myInvalidValue
double myInvalidValue
Values like this shall not be counted on aggregation.
Definition: TrackerValueDesc.h:122
TrackerValueDesc::getAggregationSpan
SUMOTime getAggregationSpan() const
get the aggregation amount
Definition: TrackerValueDesc.cpp:166
TrackerValueDesc::TrackerValueDesc
TrackerValueDesc(const std::string &name, const RGBColor &col, SUMOTime recordBegin, double aggregationSeconds)
Constructor.
Definition: TrackerValueDesc.cpp:34