test_laser_scan.cpp
Go to the documentation of this file.
1 // Copyright 2023 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 <gmock/gmock.h>
16 #include <gtest/gtest.h>
17 
18 #include <limits>
19 #include <utility>
20 #include <vector>
21 
22 #include <range/v3/range/conversion.hpp>
23 #include <range/v3/view/all.hpp>
24 
25 #include <sophus/common.hpp>
26 #include <sophus/se3.hpp>
27 
30 
31 namespace {
32 
34 using testing::ElementsAre;
35 
36 constexpr double kMinRange = 5.;
37 constexpr double kMaxRange = 100.;
38 constexpr double kTolerance = 0.001;
39 
40 class SimpleLaserScan : public beluga::BaseLaserScan<SimpleLaserScan> {
41  public:
42  using Scalar = double;
43 
44  SimpleLaserScan(std::vector<double> ranges, std::vector<double> angles, Sophus::SE3d origin = Sophus::SE3d{})
45  : ranges_{std::move(ranges)}, angles_{std::move(angles)}, origin_{std::move(origin)} {}
46 
47  [[nodiscard]] const auto& origin() const { return origin_; }
48 
49  [[nodiscard]] auto ranges() const { return ranges_ | ranges::views::all; }
50 
51  [[nodiscard]] auto angles() const { return angles_ | ranges::views::all; }
52 
53  [[nodiscard]] static auto min_range() { return kMinRange; }
54 
55  [[nodiscard]] static auto max_range() { return kMaxRange; }
56 
57  private:
58  std::vector<double> ranges_;
59  std::vector<double> angles_;
60  Sophus::SE3d origin_;
61 };
62 
63 TEST(LaserScan, InvalidValues) {
64  constexpr auto kNan = std::numeric_limits<double>::quiet_NaN();
65  auto laser_scan = SimpleLaserScan{
66  {120., 0., 50., kNan},
67  {0.1, 0.2, 0.3, 0.4},
68  };
69  auto polar = laser_scan.points_in_polar_coordinates() | ranges::to<std::vector>;
70  ASSERT_EQ(polar.size(), 1UL);
71  ASSERT_THAT(polar.front(), Vector2Near<double>({50., 0.3}, kTolerance));
72 }
73 
74 TEST(LaserScan, TransformPolar) {
75  const auto pi = Sophus::Constants<double>::pi();
76  auto laser_scan = SimpleLaserScan{
77  {10., 20., 30., 40.},
78  {0., pi / 2, pi, -pi / 2},
79  };
80  auto polar = laser_scan.points_in_polar_coordinates() | ranges::to<std::vector>;
81  ASSERT_THAT(
82  polar, ElementsAre(
83  Vector2Near<double>({10., 0.}, kTolerance), //
84  Vector2Near<double>({20., pi / 2}, kTolerance), //
85  Vector2Near<double>({30., pi}, kTolerance), //
86  Vector2Near<double>({40., -pi / 2}, kTolerance)));
87 }
88 
89 TEST(LaserScan, TransformCartesian) {
90  const auto pi = Sophus::Constants<double>::pi();
91  auto laser_scan = SimpleLaserScan{
92  {10., 20., 30., 40.},
93  {0., pi / 2, pi, -pi / 2},
94  };
95  auto polar = laser_scan.points_in_cartesian_coordinates() | ranges::to<std::vector>;
96  ASSERT_THAT(
97  polar, ElementsAre(
98  Vector2Near<double>({10., 0.}, kTolerance), //
99  Vector2Near<double>({0., 20.}, kTolerance), //
100  Vector2Near<double>({-30., 0.}, kTolerance), //
101  Vector2Near<double>({0., -40.}, kTolerance)));
102 }
103 
104 } // namespace
kTolerance
double kTolerance
Definition: test_cluster_based_estimation.cpp:47
beluga::testing::Vector2Near
auto Vector2Near(const Sophus::Vector2< Scalar > &t, Scalar e)
Vector2 element matcher.
Definition: sophus_matchers.hpp:45
common.hpp
beluga::BaseLaserScan
Laser scan 2D base type.
Definition: laser_scan.hpp:59
laser_scan.hpp
Implementation of a laser scan interface.
Sophus::SE3
sophus_matchers.hpp
Implements GTest matchers for Sophus/Eigen types.
Sophus::Constants::pi
static SOPHUS_FUNC Scalar pi()
beluga::TEST
TEST(Bresenham, MultiPassGuarantee)
Definition: test_bresenham.cpp:27
se3.hpp


beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53