mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-11-29 11:40:28 +08:00
feat(matrix): add in-place nanToZero function
Adding this to the library, as with the new thrust setpoint definition we can expect this to be needed in more and more places
This commit is contained in:
1 parent
1b7b05fb55
commit
5578e9e7ab
3 files changed
+85
No files matched your search
@@ -604,6 +604,20 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
inline void nanToZero()
|
||||
{
|
||||
|
||||
Matrix<Type, M, N> &self = *this;
|
||||
|
||||
for (size_t i = 0; i < M; i++) {
|
||||
for (size_t j = 0; j < N; j++) {
|
||||
if (std::isnan(self(i, j))) {
|
||||
self(i, j) = static_cast<Type>(0.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Matrix<Type, M, N> abs() const
|
||||
{
|
||||
Matrix<Type, M, N> r;
|
||||
|
||||
@@ -28,3 +28,4 @@ px4_add_unit_gtest(SRC MatrixUpperRightTriangleTest.cpp)
|
||||
px4_add_unit_gtest(SRC MatrixVector2Test.cpp)
|
||||
px4_add_unit_gtest(SRC MatrixVector3Test.cpp)
|
||||
px4_add_unit_gtest(SRC MatrixVectorAssignmentTest.cpp)
|
||||
px4_add_unit_gtest(SRC NanToZeroTest.cpp)
|
||||
@@ -0,0 +1,70 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (C) 2026 PX4 Development Team. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name PX4 nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <matrix/math.hpp>
|
||||
|
||||
using namespace matrix;
|
||||
|
||||
TEST(NanToZeroTest, NanToZero)
|
||||
{
|
||||
const float data[9] = {-1.f, 0.f, +INFINITY, NAN, 0.f, -INFINITY, 1.f, 0.f, -NAN};
|
||||
const float expected[9] = {-1.f, 0.f, +INFINITY, 0.f, 0.f, -INFINITY, 1.f, 0.f, 0.f};
|
||||
|
||||
Matrix<float, 3, 3> mat(data);
|
||||
mat.nanToZero();
|
||||
|
||||
const Matrix<float, 3, 3> mat_expected(expected);
|
||||
|
||||
for (size_t i = 0; i < 3; i++) {
|
||||
for (size_t j = 0; j < 3; j++) {
|
||||
EXPECT_FLOAT_EQ(mat(i, j), mat_expected(i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(NanToZeroTest, NoNanUnchanged)
|
||||
{
|
||||
const float data[4] = {-2.5f, 0.f, 3.14f, +INFINITY};
|
||||
|
||||
Matrix<float, 2, 2> mat(data);
|
||||
mat.nanToZero();
|
||||
|
||||
const Matrix<float, 2, 2> mat_orig(data);
|
||||
|
||||
for (size_t i = 0; i < 2; i++) {
|
||||
for (size_t j = 0; j < 2; j++) {
|
||||
EXPECT_FLOAT_EQ(mat(i, j), mat_orig(i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user