Home | Tutorials | Wiki | Issues
Ask Your Question
3

how to draw lines from a plugin or via messages

asked 2012-10-11 04:00:53 -0500

Karandaras gravatar image

Hello,

can anyone tell me how to draw lines via messages or from a plugin? I already tried DynamicLines and Scene->DrawLine from inside a world plugin but without results. It either crashed the server or nothing happened at all.

Thanks for the help and best regards Bastian

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2012-10-21 07:09:58 -0500

Karandaras gravatar image

updated 2013-01-05 04:57:52 -0500

Thanks for the answer. I already added a simple way of drawing to the Scene class. It is probably not the best way, but it works for me and allows me to continue my work.

Here is the code I used for that.

Added needed stuff to the Scene header

gazebo/rendering/Scene.hh

/// \brief Subscribe to draw for line drawing
private: transport::SubscriberPtr drawingSub;

/// \brief Drawing callback
/// \param[in] _msg The message data.
private: void OnDrawing(ConstDrawingPtr &_msg);

/// \brief Process a drawing message
/// \param[in] _msg The message data.
private: bool ProcessDrawingMsg(ConstDrawingPtr &_msg);

/// \def DrawingMsgs_L
/// \brief List of drawing messages.
typedef std::list<boost::shared_ptr<msgs::Drawing const> > DrawingMsgs_L;

/// \brief List of drawing message to process.
private: DrawingMsgs_L drawingMsgs;

New functions for the drawing

gazebo/rendering/Scene.cc

/////////////////////////////////////////////////
void Scene::OnDrawing(ConstDrawingPtr &_msg)
{
  boost::mutex::scoped_lock lock(*this->receiveMutex);
  this->drawingMsgs.push_back(_msg);
}

/////////////////////////////////////////////////
bool Scene::ProcessDrawingMsg(ConstDrawingPtr &_msg)
{
  Ogre::SceneNode *sceneNode = NULL;
  Ogre::ManualObject *obj = NULL;
  bool attached = false;

  if (this->manager->hasManualObject(_msg->name()))
  {
    sceneNode = this->manager->getSceneNode(_msg->name());
    obj = this->manager->getManualObject(_msg->name());
    attached = true;
  }
  else
  {
    sceneNode = this->manager->getRootSceneNode()->createChildSceneNode(_msg->name());
    obj = this->manager->createManualObject(_msg->name());
  }

  sceneNode->setVisible(_msg->visible());
  obj->setVisible(_msg->has_visible());
  obj->estimateVertexCount(_msg->point_size());

  obj->clear();
  std::string material = "Gazebo/Red";
  if(_msg->has_material()) {
    material = _msg->material();
  }

  if(!_msg->has_mode())
    obj->begin(material, Ogre::RenderOperation::OT_LINE_LIST);
  else if(_msg->mode() == msgs::Drawing::POINT_LIST)
    obj->begin(material, Ogre::RenderOperation::OT_POINT_LIST);
  else if(_msg->mode() == msgs::Drawing::LINE_LIST)
    obj->begin(material, Ogre::RenderOperation::OT_LINE_LIST);
  else if(_msg->mode() == msgs::Drawing::LINE_STRIP)
    obj->begin(material, Ogre::RenderOperation::OT_LINE_STRIP);
  else if(_msg->mode() == msgs::Drawing::TRIANGLE_LIST)
    obj->begin(material, Ogre::RenderOperation::OT_TRIANGLE_LIST);
  else if(_msg->mode() == msgs::Drawing::TRIANGLE_STRIP)
    obj->begin(material, Ogre::RenderOperation::OT_TRIANGLE_STRIP);
  else if(_msg->mode() == msgs::Drawing::TRIANGLE_FAN)
    obj->begin(material, Ogre::RenderOperation::OT_TRIANGLE_FAN);

  int p = _msg->point_size();
  for(int i=0; i<p; i++) {
    obj->position(_msg->point(i).position().x(), _msg->point(i).position().y(), _msg->point(i).position().z());
    if(_msg->point(i).has_color())
      obj->colour(Conversions::Convert(msgs::Convert(_msg->point(i).color())));
  }
  obj->end();

  if (!attached)
    sceneNode->attachObject(obj);

  if(_msg->has_pose()) {
    sceneNode->setPosition(Conversions::Convert(msgs::Convert(_msg->pose().position())));
    sceneNode->setOrientation(Conversions::Convert(msgs::Convert(_msg->pose().orientation())));
  }

  return true;
}

Changes adding the new function to be processed during prerender

gazebo/rendering/Scene.cc

void Scene::PreRender()
{
  // ...
  static SensorMsgs_L::iterator sensorIter;
  static LinkMsgs_L::iterator linkIter;
  static DrawingMsgs_L::iterator drawingIter;

  // ...
  // Process the link messages
  linkIter = this->linkMsgs.begin();
  while (linkIter != this->linkMsgs.end())
  {
    if (this->ProcessLinkMsg(*linkIter))
      this->linkMsgs.erase(linkIter++);
    else
      ++linkIter;
  }

  // Process the drawing messages
  drawingIter = this->drawingMsgs.begin();
  while (drawingIter != this->drawingMsgs.end())
  {
    if (this->ProcessDrawingMsg(*drawingIter))
      this->drawingMsgs.erase(drawingIter++);
    else
      ++drawingIter;
  }
  // ...
}

Registering the new subscriber

gazebo/rendering/Scene.cc

Scene::Scene(const std::string &_name, bool _enableVisualizations)
{
  // ...
  this->sceneSub = this->node->Subscribe("~/scene", &Scene::OnScene, this);

  this->drawingSub = this->node->Subscribe("~/draw", &Scene::OnDrawing, this);
  // ...
}

Message definition

gazebo/msgs/drawing.proto

package gazebo.msgs;

/// \ingroup gazebo_msgs ...
(more)
edit flag offensive delete link more

Comments

I would be glad to hear if my code got used. Keep up the good work with the simulator.

Karandaras gravatar imageKarandaras ( 2013-01-05 04:58:42 -0500 )edit

I will start integrating you code shortly. For future reference, it's better to submit changes through a pull request on our bitbucket site: http://bitbucket.org/osrf/gazebo.

nkoenig gravatar imagenkoenig ( 2013-01-09 15:57:26 -0500 )edit
0

answered 2012-10-20 12:11:20 -0500

nkoenig gravatar image

Hello,

There is currently no clean mechanism to draw visuals from a plugin or via a message.

The problem you're seeing, with trying to use DynamicLines within a World plugin, is the the physics sever (where your plugin lives) is in a separate process from the rendering engine.

Currently on our list of things to finish is a message-based system to draw visuals, which would suite your needs.

It will likely be a few weeks before this feature is implemented. However, I can help you get started if you want to tackle it yourself.

-nate

edit flag offensive delete link more

Comments

Thanks for the answer.

Karandaras gravatar imageKarandaras ( 2012-10-21 06:46:00 -0500 )edit
1

@nkoenig: Has the drawing mechanism in the end been integrated in Gazebo?

dejanpan gravatar imagedejanpan ( 2013-04-20 01:31:42 -0500 )edit

I guess not: the ticket says the issue is on hold. This is something I'd love to see. My use case: I'd like to use Gazebo to generate figures for my thesis. Since I'll be using screenshots rather than movies, I'd like to be able to draw the trajectories that are being traced by end-effectors.

Ben B gravatar imageBen B ( 2013-07-03 20:25:59 -0500 )edit
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2012-10-11 04:00:53 -0500

Seen: 1,551 times

Last updated: Jan 05 '13