@@ -142,6 +142,130 @@ INSTANTIATE_TEST_SUITE_P(
142142}
143143));
144144
145+ using linearInterpolationParam = std::tuple<
146+ std::pair<double , double >,
147+ std::pair<double , double >,
148+ double ,
149+ std::pair<double , double >
150+ >;
151+
152+ class linearInterpolationTest
153+ : public ::testing::TestWithParam<linearInterpolationParam>
154+ {};
155+
156+ TEST_P (linearInterpolationTest, linearInterpolation)
157+ {
158+ auto pair1 = std::get<0 >(GetParam ());
159+ auto pair2 = std::get<1 >(GetParam ());
160+ auto r = std::get<2 >(GetParam ());
161+ auto expected_pair = std::get<3 >(GetParam ());
162+ auto pair_to_point = [](std::pair<double , double > p) -> geometry_msgs::msg::Point {
163+ geometry_msgs::msg::Point point;
164+ point.x = p.first ;
165+ point.y = p.second ;
166+ point.z = 0.0 ;
167+ return point;
168+ };
169+ auto p1 = pair_to_point (pair1);
170+ auto p2 = pair_to_point (pair2);
171+ auto actual = nav2_util::linearInterpolation (p1, p2, r);
172+ auto expected_point = pair_to_point (expected_pair);
173+ EXPECT_DOUBLE_EQ (actual.x , expected_point.x );
174+ EXPECT_DOUBLE_EQ (actual.y , expected_point.y );
175+ // Expect that the intersection point is actually r away from the origin
176+ EXPECT_DOUBLE_EQ (r, std::hypot (p1.x - actual.x , p1.y - actual.y ));
177+ }
178+
179+ INSTANTIATE_TEST_SUITE_P (
180+ InterpolationTest,
181+ linearInterpolationTest,
182+ testing::Values (
183+ // Origin to the positive X axis
184+ linearInterpolationParam{
185+ {0.0 , 0.0 },
186+ {2.0 , 0.0 },
187+ 1.0 ,
188+ {1.0 , 0.0 }
189+ },
190+ // Origin to the negative X axis
191+ linearInterpolationParam{
192+ {0.0 , 0.0 },
193+ {-2.0 , 0.0 },
194+ 1.0 ,
195+ {-1.0 , 0.0 }
196+ },
197+ // Origin to the positive Y axis
198+ linearInterpolationParam{
199+ {0.0 , 0.0 },
200+ {0.0 , 2.0 },
201+ 1.0 ,
202+ {0.0 , 1.0 }
203+ },
204+ // Origin to the negative Y axis
205+ linearInterpolationParam{
206+ {0.0 , 0.0 },
207+ {0.0 , -2.0 },
208+ 1.0 ,
209+ {0.0 , -1.0 }
210+ },
211+ // non-origin to the X axis with non-unit circle, with the second point inside
212+ linearInterpolationParam{
213+ {4.0 , 0.0 },
214+ {-1.0 , 0.0 },
215+ 2.0 ,
216+ {2.0 , 0.0 }
217+ },
218+ // non-origin to the Y axis with non-unit circle, with the second point inside
219+ linearInterpolationParam{
220+ {0.0 , 4.0 },
221+ {0.0 , -0.5 },
222+ 2.0 ,
223+ {0.0 , 2.0 }
224+ },
225+ // origin to the positive X axis, on the circle
226+ linearInterpolationParam{
227+ {2.0 , 0.0 },
228+ {0.0 , 0.0 },
229+ 2.0 ,
230+ {0.0 , 0.0 }
231+ },
232+ // origin to the positive Y axis, on the circle
233+ linearInterpolationParam{
234+ {0.0 , 0.0 },
235+ {0.0 , 2.0 },
236+ 2.0 ,
237+ {0.0 , 2.0 }
238+ },
239+ // origin to the upper-right quadrant (3-4-5 triangle)
240+ linearInterpolationParam{
241+ {0.0 , 0.0 },
242+ {6.0 , 8.0 },
243+ 5.0 ,
244+ {3.0 , 4.0 }
245+ },
246+ // origin to the lower-left quadrant (3-4-5 triangle)
247+ linearInterpolationParam{
248+ {0.0 , 0.0 },
249+ {-6.0 , -8.0 },
250+ 5.0 ,
251+ {-3.0 , -4.0 }
252+ },
253+ // origin to the upper-left quadrant (3-4-5 triangle)
254+ linearInterpolationParam{
255+ {0.0 , 0.0 },
256+ {-6.0 , 8.0 },
257+ 5.0 ,
258+ {-3.0 , 4.0 }
259+ },
260+ // origin to the lower-right quadrant (3-4-5 triangle)
261+ linearInterpolationParam{
262+ {0.0 , 0.0 },
263+ {6.0 , -8.0 },
264+ 5.0 ,
265+ {3.0 , -4.0 }
266+ }
267+ ));
268+
145269TEST (InterpolationUtils, lookaheadInterpolation)
146270{
147271 // test Lookahead Point Interpolation
@@ -185,7 +309,7 @@ TEST(InterpolationUtils, lookaheadExtrapolation)
185309 path.poses [0 ].pose .position .x = 2.0 ;
186310 path.poses [1 ].pose .position .x = 3.0 ;
187311 pt = nav2_util::getLookAheadPoint (lookahead_dist, path, true );
188- EXPECT_NEAR (pt.pose .position .x , 10 .0 , EPSILON);
312+ EXPECT_NEAR (pt.pose .position .x , 12 .0 , EPSILON);
189313 EXPECT_NEAR (pt.pose .position .y , 0.0 , EPSILON);
190314
191315 // 2 poses at 45°
@@ -196,8 +320,8 @@ TEST(InterpolationUtils, lookaheadExtrapolation)
196320 path.poses [1 ].pose .position .x = 3.0 ;
197321 path.poses [1 ].pose .position .y = 3.0 ;
198322 pt = nav2_util::getLookAheadPoint (lookahead_dist, path, true );
199- EXPECT_NEAR (pt.pose .position .x , cos (45.0 * M_PI / 180 ) * 10.0 , EPSILON);
200- EXPECT_NEAR (pt.pose .position .y , sin (45.0 * M_PI / 180 ) * 10.0 , EPSILON);
323+ EXPECT_NEAR (pt.pose .position .x , 2.0 + cos (45.0 * M_PI / 180 ) * 10.0 , EPSILON);
324+ EXPECT_NEAR (pt.pose .position .y , 2.0 + sin (45.0 * M_PI / 180 ) * 10.0 , EPSILON);
201325
202326 // 2 poses at 90°
203327 path.poses .clear ();
@@ -207,8 +331,8 @@ TEST(InterpolationUtils, lookaheadExtrapolation)
207331 path.poses [1 ].pose .position .x = 0.0 ;
208332 path.poses [1 ].pose .position .y = 3.0 ;
209333 pt = nav2_util::getLookAheadPoint (lookahead_dist, path, true );
210- EXPECT_NEAR (pt.pose .position .x , cos (90.0 * M_PI / 180 ) * 10.0 , EPSILON);
211- EXPECT_NEAR (pt.pose .position .y , sin (90.0 * M_PI / 180 ) * 10.0 , EPSILON);
334+ EXPECT_NEAR (pt.pose .position .x , 0.0 + cos (90.0 * M_PI / 180 ) * 10.0 , EPSILON);
335+ EXPECT_NEAR (pt.pose .position .y , 2.0 + sin (90.0 * M_PI / 180 ) * 10.0 , EPSILON);
212336
213337 // 2 poses at 135°
214338 path.poses .clear ();
@@ -218,16 +342,16 @@ TEST(InterpolationUtils, lookaheadExtrapolation)
218342 path.poses [1 ].pose .position .x = -3.0 ;
219343 path.poses [1 ].pose .position .y = 3.0 ;
220344 pt = nav2_util::getLookAheadPoint (lookahead_dist, path, true );
221- EXPECT_NEAR (pt.pose .position .x , cos (135.0 * M_PI / 180 ) * 10.0 , EPSILON);
222- EXPECT_NEAR (pt.pose .position .y , sin (135.0 * M_PI / 180 ) * 10.0 , EPSILON);
345+ EXPECT_NEAR (pt.pose .position .x , - 2.0 + cos (135.0 * M_PI / 180 ) * 10.0 , EPSILON);
346+ EXPECT_NEAR (pt.pose .position .y , 2.0 + sin (135.0 * M_PI / 180 ) * 10.0 , EPSILON);
223347
224348 // 2 poses back
225349 path.poses .clear ();
226350 path.poses .resize (2 );
227351 path.poses [0 ].pose .position .x = -2.0 ;
228352 path.poses [1 ].pose .position .x = -3.0 ;
229353 pt = nav2_util::getLookAheadPoint (lookahead_dist, path, true );
230- EXPECT_NEAR (pt.pose .position .x , -10 .0 , EPSILON);
354+ EXPECT_NEAR (pt.pose .position .x , -12 .0 , EPSILON);
231355 EXPECT_NEAR (pt.pose .position .y , 0.0 , EPSILON);
232356
233357 // 2 poses at -135°
@@ -238,8 +362,8 @@ TEST(InterpolationUtils, lookaheadExtrapolation)
238362 path.poses [1 ].pose .position .x = -3.0 ;
239363 path.poses [1 ].pose .position .y = -3.0 ;
240364 pt = nav2_util::getLookAheadPoint (lookahead_dist, path, true );
241- EXPECT_NEAR (pt.pose .position .x , cos (-135.0 * M_PI / 180 ) * 10.0 , EPSILON);
242- EXPECT_NEAR (pt.pose .position .y , sin (-135.0 * M_PI / 180 ) * 10.0 , EPSILON);
365+ EXPECT_NEAR (pt.pose .position .x , - 2.0 + cos (-135.0 * M_PI / 180 ) * 10.0 , EPSILON);
366+ EXPECT_NEAR (pt.pose .position .y , - 2.0 + sin (-135.0 * M_PI / 180 ) * 10.0 , EPSILON);
243367
244368 // 2 poses at -90°
245369 path.poses .clear ();
@@ -249,8 +373,8 @@ TEST(InterpolationUtils, lookaheadExtrapolation)
249373 path.poses [1 ].pose .position .x = 0.0 ;
250374 path.poses [1 ].pose .position .y = -3.0 ;
251375 pt = nav2_util::getLookAheadPoint (lookahead_dist, path, true );
252- EXPECT_NEAR (pt.pose .position .x , cos (-90.0 * M_PI / 180 ) * 10.0 , EPSILON);
253- EXPECT_NEAR (pt.pose .position .y , sin (-90.0 * M_PI / 180 ) * 10.0 , EPSILON);
376+ EXPECT_NEAR (pt.pose .position .x , 0.0 + cos (-90.0 * M_PI / 180 ) * 10.0 , EPSILON);
377+ EXPECT_NEAR (pt.pose .position .y , - 2.0 + sin (-90.0 * M_PI / 180 ) * 10.0 , EPSILON);
254378
255379 // 2 poses at -45°
256380 path.poses .clear ();
@@ -260,6 +384,6 @@ TEST(InterpolationUtils, lookaheadExtrapolation)
260384 path.poses [1 ].pose .position .x = 3.0 ;
261385 path.poses [1 ].pose .position .y = -3.0 ;
262386 pt = nav2_util::getLookAheadPoint (lookahead_dist, path, true );
263- EXPECT_NEAR (pt.pose .position .x , cos (-45.0 * M_PI / 180 ) * 10.0 , EPSILON);
264- EXPECT_NEAR (pt.pose .position .y , sin (-45.0 * M_PI / 180 ) * 10.0 , EPSILON);
387+ EXPECT_NEAR (pt.pose .position .x , 2.0 + cos (-45.0 * M_PI / 180 ) * 10.0 , EPSILON);
388+ EXPECT_NEAR (pt.pose .position .y , - 2.0 + sin (-45.0 * M_PI / 180 ) * 10.0 , EPSILON);
265389}
0 commit comments