15 #include <gtest/gtest.h>
22 #include <range/v3/algorithm/count_if.hpp>
33 TEST(TestParticleCloud, Assign) {
35 const auto particles = std::vector{
41 constexpr std::size_t kSampleSize = 1000U;
42 auto message = beluga_ros::msg::PoseArray{};
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));
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);
63 TEST(TestParticleCloud, AssignNone) {
64 const auto particles = std::vector{
67 auto message = beluga_ros::msg::PoseArray{};
69 EXPECT_EQ(
message.poses.size(), 0U);
72 TEST(TestParticleCloud, AssignMatchingDistribution) {
73 const auto particles = std::vector{
76 auto message = beluga_ros::msg::PoseArray{};
78 EXPECT_EQ(
message.poses.size(), particles.size());
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);
88 TEST(TestParticleCloud, AssignMarkers) {
90 const auto particles = std::vector{
95 auto message = beluga_ros::msg::MarkerArray{};
97 ASSERT_EQ(
message.markers.size(), 2U);
98 EXPECT_EQ(
message.markers[0].points.size(), 2 * 2);
99 EXPECT_EQ(
message.markers[1].points.size(), 3 * 2);
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);