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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // Storage for a tracked value
21 /****************************************************************************/
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <string>
32 #include <vector>
33 #include <utils/common/RGBColor.h>
35 #include "TrackerValueDesc.h"
36 
37 
38 // ===========================================================================
39 // method definitions
40 // ===========================================================================
41 TrackerValueDesc::TrackerValueDesc(const std::string& name,
42  const RGBColor& col,
43  SUMOTime recordBegin,
44  double aggregationSeconds)
45  : myName(name), myActiveCol(col), myInactiveCol(col),
46  myMin(0), myMax(0),
47  myAggregationInterval(MAX2(1, (int)(TIME2STEPS(aggregationSeconds) / DELTA_T))),
48  myInvalidValue(INVALID_DOUBLE),
49  myValidNo(0),
50  myRecordingBegin(recordBegin), myTmpLastAggValue(0) {}
51 
52 
54  // just to quit cleanly on a failure
55  if (myLock.locked()) {
56  myLock.unlock();
57  }
58 }
59 
60 
61 void
63  if (myValues.size() == 0) {
64  myMin = value;
65  myMax = value;
66  } else {
67  myMin = value < myMin ? value : myMin;
68  myMax = value > myMax ? value : myMax;
69  }
71  myValues.push_back(value);
72  if (value != myInvalidValue) {
73  myTmpLastAggValue += value;
74  myValidNo++;
75  }
76  const double avg = myValidNo == 0 ? static_cast<double>(0) : myTmpLastAggValue / static_cast<double>(myValidNo);
77  if (myAggregationInterval == 1 || myValues.size() % myAggregationInterval == 1) {
78  myAggregatedValues.push_back(avg);
79  } else {
80  myAggregatedValues.back() = avg;
81  }
82  if (myValues.size() % myAggregationInterval == 0) {
84  myValidNo = 0;
85  }
86 }
87 
88 
89 double
91  return myMax - myMin;
92 }
93 
94 
95 double
97  return myMin;
98 }
99 
100 
101 double
103  return myMax;
104 }
105 
106 
107 double
109  return (myMin + myMax) / 2.0f;
110 }
111 
112 
113 const RGBColor&
115  return myActiveCol;
116 }
117 
118 
119 const std::vector<double>&
121  myLock.lock();
122  return myValues;
123 }
124 
125 
126 const std::vector<double>&
128  myLock.lock();
129  return myAggregatedValues;
130 }
131 
132 
133 const std::string&
135  return myName;
136 }
137 
138 void
140  myLock.unlock();
141 }
142 
143 
144 void
147  if (myAggregationInterval != as / DELTA_T) {
148  myAggregationInterval = (int)(as / DELTA_T);
149  // ok, the aggregation has changed,
150  // let's recompute the list of aggregated values
151  myAggregatedValues.clear();
152  std::vector<double>::const_iterator i = myValues.begin();
153  while (i != myValues.end()) {
154  myTmpLastAggValue = 0;
155  myValidNo = 0;
156  for (int j = 0; j < myAggregationInterval && i != myValues.end(); j++, ++i) {
157  if ((*i) != myInvalidValue) {
158  myTmpLastAggValue += (*i);
159  myValidNo++;
160  }
161  }
162  if (myValidNo == 0) {
163  myAggregatedValues.push_back(0);
164  } else {
165  myAggregatedValues.push_back(myTmpLastAggValue / static_cast<double>(myValidNo));
166  }
167  }
168  }
169 }
170 
171 
172 SUMOTime
175 }
176 
177 
178 SUMOTime
180  return myRecordingBegin;
181 }
182 
183 
184 
185 /****************************************************************************/
186 
double getYCenter() const
Returns the center of the value.
TrackerValueDesc(const std::string &name, const RGBColor &col, SUMOTime recordBegin, double aggregationSeconds)
Constructor.
int myAggregationInterval
The aggregation interval in simulation steps.
void unlockValues()
Releases the locking after the values have been drawn.
T MAX2(T a, T b)
Definition: StdDefs.h:73
SUMOTime DELTA_T
Definition: SUMOTime.cpp:39
SUMOTime getRecordingBegin() const
Returns the timestep the recording started.
double myInvalidValue
Values like this shall not be counted on aggregation.
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
SUMOTime getAggregationSpan() const
get the aggregation amount
SUMOTime myRecordingBegin
The time step the values are added from.
std::vector< double > myAggregatedValues
Collected values in their aggregated form.
std::vector< double > myValues
Values collected.
const std::vector< double > & getAggregatedValues()
returns the vector of aggregated values The values will be locked - no further addition will be perfo...
RGBColor myActiveCol
The color to use when the value is set as "active".
int myValidNo
Counter for valid numbers within the current aggregation interval.
double myMin
The minimum and the maximum of the value.
void unlock()
release mutex lock
Definition: MFXMutex.cpp:93
const std::string & getName() const
Returns the name of the value.
double getMax() const
Returns the values maximum.
A mutex encapsulator which locks/unlocks the given mutex on construction/destruction, respectively.
Definition: AbstractMutex.h:70
const std::vector< double > & getValues()
returns the vector of collected values The values will be locked - no further addition will be perfom...
double myTmpLastAggValue
Temporary storage for the last aggregation interval.
void lock()
lock mutex
Definition: MFXMutex.cpp:83
~TrackerValueDesc()
Destructor.
const RGBColor & getColor() const
Returns the color to use to display the value.
long long int SUMOTime
Definition: TraCIDefs.h:51
FXbool locked()
Definition: MFXMutex.h:69
double getMin() const
Returns the values minimum.
const double INVALID_DOUBLE
Definition: StdDefs.h:59
void addValue(double value)
Adds a new value to the list.
std::string myName
The name of the value.
double getRange() const
returns the maximum value range
void setAggregationSpan(SUMOTime as)
set the aggregation amount