11#include <gtest/gtest.h>
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};
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);
41TEST(MotionCompensation, InterpolatesTranslationAtPointTime) {
42 MotionTransform transform;
43 transform.end_translation[0] = 10.0F;
44 transform.max_time_offset = 100;
49 EXPECT_FLOAT_EQ(x, 3.5F);
50 EXPECT_FLOAT_EQ(y, 2.0F);
51 EXPECT_FLOAT_EQ(z, 3.0F);
54TEST(MotionCompensation, InterpolatesRotationAtPointTime) {
55 MotionTransform transform;
57 transform.end_quaternion[2] = 1.0F;
58 transform.end_quaternion[3] = 0.0F;
59 transform.max_time_offset = 100;
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);
69TEST(MotionCompensation, ClampsOffsetsBeyondScanEnd) {
70 MotionTransform transform;
71 transform.end_translation[1] = 4.0F;
72 transform.max_time_offset = 100;
77 EXPECT_FLOAT_EQ(x, 0.0F);
78 EXPECT_FLOAT_EQ(y, 4.0F);
79 EXPECT_FLOAT_EQ(z, 0.0F);
82TEST(MotionCompensation, UsesStartPoseWithoutPointTiming) {
83 MotionTransform transform;
84 transform.start_translation[2] = 2.0F;
85 transform.end_translation[2] = 8.0F;
90 EXPECT_FLOAT_EQ(x, 0.0F);
91 EXPECT_FLOAT_EQ(y, 0.0F);
92 EXPECT_FLOAT_EQ(z, 3.0F);
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;
101 EXPECT_EQ(transform.max_time_offset, 0U);
102 EXPECT_EQ(transform.end_translation, transform.start_translation);
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;
111 EXPECT_NEAR(x, 10.0F -
static_cast<float>(point_time) * 1.0e-8F, 1.0e-6F);
112 EXPECT_FLOAT_EQ(y, expected_y);
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;
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);
129TEST(ObjectMotion, DeskewedAndRawCorrectionAgreeWithTranslationAndRotation) {
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;
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}) {
145 motion.ray_origin_max_time_offset = deskewed.ray_origin_max_time_offset;
146 motion.end_ray_origin = deskewed.end_ray_origin;
149 float expected_x = 3.0F, expected_y = 4.0F, expected_z = 1.0F;
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);
158TEST(ObjectMotion, MovesAlongOriginalObliqueRayInBothTimeDirections) {
160 motion.reference_offset_sec = 0.1F;
161 float x = 3.0F, y = 4.0F, z = 0.0F;
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;
169 EXPECT_NEAR(x, 2.7F, 1.0e-6F);
170 EXPECT_NEAR(y, 3.6F, 1.0e-6F);
173TEST(ObjectMotion, LeavesDegenerateRayAndUnknownMeasurementsUnchanged) {
175 motion.reference_offset_sec = 0.1F;
176 float x = 0.0F, y = 0.0F, z = 0.0F;
179 for (
float velocity : {0.0F, 101.0F, std::numeric_limits<float>::quiet_NaN(), std::numeric_limits<float>::infinity()}) {
181 EXPECT_FLOAT_EQ(x, 10.0F);
185TEST(ObjectMotion, IncludesExactLimitsAndRejectsValuesImmediatelyOutside) {
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;
193 EXPECT_FLOAT_EQ(x, 18.0F);
196 EXPECT_FLOAT_EQ(x, 10.0F);
197 motion.reference_offset_sec = direction * std::nextafter(0.125F, 1.0F);
199 EXPECT_FLOAT_EQ(x, 10.0F);
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;
205 EXPECT_FLOAT_EQ(x, 10.0F);
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) {
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;
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;
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