point_cloud_fusion v1.6.0
Loading...
Searching...
No Matches
test_motion_compensation.cpp
Go to the documentation of this file.
1// Copyright Institute for Automotive Engineering (ika), RWTH Aachen University
2// SPDX-License-Identifier: Apache-2.0
3
4#include <array>
5#include <cmath>
6#include <limits>
7#include <memory>
8#include <random>
9#include <vector>
10
11#include <gtest/gtest.h>
12
15
16namespace point_cloud_fusion {
17namespace {
18
19struct TestCloud {
20 std::size_t point_step;
21 std::vector<int> fields;
22};
23
24TEST(MotionCompensation, ExcludesIncompatibleCloudsWithoutChangingBatchIndices) {
25 using CloudPtr = std::shared_ptr<const TestCloud>;
26 const std::vector<int> reference_fields{1, 2, 3};
27 const CloudPtr compatible = std::make_shared<TestCloud>(TestCloud{16, reference_fields});
28 const CloudPtr incompatible_point_step = std::make_shared<TestCloud>(TestCloud{20, reference_fields});
29 const CloudPtr incompatible_fields = std::make_shared<TestCloud>(TestCloud{16, std::vector<int>{1, 2}});
30 const std::vector<CloudPtr> msgs{compatible, incompatible_point_step, nullptr, incompatible_fields};
31
32 const auto motion_msgs = selectMotionCompatibleClouds(msgs, 16, reference_fields);
33
34 ASSERT_EQ(motion_msgs.size(), msgs.size());
35 EXPECT_EQ(motion_msgs[0], compatible);
36 EXPECT_EQ(motion_msgs[1], nullptr);
37 EXPECT_EQ(motion_msgs[2], nullptr);
38 EXPECT_EQ(motion_msgs[3], nullptr);
39}
40
41TEST(MotionCompensation, InterpolatesTranslationAtPointTime) {
42 MotionTransform transform;
43 transform.end_translation[0] = 10.0F;
44 transform.max_time_offset = 100;
45
46 float x, y, z;
47 transformPointInterpolated(transform, 25, 1.0F, 2.0F, 3.0F, x, y, z);
48
49 EXPECT_FLOAT_EQ(x, 3.5F);
50 EXPECT_FLOAT_EQ(y, 2.0F);
51 EXPECT_FLOAT_EQ(z, 3.0F);
52}
53
54TEST(MotionCompensation, InterpolatesRotationAtPointTime) {
55 MotionTransform transform;
56 // Identity to 180 degrees around Z; normalized lerp is 90 degrees halfway.
57 transform.end_quaternion[2] = 1.0F;
58 transform.end_quaternion[3] = 0.0F;
59 transform.max_time_offset = 100;
60
61 float x, y, z;
62 transformPointInterpolated(transform, 50, 1.0F, 0.0F, 0.0F, x, y, z);
63
64 EXPECT_NEAR(x, 0.0F, 1.0e-5F);
65 EXPECT_NEAR(y, 1.0F, 1.0e-5F);
66 EXPECT_NEAR(z, 0.0F, 1.0e-5F);
67}
68
69TEST(MotionCompensation, ClampsOffsetsBeyondScanEnd) {
70 MotionTransform transform;
71 transform.end_translation[1] = 4.0F;
72 transform.max_time_offset = 100;
73
74 float x, y, z;
75 transformPointInterpolated(transform, 150, 0.0F, 0.0F, 0.0F, x, y, z);
76
77 EXPECT_FLOAT_EQ(x, 0.0F);
78 EXPECT_FLOAT_EQ(y, 4.0F);
79 EXPECT_FLOAT_EQ(z, 0.0F);
80}
81
82TEST(MotionCompensation, UsesStartPoseWithoutPointTiming) {
83 MotionTransform transform;
84 transform.start_translation[2] = 2.0F;
85 transform.end_translation[2] = 8.0F;
86
87 float x, y, z;
88 transformPointInterpolated(transform, 0, 0.0F, 0.0F, 1.0F, x, y, z);
89
90 EXPECT_FLOAT_EQ(x, 0.0F);
91 EXPECT_FLOAT_EQ(y, 0.0F);
92 EXPECT_FLOAT_EQ(z, 3.0F);
93}
94
95TEST(ObjectMotion, DeskewedTranslationPreservesAcquisitionRay) {
96 MotionTransform transform;
97 transform.start_translation = {10.0F, 20.0F, 0.0F};
98 transform.end_translation = {10.0F, 21.0F, 0.0F};
99 transform.max_time_offset = 100000000;
100 retainDeskewedPose(transform);
101 EXPECT_EQ(transform.max_time_offset, 0U);
102 EXPECT_EQ(transform.end_translation, transform.start_translation);
103
104 ObjectMotion motion;
105 motion.ray_origin_max_time_offset = transform.ray_origin_max_time_offset;
106 motion.end_ray_origin = transform.end_ray_origin;
107 for (const uint32_t point_time : {50000000U, 100000000U}) {
108 float x = 10.0F, y = static_cast<float>(point_time) * 1.0e-8F, z = 0.0F;
109 const float expected_y = y;
110 EXPECT_TRUE(compensateObjectMotion(motion, 10.0F, point_time, x, y, z));
111 EXPECT_NEAR(x, 10.0F - static_cast<float>(point_time) * 1.0e-8F, 1.0e-6F);
112 EXPECT_FLOAT_EQ(y, expected_y);
113 }
114}
115
116TEST(ObjectMotion, DeskewedOriginIsExpressedInHeaderOrientation) {
117 MotionTransform transform;
118 transform.start_translation = {10.0F, 20.0F, 0.0F};
119 transform.end_translation = {10.0F, 21.0F, 0.0F};
120 transform.start_quaternion = {0.0F, 0.0F, std::sqrt(0.5F), std::sqrt(0.5F)};
121 transform.end_quaternion = transform.start_quaternion;
122 transform.max_time_offset = 100000000;
123 retainDeskewedPose(transform);
124 EXPECT_NEAR(transform.end_ray_origin[0], 1.0F, 1.0e-6F);
125 EXPECT_NEAR(transform.end_ray_origin[1], 0.0F, 1.0e-6F);
126 EXPECT_NEAR(transform.end_ray_origin[2], 0.0F, 1.0e-6F);
127}
128
129TEST(ObjectMotion, DeskewedAndRawCorrectionAgreeWithTranslationAndRotation) {
130 MotionTransform raw;
131 raw.start_translation = {10.0F, 20.0F, 3.0F};
132 raw.end_translation = {12.0F, 21.0F, 4.0F};
133 raw.start_quaternion = {0.0F, 0.0F, std::sqrt(0.5F), std::sqrt(0.5F)};
134 raw.end_quaternion = {0.0F, 0.0F, 1.0F, 0.0F};
135 raw.max_time_offset = 100000000;
136 auto deskewed = raw;
137 retainDeskewedPose(deskewed);
138 MotionTransform inverse_rotation;
139 inverse_rotation.start_quaternion = {0.0F, 0.0F, -std::sqrt(0.5F), std::sqrt(0.5F)};
140 for (const uint32_t time : {25000000U, 50000000U, 100000000U}) {
141 float x, y, z;
142 transformPointInterpolated(raw, time, 3.0F, 4.0F, 1.0F, x, y, z);
143 transformPointInterpolated(inverse_rotation, 0, x - 10.0F, y - 20.0F, z - 3.0F, x, y, z);
144 ObjectMotion motion;
145 motion.ray_origin_max_time_offset = deskewed.ray_origin_max_time_offset;
146 motion.end_ray_origin = deskewed.end_ray_origin;
147 ASSERT_TRUE(compensateObjectMotion(motion, 5.0F, time, x, y, z));
148 transformPointInterpolated(deskewed, time, x, y, z, x, y, z);
149 float expected_x = 3.0F, expected_y = 4.0F, expected_z = 1.0F;
150 ASSERT_TRUE(compensateObjectMotion(ObjectMotion{}, 5.0F, time, expected_x, expected_y, expected_z));
151 transformPointInterpolated(raw, time, expected_x, expected_y, expected_z, expected_x, expected_y, expected_z);
152 EXPECT_NEAR(x, expected_x, 1.0e-5F);
153 EXPECT_NEAR(y, expected_y, 1.0e-5F);
154 EXPECT_NEAR(z, expected_z, 1.0e-5F);
155 }
156}
157
158TEST(ObjectMotion, MovesAlongOriginalObliqueRayInBothTimeDirections) {
159 ObjectMotion motion;
160 motion.reference_offset_sec = 0.1F;
161 float x = 3.0F, y = 4.0F, z = 0.0F;
162 EXPECT_TRUE(compensateObjectMotion(motion, 5.0F, 0, x, y, z));
163 EXPECT_NEAR(x, 3.3F, 1.0e-6F);
164 EXPECT_NEAR(y, 4.4F, 1.0e-6F);
165 motion.reference_offset_sec = 0.0F;
166 x = 3.0F;
167 y = 4.0F;
168 EXPECT_TRUE(compensateObjectMotion(motion, 5.0F, 100000000, x, y, z));
169 EXPECT_NEAR(x, 2.7F, 1.0e-6F);
170 EXPECT_NEAR(y, 3.6F, 1.0e-6F);
171}
172
173TEST(ObjectMotion, LeavesDegenerateRayAndUnknownMeasurementsUnchanged) {
174 ObjectMotion motion;
175 motion.reference_offset_sec = 0.1F;
176 float x = 0.0F, y = 0.0F, z = 0.0F;
177 EXPECT_FALSE(compensateObjectMotion(motion, 5.0F, 0, x, y, z));
178 x = 10.0F;
179 for (float velocity : {0.0F, 101.0F, std::numeric_limits<float>::quiet_NaN(), std::numeric_limits<float>::infinity()}) {
180 EXPECT_FALSE(compensateObjectMotion(motion, velocity, 0, x, y, z));
181 EXPECT_FLOAT_EQ(x, 10.0F);
182 }
183}
184
185TEST(ObjectMotion, IncludesExactLimitsAndRejectsValuesImmediatelyOutside) {
186 ObjectMotion motion;
187 motion.max_time_offset_sec = 0.125F;
188 motion.max_speed_mps = 64.0F;
189 for (const float direction : {-1.0F, 1.0F}) {
190 motion.reference_offset_sec = direction * motion.max_time_offset_sec;
191 float x = 10.0F, y = 0.0F, z = 0.0F;
192 EXPECT_TRUE(compensateObjectMotion(motion, direction * 64.0F, 0, x, y, z));
193 EXPECT_FLOAT_EQ(x, 18.0F);
194 x = 10.0F;
195 EXPECT_FALSE(compensateObjectMotion(motion, direction * std::nextafter(64.0F, 65.0F), 0, x, y, z));
196 EXPECT_FLOAT_EQ(x, 10.0F);
197 motion.reference_offset_sec = direction * std::nextafter(0.125F, 1.0F);
198 EXPECT_FALSE(compensateObjectMotion(motion, 5.0F, 0, x, y, z));
199 EXPECT_FLOAT_EQ(x, 10.0F);
200 }
201 for (float dt : {0.0F, std::numeric_limits<float>::quiet_NaN(), std::numeric_limits<float>::infinity()}) {
202 motion.reference_offset_sec = dt;
203 float x = 10.0F, y = 0.0F, z = 0.0F;
204 EXPECT_FALSE(compensateObjectMotion(motion, 5.0F, 0, x, y, z));
205 EXPECT_FLOAT_EQ(x, 10.0F);
206 }
207}
208
209TEST(ObjectMotion, MatchesDoublePrecisionReferenceAcrossRaysSignsAndTimeScales) {
210 std::mt19937 random(1701);
211 std::uniform_real_distribution<float> coordinate(-100.0F, 100.0F);
212 std::uniform_real_distribution<float> speed(-100.0F, 100.0F);
213 std::uniform_real_distribution<float> reference(-0.05F, 0.05F);
214 std::uniform_int_distribution<uint32_t> time(0, 100000);
215 for (const float time_scale : {1.0e-9F, 1.0e-6F}) {
216 for (const float sign : {-1.0F, 1.0F}) {
217 for (int sample = 0; sample < 1000; ++sample) {
218 ObjectMotion motion;
219 motion.time_scale_sec = time_scale;
220 motion.doppler_sign = sign;
221 motion.reference_offset_sec = reference(random);
222 const auto point_time = time(random);
223 const float velocity = speed(random);
224 float x = coordinate(random), y = coordinate(random), z = coordinate(random);
225 const std::array<double, 3> ray{x, y, z};
226 const double range = std::sqrt(ray[0] * ray[0] + ray[1] * ray[1] + ray[2] * ray[2]);
227 const double dt = static_cast<double>(motion.reference_offset_sec) - static_cast<double>(point_time) * time_scale;
228 ASSERT_TRUE(compensateObjectMotion(motion, velocity, point_time, x, y, z));
229 const std::array<float, 3> actual{x, y, z};
230 for (std::size_t axis = 0; axis < ray.size(); ++axis) {
231 const double expected = ray[axis] + sign * velocity * dt * ray[axis] / range;
232 EXPECT_NEAR(actual[axis], expected, 3.0e-5) << "sample=" << sample << " axis=" << axis;
233 }
234 }
235 }
236 }
237}
238
239} // namespace
240} // namespace point_cloud_fusion
bool compensateObjectMotion(const ObjectMotion &motion, float velocity, uint32_t point_time, float &x, float &y, float &z)
Advance a measured acquisition ray using absolute radial velocity.
std::vector< PointCloudPtr > selectMotionCompatibleClouds(const std::vector< PointCloudPtr > &msgs, std::size_t point_step, const Fields &fields)
Keep batch indices while excluding clouds with incompatible layouts.
void transformPointInterpolated(const MotionTransform &transform, uint32_t time_offset, float x, float y, float z, float &transformed_x, float &transformed_y, float &transformed_z)
Transform a point using normalized quaternion interpolation.
void retainDeskewedPose(MotionTransform &transform)
std::vector< int > fields
std::size_t point_step