Gazebo | Ignition | Community
Ask Your Question
0

where I can find contact_size() function

asked 2017-05-25 07:58:38 -0500

shawnysh gravatar image

In tutorial, there is a set of code shown below:

void ContactPlugin::OnUpdate()
{
  // Get all the contacts.
  msgs::Contacts contacts;
  contacts = this->parentSensor->GetContacts();
  for (unsigned int i = 0; i < contacts.contact_size(); ++i)
  {
    std::cout << "Collision between[" << contacts.contact(i).collision1()
              << "] and [" << contacts.contact(i).collision2() << "]\n";

    for (unsigned int j = 0; j < contacts.contact(i).position_size(); ++j)
    {
      std::cout << j << "  Position:"
                << contacts.contact(i).position(j).x() << " "
                << contacts.contact(i).position(j).y() << " "
                << contacts.contact(i).position(j).z() << "\n";
          std::cout << "   Normal:"
                << contacts.contact(i).normal(j).x() << " "
                << contacts.contact(i).normal(j).y() << " "
                << contacts.contact(i).normal(j).z() << "\n";
      std::cout << "   Depth:" << contacts.contact(i).depth(j) << "\n";
    }
  }
}

I wonder where I can find the API information about usage and definition of contact_size() function. I am not familiar with C++, is it because related to C++ library? Any hint or guide would be appreciated.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2017-05-25 10:41:02 -0500

chapulina gravatar image

Protobuf intro

Gazebo messages are generated using Protocol buffers (protobuf). The messages are described in .proto files, and Protobuf uses that file to generate a C++ class.

For example, this is the description for the contacts message:

import "contact.proto";
import "time.proto";

message Contacts
{
  repeated Contact contact   = 1;
  required Time time         = 2;
}

You can see that the contact field is a repeated field of the Contact (singular) message. For every repeated field, Protobuf generates a <field_name>_size() function, such as the contact_size() one.

This is all described in the Protocol Buffers C++ Generated Code documentation.

Header files

This is not ideal, but personally, what I do when I want to check a message's API, is to check its header files (who has time to read the protobuf documentation? :P).

Protobuf creates a <message_name>.pb.h header for each message and installs it in your system. You can look for the contacts.pb.h header like this:

find /usr | grep contacts.pb.h

Then you'll find something similar to this:

/usr/local/include/gazebo-7/gazebo/msgs/contacts.pb.h

So you can open that file and take a look at the API:

gedit /usr/local/include/gazebo-7/gazebo/msgs/contacts.pb.h

Digging through header files is definitely not ideal... We do have some form of message API documentation, but that's broken, as described on this issue.

edit flag offensive delete link more

Comments

Thanks for your quick, helpful and considerate reply!

shawnysh gravatar imageshawnysh ( 2017-05-26 00:15:09 -0500 )edit

Question Tools

Stats

Asked: 2017-05-25 07:58:38 -0500

Seen: 603 times

Last updated: May 25 '17