ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to correctly declare a Polygon message

asked 2014-03-26 21:42:45 -0600

Maya gravatar image

updated 2016-10-24 08:59:42 -0600

ngrennan gravatar image

Hellow,

Easy question, I'm not able to declare a Polygone message that does not core dump. Here is my code for now :

geometry_msgs::Point32 tl;
polyStamp.polygon.points[0]=tl;

I have the doc of the Polygon Stamped and the Polygon but I guess I'm reading it really badly.

I tried declaring a table of Point32 as well and directly passing it to polygon.points but it wont compile then.

Thanks a lot for any answer.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2014-03-26 22:20:03 -0600

ahendrix gravatar image

You need to pre-allocate the points array before you assign members into it, or use a function that does the appropriate bounds-checking and resize for you. In particular, arrays in ROS messages in C++ are implemented as std::vectors, and std::vector::operator[] doesn't do any bounds checking.

If you just want to append items to your array, try:

geometry_msgs::Point32 tl;
polyStamp.polygon.points.push_back(tl);

If you already know how big your array is going to be, and you want to assign to specific indicies, try:

geometry_msgs::Point32 tl;
polyStamp.polygon.points.reserve(10); // ensure that there will be space for at least 10 points
polyStamp.polygon.points[0]=tl;
edit flag offensive delete link more

Comments

Thanks a lot ! I didn't realize they were implemented as vectors !

Maya gravatar image Maya  ( 2014-03-26 22:29:17 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2014-03-26 21:42:45 -0600

Seen: 4,989 times

Last updated: Mar 26 '14