# This contains the position of a point in free space
float64 x
float64 y
float64 z
Now the geometry_msgs/Point[] points from Marker.msg is made of array of Point.msg. Now say i wanted to store the values (x=1,y=1,z=1) and (x=2,y=2,z=2) into the points array mentioned in Marker.msg, how would i do it? I am unsure how to work with arrays in messages.
Can someone share the basic idea / code on how to go about it?
If you are using C++ use a suitable container like list or vector with the correct datatype.
So, basically, a geometry_msgs/Point is a 3-value float64 data.
So you can initialize an array of Points as vector<geometry_msgs::Point> list_of_points;
Use vector to access elements by index. Use list if you just want to push data into an array and if you don’t want to edit the items in the array.
If you are using python - things get simpler - everything is just a list of dicts or just dicts or lists.
Accessing an array is just by calling the id of the data.
So, in case of array of Points, you will have a list or tuple of Points. Each Point would be a dict (since you have x, y, z structure) so you would essentially have a list of dicts (in most cases) or tuple of dicts.
this is a minimal example in python and ROS1. I have not tested this particular snipped myself, and I would say it’s probably not best practices to write the code as I did. But it should get you started and you can later improve upon it in several ways.
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import rospy
from visualization_msgs.msg import Marker
from std_msgs.msg import ColorRGBA
from geometry_msgs.msg import Point
from geometry_msgs.msg import Vector3
rospy.init_node("rviz_marker")
marker_pub = rospy.Publisher("marker", Marker, queue_size=1)
rviz_points = Marker()
rviz_points.header.frame_id = "map"
rviz_points.ns = "points"
rviz_points.id = 1
rviz_points.type = Marker.POINTS
rviz_points.action = Marker.ADD
rviz_points.color = ColorRGBA(1.0, 1.0, 0.0, 1.0)
rviz_points.scale = Vector3(0.2, 0.2, 0.2)
# Add as many points as required, or use a loop instead
rviz_points.points.append(Point(0, 0, 0))
rviz_points.points.append(Point(1, 5, 0))
rviz_points.points.append(Point(1, 7, 0))
while not rospy.is_shutdown():
marker_pub.publish(rviz_points)