test_particle_cloud.cpp
Go to the documentation of this file.
1 // Copyright 2024 Ekumen, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <gtest/gtest.h>
16 
17 #include <cmath>
18 #include <cstddef>
19 #include <tuple>
20 #include <vector>
21 
22 #include <range/v3/algorithm/count_if.hpp>
23 
24 #include <sophus/common.hpp>
25 #include <sophus/se2.hpp>
26 
27 #include "beluga/primitives.hpp"
28 #include "beluga_ros/messages.hpp"
30 
31 namespace {
32 
33 TEST(TestParticleCloud, Assign) {
34  using Constants = Sophus::Constants<double>;
35  const auto particles = std::vector{
36  std::make_tuple(
37  Sophus::SE2d{Sophus::SO2d{-Constants::pi() / 2.0}, Eigen::Vector2d{0.0, 1.0}}, beluga::Weight(0.5)),
38  std::make_tuple(
39  Sophus::SE2d{Sophus::SO2d{Constants::pi() / 2.0}, Eigen::Vector2d{0.0, -1.0}}, beluga::Weight(0.5)),
40  };
41  constexpr std::size_t kSampleSize = 1000U;
42  auto message = beluga_ros::msg::PoseArray{};
43  beluga_ros::assign_particle_cloud(particles, kSampleSize, message);
44  EXPECT_EQ(message.poses.size(), kSampleSize);
45  for (const auto& pose : message.poses) {
46  EXPECT_DOUBLE_EQ(pose.position.x, 0.0);
47  EXPECT_DOUBLE_EQ(std::abs(pose.position.y), 1.0);
48  EXPECT_DOUBLE_EQ(pose.position.z, 0.0);
49  EXPECT_DOUBLE_EQ(pose.orientation.x, 0.0);
50  EXPECT_DOUBLE_EQ(pose.orientation.y, 0.0);
51  EXPECT_DOUBLE_EQ(pose.orientation.x, 0.0);
52  EXPECT_DOUBLE_EQ(pose.orientation.y, 0.0);
53  EXPECT_DOUBLE_EQ(std::abs(pose.orientation.z), std::sin(Constants::pi() / 4.0));
54  EXPECT_DOUBLE_EQ(pose.orientation.w, std::cos(Constants::pi() / 4.0));
55  }
56  const auto num_particles_in_upper_half_xy_plane =
57  ranges::count_if(message.poses, [](const auto& pose) { return pose.position.y > 0.0; });
58  const double upper_half_xy_plane_weight =
59  static_cast<double>(num_particles_in_upper_half_xy_plane) / static_cast<double>(message.poses.size());
60  EXPECT_NEAR(upper_half_xy_plane_weight, 0.5, kSampleSize * 0.01); // allow 1% deviations
61 }
62 
63 TEST(TestParticleCloud, AssignNone) {
64  const auto particles = std::vector{
65  std::make_tuple(Sophus::SE2d{}, beluga::Weight(1.0)),
66  };
67  auto message = beluga_ros::msg::PoseArray{};
69  EXPECT_EQ(message.poses.size(), 0U);
70 }
71 
72 TEST(TestParticleCloud, AssignMatchingDistribution) {
73  const auto particles = std::vector{
74  std::make_tuple(Sophus::SE2d{}, beluga::Weight(1.0)),
75  };
76  auto message = beluga_ros::msg::PoseArray{};
78  EXPECT_EQ(message.poses.size(), particles.size());
79 }
80 
81 TEST(TestParticleCloud, AssignMatchingEmpty) {
82  const auto particles = std::vector<std::tuple<Sophus::SE2d, beluga::Weight>>{};
83  auto message = beluga_ros::msg::PoseArray{};
85  EXPECT_EQ(message.poses.size(), 0U);
86 }
87 
88 TEST(TestParticleCloud, AssignMarkers) {
89  using Constants = Sophus::Constants<double>;
90  const auto particles = std::vector{
91  std::make_tuple(Sophus::SE2d{Sophus::SO2d{}, Eigen::Vector2d{1.0, 0.0}}, beluga::Weight(0.35)),
92  std::make_tuple(Sophus::SE2d{Sophus::SO2d{}, Eigen::Vector2d{1.0, 0.0}}, beluga::Weight(0.25)),
93  std::make_tuple(Sophus::SE2d{Sophus::SO2d{Constants::pi()}, Eigen::Vector2d{0.0, -1.0}}, beluga::Weight(0.4)),
94  };
95  auto message = beluga_ros::msg::MarkerArray{};
97  ASSERT_EQ(message.markers.size(), 2U);
98  EXPECT_EQ(message.markers[0].points.size(), 2 * 2); // 2 arrows, 2 vertices each
99  EXPECT_EQ(message.markers[1].points.size(), 3 * 2); // 2 arrows, 3 vertices each
100 }
101 
102 TEST(TestParticleCloud, AssignNoMarkers) {
103  const auto particles = std::vector<std::tuple<Sophus::SE2d, beluga::Weight>>{};
104  auto message = beluga_ros::msg::MarkerArray{};
106  EXPECT_EQ(message.markers.size(), 0U);
107 }
108 
109 } // namespace
Sophus::SO2
TEST
TEST(ActionClientDestruction, persistent_goal_handles_1)
beluga_ros::assign_particle_cloud
beluga_ros::msg::PoseArray & assign_particle_cloud(Particles &&particles, std::size_t size, beluga_ros::msg::PoseArray &message)
Assign a pose distribution to a particle cloud message.
Definition: particle_cloud.hpp:111
se2.hpp
beluga_ros.conversion_utils.message
message
Definition: conversion_utils.py:29
common.hpp
beluga::Weight
Numeric< double, struct WeightTag > Weight
Sophus::SE2
primitives.hpp
Sophus::Constants
particle_cloud.hpp
Utilities for particle cloud I/O over ROS interfaces.
messages.hpp


beluga_ros
Author(s):
autogenerated on Tue Jul 16 2024 03:00:02