Gazebo Math

API Reference

8.0.0
Vector example

This tutorial explains how to use the Vector2 class from Gazebo Math library.

C++ example

Compile the code

To compile the code, go to gz-math/examples and use cmake:

git clone https://github.com/gazebosim/gz-math/ -b gz-math8
cd gz-math/examples
mkdir build
cd build
cmake ..
# Linux and macOS
make
# Windows
cd ..
cmake --build build --config Release

When the code is compiled, run:

# Linux and macOS
./vector2_example
# Windows
.\build\Release\vector2_example.exe

The output of the program:

Vec2: 2 4
Vec2a: 1 2
Vec2b: 1.2 3.4
Vec2: x=2 y=4
Vec2a: x=1 y=2
Vec2b: x=1.2 y=3.4
4
2 8
3 6
1 2
2 2
2.23607

Code

Create a Vector2 called vec2 of doubles using the typedef Vector2d. The initial x and y values are zero. The x and y components of vec2 can be set at anytime.

vec2.Set(2.0, 4.0);

The Vector2 class is a template, so you can also create a Vector2 using gz::math::Vector2<double>:

vec2a.Set(1.0, 2.0);

It's also possible to set initial values. Here we are using a Vector2 of floats:

gz::math::Vector2f vec2b(1.2f, 3.4f);

We can output the contents of each vector using std::cout.

std::cout << "Vec2: " << vec2 << "\n"
<< "Vec2a: " << vec2a << "\n"
<< "Vec2b: " << vec2b << "\n";

You can also get access to each component in the vector using the X(), Y() accessors or the [] operator, The operator is clamped to the range [0, 1].

std::cout << "Vec2: x=" << vec2.X() << " y=" << vec2.Y() << "\n";
std::cout << "Vec2a: x=" << vec2a[0] << " y=" << vec2a[1] << "\n";
std::cout << "Vec2b: x=" << vec2b.X() << " y=" << vec2b[1] << "\n";

The Vector2 class overloads many common operators, such as:

std::cout << vec2 * vec2a << "\n"
<< vec2 + vec2a << "\n"
<< vec2 - vec2a << "\n"
<< vec2 / vec2a << "\n";

There are also many useful functions, such as finding the distance between two vectors.

std::cout << vec2.Distance(vec2a) << std::endl;

There are more functions in Vector2. Take a look at the API

Ruby examples

This example will only work if the Ruby interface library was compiled and installed. For example, on Ubuntu:

sudo apt install ruby-gz-math<#>

Be sure to replace <#> with a number value, such as 8 or 7, depending on which version you need.<#>.

Modify the RUBYLIB environment variable to include the Gazebo Math library install path. For example, if you install to /usr:

export RUBYLIB=/usr/lib/ruby:$RUBYLIB

Move to the examples folder:

cd examples

Execute the example:

ruby vector2_example.rb

The output of the program:

va = 1.000000 2.000000
vb = 3.000000 4.000000
vc = 3.000000 4.000000
vb += va: 4.000000 6.000000
vb.Normalize = 0.554700 0.832050
vb.Distance(va) = 1.249959

Execute the example:

ruby vector3_example.rb

The output of the program:

v1 = 0.000000 0.000000 0.000000
v2 = 1.000000 0.000000 0.000000
v1 + v2 = 1.000000 0.000000 0.000000
v1.Distance(v2) = 1.000000

Code

Create a Vector2 of doubles using the typedef Vector2d. It's possible to set initial values or use another object to create an identical copy.

va = Gz::Math::Vector2d.new(1, 2)

You can get access to each component in the vector using the X(), Y() accessors.

printf("va = %f %f\n", va.X(), va.Y())
printf("vb = %f %f\n", vb.X(), vb.Y())
printf("vc = %f %f\n", vc.X(), vc.Y())

The Vector2 class overloads many common operators, such as:

vb += va
printf("vb += va: %f %f\n", vb.X(), vb.Y())

There are also many useful functions, such as finding the distance between two vectors or normalizing a vector.

vb.Normalize
printf("vb.Normalize = %f %f\n", vb.X(), vb.Y())
printf("vb.Distance(va) = %f\n", vb.Distance(va))

You can create vectors with 3 dimensions using the typedef Vector3d:

v1 = Gz::Math::Vector3d.new(0, 0, 0)

You can also get access to each component in the vector using the X(), Y() and Z() accessors:

printf("v1 =: %f %f %f\n", v1.X(), v1.Y(), v1.Z())