Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWeiss David2020-09-24 11:04:28 +0000
committerBiegel Reinhard2020-09-25 10:18:50 +0000
commita3a70eac88180db29f4633c30f98a2fa62baf0c7 (patch)
tree8a6c935c255f9ed586f8b86768a22e80b8e282e6
parent3d56c7eb009df93043be34dd8cda24eebbc26422 (diff)
downloadsimopenpass-a3a70eac88180db29f4633c30f98a2fa62baf0c7.tar.gz
simopenpass-a3a70eac88180db29f4633c30f98a2fa62baf0c7.tar.xz
simopenpass-a3a70eac88180db29f4633c30f98a2fa62baf0c7.zip
add(CommonTools): Add calculation of cartesian net distance
-rw-r--r--sim/src/common/commonTools.h50
-rw-r--r--sim/tests/unitTests/common/CMakeLists.txt3
-rw-r--r--sim/tests/unitTests/common/Common_Tests.pro1
-rw-r--r--sim/tests/unitTests/common/commonHelper_Tests.cpp48
4 files changed, 102 insertions, 0 deletions
diff --git a/sim/src/common/commonTools.h b/sim/src/common/commonTools.h
index c9ae21d8..0b3c00ab 100644
--- a/sim/src/common/commonTools.h
+++ b/sim/src/common/commonTools.h
@@ -117,6 +117,56 @@ static double CalculateMomentInertiaYaw(double mass, double length, double width
return std::make_optional<Common::Vector2d>(intersectionPointX, intersectionPointY);
}
+//! Calculates the net distance of the x and y coordinates of two bounding boxes
+//!
+//! \param ownBoundingBox first bounding box
+//! \param otherBoundingBox second bounding box
+//! \return net distance x, net distance y
+[[maybe_unused]] static std::pair<double, double> GetCartesianNetDistance(polygon_t ownBoundingBox, polygon_t otherBoundingBox)
+{
+ double ownMaxX{std::numeric_limits<double>::lowest()};
+ double ownMinX{std::numeric_limits<double>::max()};
+ double ownMaxY{std::numeric_limits<double>::lowest()};
+ double ownMinY{std::numeric_limits<double>::max()};
+ for (const auto& point : ownBoundingBox.outer())
+ {
+ ownMaxX = std::max(ownMaxX, bg::get<0>(point));
+ ownMinX = std::min(ownMinX, bg::get<0>(point));
+ ownMaxY = std::max(ownMaxY, bg::get<1>(point));
+ ownMinY = std::min(ownMinY, bg::get<1>(point));
+ }
+ double otherMaxX{std::numeric_limits<double>::lowest()};
+ double otherMinX{std::numeric_limits<double>::max()};
+ double otherMaxY{std::numeric_limits<double>::lowest()};
+ double otherMinY{std::numeric_limits<double>::max()};
+ for (const auto& point : otherBoundingBox.outer())
+ {
+ otherMaxX = std::max(otherMaxX, bg::get<0>(point));
+ otherMinX = std::min(otherMinX, bg::get<0>(point));
+ otherMaxY = std::max(otherMaxY, bg::get<1>(point));
+ otherMinY = std::min(otherMinY, bg::get<1>(point));
+ }
+ double netX{0.0};
+ if (ownMaxX < otherMinX)
+ {
+ netX = otherMinX - ownMaxX;
+ }
+ if (ownMinX > otherMaxX)
+ {
+ netX = otherMaxX - ownMinX;
+ }
+ double netY{0.0};
+ if (ownMaxY < otherMinY)
+ {
+ netY = otherMinY - ownMaxY;
+ }
+ if (ownMinY > otherMaxY)
+ {
+ netY = otherMaxY - ownMinY;
+ }
+ return {netX, netY};
+}
+
//-----------------------------------------------------------------------------
//! @brief Tokenizes string by delimiter.
//!
diff --git a/sim/tests/unitTests/common/CMakeLists.txt b/sim/tests/unitTests/common/CMakeLists.txt
index ac93f696..c3d7fdc9 100644
--- a/sim/tests/unitTests/common/CMakeLists.txt
+++ b/sim/tests/unitTests/common/CMakeLists.txt
@@ -7,7 +7,10 @@ add_openpass_target(
LINK_OSI
SOURCES
+ commonHelper_Tests.cpp
ttcCalculation_Tests.cpp
+ tokenizeString_Tests.cpp
+ vectorToString_Tests.cpp
HEADERS
${COMPONENT_SOURCE_DIR}/commonTools.h
diff --git a/sim/tests/unitTests/common/Common_Tests.pro b/sim/tests/unitTests/common/Common_Tests.pro
index 25d0ab58..a48f5c36 100644
--- a/sim/tests/unitTests/common/Common_Tests.pro
+++ b/sim/tests/unitTests/common/Common_Tests.pro
@@ -29,6 +29,7 @@ HEADERS += \
$$UNIT_UNDER_TEST/commonTools.h
SOURCES += \
+ commonHelper_Tests.cpp \
tokenizeString_Tests.cpp \
ttcCalculation_Tests.cpp \
vectorToString_Tests.cpp
diff --git a/sim/tests/unitTests/common/commonHelper_Tests.cpp b/sim/tests/unitTests/common/commonHelper_Tests.cpp
new file mode 100644
index 00000000..fde532a4
--- /dev/null
+++ b/sim/tests/unitTests/common/commonHelper_Tests.cpp
@@ -0,0 +1,48 @@
+# /*********************************************************************
+# * Copyright (c) 2020 in-tech GmbH
+# *
+# * This program and the accompanying materials are made
+# * available under the terms of the Eclipse Public License 2.0
+# * which is available at https://www.eclipse.org/legal/epl-2.0/
+# *
+# * SPDX-License-Identifier: EPL-2.0
+# **********************************************************************/
+
+#include "common/commonTools.h"
+
+#include "gtest/gtest.h"
+#include "gmock/gmock.h"
+
+using ::testing::TestWithParam;
+using ::testing::Values;
+using ::testing::Return;
+using ::testing::DoubleNear;
+
+struct CartesianNetDistance_Data
+{
+ polygon_t ownBoundingBox;
+ polygon_t otherBoundingBox;
+ double expectedNetX;
+ double expectedNetY;
+};
+
+class CartesianNetDistanceTest: public ::TestWithParam<CartesianNetDistance_Data>
+{
+};
+
+TEST_P(CartesianNetDistanceTest, GetCartesianNetDistance_ReturnsCorrectDistances)
+{
+ auto data = GetParam();
+ auto [netX, netY] = CommonHelper::GetCartesianNetDistance(data.ownBoundingBox, data.otherBoundingBox);
+
+ ASSERT_THAT(netX, DoubleNear(data.expectedNetX, 1e-3));
+ ASSERT_THAT(netY, DoubleNear(data.expectedNetY, 1e-3));
+}
+
+INSTANTIATE_TEST_CASE_P(CartesianNetDistanceTestCase, CartesianNetDistanceTest, ::testing::Values(
+// ownBoundingBox otherBoundingBox x y
+ CartesianNetDistance_Data{polygon_t{{{0,0},{1,0},{1,1},{0,1}}}, polygon_t{{{1,0},{2,0},{2,1},{1,1}}}, 0,0},
+ CartesianNetDistance_Data{polygon_t{{{-1,0},{0,-1},{1,0},{0,1}}},polygon_t{{{1,1},{2,1},{2,2},{1,2}}}, 0,0},
+ CartesianNetDistance_Data{polygon_t{{{-1,0},{0,-1},{1,0},{0,1}}},polygon_t{{{3,4},{4,4},{4,6},{3,6}}}, 2,3},
+ CartesianNetDistance_Data{polygon_t{{{-1,0},{0,-1},{1,0},{0,1}}},polygon_t{{{-10,-10},{-8,-10},{-8,-9},{-10,-9}}},-7,-8}
+));

Back to the top