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