Skip to content

Commit e437b44

Browse files
committed
Add and correct LineSegment test cases
Signed-off-by: Mateusz Palczuk <[email protected]>
1 parent 1add0ef commit e437b44

File tree

1 file changed

+189
-22
lines changed

1 file changed

+189
-22
lines changed

common/math/geometry/test/test_line_segment.cpp

Lines changed: 189 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include <gtest/gtest.h>
16+
#include <quaternion_operation/quaternion_operation.h>
1617

1718
#include <cmath>
1819
#include <geometry/polygon/line_segment.hpp>
@@ -95,35 +96,28 @@ TEST(LineSegment, getIntersection2DDisjoint)
9596
EXPECT_FALSE(line0.getIntersection2D(line1));
9697
}
9798

98-
TEST(LineSegment, getIntersection2DIntersect)
99+
TEST(LineSegment, getVector)
99100
{
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));
105103
}
106104

107-
TEST(LineSegment, getIntersection2DIdentical)
105+
TEST(LineSegment, getVectorZeroLength)
108106
{
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));
115109
}
116110

117-
TEST(LineSegment, getVector)
111+
TEST(LineSegment, getNormalVector)
118112
{
119113
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));
121115
}
122116

123-
TEST(LineSegment, getVectorZeroLength)
117+
TEST(LineSegment, getNormalVector_zeroLength)
124118
{
125119
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));
127121
}
128122

129123
TEST(LineSegment, get2DVector)
@@ -174,12 +168,54 @@ TEST(LineSegment, getSlopeZeroLength)
174168
EXPECT_TRUE(std::isnan(line.getSlope()));
175169
}
176170

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+
177198
TEST(LineSegment, getLineSegments)
178199
{
179200
const std::vector<geometry_msgs::msg::Point> points{
180201
makePoint(1, 2, 3), makePoint(2, 3, 4), makePoint(3, 4, 5), makePoint(4, 5, 6)};
181202
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);
183219
EXPECT_EQ(lines.size(), size_t(4));
184220
EXPECT_POINT_EQ(lines[0].start_point, points[0]);
185221
EXPECT_POINT_EQ(lines[0].end_point, points[1]);
@@ -203,7 +239,22 @@ TEST(LineSegment, getLineSegmentsVectorIdentical)
203239
geometry_msgs::msg::Point point = makePoint(1, 2, 3);
204240
const std::vector<geometry_msgs::msg::Point> points{point, point, point, point};
205241
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);
207258
EXPECT_EQ(lines.size(), size_t(4));
208259
EXPECT_POINT_EQ(lines[0].start_point, point);
209260
EXPECT_POINT_EQ(lines[0].end_point, point);
@@ -215,8 +266,62 @@ TEST(LineSegment, getLineSegmentsVectorIdentical)
215266
EXPECT_POINT_EQ(lines[3].end_point, point);
216267
}
217268

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+
218323
/// @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)
220325
{
221326
{
222327
math::geometry::LineSegment line(
@@ -430,6 +535,63 @@ TEST(LineSegment, getIntersection2DSValue)
430535
// [Snippet_getIntersection2DSValue_with_point_1_0_0]
431536
/// @snippet test/test_line_segment.cpp Snippet_getIntersection2DSValue_with_point_1_0_0
432537

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+
433595
/**
434596
* @note Testing the `LineSegment::getIntersection2D` function can throw error getting intersection with exact same line segment.
435597
* In this case, any s value can be a intersection point, so we cannot return single value.
@@ -459,9 +621,14 @@ TEST(LineSegment, getIntersection2DSValue)
459621
/// @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`.
460622
// [Snippet_getIntersection2D_line_1_0_0_-1_0_0]
461623
{
462-
EXPECT_TRUE(line.getIntersection2D(math::geometry::LineSegment(
624+
const auto point = line.getIntersection2D(math::geometry::LineSegment(
463625
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+
}
465632
}
466633
// [Snippet_getIntersection2D_line_1_0_0_-1_0_0]
467634
/// @snippet test/test_line_segment.cpp Snippet_getIntersection2D_line_1_0_0_-1_0_0

0 commit comments

Comments
 (0)