MultiRobotInfoVisual.cpp
Go to the documentation of this file.
2 
3 namespace tuw_multi_robot_rviz {
4  MultiRobotInfoVisual::MultiRobotInfoVisual(Ogre::SceneManager* _scene_manager, Ogre::SceneNode* _parent_node) : scene_manager_(_scene_manager), frame_node_(_parent_node->createChildSceneNode())
5  {
7  }
8 
10  {
11  scene_manager_->destroySceneNode(frame_node_);
12  }
13 
15  {
16  recycle_thresh_ = newDur;
17  }
18 
20  {
21  default_size_ = c;
22  for (auto &it : robot2pose_map_)
23  {
24  it.second.resize(default_size_);
25  }
26  }
27 
28  std::vector<std::string> MultiRobotInfoVisual::recycle()
29  {
30  std::vector<std::string> mark_for_deletion;
31  auto ts_now = ros::Time::now();
32  for (auto &elem : recycle_map_)
33  {
34  auto dur = ts_now - elem.second;
35  if (dur > recycle_thresh_)
36  {
37  mark_for_deletion.push_back(elem.first);
38  }
39  }
40 
41  for (const std::string &elem : mark_for_deletion)
42  {
43  auto it_pose = robot2pose_map_.find(elem);
44  if (it_pose != robot2pose_map_.end())
45  {
46  robot2pose_map_.erase(it_pose);
47  }
48 
49  auto it_arrows = robot_renderings_map_.find(elem);
50  if (it_arrows != robot_renderings_map_.end())
51  {
52  robot_renderings_map_.erase(it_arrows);
53  }
54 
55  auto it_rcle = recycle_map_.find(elem);
56  if (it_rcle != recycle_map_.end())
57  {
58  recycle_map_.erase(it_rcle);
59  }
60  }
61 
62  return std::move(mark_for_deletion);
63  }
64 
65  void MultiRobotInfoVisual::enableRobot(const std::string &rName)
66  {
67  auto it = disabled_robots_.find(rName);
68  if (it != disabled_robots_.end())
69  {
70  disabled_robots_.erase(it);
71  }
72  }
73 
74  void MultiRobotInfoVisual::disableRobot(const std::string &rName)
75  {
76  disabled_robots_.insert(rName);
77  auto it_arrow = robot_renderings_map_.find(rName);
78  if (robot_renderings_map_.end() != it_arrow)
79  {
80  //No need for detach objects call since this is handled in the SceneNode destructor
81  robot_renderings_map_.erase(it_arrow);
82  }
83  }
84 
85  std::vector<rviz::Object*> MultiRobotInfoVisual::make_robot(Ogre::Vector3 &position, Ogre::Quaternion &orientation)
86  {
87  rviz::Object* arrow_ptr;
88  //std::shared_ptr<rviz::BillboardLine> billboard_ptr;
89 
90  //Arrow
91  {
92  arrow_ptr = new rviz::Arrow(
93  this->scene_manager_,
94  this->frame_node_, 1.5,0.2,0.2,0.25);
95  arrow_ptr->setScale(Ogre::Vector3(1,1,1));
96  arrow_ptr->setColor(1,0,0,1);
97  }
98 
99  {
100  //billboard_ptr = std::make_shared<rviz::BillboardLine>(this->scene_manager_,
101  // this->frame_node_);
102  //billboard_ptr->setLineWidth(5.0);
103  //billboard_ptr->setMaxPointsPerLine(2);
104  //Ogre::Vector3 offset = Ogre::Vector3(1,0.0,0);
105  //double accuracy = 50;
106  //double factor = 2.0 * M_PI / accuracy;
107 
108  //billboard_ptr->setNumLines(static_cast<int>(accuracy));
109  //double theta=0.0;
110  //for (; theta < 2.0*M_PI; theta += factor) {
111  // double s_theta = sin(theta);
112  // double c_theta = cos(theta);
113  // billboard_ptr->addPoint(Ogre::Vector3(c_theta, 0, s_theta));
114  // theta += factor;
115  // s_theta = sin(theta);
116  // c_theta = cos(theta);
117  // billboard_ptr->addPoint(Ogre::Vector3(c_theta, 0, s_theta));
118  // billboard_ptr->newLine();
119  //}
120  }
121 
122  //billboard_ptr->setPosition(position);
123 
124  Ogre::Quaternion orient_x = Ogre::Quaternion(Ogre::Radian(-Ogre::Math::HALF_PI), Ogre::Vector3::UNIT_Y);
125  arrow_ptr->setPosition(position);
126  arrow_ptr->setOrientation(orientation * orient_x);
127 
128  return std::vector<rviz::Object*>{ arrow_ptr /*, billboard_ptr */};
129  }
130 
132  {
133  for (map_iterator it = robot2pose_map_.begin(); it != robot2pose_map_.end(); ++it)
134  {
135  if (disabled_robots_.find(it->first) != disabled_robots_.end())
136  {
137  return;
138  }
139 
140  //TODO: for now only the last pose is shown
141  const auto pose = it->second.front().pose;
142  //TODO: this needs to be done otherwise rviz crashes when all arrows are painted at position zero (which is a bad indicator for rviz...)
143  if (pose.position.x == 0 && pose.position.y == 0 && pose.position.z == 0)
144  continue;
145  Ogre::Vector3 position(pose.position.x, pose.position.y, pose.position.z);
146  Ogre::Quaternion orientation(pose.orientation.w,
147  pose.orientation.x,
148  pose.orientation.y,
149  pose.orientation.z);
150 
151  auto it_arrows = robot_renderings_map_.find(it->first);
152  if (it_arrows == robot_renderings_map_.end())
153  {
154  robot_renderings_map_.insert(std::pair<std::string,
155  std::vector<rviz::Object*>>(
156  it->first,
157  make_robot(position, orientation)));
158  } else {
159  for (int ii = 0; ii < it_arrows->second.size(); ++ii)
160  {
161  rviz::Object* r_obj = it_arrows->second[ii];
162  if (dynamic_cast<rviz::Arrow*>(r_obj))
163  {
164  Ogre::Quaternion orient_x = Ogre::Quaternion(Ogre::Radian(-Ogre::Math::HALF_PI), Ogre::Vector3::UNIT_Y);
165  r_obj->setPosition(position);
166  r_obj->setOrientation(orientation * orient_x);
167  }
168  else
169  {
170  r_obj->setPosition(position);
171  r_obj->setOrientation(orientation);
172  }
173  }
174  }
175  }
176 
178  }
179 
180  void MultiRobotInfoVisual::setMessage(const tuw_multi_robot_msgs::RobotInfoConstPtr _msg)
181  {
182  map_iterator it = robot2pose_map_.find(_msg->robot_name);
183 
184  if (it == robot2pose_map_.end())
185  {
186  robot2pose_map_.insert(internal_map_type(_msg->robot_name, boost::circular_buffer<geometry_msgs::PoseWithCovariance>(default_size_)));
187  it = robot2pose_map_.find(_msg->robot_name);
188  recycle_map_.insert(std::pair<std::string, ros::Time>(_msg->robot_name, ros::Time(0)));
189  }
190 
191  it->second.push_front(_msg->pose);
193  {
194  //std::cout << "Rendering now " << std::endl;
195  doRender();
196  //std::cout << "Rendering finished " << std::endl;
197  }
198 
199  //recycle_map_.find(_msg->robot_name)->second = ros::Time::now();
200  //auto recycled_robots_ = recycle();
201 
202  //Just for testing, this should never be needed.
203  //auto rName = _msg->robot_name;
204  //auto it_r = std::find_if(recycled_robots_.begin(), recycled_robots_.end(),
205  // [&rName](const std::string &n)
206  // {
207  // if (n==rName) {
208  // return true;
209  // }});
210 
211  //if (it_r != recycled_robots_.end())
212  //{
213  // return;
214  //}
215 
216  //Always store the current robot pose, even if the robot is not visualized currently therefore check below
217 
218 
219  //ROS_INFO("Tracking: %d poses", robot2pose_map_.size());
220  //ROS_INFO("Rendering %d robots", robot_renderings_map_.size());
221  //ROS_INFO("Disabled robots %d", disabled_robots_.size());
222 
223  }
224 
225  void MultiRobotInfoVisual::setFramePosition(const Ogre::Vector3& position)
226  {
227  frame_node_->setPosition(position);
228  }
229 
230  void MultiRobotInfoVisual::setFrameOrientation(const Ogre::Quaternion& orientation)
231  {
232  frame_node_->setOrientation(orientation);
233  }
234 
236  {
237  for (auto &it : robot_renderings_map_)
238  {
239  for (auto &renderings : it.second)
240  {
241  renderings->setScale(Ogre::Vector3(scale,scale,scale));
242  }
243  }
244  }
245 
246  void MultiRobotInfoVisual::setColorPose(Ogre::ColourValue color)
247  {
248  for (auto &it : robot_renderings_map_)
249  {
250  for (auto &renderings : it.second)
251  {
252  renderings->setColor(color.r,color.g,color.b,color.a);
253  }
254  }
255  }
256 }
virtual void setPosition(const Ogre::Vector3 &position)=0
void setMessage(const tuw_multi_robot_msgs::RobotInfoConstPtr _msg)
virtual void setOrientation(const Ogre::Quaternion &orientation)=0
void resetKeepMeasurementsCount(const unsigned int c)
virtual void setScale(const Ogre::Vector3 &scale)=0
std::vector< rviz::Object * > make_robot(Ogre::Vector3 &position, Ogre::Quaternion &orientation)
void setFramePosition(const Ogre::Vector3 &_position)
std::pair< std::string, boost::circular_buffer< geometry_msgs::PoseWithCovariance >> internal_map_type
MultiRobotInfoVisual(Ogre::SceneManager *_scene_manager, Ogre::SceneNode *_parent_node)
virtual void setColor(float r, float g, float b, float a)=0
static Time now()
std::map< std::string, boost::circular_buffer< geometry_msgs::PoseWithCovariance >>::iterator map_iterator
std::map< std::string, std::vector< rviz::Object * > > robot_renderings_map_
void setFrameOrientation(const Ogre::Quaternion &orientation)


tuw_multi_robot_rviz
Author(s): Benjamin Binder
autogenerated on Mon Jun 10 2019 15:42:40