34 #include <gtest/gtest.h> 39 using nav_2d_msgs::Polygon2D;
43 TEST(array_parser, basic_operation)
45 std::vector<std::vector<double> > vvd;
46 vvd =
parseVVD(
"[[1, 2.2], [.3, -4e4]]");
47 EXPECT_DOUBLE_EQ(2U, vvd.size());
48 EXPECT_DOUBLE_EQ(2U, vvd[0].size());
49 EXPECT_DOUBLE_EQ(2U, vvd[1].size());
50 EXPECT_DOUBLE_EQ(1.0, vvd[0][0]);
51 EXPECT_DOUBLE_EQ(2.2, vvd[0][1]);
52 EXPECT_DOUBLE_EQ(0.3, vvd[1][0]);
53 EXPECT_DOUBLE_EQ(-40000.0, vvd[1][1]);
56 TEST(array_parser, missing_open)
61 TEST(array_parser, missing_close)
66 TEST(array_parser, wrong_depth)
71 TEST(Polygon2D, radius_param)
75 ASSERT_EQ(16U, footprint.points.size());
78 EXPECT_EQ(10.0, footprint.points[0].x);
79 EXPECT_EQ(0.0, footprint.points[0].y);
82 EXPECT_NEAR(0.0, footprint.points[4].x, 0.0001);
83 EXPECT_NEAR(10.0, footprint.points[4].y, 0.0001);
86 TEST(Polygon2D, string_param)
89 ASSERT_EQ(3U, footprint.points.size());
91 EXPECT_EQ(1.0, footprint.points[ 0 ].x);
92 EXPECT_EQ(1.0, footprint.points[ 0 ].y);
94 EXPECT_EQ(-1.0, footprint.points[ 1 ].x);
95 EXPECT_EQ(1.0, footprint.points[ 1 ].y);
97 EXPECT_EQ(-1.0, footprint.points[ 2 ].x);
98 EXPECT_EQ(-1.0, footprint.points[ 2 ].y);
101 TEST(Polygon2D, broken_string_param)
124 std::vector<double> xs = {1, -1, -1};
125 std::vector<double> ys = {1, 1, -1};
127 ASSERT_EQ(3U, footprint.points.size());
129 EXPECT_EQ(1.0, footprint.points[ 0 ].x);
130 EXPECT_EQ(1.0, footprint.points[ 0 ].y);
132 EXPECT_EQ(-1.0, footprint.points[ 1 ].x);
133 EXPECT_EQ(1.0, footprint.points[ 1 ].y);
135 EXPECT_EQ(-1.0, footprint.points[ 2 ].x);
136 EXPECT_EQ(-1.0, footprint.points[ 2 ].y);
141 std::vector<double> shorty = {1, -1};
142 std::vector<double> three = {1, 1, -1};
143 std::vector<double> four = {1, 1, -1, -1};
150 Polygon2D square =
polygonFromString(
"[[0.5, 0.5], [0.5, -0.5], [-0.5, -0.5], [-0.5, 0.5]]");
151 geometry_msgs::Pose2D pose;
156 pose.theta = M_PI / 4;
158 ASSERT_EQ(4U, diamond.points.size());
159 double side = 1.0 / sqrt(2);
161 EXPECT_DOUBLE_EQ(pose.x, diamond.points[ 0 ].x);
162 EXPECT_DOUBLE_EQ(pose.y + side, diamond.points[ 0 ].y);
163 EXPECT_DOUBLE_EQ(pose.x + side, diamond.points[ 1 ].x);
164 EXPECT_DOUBLE_EQ(pose.y, diamond.points[ 1 ].y);
165 EXPECT_DOUBLE_EQ(pose.x, diamond.points[ 2 ].x);
166 EXPECT_DOUBLE_EQ(pose.y - side, diamond.points[ 2 ].y);
167 EXPECT_DOUBLE_EQ(pose.x - side, diamond.points[ 3 ].x);
168 EXPECT_DOUBLE_EQ(pose.y, diamond.points[ 3 ].y);
173 Polygon2D square =
polygonFromString(
"[[0.5, 0.5], [0.5, -0.5], [-0.5, -0.5], [-0.5, 0.5]]");
181 int main(
int argc,
char** argv)
183 testing::InitGoogleTest(&argc, argv);
184 return RUN_ALL_TESTS();
std::vector< std::vector< double > > parseVVD(const std::string &input)
Parse a vector of vectors of doubles from a string. Syntax is [[1.0, 2.0], [3.3, 4.4, 5.5], ...].
bool equals(const nav_2d_msgs::Polygon2D &polygon0, const nav_2d_msgs::Polygon2D &polygon1)
check if two polygons are equal. Used in testing
Exception to throw when Polygon doesn't load correctly.
nav_2d_msgs::Polygon2D polygonFromRadius(const double radius, const unsigned int num_points=16)
Create a "circular" polygon from a given radius.
TEST(array_parser, basic_operation)
nav_2d_msgs::Polygon2D polygonFromParallelArrays(const std::vector< double > &xs, const std::vector< double > &ys)
Create a polygon from two parallel arrays.
int main(int argc, char **argv)
nav_2d_msgs::Polygon2D polygonFromString(const std::string &polygon_string)
Make a polygon from the given string. Format should be bracketed array of arrays of doubles...
nav_2d_msgs::Polygon2D movePolygonToPose(const nav_2d_msgs::Polygon2D &polygon, const geometry_msgs::Pose2D &pose)
Translate and rotate a polygon to a new pose.
bool isInside(const nav_2d_msgs::Polygon2D &polygon, const double x, const double y)
Check if a given point is inside of a polygon.