Different color for each cube in CUBE_LIST using std_msgs/ColorRGBA[]
Hey there,
I have a problem creating a CUBE_LIST with different colors for each cube. Below is my code. The strange thing is, that when setColor is set to true, all cubes are drawn in the same color (as expected in green), but when it is set to false, nothing is shown at all. How exactly is the use of the markerMsg attribute colors? Do you see the mistake I made? In the documentation it states "In visualization 1.1+ will also optionally use the colors member for per-vertex color.". How can I find out which version of visualization I am using? I have ros fuerte installed.
ros::Publisher pubTrackMarkers;
pubTrackMarkers = n.advertise<visualization_msgs::Marker>("/track_markers",queue_size, true);
visualization_msgs::Marker markerMsg;
markerMsg.header.frame_id = "/trackMarkers_frame";
markerMsg.ns = "trackMarkers";
markerMsg.action = visualization_msgs::Marker::ADD;
markerMsg.pose.orientation.w = 1.0;
markerMsg.type = visualization_msgs::Marker::CUBE_LIST;
markerMsg.scale.x = 0.8f;
markerMsg.scale.y = 0.8f;
markerMsg.scale.z = 0.8f;
if (setColor){
markerMsg.color.r = 0;
markerMsg.color.g = 1;
markerMsg.color.b = 0;
markerMsg.color.a = 1;
}
for (int i=0; i<10; i++){
geometry_msgs::Point temp;
temp.x = i;
temp.y = 1;
temp.z = 0.5;
markerMsg.points.push_back(temp);
std_msgs::ColorRGBA c;
c.r = (float)i/10.0;
c.g = 0;
c.b = 0;
c.a = 1;
markerMsg.colors.push_back(c);
}
pubTrackMarkers.publish(markerMsg);
markerMsg->points.clear();
markerMsg->colors.clear();
Thank you so much for your help! Jens
edit: solved! added the line "markerMsg->colors.clear();" now the number of color values and points is the same and it works. Thanks everyone!
Since i is in the range of zero to nine, could the value you are setting in the red channel be rounding down to zero?
Good point, but that's not it. Changed it to a switch case statement with 5 "hard coded" colors, still not working. Shouldn't the boxes still be drawn but black then? I just added clearing the colors vector in the markerMsg, as I forget that earlier. That was it!! Thanks everybody for the helpt!
I have some problem about with CUBE_LIST marker. I use your code, set setColor=true, and I only get 10 points in the RVIZ, not ten cube.
I tried to write my own code to display CUBE_LIST, but still can't get correct result. Is there something error with my RVIZ?
@RobustControl This question is very old and most answers you see might be outdated. Are you setting the scale field for your cubes?