13
13
// limitations under the License.
14
14
15
15
#include < gtest/gtest.h>
16
+ #include < quaternion_operation/quaternion_operation.h>
16
17
17
18
#include < cmath>
18
19
#include < geometry/polygon/line_segment.hpp>
@@ -95,35 +96,28 @@ TEST(LineSegment, getIntersection2DDisjoint)
95
96
EXPECT_FALSE (line0.getIntersection2D (line1));
96
97
}
97
98
98
- TEST (LineSegment, getIntersection2DIntersect )
99
+ TEST (LineSegment, getVector )
99
100
{
100
- const math::geometry::LineSegment line0 (makePoint (0 , 0 ), makePoint (1 , 1 ));
101
- const math::geometry::LineSegment line1 (makePoint (1 , 0 ), makePoint (0 , 1 ));
102
- auto ans = line0.getIntersection2D (line1);
103
- EXPECT_TRUE (ans);
104
- EXPECT_POINT_EQ (ans.value (), makePoint (0.5 , 0.5 ));
101
+ const math::geometry::LineSegment line (makePoint (1 , 2 , 3 ), makePoint (2 , 3 , 4 ));
102
+ EXPECT_POINT_EQ (line.getVector (), makeVector (1 , 1 , 1 ));
105
103
}
106
104
107
- TEST (LineSegment, getIntersection2DIdentical )
105
+ TEST (LineSegment, getVectorZeroLength )
108
106
{
109
- const math::geometry::LineSegment line (makePoint (0 , 0 ), makePoint (1 , 1 ));
110
- EXPECT_THROW (line.getIntersection2D (line), common::SimulationError);
111
- // auto ans = line.getIntersection2D(line);
112
- // EXPECT_TRUE(ans);
113
- // EXPECT_TRUE(std::isnan(ans.value().x));
114
- // EXPECT_TRUE(std::isnan(ans.value().y));
107
+ const math::geometry::LineSegment line (makePoint (1 , 2 , 3 ), makePoint (1 , 2 , 3 ));
108
+ EXPECT_POINT_EQ (line.getVector (), makeVector (0 , 0 , 0 ));
115
109
}
116
110
117
- TEST (LineSegment, getVector )
111
+ TEST (LineSegment, getNormalVector )
118
112
{
119
113
const math::geometry::LineSegment line (makePoint (1 , 2 , 3 ), makePoint (2 , 3 , 4 ));
120
- EXPECT_POINT_EQ (line.getVector (), makeVector (1 , 1 , 1 ));
114
+ EXPECT_VECTOR3_EQ (line.getNormalVector (), makeVector (- 1 , 1 , 0 ));
121
115
}
122
116
123
- TEST (LineSegment, getVectorZeroLength )
117
+ TEST (LineSegment, getNormalVector_zeroLength )
124
118
{
125
119
const math::geometry::LineSegment line (makePoint (1 , 2 , 3 ), makePoint (1 , 2 , 3 ));
126
- EXPECT_POINT_EQ (line.getVector (), makeVector (0 , 0 , 0 ));
120
+ EXPECT_VECTOR3_EQ (line.getNormalVector (), makeVector (0 , 0 , 0 ));
127
121
}
128
122
129
123
TEST (LineSegment, get2DVector)
@@ -174,12 +168,54 @@ TEST(LineSegment, getSlopeZeroLength)
174
168
EXPECT_TRUE (std::isnan (line.getSlope ()));
175
169
}
176
170
171
+ TEST (LineSegment, getSquaredDistanceIn2D)
172
+ {
173
+ const math::geometry::LineSegment line (makePoint (1 , 2 , 3 ), makePoint (3 , 4 , 5 ));
174
+ EXPECT_DOUBLE_EQ (line.getSquaredDistanceIn2D (makePoint (0 , 1 , 2 ), 0.5 , false ), 8.0 );
175
+ }
176
+
177
+ TEST (LineSegment, getSquaredDistanceIn2D_denormalize)
178
+ {
179
+ const math::geometry::LineSegment line (makePoint (1 , 2 , 3 ), makePoint (3 , 4 , 5 ));
180
+ EXPECT_DOUBLE_EQ (line.getSquaredDistanceIn2D (makePoint (0 , 1 , 2 ), std::sqrt (3.0 ), true ), 8.0 );
181
+ }
182
+
183
+ TEST (LineSegment, getSquaredDistanceVector)
184
+ {
185
+ const math::geometry::LineSegment line (makePoint (1 , 2 , 3 ), makePoint (3 , 4 , 5 ));
186
+ EXPECT_VECTOR3_EQ (
187
+ line.getSquaredDistanceVector (makePoint (0 , 1 , 2 ), 0.5 , false ), makeVector (-2 , -2 , -2 ));
188
+ }
189
+
190
+ TEST (LineSegment, getSquaredDistanceVector_denormalize)
191
+ {
192
+ const math::geometry::LineSegment line (makePoint (1 , 2 , 3 ), makePoint (3 , 4 , 5 ));
193
+ EXPECT_VECTOR3_EQ (
194
+ line.getSquaredDistanceVector (makePoint (0 , 1 , 2 ), std::sqrt (3.0 ), true ),
195
+ makeVector (-2 , -2 , -2 ));
196
+ }
197
+
177
198
TEST (LineSegment, getLineSegments)
178
199
{
179
200
const std::vector<geometry_msgs::msg::Point> points{
180
201
makePoint (1 , 2 , 3 ), makePoint (2 , 3 , 4 ), makePoint (3 , 4 , 5 ), makePoint (4 , 5 , 6 )};
181
202
const std::vector<math::geometry::LineSegment> lines =
182
- math::geometry::getLineSegments (points, true ); // close start and end
203
+ math::geometry::getLineSegments (points, false );
204
+ EXPECT_EQ (lines.size (), size_t (3 ));
205
+ EXPECT_POINT_EQ (lines[0 ].start_point , points[0 ]);
206
+ EXPECT_POINT_EQ (lines[0 ].end_point , points[1 ]);
207
+ EXPECT_POINT_EQ (lines[1 ].start_point , points[1 ]);
208
+ EXPECT_POINT_EQ (lines[1 ].end_point , points[2 ]);
209
+ EXPECT_POINT_EQ (lines[2 ].start_point , points[2 ]);
210
+ EXPECT_POINT_EQ (lines[2 ].end_point , points[3 ]);
211
+ }
212
+
213
+ TEST (LineSegment, getLineSegments_closeStartEnd)
214
+ {
215
+ const std::vector<geometry_msgs::msg::Point> points{
216
+ makePoint (1 , 2 , 3 ), makePoint (2 , 3 , 4 ), makePoint (3 , 4 , 5 ), makePoint (4 , 5 , 6 )};
217
+ const std::vector<math::geometry::LineSegment> lines =
218
+ math::geometry::getLineSegments (points, true );
183
219
EXPECT_EQ (lines.size (), size_t (4 ));
184
220
EXPECT_POINT_EQ (lines[0 ].start_point , points[0 ]);
185
221
EXPECT_POINT_EQ (lines[0 ].end_point , points[1 ]);
@@ -203,7 +239,22 @@ TEST(LineSegment, getLineSegmentsVectorIdentical)
203
239
geometry_msgs::msg::Point point = makePoint (1 , 2 , 3 );
204
240
const std::vector<geometry_msgs::msg::Point> points{point, point, point, point};
205
241
const std::vector<math::geometry::LineSegment> lines =
206
- math::geometry::getLineSegments (points, true ); // close start and end
242
+ math::geometry::getLineSegments (points, false );
243
+ EXPECT_EQ (lines.size (), size_t (3 ));
244
+ EXPECT_POINT_EQ (lines[0 ].start_point , point);
245
+ EXPECT_POINT_EQ (lines[0 ].end_point , point);
246
+ EXPECT_POINT_EQ (lines[1 ].start_point , point);
247
+ EXPECT_POINT_EQ (lines[1 ].end_point , point);
248
+ EXPECT_POINT_EQ (lines[2 ].start_point , point);
249
+ EXPECT_POINT_EQ (lines[2 ].end_point , point);
250
+ }
251
+
252
+ TEST (LineSegment, getLineSegmentsVectorIdentical_closeStartEnd)
253
+ {
254
+ geometry_msgs::msg::Point point = makePoint (1 , 2 , 3 );
255
+ const std::vector<geometry_msgs::msg::Point> points{point, point, point, point};
256
+ const std::vector<math::geometry::LineSegment> lines =
257
+ math::geometry::getLineSegments (points, true );
207
258
EXPECT_EQ (lines.size (), size_t (4 ));
208
259
EXPECT_POINT_EQ (lines[0 ].start_point , point);
209
260
EXPECT_POINT_EQ (lines[0 ].end_point , point);
@@ -215,8 +266,62 @@ TEST(LineSegment, getLineSegmentsVectorIdentical)
215
266
EXPECT_POINT_EQ (lines[3 ].end_point , point);
216
267
}
217
268
269
+ TEST (LineSegment, getSValue)
270
+ {
271
+ math::geometry::LineSegment line (makePoint (0 , 0 , 0 ), makePoint (3 , 3 , 3 ));
272
+ const auto s = line.getSValue (makePose (2 , 2 , 2 ), 1 , false );
273
+ EXPECT_TRUE (s);
274
+ if (s) {
275
+ EXPECT_DOUBLE_EQ (s.value (), 2.0 / 3.0 );
276
+ }
277
+ }
278
+
279
+ TEST (LineSegment, getSValue_denormalize)
280
+ {
281
+ math::geometry::LineSegment line (makePoint (0 , 0 , 0 ), makePoint (3 , 3 , 3 ));
282
+ const auto s = line.getSValue (makePose (2 , 2 , 2 ), 1 , true );
283
+ EXPECT_TRUE (s);
284
+ if (s) {
285
+ EXPECT_DOUBLE_EQ (s.value (), std::hypot (2.0 , 2.0 , 2.0 ));
286
+ }
287
+ }
288
+
289
+ TEST (LineSegment, getSValue_outOfRange)
290
+ {
291
+ math::geometry::LineSegment line (makePoint (0 , 0 , 0 ), makePoint (3 , 3 , 3 ));
292
+ const auto s = line.getSValue (makePose (4 , 4 , 4 ), 1 , false );
293
+ EXPECT_FALSE (s);
294
+ }
295
+
296
+ TEST (LineSegment, getSValue_outOfRangeDenormalize)
297
+ {
298
+ math::geometry::LineSegment line (makePoint (0 , 0 , 0 ), makePoint (3 , 3 , 3 ));
299
+ const auto s = line.getSValue (makePose (4 , 4 , 4 ), 1 , true );
300
+ EXPECT_FALSE (s);
301
+ }
302
+
303
+ TEST (LineSegment, getSValue_parallel)
304
+ {
305
+ math::geometry::LineSegment line (makePoint (0 , 0 , 0 ), makePoint (3 , 3 , 0 ));
306
+ const auto s = line.getSValue (
307
+ makePose (
308
+ 1 , 0 , 0 , quaternion_operation::convertEulerAngleToQuaternion (makeVector (0 , 0 , M_PI_4 * 3.0 ))),
309
+ 1000 , false );
310
+ EXPECT_FALSE (s);
311
+ }
312
+
313
+ TEST (LineSegment, getSValue_parallelDenormalize)
314
+ {
315
+ math::geometry::LineSegment line (makePoint (0 , 0 , 0 ), makePoint (3 , 3 , 0 ));
316
+ const auto s = line.getSValue (
317
+ makePose (
318
+ 1 , 0 , 0 , quaternion_operation::convertEulerAngleToQuaternion (makeVector (0 , 0 , M_PI_4 * 3.0 ))),
319
+ 1000 , true );
320
+ EXPECT_FALSE (s);
321
+ }
322
+
218
323
// / @brief In this test case, testing the `LineSegment::getPoint` function can find the point on the line segment with start point (x,y,z) = (0,0,0) and end point (x,y,z) = (1,1,1) in the cartesian coordinate system. (variable name `line`).
219
- TEST (LineSegmentTest , GetPoint)
324
+ TEST (LineSegment , GetPoint)
220
325
{
221
326
{
222
327
math::geometry::LineSegment line (
@@ -430,6 +535,63 @@ TEST(LineSegment, getIntersection2DSValue)
430
535
// [Snippet_getIntersection2DSValue_with_point_1_0_0]
431
536
// / @snippet test/test_line_segment.cpp Snippet_getIntersection2DSValue_with_point_1_0_0
432
537
538
+ { // parallel no denormalize
539
+ EXPECT_THROW (
540
+ line.getIntersection2DSValue (
541
+ math::geometry::LineSegment (
542
+ geometry_msgs::build<geometry_msgs::msg::Point>().x (0 ).y (-1 ).z (0 ),
543
+ geometry_msgs::build<geometry_msgs::msg::Point>().x (0 ).y (1 ).z (0 )),
544
+ false ),
545
+ common::SimulationError);
546
+ }
547
+ { // parallel denormalize
548
+ EXPECT_THROW (
549
+ line.getIntersection2DSValue (
550
+ math::geometry::LineSegment (
551
+ geometry_msgs::build<geometry_msgs::msg::Point>().x (0 ).y (-1 ).z (0 ),
552
+ geometry_msgs::build<geometry_msgs::msg::Point>().x (0 ).y (1 ).z (0 )),
553
+ true ),
554
+ common::SimulationError);
555
+ }
556
+ { // intersect no denormalize
557
+ const auto collision_s = line.getIntersection2DSValue (
558
+ math::geometry::LineSegment (
559
+ geometry_msgs::build<geometry_msgs::msg::Point>().x (-1 ).y (0.5 ).z (0 ),
560
+ geometry_msgs::build<geometry_msgs::msg::Point>().x (1 ).y (0.5 ).z (0 )),
561
+ false );
562
+ EXPECT_TRUE (collision_s);
563
+ if (collision_s) {
564
+ EXPECT_DOUBLE_EQ (collision_s.value (), 0.75 );
565
+ }
566
+ }
567
+ { // intersect denormalize
568
+ const auto collision_s = line.getIntersection2DSValue (
569
+ math::geometry::LineSegment (
570
+ geometry_msgs::build<geometry_msgs::msg::Point>().x (-1 ).y (0.5 ).z (0 ),
571
+ geometry_msgs::build<geometry_msgs::msg::Point>().x (1 ).y (0.5 ).z (0 )),
572
+ true );
573
+ EXPECT_TRUE (collision_s);
574
+ if (collision_s) {
575
+ EXPECT_DOUBLE_EQ (collision_s.value (), 1.5 );
576
+ }
577
+ }
578
+ { // no intersect no denormalize
579
+ const auto collision_s = line.getIntersection2DSValue (
580
+ math::geometry::LineSegment (
581
+ geometry_msgs::build<geometry_msgs::msg::Point>().x (-1 ).y (1.5 ).z (0 ),
582
+ geometry_msgs::build<geometry_msgs::msg::Point>().x (1 ).y (1.5 ).z (0 )),
583
+ false );
584
+ EXPECT_FALSE (collision_s);
585
+ }
586
+ { // no intersect denormalize
587
+ const auto collision_s = line.getIntersection2DSValue (
588
+ math::geometry::LineSegment (
589
+ geometry_msgs::build<geometry_msgs::msg::Point>().x (-1 ).y (1.5 ).z (0 ),
590
+ geometry_msgs::build<geometry_msgs::msg::Point>().x (1 ).y (1.5 ).z (0 )),
591
+ true );
592
+ EXPECT_FALSE (collision_s);
593
+ }
594
+
433
595
/* *
434
596
* @note Testing the `LineSegment::getIntersection2D` function can throw error getting intersection with exact same line segment.
435
597
* In this case, any s value can be a intersection point, so we cannot return single value.
@@ -459,9 +621,14 @@ TEST(LineSegment, getIntersection2DSValue)
459
621
// / @note @note Testing the `LineSegment::getIntersection2D` function can find collision with the line segment with start point (x,y,z) = (1,0,0) and end point (x,y,z) = (-1,0,0) in the cartesian coordinate system and `line`.
460
622
// [Snippet_getIntersection2D_line_1_0_0_-1_0_0]
461
623
{
462
- EXPECT_TRUE ( line.getIntersection2D (math::geometry::LineSegment (
624
+ const auto point = line.getIntersection2D (math::geometry::LineSegment (
463
625
geometry_msgs::build<geometry_msgs::msg::Point>().x (1 ).y (0 ).z (0 ),
464
- geometry_msgs::build<geometry_msgs::msg::Point>().x (-1 ).y (0 ).z (0 ))));
626
+ geometry_msgs::build<geometry_msgs::msg::Point>().x (-1 ).y (0 ).z (0 )));
627
+ EXPECT_TRUE (point);
628
+ if (point) {
629
+ EXPECT_POINT_EQ (
630
+ point.value (), geometry_msgs::build<geometry_msgs::msg::Point>().x (0 ).y (0 ).z (0 ));
631
+ }
465
632
}
466
633
// [Snippet_getIntersection2D_line_1_0_0_-1_0_0]
467
634
// / @snippet test/test_line_segment.cpp Snippet_getIntersection2D_line_1_0_0_-1_0_0
0 commit comments