Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gz-math: Vector example #1371 #629

Merged
merged 5 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/vector3_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import gz.math8

v1 = gz.math8.Vector3d(0, 0, 3)
print("v =: {} {} {}\n".format(v1.x(), v1.y(), v1.z()))
print("v1 = {} {} {}\n".format(v1.x(), v1.y(), v1.z()))

v2 = gz.math8.Vector3d(4, 0, 0)
print("v2 = {} {} {}\n".format(v2.x(), v2.y(), v2.z()))
Expand Down
2 changes: 1 addition & 1 deletion examples/vector3_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
require 'gz/math'

v1 = Gz::Math::Vector3d.new(0, 0, 0)
printf("v =: %f %f %f\n", v1.X(), v1.Y(), v1.Z())
printf("v1 = %f %f %f\n", v1.X(), v1.Y(), v1.Z())

v2 = Gz::Math::Vector3d.new(1, 0, 0)
printf("v2 = %f %f %f\n", v2.X(), v2.Y(), v2.Z())
Expand Down
65 changes: 46 additions & 19 deletions tutorials/example_vector2.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This tutorial explains how to use the `Vector2` class from Gazebo Math library.

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

```{.sh}
```bash
git clone https://github.com/gazebosim/gz-math/ -b gz-math8
cd gz-math/examples
mkdir build
Expand All @@ -19,13 +19,13 @@ make

When the code is compiled, run:

```{.sh}
```bash
./vector2_example
```

The ouput of the program:
The output of the program:

```{.sh}
```bash
Vec2: 2 4
Vec2a: 1 2
Vec2b: 1.2 3.4
Expand All @@ -42,7 +42,7 @@ Vec2b: x=1.2 y=3.4

### Code

Create a `Vector2` called `vec2` of doubles using the typedef `Vector2d`. **The initial x and y values are zero**. The x and y component of `vec2` can be set at anytime.
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.

\snippet examples/vector2_example.cc constructor

Expand All @@ -67,7 +67,7 @@ The `Vector2` class overloads many common operators, such as:

\snippet examples/vector2_example.cc operators

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

\snippet examples/vector2_example.cc distance

Expand All @@ -78,68 +78,95 @@ There are also many useful function such as finding the distance between two vec
This example will only work if the Ruby interface library was compiled and installed. For example,
on Ubuntu:

```{.sh}
```bash
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`:

```{.sh}
```bash
export RUBYLIB=/usr/lib/ruby:$RUBYLIB
```

Move to the examples folder:

```{.sh}
```bash
cd examples
```

Execute the examples:
Execute the example:

```{.sh}
```bash
ruby vector2_example.rb
```

The output of the program:

```bash
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:

```bash
ruby vector3_example.rb
```

The output of the program:

```bash
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 a identical copy.
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.

```{.rb}
```bash
va = Gz::Math::Vector2d.new(1, 2)
```

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

```{.rb}
```bash
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:

```{.rb}
```bash
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.

```{.rb}
```bash
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`:

```{.rb}
```bash
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:

```{.rb}
printf("v =: %f %f %f\n", v1.X(), v1.Y(), v1.Z())
```bash
printf("v1 =: %f %f %f\n", v1.X(), v1.Y(), v1.Z())
```