|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 | 15 | #include <gtest/gtest.h>
|
| 16 | +#include <quaternion_operation/quaternion_operation.h> |
16 | 17 |
|
17 | 18 | #include <geometry/bounding_box.hpp>
|
18 | 19 | #include <scenario_simulator_exception/exception.hpp>
|
19 | 20 |
|
20 |
| -TEST(BoundingBox, GetPolygonDistanceWithCollision) |
| 21 | +#include "expect_eq_macros.hpp" |
| 22 | +#include "test_utils.hpp" |
| 23 | + |
| 24 | +TEST(BoundingBox, getPointsFromBboxDefault) |
| 25 | +{ |
| 26 | + geometry_msgs::msg::Pose pose; |
| 27 | + traffic_simulator_msgs::msg::BoundingBox bbox; |
| 28 | + std::vector<geometry_msgs::msg::Point> points = math::geometry::getPointsFromBbox(bbox); |
| 29 | + EXPECT_EQ(points.size(), size_t(4)); |
| 30 | + EXPECT_POINT_EQ(points[0], makePoint(0.0, 0.0, 0.0)); |
| 31 | + EXPECT_POINT_EQ(points[1], makePoint(0.0, 0.0, 0.0)); |
| 32 | + EXPECT_POINT_EQ(points[2], makePoint(0.0, 0.0, 0.0)); |
| 33 | + EXPECT_POINT_EQ(points[3], makePoint(0.0, 0.0, 0.0)); |
| 34 | +} |
| 35 | + |
| 36 | +TEST(BoundingBox, getPointsFromBboxCustom) |
| 37 | +{ |
| 38 | + geometry_msgs::msg::Pose pose; |
| 39 | + traffic_simulator_msgs::msg::BoundingBox bbox = makeBbox(5.0, 2.0, 2.0, 1.0); |
| 40 | + std::vector<geometry_msgs::msg::Point> points = |
| 41 | + math::geometry::getPointsFromBbox(bbox, 1.0, 2.0, 3.0, 4.0); |
| 42 | + EXPECT_EQ(points.size(), size_t(4)); |
| 43 | + EXPECT_POINT_EQ(points[0], makePoint(6.5, 3.0, 1.0)); |
| 44 | + EXPECT_POINT_EQ(points[1], makePoint(-5.5, 3.0, 1.0)); |
| 45 | + EXPECT_POINT_EQ(points[2], makePoint(-5.5, -2.0, 1.0)); |
| 46 | + EXPECT_POINT_EQ(points[3], makePoint(6.5, -2.0, 1.0)); |
| 47 | +} |
| 48 | + |
| 49 | +TEST(BoundingBox, get2DPolygonZeroPose) |
| 50 | +{ |
| 51 | + geometry_msgs::msg::Pose pose; |
| 52 | + traffic_simulator_msgs::msg::BoundingBox bbox = makeBbox(2.0, 2.0, 2.0); |
| 53 | + boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double>> poly = |
| 54 | + math::geometry::get2DPolygon(pose, bbox); |
| 55 | + EXPECT_EQ(poly.inners().size(), size_t(0)); |
| 56 | + EXPECT_EQ(poly.outer().size(), size_t(5)); |
| 57 | + EXPECT_BOOST_POINT_2D_AND_POINT_EQ(poly.outer()[0], makePoint(1.0, 1.0)); |
| 58 | + EXPECT_BOOST_POINT_2D_AND_POINT_EQ(poly.outer()[1], makePoint(-1.0, 1.0)); |
| 59 | + EXPECT_BOOST_POINT_2D_AND_POINT_EQ(poly.outer()[2], makePoint(-1.0, -1.0)); |
| 60 | + EXPECT_BOOST_POINT_2D_AND_POINT_EQ(poly.outer()[3], makePoint(1.0, -1.0)); |
| 61 | + EXPECT_BOOST_POINT_2D_AND_POINT_EQ(poly.outer()[4], makePoint(1.0, 1.0)); |
| 62 | +} |
| 63 | + |
| 64 | +TEST(BoundingBox, get2DPolygonOnlyTranslation) |
| 65 | +{ |
| 66 | + geometry_msgs::msg::Pose pose = makePose(1.0, 2.0); |
| 67 | + traffic_simulator_msgs::msg::BoundingBox bbox = makeBbox(2.0, 2.0, 2.0); |
| 68 | + boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double>> poly = |
| 69 | + math::geometry::get2DPolygon(pose, bbox); |
| 70 | + EXPECT_EQ(poly.inners().size(), size_t(0)); |
| 71 | + EXPECT_EQ(poly.outer().size(), size_t(5)); |
| 72 | + EXPECT_BOOST_POINT_2D_AND_POINT_EQ(poly.outer()[0], makePoint(2.0, 3.0)); |
| 73 | + EXPECT_BOOST_POINT_2D_AND_POINT_EQ(poly.outer()[1], makePoint(0.0, 3.0)); |
| 74 | + EXPECT_BOOST_POINT_2D_AND_POINT_EQ(poly.outer()[2], makePoint(0.0, 1.0)); |
| 75 | + EXPECT_BOOST_POINT_2D_AND_POINT_EQ(poly.outer()[3], makePoint(2.0, 1.0)); |
| 76 | + EXPECT_BOOST_POINT_2D_AND_POINT_EQ(poly.outer()[4], makePoint(2.0, 3.0)); |
| 77 | +} |
| 78 | + |
| 79 | +TEST(BoundingBox, get2DPolygonFullPose) |
| 80 | +{ |
| 81 | + geometry_msgs::msg::Pose pose = makePose(1.0, 2.0); |
| 82 | + pose.orientation = quaternion_operation::convertEulerAngleToQuaternion( |
| 83 | + geometry_msgs::build<geometry_msgs::msg::Vector3>().x(0.0).y(0.0).z(30.0 * M_PI / 180.0)); |
| 84 | + traffic_simulator_msgs::msg::BoundingBox bbox = makeBbox(2.0, 2.0, 2.0); |
| 85 | + boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double>> poly = |
| 86 | + math::geometry::get2DPolygon(pose, bbox); |
| 87 | + EXPECT_EQ(poly.inners().size(), size_t(0)); |
| 88 | + EXPECT_EQ(poly.outer().size(), size_t(5)); |
| 89 | + const double x = std::sqrt(2.0) * std::cos((30.0 + 45.0) * M_PI / 180.0); |
| 90 | + const double y = std::sqrt(2.0) * std::sin((30.0 + 45.0) * M_PI / 180.0); |
| 91 | + EXPECT_BOOST_POINT_2D_AND_POINT_EQ(poly.outer()[0], makePoint(x + 1.0, y + 2.0)); |
| 92 | + EXPECT_BOOST_POINT_2D_AND_POINT_EQ(poly.outer()[1], makePoint(-y + 1.0, x + 2.0)); |
| 93 | + EXPECT_BOOST_POINT_2D_AND_POINT_EQ(poly.outer()[2], makePoint(-x + 1.0, -y + 2.0)); |
| 94 | + EXPECT_BOOST_POINT_2D_AND_POINT_EQ(poly.outer()[3], makePoint(y + 1.0, -x + 2.0)); |
| 95 | + EXPECT_BOOST_POINT_2D_AND_POINT_EQ(poly.outer()[4], makePoint(x + 1.0, y + 2.0)); |
| 96 | +} |
| 97 | + |
| 98 | +TEST(BoundingBox, getPolygonDistanceWithCollision) |
21 | 99 | {
|
22 | 100 | geometry_msgs::msg::Pose pose0;
|
23 |
| - traffic_simulator_msgs::msg::BoundingBox bbox0; |
24 |
| - bbox0.dimensions.x = 3; |
25 |
| - bbox0.dimensions.y = 3; |
26 |
| - bbox0.dimensions.z = 3; |
| 101 | + traffic_simulator_msgs::msg::BoundingBox bbox0 = makeBbox(3.0, 3.0, 3.0); |
27 | 102 | geometry_msgs::msg::Pose pose1;
|
28 |
| - traffic_simulator_msgs::msg::BoundingBox bbox1; |
29 |
| - bbox1.dimensions.x = 1; |
30 |
| - bbox1.dimensions.y = 1; |
31 |
| - bbox1.dimensions.z = 1; |
| 103 | + traffic_simulator_msgs::msg::BoundingBox bbox1 = makeBbox(1.0, 1.0, 1.0); |
32 | 104 | EXPECT_EQ(math::geometry::getPolygonDistance(pose0, bbox0, pose1, bbox1), std::nullopt);
|
33 | 105 | }
|
34 | 106 |
|
35 |
| -TEST(BoundingBox, GetPolygonDistanceWithoutCollision) |
| 107 | +TEST(BoundingBox, getPolygonDistanceTouch) |
36 | 108 | {
|
37 | 109 | geometry_msgs::msg::Pose pose0;
|
38 |
| - traffic_simulator_msgs::msg::BoundingBox bbox0; |
39 |
| - bbox0.dimensions.x = 3; |
40 |
| - bbox0.dimensions.y = 3; |
41 |
| - bbox0.dimensions.z = 3; |
42 |
| - geometry_msgs::msg::Pose pose1; |
43 |
| - pose1.position.y = 5; |
44 |
| - traffic_simulator_msgs::msg::BoundingBox bbox1; |
45 |
| - bbox1.dimensions.x = 1; |
46 |
| - bbox1.dimensions.y = 1; |
47 |
| - bbox1.dimensions.z = 1; |
48 |
| - EXPECT_TRUE(math::geometry::getPolygonDistance(pose0, bbox0, pose1, bbox1)); |
49 |
| - EXPECT_DOUBLE_EQ(math::geometry::getPolygonDistance(pose0, bbox0, pose1, bbox1).value(), 3.0); |
| 110 | + traffic_simulator_msgs::msg::BoundingBox bbox0 = makeBbox(4.0, 4.0, 4.0); |
| 111 | + geometry_msgs::msg::Pose pose1 = makePose(3.0, 3.0, 3.0); |
| 112 | + traffic_simulator_msgs::msg::BoundingBox bbox1 = makeBbox(2.0, 2.0, 2.0); |
| 113 | + EXPECT_EQ(math::geometry::getPolygonDistance(pose0, bbox0, pose1, bbox1), std::nullopt); |
| 114 | +} |
| 115 | + |
| 116 | +TEST(BoundingBox, getPolygonDistanceWithoutCollision) |
| 117 | +{ |
| 118 | + geometry_msgs::msg::Pose pose0; |
| 119 | + traffic_simulator_msgs::msg::BoundingBox bbox0 = makeBbox(3.0, 3.0, 3.0); |
| 120 | + geometry_msgs::msg::Pose pose1 = makePose(0.0, 5.0); |
| 121 | + traffic_simulator_msgs::msg::BoundingBox bbox1 = makeBbox(1.0, 1.0, 1.0); |
| 122 | + const auto ans = math::geometry::getPolygonDistance(pose0, bbox0, pose1, bbox1); |
| 123 | + EXPECT_TRUE(ans); |
| 124 | + EXPECT_DOUBLE_EQ(ans.value(), 3.0); |
50 | 125 | }
|
51 | 126 |
|
52 | 127 | int main(int argc, char ** argv)
|
|
0 commit comments