marker_manager.cpp
Go to the documentation of this file.
2 
4 {
6 
8 
9 bool MarkerManager::addMarkerGroup(const std::string &group_key,
10  const std::string &topic_name)
11 {
12  if (marker_map_.find(group_key) != marker_map_.end())
13  {
14  ROS_ERROR_STREAM("MarkerManager: Tried to add already existing group "
15  << group_key);
16  return false;
17  }
18 
19  std::shared_ptr<
22  visualization_msgs::MarkerArray>(n_, topic_name, 1));
23  std::map<std::string, int> m;
24 
25  marker_pub_[group_key] = rt_pub;
26  marker_map_[group_key] = m;
27  marker_array_[group_key] = visualization_msgs::MarkerArray();
28  return true;
29 }
30 
31 bool MarkerManager::addMarker(const std::string &group_key,
32  const std::string &marker_name,
33  const std::string &ns,
34  const std::string &frame_id, MarkerType type)
35 {
36  if (marker_map_.find(group_key) == marker_map_.end())
37  {
38  return false;
39  }
40 
41  int id;
42  if (getMarkerId(group_key, marker_name, id))
43  {
44  ROS_ERROR_STREAM("MarkerManager: Tried to add marker "
45  << marker_name << " that is already on the marker array");
46  return false;
47  }
48 
49  visualization_msgs::Marker new_marker;
50  new_marker.header.frame_id = frame_id;
51  new_marker.header.stamp = ros::Time();
52  new_marker.ns = ns;
53 
54  if (type == sphere)
55  {
56  new_marker.type = new_marker.SPHERE;
57  }
58  else
59  {
60  new_marker.type = new_marker.ARROW;
61  }
62 
63  new_marker.action = new_marker.ADD;
64  new_marker.scale.x = 0.01;
65  new_marker.scale.y = 0.01;
66  new_marker.scale.z = 0.01;
67  new_marker.lifetime = ros::Duration(0);
68  new_marker.frame_locked = false;
69  new_marker.color.r = 1.0;
70  new_marker.color.a = 1.0;
71  new_marker.pose.orientation.w = 1.0;
72 
73  int max_id = -1;
74  for (auto const &entry : marker_map_.at(group_key))
75  {
76  if (entry.second > max_id)
77  {
78  max_id = entry.second;
79  }
80  }
81 
82  new_marker.id = max_id + 1;
83 
84  marker_map_.at(group_key)[marker_name] = max_id + 1;
85  marker_array_.at(group_key).markers.push_back(new_marker);
86 
87  return true;
88 }
89 
90 bool MarkerManager::setMarkerColor(const std::string &group_key,
91  const std::string &marker_name, double r,
92  double g, double b)
93 {
94  if (marker_map_.find(group_key) == marker_map_.end())
95  {
96  return false;
97  }
98 
99  int id;
100  if (!getMarkerId(group_key, marker_name, id))
101  {
102  return false;
103  }
104 
105  marker_array_.at(group_key).markers[id].color.r = r;
106  marker_array_.at(group_key).markers[id].color.g = g;
107  marker_array_.at(group_key).markers[id].color.b = b;
108  return true;
109 }
110 
111 bool MarkerManager::setMarkerScale(const std::string &group_key,
112  const std::string &marker_name, double x,
113  double y, double z)
114 {
115  if (marker_map_.find(group_key) == marker_map_.end())
116  {
117  return false;
118  }
119 
120  int id;
121  if (!getMarkerId(group_key, marker_name, id))
122  {
123  return false;
124  }
125 
126  marker_array_.at(group_key).markers[id].scale.x = x;
127  marker_array_.at(group_key).markers[id].scale.y = y;
128  marker_array_.at(group_key).markers[id].scale.z = z;
129  return true;
130 }
131 
132 bool MarkerManager::setMarkerPoints(const std::string &group_key,
133  const std::string &marker_name,
134  const Eigen::Vector3d &initial_point,
135  const Eigen::Vector3d &final_point)
136 {
137  if (marker_map_.find(group_key) == marker_map_.end())
138  {
139  return false;
140  }
141 
142  int id;
143  if (!getMarkerId(group_key, marker_name, id))
144  {
145  return false;
146  }
147 
148  geometry_msgs::Point point;
149  marker_array_.at(group_key).markers[id].points.clear();
150  tf::pointEigenToMsg(initial_point, point);
151  marker_array_.at(group_key).markers[id].points.push_back(point);
152  tf::pointEigenToMsg(final_point, point);
153  marker_array_.at(group_key).markers[id].points.push_back(point);
154  return true;
155 }
156 
157 bool MarkerManager::setMarkerPose(const std::string &group_key,
158  const std::string &marker_name,
159  const Eigen::Affine3d &pose)
160 {
161  if (marker_map_.find(group_key) == marker_map_.end())
162  {
163  return false;
164  }
165 
166  int id;
167  if (!getMarkerId(group_key, marker_name, id))
168  {
169  return false;
170  }
171 
172  tf::poseEigenToMsg(pose, marker_array_.at(group_key).markers[id].pose);
173  return true;
174 }
175 
177 {
178  for (auto const &publisher : marker_pub_)
179  {
180  if (publisher.second->trylock())
181  {
182  publisher.second->msg_ = marker_array_.at(publisher.first);
183  publisher.second->unlockAndPublish();
184  }
185  }
186 }
187 
188 bool MarkerManager::getMarkerId(const std::string &group_key,
189  const std::string &marker_name, int &id) const
190 {
191  if (marker_map_.find(group_key) == marker_map_.end())
192  {
193  return false;
194  }
195 
196  for (auto const &it : marker_map_.at(group_key))
197  {
198  if (it.first == marker_name)
199  {
200  for (unsigned long i = 0; i < marker_array_.at(group_key).markers.size();
201  i++)
202  {
203  if (marker_array_.at(group_key).markers[i].id == it.second)
204  {
205  id = it.second;
206  return true;
207  }
208  }
209  }
210  }
211 
212  ROS_ERROR_STREAM("MarkerManager: Marker name " << marker_name
213  << " not found");
214  return false;
215 }
216 } // namespace generic_control_toolbox
bool setMarkerPoints(const std::string &group_key, const std::string &marker_name, const Eigen::Vector3d &initial_point, const Eigen::Vector3d &final_point)
std::map< std::string, std::map< std::string, int > > marker_map_
std::map< std::string, visualization_msgs::MarkerArray > marker_array_
void poseEigenToMsg(const Eigen::Affine3d &e, geometry_msgs::Pose &m)
bool addMarkerGroup(const std::string &group_key, const std::string &topic_name)
bool setMarkerPose(const std::string &group_key, const std::string &marker_name, const Eigen::Affine3d &pose)
bool setMarkerColor(const std::string &group_key, const std::string &marker_name, double r, double g, double b)
bool addMarker(const std::string &group_key, const std::string &marker_name, const std::string &ns, const std::string &frame_id, MarkerType type)
void pointEigenToMsg(const Eigen::Vector3d &e, geometry_msgs::Point &m)
std::map< std::string, std::shared_ptr< realtime_tools::RealtimePublisher< visualization_msgs::MarkerArray > > > marker_pub_
bool getMarkerId(const std::string &group_key, const std::string &marker_name, int &id) const
bool setMarkerScale(const std::string &group_key, const std::string &marker_name, double x, double y, double z)
#define ROS_ERROR_STREAM(args)


generic_control_toolbox
Author(s): diogo
autogenerated on Thu Jun 6 2019 19:54:44