From 55fbcc693e89f5375a23e025da939ef66c1437a0 Mon Sep 17 00:00:00 2001 From: Jose Luis Rivero Date: Wed, 1 Nov 2023 18:27:13 +0100 Subject: [PATCH 1/4] Use single header for MovingWindowFilter Signed-off-by: Jose Luis Rivero --- include/gz/math/MovingWindowFilter.hh | 123 ++++++++++++++++++++- src/MovingWindowFilter.cc | 149 -------------------------- 2 files changed, 122 insertions(+), 150 deletions(-) delete mode 100644 src/MovingWindowFilter.cc diff --git a/include/gz/math/MovingWindowFilter.hh b/include/gz/math/MovingWindowFilter.hh index 3a24f4cf7..40809e0dd 100644 --- a/include/gz/math/MovingWindowFilter.hh +++ b/include/gz/math/MovingWindowFilter.hh @@ -37,7 +37,7 @@ namespace gz /// /// The default window size is 4. template< typename T> - class GZ_MATH_VISIBLE MovingWindowFilter + class MovingWindowFilter { /// \brief Constructor public: MovingWindowFilter(unsigned int _windowSize = 4); @@ -90,6 +90,127 @@ namespace gz using MovingWindowFilterVector3f = MovingWindowFilter; using MovingWindowFilterVector3d = MovingWindowFilter; } + +////////////////////////////////////////////////// +template +MovingWindowFilter::MovingWindowFilter(unsigned int _windowSize) +{ + this->SetWindowSize(_windowSize); +} + +////////////////////////////////////////////////// +template +void MovingWindowFilter::Update(const T _val) +{ + // update sum and sample size with incoming _val + + // keep running sum + this->sum += _val; + + // shift pointer, wrap around if end has been reached. + ++this->valIter; + if (this->valIter == this->valHistory.end()) + { + // reset iterator to beginning of queue + this->valIter = this->valHistory.begin(); + } + + // increment sample size + ++this->samples; + + if (this->samples > this->valWindowSize) + { + // subtract old value if buffer already filled + this->sum -= (*this->valIter); + // put new value into queue + (*this->valIter) = _val; + // reduce sample size + --this->samples; + } + else + { + // put new value into queue + (*this->valIter) = _val; + } +} + +////////////////////////////////////////////////// +template +void MovingWindowFilter::SetWindowSize(const unsigned int _n) +{ + this->valWindowSize = _n; + this->valHistory = std::vector(_n, T()); + this->valIter = this->valHistory.begin(); + this->sum = T(); + this->samples = 0; +} + +////////////////////////////////////////////////// +template +unsigned int MovingWindowFilter::WindowSize() const +{ + return this->valWindowSize; +} + +////////////////////////////////////////////////// +template +bool MovingWindowFilter::WindowFilled() const +{ + return this->samples == this->valWindowSize; +} + +////////////////////////////////////////////////// +template<> +inline gz::math::Vector3i +MovingWindowFilter::Value() const +{ + auto value = this->sum / this->samples; + return value; +} + +////////////////////////////////////////////////// +template<> +inline gz::math::Vector3f +MovingWindowFilter::Value() const +{ + gz::math::Vector3f divisor; + divisor = static_cast(this->samples); + auto value = this->sum / divisor; + return value; +} + +////////////////////////////////////////////////// +template<> +inline gz::math::Vector3d +MovingWindowFilter::Value() const +{ + auto value = this->sum / this->samples; + return value; +} + +////////////////////////////////////////////////// +template +T MovingWindowFilter::Value() const +{ + if (std::is_integral_v) + { + auto value = this->sum / this->samples; + return T(value); + } + else + { + auto value = this->sum / static_cast(this->samples); + return T(value); + } +} + +template class MovingWindowFilter; +template class MovingWindowFilter; +template class MovingWindowFilter; +template class MovingWindowFilter; +template class MovingWindowFilter; +template class MovingWindowFilter; + } // namespace math } // namespace gz #endif diff --git a/src/MovingWindowFilter.cc b/src/MovingWindowFilter.cc deleted file mode 100644 index b87240862..000000000 --- a/src/MovingWindowFilter.cc +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright (C) 2021 Open Source Robotics Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "gz/math/MovingWindowFilter.hh" -#include "gz/math/Vector3.hh" - -namespace gz -{ -namespace math -{ -inline namespace GZ_MATH_VERSION_NAMESPACE { - -////////////////////////////////////////////////// -template -MovingWindowFilter::MovingWindowFilter(unsigned int _windowSize) -{ - this->SetWindowSize(_windowSize); -} - -////////////////////////////////////////////////// -template -void MovingWindowFilter::Update(const T _val) -{ - // update sum and sample size with incoming _val - - // keep running sum - this->sum += _val; - - // shift pointer, wrap around if end has been reached. - ++this->valIter; - if (this->valIter == this->valHistory.end()) - { - // reset iterator to beginning of queue - this->valIter = this->valHistory.begin(); - } - - // increment sample size - ++this->samples; - - if (this->samples > this->valWindowSize) - { - // subtract old value if buffer already filled - this->sum -= (*this->valIter); - // put new value into queue - (*this->valIter) = _val; - // reduce sample size - --this->samples; - } - else - { - // put new value into queue - (*this->valIter) = _val; - } -} - -////////////////////////////////////////////////// -template -void MovingWindowFilter::SetWindowSize(const unsigned int _n) -{ - this->valWindowSize = _n; - this->valHistory = std::vector(_n, T()); - this->valIter = this->valHistory.begin(); - this->sum = T(); - this->samples = 0; -} - -////////////////////////////////////////////////// -template -unsigned int MovingWindowFilter::WindowSize() const -{ - return this->valWindowSize; -} - -////////////////////////////////////////////////// -template -bool MovingWindowFilter::WindowFilled() const -{ - return this->samples == this->valWindowSize; -} - -////////////////////////////////////////////////// -template -T MovingWindowFilter::Value() const -{ - if (std::is_integral_v) - { - auto value = this->sum / this->samples; - return T(value); - } - else - { - auto value = this->sum / static_cast(this->samples); - return T(value); - } -} - -////////////////////////////////////////////////// -template<> -gz::math::Vector3i -MovingWindowFilter::Value() const -{ - auto value = this->sum / this->samples; - return value; -} - -////////////////////////////////////////////////// -template<> -gz::math::Vector3f -MovingWindowFilter::Value() const -{ - gz::math::Vector3f divisor; - divisor = static_cast(this->samples); - auto value = this->sum / divisor; - return value; -} - -////////////////////////////////////////////////// -template<> -gz::math::Vector3d -MovingWindowFilter::Value() const -{ - auto value = this->sum / this->samples; - return value; -} - -template class MovingWindowFilter; -template class MovingWindowFilter; -template class MovingWindowFilter; -template class MovingWindowFilter; -template class MovingWindowFilter; -template class MovingWindowFilter; - -} -} // namespace math -} // namespace gz From 75a19c734642f6e8489593048947a42dfd558b83 Mon Sep 17 00:00:00 2001 From: Steve Peters Date: Mon, 6 Nov 2023 13:38:51 -0800 Subject: [PATCH 2/4] Use HIDE_SYMBOLS_BY_DEFAULT Part of testing gazebosim/gz-cmake#392 Signed-off-by: Steve Peters Signed-off-by: Steve Peters --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 439088a0b..46ffb0569 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -128,6 +128,7 @@ set(FAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/fake/install") # Configure the build #============================================================================ gz_configure_build(QUIT_IF_BUILD_ERRORS + HIDE_SYMBOLS_BY_DEFAULT COMPONENTS eigen3) From 56f145f1cd10ad34d5403c5f51c720057343f96c Mon Sep 17 00:00:00 2001 From: Jose Luis Rivero Date: Tue, 7 Nov 2023 13:21:56 +0100 Subject: [PATCH 3/4] Remove specialization types Signed-off-by: Jose Luis Rivero --- include/gz/math/MovingWindowFilter.hh | 193 +++++++++++++------------- 1 file changed, 93 insertions(+), 100 deletions(-) diff --git a/include/gz/math/MovingWindowFilter.hh b/include/gz/math/MovingWindowFilter.hh index 40809e0dd..2f58a4d73 100644 --- a/include/gz/math/MovingWindowFilter.hh +++ b/include/gz/math/MovingWindowFilter.hh @@ -91,125 +91,118 @@ namespace gz using MovingWindowFilterVector3d = MovingWindowFilter; } -////////////////////////////////////////////////// -template -MovingWindowFilter::MovingWindowFilter(unsigned int _windowSize) -{ - this->SetWindowSize(_windowSize); -} + ////////////////////////////////////////////////// + template + MovingWindowFilter::MovingWindowFilter(unsigned int _windowSize) + { + this->SetWindowSize(_windowSize); + } -////////////////////////////////////////////////// -template -void MovingWindowFilter::Update(const T _val) -{ - // update sum and sample size with incoming _val + ////////////////////////////////////////////////// + template + void MovingWindowFilter::Update(const T _val) + { + // update sum and sample size with incoming _val + + // keep running sum + this->sum += _val; + + // shift pointer, wrap around if end has been reached. + ++this->valIter; + if (this->valIter == this->valHistory.end()) + { + // reset iterator to beginning of queue + this->valIter = this->valHistory.begin(); + } - // keep running sum - this->sum += _val; + // increment sample size + ++this->samples; - // shift pointer, wrap around if end has been reached. - ++this->valIter; - if (this->valIter == this->valHistory.end()) + if (this->samples > this->valWindowSize) + { + // subtract old value if buffer already filled + this->sum -= (*this->valIter); + // put new value into queue + (*this->valIter) = _val; + // reduce sample size + --this->samples; + } + else + { + // put new value into queue + (*this->valIter) = _val; + } + } + + ////////////////////////////////////////////////// + template + void MovingWindowFilter::SetWindowSize(const unsigned int _n) { - // reset iterator to beginning of queue + this->valWindowSize = _n; + this->valHistory = std::vector(_n, T()); this->valIter = this->valHistory.begin(); + this->sum = T(); + this->samples = 0; } - // increment sample size - ++this->samples; - - if (this->samples > this->valWindowSize) + ////////////////////////////////////////////////// + template + unsigned int MovingWindowFilter::WindowSize() const { - // subtract old value if buffer already filled - this->sum -= (*this->valIter); - // put new value into queue - (*this->valIter) = _val; - // reduce sample size - --this->samples; + return this->valWindowSize; } - else + + ////////////////////////////////////////////////// + template + bool MovingWindowFilter::WindowFilled() const { - // put new value into queue - (*this->valIter) = _val; + return this->samples == this->valWindowSize; } -} - -////////////////////////////////////////////////// -template -void MovingWindowFilter::SetWindowSize(const unsigned int _n) -{ - this->valWindowSize = _n; - this->valHistory = std::vector(_n, T()); - this->valIter = this->valHistory.begin(); - this->sum = T(); - this->samples = 0; -} - -////////////////////////////////////////////////// -template -unsigned int MovingWindowFilter::WindowSize() const -{ - return this->valWindowSize; -} -////////////////////////////////////////////////// -template -bool MovingWindowFilter::WindowFilled() const -{ - return this->samples == this->valWindowSize; -} + ////////////////////////////////////////////////// + template<> + inline gz::math::Vector3i + MovingWindowFilter::Value() const + { + auto value = this->sum / this->samples; + return value; + } -////////////////////////////////////////////////// -template<> -inline gz::math::Vector3i -MovingWindowFilter::Value() const -{ - auto value = this->sum / this->samples; - return value; -} - -////////////////////////////////////////////////// -template<> -inline gz::math::Vector3f -MovingWindowFilter::Value() const -{ - gz::math::Vector3f divisor; - divisor = static_cast(this->samples); - auto value = this->sum / divisor; - return value; -} - -////////////////////////////////////////////////// -template<> -inline gz::math::Vector3d -MovingWindowFilter::Value() const -{ - auto value = this->sum / this->samples; - return value; -} + ////////////////////////////////////////////////// + template<> + inline gz::math::Vector3f + MovingWindowFilter::Value() const + { + gz::math::Vector3f divisor; + divisor = static_cast(this->samples); + auto value = this->sum / divisor; + return value; + } -////////////////////////////////////////////////// -template -T MovingWindowFilter::Value() const -{ - if (std::is_integral_v) + ////////////////////////////////////////////////// + template<> + inline gz::math::Vector3d + MovingWindowFilter::Value() const { auto value = this->sum / this->samples; - return T(value); + return value; } - else + + ////////////////////////////////////////////////// + template + T MovingWindowFilter::Value() const { - auto value = this->sum / static_cast(this->samples); - return T(value); + if (std::is_integral_v) + { + auto value = this->sum / this->samples; + return T(value); + } + else + { + auto value = this->sum / static_cast(this->samples); + return T(value); + } } -} - -template class MovingWindowFilter; -template class MovingWindowFilter; -template class MovingWindowFilter; -template class MovingWindowFilter; -template class MovingWindowFilter; -template class MovingWindowFilter; } // namespace math } // namespace gz From 17970d787fc5e348c11c8da6a86a0ab2088f0ec1 Mon Sep 17 00:00:00 2001 From: Steve Peters Date: Tue, 7 Nov 2023 13:53:30 -0800 Subject: [PATCH 4/4] adjust whitespace Signed-off-by: Steve Peters Signed-off-by: Steve Peters --- include/gz/math/MovingWindowFilter.hh | 1 - 1 file changed, 1 deletion(-) diff --git a/include/gz/math/MovingWindowFilter.hh b/include/gz/math/MovingWindowFilter.hh index 2f58a4d73..2a0b331d1 100644 --- a/include/gz/math/MovingWindowFilter.hh +++ b/include/gz/math/MovingWindowFilter.hh @@ -203,7 +203,6 @@ namespace gz return T(value); } } - } // namespace math } // namespace gz #endif