SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-sim.org/
13 // Copyright (C) 2001-2013 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 #ifdef CHECK_MEMORY_LEAKS
39 #include <foreign/nvwa/debug_new.h>
40 #endif // CHECK_MEMORY_LEAKS
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 TrackerValueDesc::TrackerValueDesc(const std::string& name,
47  const RGBColor& col,
48  SUMOTime recordBegin)
49  : myName(name), myActiveCol(col), myInactiveCol(col),
50  myMin(0), myMax(0),
51  myAggregationInterval(TIME2STEPS(1) / DELTA_T), myInvalidValue(-1), myValidNo(0),
52  myRecordingBegin(recordBegin), myTmpLastAggValue(0) {}
53 
54 
56  // just to quit cleanly on a failure
57  if (myLock.locked()) {
58  myLock.unlock();
59  }
60 }
61 
62 
63 void
65  if (myValues.size() == 0) {
66  myMin = value;
67  myMax = value;
68  } else {
69  myMin = value < myMin ? value : myMin;
70  myMax = value > myMax ? value : myMax;
71  }
73  myValues.push_back(value);
74  if (value != myInvalidValue) {
75  myTmpLastAggValue += value;
76  myValidNo++;
77  }
78  const SUMOReal avg = myValidNo == 0 ? static_cast<SUMOReal>(0) : myTmpLastAggValue / static_cast<SUMOReal>(myValidNo);
79  if (myAggregationInterval == 1 || myValues.size() % myAggregationInterval == 1) {
80  myAggregatedValues.push_back(avg);
81  } else {
82  myAggregatedValues.back() = avg;
83  }
84  if (myValues.size() % myAggregationInterval == 0) {
86  myValidNo = 0;
87  }
88 }
89 
90 
93  return myMax - myMin;
94 }
95 
96 
99  return myMin;
100 }
101 
102 
103 SUMOReal
105  return myMax;
106 }
107 
108 
109 SUMOReal
111  return (myMin + myMax) / 2.0f;
112 }
113 
114 
115 const RGBColor&
117  return myActiveCol;
118 }
119 
120 
121 const std::vector<SUMOReal>&
123  myLock.lock();
124  return myValues;
125 }
126 
127 
128 const std::vector<SUMOReal>&
130  myLock.lock();
131  return myAggregatedValues;
132 }
133 
134 
135 const std::string&
137  return myName;
138 }
139 
140 void
142  myLock.unlock();
143 }
144 
145 
146 void
149  if (myAggregationInterval != as / DELTA_T) {
151  // ok, the aggregation has changed,
152  // let's recompute the list of aggregated values
153  myAggregatedValues.clear();
154  std::vector<SUMOReal>::const_iterator i = myValues.begin();
155  while (i != myValues.end()) {
156  myTmpLastAggValue = 0;
157  myValidNo = 0;
158  for (int j = 0; j < myAggregationInterval && i != myValues.end(); j++, ++i) {
159  if ((*i) != myInvalidValue) {
160  myTmpLastAggValue += (*i);
161  myValidNo++;
162  }
163  }
164  if (myValidNo == 0) {
165  myAggregatedValues.push_back(0);
166  } else {
167  myAggregatedValues.push_back(myTmpLastAggValue / static_cast<SUMOReal>(myValidNo));
168  }
169  }
170  }
171 }
172 
173 
174 SUMOTime
177 }
178 
179 
180 SUMOTime
182  return myRecordingBegin;
183 }
184 
185 
186 
187 /****************************************************************************/
188 
SUMOReal myTmpLastAggValue
Temporary storage for the last aggregation interval.
SUMOReal getYCenter() const
Returns the center of the value.
TrackerValueDesc(const std::string &name, const RGBColor &col, SUMOTime recordBegin)
Constructor.
const std::vector< SUMOReal > & getValues()
returns the vector of collected values The values will be locked - no further addition will be perfom...
int myAggregationInterval
The aggregation interval in simulation steps.
void unlockValues()
Releases the locking after the values have been drawn.
SUMOReal getMin() const
Returns the values minimum.
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
size_t myValidNo
Counter for valid numbers within the current aggregation interval.
const std::vector< SUMOReal > & getAggregatedValues()
returns the vector of aggregated values The values will be locked - no further addition will be perfo...
SUMOTime myRecordingBegin
The time step the values are added from.
SUMOReal getMax() const
Returns the values maximum.
SUMOReal getRange() const
returns the maximum value range
RGBColor myActiveCol
The color to use when the value is set as &quot;active&quot;.
SUMOTime getRecordingBegin() const
Returns the timestep the recording started.
std::vector< SUMOReal > myValues
Values collected.
void unlock()
release mutex lock
Definition: MFXMutex.cpp:93
SUMOTime getAggregationSpan() const
get the aggregation amount
std::vector< SUMOReal > myAggregatedValues
Collected values in their aggregated form.
A mutex encapsulator which locks/unlocks the given mutex on construction/destruction, respectively.
Definition: AbstractMutex.h:71
int SUMOTime
Definition: SUMOTime.h:43
void lock()
lock mutex
Definition: MFXMutex.cpp:83
const std::string & getName() const
Returns the name of the value.
~TrackerValueDesc()
Destructor.
#define SUMOReal
Definition: config.h:215
SUMOReal myInvalidValue
Values like this shall not be counted on aggregation.
FXbool locked()
Definition: MFXMutex.h:68
#define DELTA_T
Definition: SUMOTime.h:50
void addValue(SUMOReal value)
Adds a new value to the list.
SUMOReal myMin
The minimum and the maximum of the value.
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