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

How to publish an array of 3D points for visualization in RVIZ? [Solved]

asked 2016-04-06 08:39:35 -0600

PKumars gravatar image

updated 2016-04-07 05:04:16 -0600

Hello all, I did all the basic tutorials of visualization_msgs for publishing points as different shaped markers. is it possible for anyone to let me know how to publish an array of points so that I can see them in rviz in real time.

I have an array of 20 points in 3D and I want to publish them as visualization_msgs/markerArray. But I have no idea how to do that.

It will be a great help if anyone can provide a snippet in c++ for this purpose.

Thanks, Prasanna.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2016-04-06 08:52:14 -0600

NEngelhard gravatar image

updated 2016-04-06 08:54:29 -0600

You can use a simple Marker (no Markerarray!) and use the Points-Type ( ). The Marker ( ) has a points member that can be used to pass a list of points to RVIZ. If you want to have a nicer output you could also use the SPHERE_LIST although all spheres will have the same color.

edit flag offensive delete link more


Thanks for your quick reply. what if the points are not fixed and are changing and then I have just an array. I know only that it is of 3d type and I don'n want to change the points manually? the points are also not linear in spacing. I mean to say that the points of my array can be any where.

PKumars gravatar image PKumars  ( 2016-04-06 08:59:48 -0600 )edit

You can just republish your points after their position changed. How often do you want to update the position in RVIZ?

NEngelhard gravatar image NEngelhard  ( 2016-04-06 10:13:56 -0600 )edit

I want to publish all the points at once. is it possible? and then delete in next iteration.

PKumars gravatar image PKumars  ( 2016-04-06 10:45:12 -0600 )edit

Yes. You send one marker that contains your 20 points. If you publish again (with the same namespace and id), the old points are replaced by the new ones.

NEngelhard gravatar image NEngelhard  ( 2016-04-06 10:48:23 -0600 )edit

I'm sorry But I can't figure out how to do that. I followed this tutorial and used only points and not lines. Still now I'm unable to solve this problem.

PKumars gravatar image PKumars  ( 2016-04-06 11:08:58 -0600 )edit

Thanks for your kind and detailed suggestion. I solved the problem and I'm posting that as an answer.

PKumars gravatar image PKumars  ( 2016-04-07 04:59:02 -0600 )edit

answered 2016-04-06 11:03:35 -0600

lucasw gravatar image

updated 2016-04-06 11:04:28 -0600

There is example C++ marker array code in ros-visualization/rviz//src/test/marker_test.cpp

edit flag offensive delete link more


Hi Lucas, Thanks for your answer but is it so long and complicated? I need your explanation about which part of your code I nedd to use if I want to publish 20 points at once and want to see them in Rviz.

PKumars gravatar image PKumars  ( 2016-04-06 11:10:32 -0600 )edit

Skip down to the part which uses visualization_msgs::Marker::POINTS, the file is very long because it is testing all of the marker types.

lucasw gravatar image lucasw  ( 2016-04-06 11:26:00 -0600 )edit

Thanks Lucas for your kind help. I solved that and I'm posting that as an answer.

PKumars gravatar image PKumars  ( 2016-04-07 04:59:52 -0600 )edit

answered 2016-04-07 05:03:13 -0600

PKumars gravatar image

updated 2016-04-07 16:09:34 -0600

lucasw gravatar image

Here is the solution that I wanted. both of the above answers helped me in getting a beautiful output.

I'm posting the solution now.

#include <ros/ros.h>
#include <visualization_msgs/Marker.h>
#include <cmath>

int main( int argc, char** argv )
  ros::init(argc, argv, "spheres");
  ros::NodeHandle n;
  ros::Publisher marker_pub = n.advertise<visualization_msgs::Marker>("ball_trajectory", 10);

  ros::Rate r(30);

  float f = 0.0;
  while (ros::ok())

    visualization_msgs::Marker sphere_list;
    sphere_list.header.frame_id= "/my_frame";
    sphere_list.header.stamp= ros::Time::now();
    sphere_list.ns= "spheres";
    sphere_list.action= visualization_msgs::Marker::ADD;
    sphere_list.pose.orientation.w= 1.0; = 0;

    sphere_list.type = visualization_msgs::Marker::SPHERE_LIST;

    // POINTS markers use x and y scale for width/height respectively
    sphere_list.scale.x = 0.1;
    sphere_list.scale.y = 0.1;
    sphere_list.scale.z = 0.1;

    // Points are green
    sphere_list.color.r = 1.0f;
    sphere_list.color.a = 1.0;

    // Create the vertices for the points and lines
    for (uint32_t i = 0; i < 25; ++i)
      float y = sin(f + i / 100.0f * 2 * M_PI);
      float z = cos(f + i / 100.0f * 2 * M_PI);

      geometry_msgs::Point p;
      p.x = (int32_t)i*0.2;
      p.y = y;
      p.z = z;




    f += 0.04;

if there is any mistake or anyone has got any doubt regarding this code then please let me know. I will be happy to help and correct my mistakes.

EDIT Sorry I forgot to mention other details.

put this in CMakelists.txt add_executable(ball_trajectory src/ball_trajectory.cpp) target_link_libraries(ball_trajectory ${catkin_LIBRARIES})

where ball_trajectory is the .cpp file

then catkin_make

then in RVIZ do this.

  1. set Fixed Frame to my_frame. (Note:- if there is no option my_frame then edit yourself)
  2. Add ->options ->Marker
  3. set Marker Topic to ball_trajectory. (here visualization msg topic is ball_trajectory)

Note:- Do not get confused with .cpp file name and published topic name, can be changed easily and independent of each other.

Regards, Prasanna

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-04-06 08:39:35 -0600

Seen: 8,100 times

Last updated: Apr 07 '16