Gazebo Math

API Reference

8.0.0
MovingWindowFilter.hh
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 Open Source Robotics Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16*/
17#ifndef GZ_MATH_MOVINGWINDOWFILTER_HH_
18#define GZ_MATH_MOVINGWINDOWFILTER_HH_
19
20#include <memory>
21#include <vector>
22
23#include <gz/math/config.hh>
24#include <gz/math/Export.hh>
25#include <gz/math/Vector3.hh>
26
27#include <gz/utils/SuppressWarning.hh>
28
29namespace gz::math
30{
31 // Inline bracket to help doxygen filtering.
32 inline namespace GZ_MATH_VERSION_NAMESPACE {
37 template< typename T>
39 {
41 public: MovingWindowFilter(unsigned int _windowSize = 4);
42
44 public: virtual ~MovingWindowFilter() = default;
45
48 public: void Update(const T _val);
49
52 public: void SetWindowSize(const unsigned int _n);
53
56 public: unsigned int WindowSize() const;
57
60 public: bool WindowFilled() const;
61
64 public: T Value() const;
65
67 public: unsigned int valWindowSize = 4;
68
70 public: unsigned int samples = 0;
71
75
78
80 public: T sum;
82 };
83
90 } // namespace GZ_MATH_VERSION_NAMESPACE
91
93 template<typename T>
95 {
96 this->SetWindowSize(_windowSize);
97 }
98
100 template<typename T>
102 {
103 // update sum and sample size with incoming _val
104
105 // keep running sum
106 this->sum += _val;
107
108 // shift pointer, wrap around if end has been reached.
109 ++this->valIter;
110 if (this->valIter == this->valHistory.end())
111 {
112 // reset iterator to beginning of queue
113 this->valIter = this->valHistory.begin();
114 }
115
116 // increment sample size
117 ++this->samples;
118
119 if (this->samples > this->valWindowSize)
120 {
121 // subtract old value if buffer already filled
122 this->sum -= (*this->valIter);
123 // put new value into queue
124 (*this->valIter) = _val;
125 // reduce sample size
126 --this->samples;
127 }
128 else
129 {
130 // put new value into queue
131 (*this->valIter) = _val;
132 }
133 }
134
136 template<typename T>
138 {
139 this->valWindowSize = _n;
140 this->valHistory = std::vector<T>(_n, T());
141 this->valIter = this->valHistory.begin();
142 this->sum = T();
143 this->samples = 0;
144 }
145
147 template<typename T>
149 {
150 return this->valWindowSize;
151 }
152
154 template<typename T>
156 {
157 return this->samples == this->valWindowSize;
158 }
159
161 template<>
162 inline gz::math::Vector3i
164 {
165 auto value = this->sum / this->samples;
166 return value;
167 }
168
170 template<>
171 inline gz::math::Vector3f
173 {
175 divisor = static_cast<float>(this->samples);
176 auto value = this->sum / divisor;
177 return value;
178 }
179
181 template<>
182 inline gz::math::Vector3d
184 {
185 auto value = this->sum / this->samples;
186 return value;
187 }
188
190 template<typename T>
192 {
193 if (std::is_integral_v<T>)
194 {
195 auto value = this->sum / this->samples;
196 return T(value);
197 }
198 else
199 {
200 auto value = this->sum / static_cast<double>(this->samples);
201 return T(value);
202 }
203 }
204} // namespace gz::math
205#endif // GZ_MATH_MOVINGWINDOWFILTER_HH_