SUMO - Simulation of Urban MObility
TrackerValueDesc.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Storage for a tracked value
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <string>
33 #include <vector>
34 #include <utils/common/RGBColor.h>
36 #include "TrackerValueDesc.h"
37 
38 
39 // ===========================================================================
40 // method definitions
41 // ===========================================================================
42 TrackerValueDesc::TrackerValueDesc(const std::string& name,
43  const RGBColor& col,
44  SUMOTime recordBegin,
45  double aggregationSeconds)
46  : myName(name), myActiveCol(col), myInactiveCol(col),
47  myMin(0), myMax(0),
48  myAggregationInterval(MAX2(1, (int)(TIME2STEPS(aggregationSeconds) / DELTA_T))),
49  myInvalidValue(INVALID_DOUBLE),
50  myValidNo(0),
51  myRecordingBegin(recordBegin), myTmpLastAggValue(0) {}
52 
53 
55  // just to quit cleanly on a failure
56  if (myLock.locked()) {
57  myLock.unlock();
58  }
59 }
60 
61 
62 void
64  if (myValues.size() == 0) {
65  myMin = value;
66  myMax = value;
67  } else {
68  myMin = value < myMin ? value : myMin;
69  myMax = value > myMax ? value : myMax;
70  }
72  myValues.push_back(value);
73  if (value != myInvalidValue) {
74  myTmpLastAggValue += value;
75  myValidNo++;
76  }
77  const double avg = myValidNo == 0 ? static_cast<double>(0) : myTmpLastAggValue / static_cast<double>(myValidNo);
78  if (myAggregationInterval == 1 || myValues.size() % myAggregationInterval == 1) {
79  myAggregatedValues.push_back(avg);
80  } else {
81  myAggregatedValues.back() = avg;
82  }
83  if (myValues.size() % myAggregationInterval == 0) {
85  myValidNo = 0;
86  }
87 }
88 
89 
90 double
92  return myMax - myMin;
93 }
94 
95 
96 double
98  return myMin;
99 }
100 
101 
102 double
104  return myMax;
105 }
106 
107 
108 double
110  return (myMin + myMax) / 2.0f;
111 }
112 
113 
114 const RGBColor&
116  return myActiveCol;
117 }
118 
119 
120 const std::vector<double>&
122  myLock.lock();
123  return myValues;
124 }
125 
126 
127 const std::vector<double>&
129  myLock.lock();
130  return myAggregatedValues;
131 }
132 
133 
134 const std::string&
136  return myName;
137 }
138 
139 void
141  myLock.unlock();
142 }
143 
144 
145 void
148  if (myAggregationInterval != as / DELTA_T) {
149  myAggregationInterval = (int)(as / DELTA_T);
150  // ok, the aggregation has changed,
151  // let's recompute the list of aggregated values
152  myAggregatedValues.clear();
153  std::vector<double>::const_iterator i = myValues.begin();
154  while (i != myValues.end()) {
155  myTmpLastAggValue = 0;
156  myValidNo = 0;
157  for (int j = 0; j < myAggregationInterval && i != myValues.end(); j++, ++i) {
158  if ((*i) != myInvalidValue) {
159  myTmpLastAggValue += (*i);
160  myValidNo++;
161  }
162  }
163  if (myValidNo == 0) {
164  myAggregatedValues.push_back(0);
165  } else {
166  myAggregatedValues.push_back(myTmpLastAggValue / static_cast<double>(myValidNo));
167  }
168  }
169  }
170 }
171 
172 
173 SUMOTime
176 }
177 
178 
179 SUMOTime
181  return myRecordingBegin;
182 }
183 
184 
185 
186 /****************************************************************************/
187 
double getMax() const
Returns the values maximum.
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:74
SUMOTime DELTA_T
Definition: SUMOTime.cpp:40
double myInvalidValue
Values like this shall not be counted on aggregation.
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
double getYCenter() const
Returns the center of the value.
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.
SUMOTime getRecordingBegin() const
Returns the timestep the recording started.
double myMin
The minimum and the maximum of the value.
void unlock()
release mutex lock
Definition: MFXMutex.cpp:94
double getRange() const
returns the maximum value range
SUMOTime getAggregationSpan() const
get the aggregation amount
A mutex encapsulator which locks/unlocks the given mutex on construction/destruction, respectively.
Definition: AbstractMutex.h:71
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:84
const std::string & getName() const
Returns the name of the value.
~TrackerValueDesc()
Destructor.
long long int SUMOTime
Definition: TraCIDefs.h:52
FXbool locked()
Definition: MFXMutex.h:70
const double INVALID_DOUBLE
Definition: StdDefs.h:60
void addValue(double value)
Adds a new value to the list.
double getMin() const
Returns the values minimum.
std::string myName
The name of the value.
const RGBColor & getColor() const
Returns the color to use to display the value.
void setAggregationSpan(SUMOTime as)
set the aggregation amount