Gazebo Math
API Reference
8.1.0
insert_drive_file
Tutorials
library_books
Classes
toc
Namespaces
insert_drive_file
Files
launch
Gazebo Website
Index
List
Hierarchy
Members: All
Members: Functions
Members: Variables
Members: Typedefs
Members: Enumerations
Members: Enumerator
List
Members
Functions
Typedefs
Variables
Enumerations
Enumerator
src
gz-math
include
gz
math
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
29
namespace
gz::math
30
{
31
// Inline bracket to help doxygen filtering.
32
inline
namespace
GZ_MATH_VERSION_NAMESPACE {
37
template
<
typename
T>
38
class
MovingWindowFilter
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
72
GZ_UTILS_WARN_IGNORE__DLL_INTERFACE_MISSING
74
public
:
std::vector<T>
valHistory
;
75
77
public
:
typename
std::vector<T>::iterator
valIter
;
78
80
public
: T
sum
;
81
GZ_UTILS_WARN_RESUME__DLL_INTERFACE_MISSING
82
};
83
84
using
MovingWindowFilteri
=
MovingWindowFilter<int>
;
85
using
MovingWindowFilterf
=
MovingWindowFilter<float>
;
86
using
MovingWindowFilterd
=
MovingWindowFilter<double>
;
87
using
MovingWindowFilterVector3i
=
MovingWindowFilter<Vector3i>
;
88
using
MovingWindowFilterVector3f
=
MovingWindowFilter<Vector3f>
;
89
using
MovingWindowFilterVector3d
=
MovingWindowFilter<Vector3d>
;
90
}
// namespace GZ_MATH_VERSION_NAMESPACE
91
93
template
<
typename
T>
94
MovingWindowFilter<T>::MovingWindowFilter
(
unsigned
int
_windowSize
)
95
{
96
this->SetWindowSize(
_windowSize
);
97
}
98
100
template
<
typename
T>
101
void
MovingWindowFilter<T>::Update
(
const
T
_val
)
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>
137
void
MovingWindowFilter<T>::SetWindowSize
(
const
unsigned
int
_n
)
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>
148
unsigned
int
MovingWindowFilter<T>::WindowSize
()
const
149
{
150
return
this->valWindowSize;
151
}
152
154
template
<
typename
T>
155
bool
MovingWindowFilter<T>::WindowFilled
()
const
156
{
157
return
this->samples == this->valWindowSize;
158
}
159
161
template
<>
162
inline
gz::math::Vector3i
163
MovingWindowFilter<gz::math::Vector3i>::Value
()
const
164
{
165
auto
value = this->sum / this->samples;
166
return
value;
167
}
168
170
template
<>
171
inline
gz::math::Vector3f
172
MovingWindowFilter<gz::math::Vector3f>::Value
()
const
173
{
174
gz::math::Vector3f
divisor
;
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
183
MovingWindowFilter<gz::math::Vector3d>::Value
()
const
184
{
185
auto
value = this->sum / this->samples;
186
return
value;
187
}
188
190
template
<
typename
T>
191
T
MovingWindowFilter<T>::Value
()
const
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_