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  : myName(name), myActiveCol(col), myInactiveCol(col),
46  myMin(0), myMax(0),
47  myAggregationInterval((int)(TIME2STEPS(1) / DELTA_T)), myInvalidValue(-1), myValidNo(0),
48  myRecordingBegin(recordBegin), myTmpLastAggValue(0) {}
49 
50 
52  // just to quit cleanly on a failure
53  if (myLock.locked()) {
54  myLock.unlock();
55  }
56 }
57 
58 
59 void
61  if (myValues.size() == 0) {
62  myMin = value;
63  myMax = value;
64  } else {
65  myMin = value < myMin ? value : myMin;
66  myMax = value > myMax ? value : myMax;
67  }
69  myValues.push_back(value);
70  if (value != myInvalidValue) {
71  myTmpLastAggValue += value;
72  myValidNo++;
73  }
74  const double avg = myValidNo == 0 ? static_cast<double>(0) : myTmpLastAggValue / static_cast<double>(myValidNo);
75  if (myAggregationInterval == 1 || myValues.size() % myAggregationInterval == 1) {
76  myAggregatedValues.push_back(avg);
77  } else {
78  myAggregatedValues.back() = avg;
79  }
80  if (myValues.size() % myAggregationInterval == 0) {
82  myValidNo = 0;
83  }
84 }
85 
86 
87 double
89  return myMax - myMin;
90 }
91 
92 
93 double
95  return myMin;
96 }
97 
98 
99 double
101  return myMax;
102 }
103 
104 
105 double
107  return (myMin + myMax) / 2.0f;
108 }
109 
110 
111 const RGBColor&
113  return myActiveCol;
114 }
115 
116 
117 const std::vector<double>&
119  myLock.lock();
120  return myValues;
121 }
122 
123 
124 const std::vector<double>&
126  myLock.lock();
127  return myAggregatedValues;
128 }
129 
130 
131 const std::string&
133  return myName;
134 }
135 
136 void
138  myLock.unlock();
139 }
140 
141 
142 void
145  if (myAggregationInterval != as / DELTA_T) {
146  myAggregationInterval = (int)(as / DELTA_T);
147  // ok, the aggregation has changed,
148  // let's recompute the list of aggregated values
149  myAggregatedValues.clear();
150  std::vector<double>::const_iterator i = myValues.begin();
151  while (i != myValues.end()) {
152  myTmpLastAggValue = 0;
153  myValidNo = 0;
154  for (int j = 0; j < myAggregationInterval && i != myValues.end(); j++, ++i) {
155  if ((*i) != myInvalidValue) {
156  myTmpLastAggValue += (*i);
157  myValidNo++;
158  }
159  }
160  if (myValidNo == 0) {
161  myAggregatedValues.push_back(0);
162  } else {
163  myAggregatedValues.push_back(myTmpLastAggValue / static_cast<double>(myValidNo));
164  }
165  }
166  }
167 }
168 
169 
170 SUMOTime
173 }
174 
175 
176 SUMOTime
178  return myRecordingBegin;
179 }
180 
181 
182 
183 /****************************************************************************/
184 
double getYCenter() const
Returns the center of the value.
TrackerValueDesc(const std::string &name, const RGBColor &col, SUMOTime recordBegin)
Constructor.
int myAggregationInterval
The aggregation interval in simulation steps.
void unlockValues()
Releases the locking after the values have been drawn.
SUMOTime DELTA_T
Definition: SUMOTime.cpp:40
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:92
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: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:82
~TrackerValueDesc()
Destructor.
const RGBColor & getColor() const
Returns the color to use to display the value.
long long int SUMOTime
Definition: TraCIDefs.h:52
FXbool locked()
Definition: MFXMutex.h:70
double getMin() const
Returns the values minimum.
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