From 6bee0893de14c1c3eed88a71ba23e3b4a52b058e Mon Sep 17 00:00:00 2001 From: bresch Date: Wed, 1 Mar 2023 15:10:23 +0100 Subject: [PATCH] add circle intersection unit tests --- src/lib/mathlib/CMakeLists.txt | 1 + .../mathlib/math/MaxDistanceToCircleTest.cpp | 122 ++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 src/lib/mathlib/math/MaxDistanceToCircleTest.cpp diff --git a/src/lib/mathlib/CMakeLists.txt b/src/lib/mathlib/CMakeLists.txt index 4c63ebd17b..aad1d3446c 100644 --- a/src/lib/mathlib/CMakeLists.txt +++ b/src/lib/mathlib/CMakeLists.txt @@ -48,3 +48,4 @@ px4_add_unit_gtest(SRC math/FunctionsTest.cpp) px4_add_unit_gtest(SRC math/test/UtilitiesTest.cpp) px4_add_unit_gtest(SRC math/WelfordMeanTest.cpp) px4_add_unit_gtest(SRC math/WelfordMeanVectorTest.cpp) +px4_add_unit_gtest(SRC math/MaxDistanceToCircleTest.cpp) diff --git a/src/lib/mathlib/math/MaxDistanceToCircleTest.cpp b/src/lib/mathlib/math/MaxDistanceToCircleTest.cpp new file mode 100644 index 0000000000..58a3746cfe --- /dev/null +++ b/src/lib/mathlib/math/MaxDistanceToCircleTest.cpp @@ -0,0 +1,122 @@ +/**************************************************************************** + * + * Copyright (C) 2023 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 +#include +#include "Functions.hpp" + +#include "TrajMath.hpp" + +using namespace math; +using matrix::Vector2f; + +TEST(MaxDistanceToCircle, noDirection) +{ + Vector2f pos; + Vector2f circle_center; + float radius = 3.f; + Vector2f direction; + float distance = trajectory::getMaxDistanceToCircle(pos, circle_center, radius, direction); + EXPECT_TRUE(isnan(distance)); +} + +TEST(MaxDistanceToCircle, insideCircle) +{ + // North position, South direction + Vector2f pos(1.f, 0.f); + Vector2f circle_center; + float radius = 3.f; + Vector2f direction(-1.f, 0.f); + float distance = trajectory::getMaxDistanceToCircle(pos, circle_center, radius, direction); + EXPECT_FLOAT_EQ(distance, 4.f); + + // North position, West direction (direction doesn't need to be unit length) + direction = Vector2f(0.f, -3.42f); + distance = trajectory::getMaxDistanceToCircle(pos, circle_center, radius, direction); + EXPECT_FLOAT_EQ(distance, sqrtf(radius * radius - 1.f)); + + // SE position, directed towards the center + pos = Vector2f(-2.f, 1.f); + direction = Vector2f(2.f, -1.f); + distance = trajectory::getMaxDistanceToCircle(pos, circle_center, radius, direction); + EXPECT_FLOAT_EQ(distance, pos.norm() + radius); +} + +TEST(MaxDistanceToCircle, outsideCircle) +{ + // North position, South direction + Vector2f pos(4.f, 0.f); + Vector2f circle_center; + float radius = 3.f; + Vector2f direction(-1.f, 0.f); + float distance = trajectory::getMaxDistanceToCircle(pos, circle_center, radius, direction); + EXPECT_FLOAT_EQ(distance, pos(0) + radius); + + // looking away from the circle + direction = Vector2f(0.f, -3.42f); + distance = trajectory::getMaxDistanceToCircle(pos, circle_center, radius, direction); + EXPECT_TRUE(isnan(distance)); + + // SE position, directed towards the center + pos = Vector2f(-4.f, 2.f); + direction = Vector2f(2.f, -1.f); + distance = trajectory::getMaxDistanceToCircle(pos, circle_center, radius, direction); + EXPECT_FLOAT_EQ(distance, pos.norm() + radius); +} + +TEST(MaxDistanceToCircle, onCircle) +{ + // South, looking North + Vector2f pos(-4.f, 1.f); + Vector2f circle_center(-1.f, 1.f); + float radius = 3.f; + Vector2f direction(1.f, 0.f); + float distance = trajectory::getMaxDistanceToCircle(pos, circle_center, radius, direction); + EXPECT_FLOAT_EQ(distance, 2.f * radius); + + // looking tangent to the circle + direction = Vector2f(0.f, -3.42f); + distance = trajectory::getMaxDistanceToCircle(pos, circle_center, radius, direction); + EXPECT_FLOAT_EQ(distance, 0.f); + + // looking away from the circle + direction = Vector2f(-10.f, -3.42f); + distance = trajectory::getMaxDistanceToCircle(pos, circle_center, radius, direction); + EXPECT_FLOAT_EQ(distance, 0.f); + + // SE position, directed towards the center + pos = Vector2f(-sqrtf(2.f) / 2.f, sqrtf(2.f) / 2.f) * radius + circle_center; + direction = -pos; + distance = trajectory::getMaxDistanceToCircle(pos, circle_center, radius, direction); + EXPECT_FLOAT_EQ(distance, 2.f * radius); +}