diff --git a/Changelog.md b/Changelog.md index 324e54232..6d50ee8d7 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,15 @@ ## Gazebo Math 8.x -### Gazebo Math 8.0.0 (2024-09-XX) +### Gazebo Math 8.0.0 (2024-09-25) + +1. Miscellaneous documentation fixes + * [Pull request #632](https://github.com/gazebosim/gz-math/pull/632) + * [Pull request #631](https://github.com/gazebosim/gz-math/pull/631) + * [Pull request #630](https://github.com/gazebosim/gz-math/pull/630) + * [Pull request #628](https://github.com/gazebosim/gz-math/pull/628) + +1. Fix Vector examples + * [Pull request #629](https://github.com/gazebosim/gz-math/pull/629) 1. Added CoordinateVector3 and used it in SphericalCoordinates * [Pull request #616](https://github.com/gazebosim/gz-math/pull/616) @@ -8,11 +17,9 @@ 1. Clamping color channel values to within [0, 1] * [Pull request #613](https://github.com/gazebosim/gz-math/pull/613) -1. Implement rule of zero part 2 - * [Pull request #614](https://github.com/gazebosim/gz-math/pull/614) - -1. Use ImplPtr, fix rule of 0 part 1 +1. Use ImplPtr, Implement rule of zero * [Pull request #608](https://github.com/gazebosim/gz-math/pull/608) + * [Pull request #614](https://github.com/gazebosim/gz-math/pull/614) 1. Fix flaky Stopwatch test * [Pull request #610](https://github.com/gazebosim/gz-math/pull/610) diff --git a/README.md b/README.md index 0cf0dffad..1a3e42baa 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Build | Status -- | -- -Test coverage | [![codecov](https://codecov.io/gh/gazebosim/gz-math/branch/main/graph/badge.svg)](https://codecov.io/gh/gazebosim/gz-math/branch/main) +Test coverage | [![codecov](https://codecov.io/gh/gazebosim/gz-math/branch/main/graph/badge.svg)](https://codecov.io/gh/gazebosim/gz-math/tree/main) Ubuntu Noble | [![Build Status](https://build.osrfoundation.org/buildStatus/icon?job=gz_math-ci-main-noble-amd64)](https://build.osrfoundation.org/job/gz_math-ci-main-noble-amd64) Homebrew | [![Build Status](https://build.osrfoundation.org/buildStatus/icon?job=gz_math-ci-main-homebrew-amd64)](https://build.osrfoundation.org/job/gz_math-ci-main-homebrew-amd64) Windows | [![Build Status](https://build.osrfoundation.org/buildStatus/icon?job=gz_math-main-win)](https://build.osrfoundation.org/job/gz_math-main-win) @@ -27,9 +27,9 @@ classes and functions designed for robotic applications. [Folder Structure](#folder-structure) -[Code of Conduct](#code-of-conduct) +[Contributing](#contributing) -[Contributing](#code-of-contributing) +[Code of Conduct](#code-of-conduct) [Versioning](#versioning) @@ -47,7 +47,7 @@ Math types. # Install -See the [installation tutorial](https://gazebosim.org/api/math/7/install.html). +See the [installation tutorial](https://gazebosim.org/api/math/8/install.html). # Usage diff --git a/eigen3/include/CMakeLists.txt b/eigen3/include/CMakeLists.txt index 992a1312e..a35a0475e 100644 --- a/eigen3/include/CMakeLists.txt +++ b/eigen3/include/CMakeLists.txt @@ -1,2 +1 @@ add_subdirectory(gz) -install(DIRECTORY ignition DESTINATION ${GZ_INCLUDE_INSTALL_DIR_FULL}) diff --git a/eigen3/include/ignition/math/eigen3.hh b/eigen3/include/ignition/math/eigen3.hh deleted file mode 100644 index f0a14b78e..000000000 --- a/eigen3/include/ignition/math/eigen3.hh +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (C) 2017 Open Source Robotics Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include -#include diff --git a/eigen3/include/ignition/math/eigen3/Conversions.hh b/eigen3/include/ignition/math/eigen3/Conversions.hh deleted file mode 100644 index bab4a3f07..000000000 --- a/eigen3/include/ignition/math/eigen3/Conversions.hh +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (C) 2022 Open Source Robotics Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include -#include diff --git a/eigen3/include/ignition/math/eigen3/Util.hh b/eigen3/include/ignition/math/eigen3/Util.hh deleted file mode 100644 index 962463fa3..000000000 --- a/eigen3/include/ignition/math/eigen3/Util.hh +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (C) 2022 Open Source Robotics Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include -#include diff --git a/examples/vector3_example.py b/examples/vector3_example.py index b573da4ab..ffecc3786 100644 --- a/examples/vector3_example.py +++ b/examples/vector3_example.py @@ -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())) diff --git a/examples/vector3_example.rb b/examples/vector3_example.rb index 55f675a63..ca330720f 100644 --- a/examples/vector3_example.rb +++ b/examples/vector3_example.rb @@ -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()) diff --git a/tutorials/angle.md b/tutorials/angle.md deleted file mode 100644 index ad468160e..000000000 --- a/tutorials/angle.md +++ /dev/null @@ -1,122 +0,0 @@ -\page angle Angle example - -This tutorial explains how to use the `Angle` class from Gazebo Math library. - -## C++ example - -### Compile the code - -Go to `gz-math/examples` and use `cmake` to compile the code: - -```{.sh} -git clone https://github.com/gazebosim/gz-math/ -b gz-math8 -cd gz-math/examples -mkdir build -cd build -cmake .. -make -``` - -When the code is compiled, run: - -```{.sh} -./angle_example -``` - -The ouput of the program: - -```{.sh} -The angle 'a' should be zero: 0 -Pi in radians: 3.14159 -Pi in degrees: 180 -Pi + PI/2 in radians: 4.71239 -Normalized to the range -Pi and Pi: -1.5708 -``` - -### Code - -The code instantiates an angle class. The default constructed angle should be zero. - -\snippet examples/angle_example.cc Create an angle - -There are some predefined angles, such as: - -\snippet examples/angle_example.cc constant pi - -By default, all values are in radians, but you can use the method `Degree` to convert to degrees. - -\snippet examples/angle_example.cc Output the angle in radians and degrees. - -The `Angle` class overloads the `+=`, and many other, math operators. - -\snippet examples/angle_example.cc The Angle class overloads the +=, and many other, math operators. - -Use the method `Normalized` to bound the value between `-PI` and `PI`. - -\snippet examples/angle_example.cc normalized - -## Ruby example - -This example will only work if the Ruby interface library was compiled and installed. For example, -on Ubuntu: - -```{.sh} -sudo apt install ruby-gz-math<#> -``` - -Modify the `RUBYLIB` environment variable to include the Gazebo Math library install path. For example, if you install to `/usr`: - -```{.sh} -export RUBYLIB=/usr/lib/ruby:$RUBYLIB -``` - -Move to the examples folder: - -```{.sh} -cd examples -``` - -Execute the code: - -```{.sh} -ruby angle_example.rb -``` - -### Code - -There are some predefined values: - -```{.rb} -printf("PI in degrees = %f\n", Gz::Math::Angle.Pi.Degree) -``` - -Create new objects: - -```{.rb} -a1 = Gz::Math::Angle.new(1.5707) -a2 = Gz::Math::Angle.new(0.7854) -``` - -Use the values in radians or degrees: - -```{.rb} -printf("a1 = %f radians, %f degrees\n", a1.Radian, a1.Degree) -printf("a2 = %f radians, %f degrees\n", a2.Radian, a2.Degree) -``` - -The `Angle` class overloads math operators. - -```{.rb} -printf("a1 * a2 = %f radians, %f degrees\n", (a1 * a2).Radian, (a1 * a2).Degree) -printf("a1 + a2 = %f radians, %f degrees\n", (a1 + a2).Radian, (a1 + a2).Degree) -printf("a1 - a2 = %f radians, %f degrees\n", (a1 - a2).Radian, (a1 - a2).Degree) -``` - -Normalize the value between `-PI` and `PI`. - -```{.rb} -a3 = Gz::Math::Angle.new(15.707) -printf("a3 = %f radians, %f degrees\n", a3.Radian, a3.Degree) -a3.Normalize -printf("a3.Normalize = %f radians, %f degrees\n", a3.Radian, a3.Degree) -``` diff --git a/tutorials/color.md b/tutorials/color.md index 92ea8a3f0..b209309b1 100644 --- a/tutorials/color.md +++ b/tutorials/color.md @@ -54,4 +54,4 @@ You can also set or read a color in HSV. \snippet examples/color_example.cc Set value from HSV -There are more functions in `Color`. Take a look at the [API](https://gazebosim.org/api/math/7/classgz_1_1math_1_1Color.html) +There are more functions in `Color`. Take a look at the [API](https://gazebosim.org/api/math/8/classgz_1_1math_1_1Color.html) diff --git a/tutorials/example_angle.md b/tutorials/example_angle.md index 00e660272..5827504cf 100644 --- a/tutorials/example_angle.md +++ b/tutorials/example_angle.md @@ -14,13 +14,20 @@ 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: ```{.sh} +# Linux and macOS ./angle_example +# Windows +.\build\Release\angle_example.exe ``` The ouput of the program: diff --git a/tutorials/example_triangle.md b/tutorials/example_triangle.md index 9d931ccde..e42ffe93d 100644 --- a/tutorials/example_triangle.md +++ b/tutorials/example_triangle.md @@ -12,13 +12,20 @@ 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: ```{.sh} +# Linux and macOS ./triangle_example +# Windows +.\build\Release\triangle_example.exe ``` The ouput of the program: @@ -69,4 +76,4 @@ The `Intersects` function checks if a line segment intersects the triangle. It a \snippet examples/triangle_example.cc intersect -There are more functions in `Triangle`. Take a look at the [API](https://gazebosim.org/api/math/7/) +There are more functions in `Triangle`. Take a look at the [API](https://gazebosim.org/api/math/8/) diff --git a/tutorials/example_vector2.md b/tutorials/example_vector2.md index f16216910..484b69872 100644 --- a/tutorials/example_vector2.md +++ b/tutorials/example_vector2.md @@ -8,24 +8,31 @@ 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 cd build cmake .. +# Linux and macOS make +# Windows +cd .. +cmake --build build --config Release ``` When the code is compiled, run: -```{.sh} +```bash +# Linux and macOS ./vector2_example +# Windows +.\build\Release\vector2_example.exe ``` -The ouput of the program: +The output of the program: -```{.sh} +```bash Vec2: 2 4 Vec2a: 1 2 Vec2b: 1.2 3.4 @@ -42,7 +49,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 @@ -67,7 +74,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 @@ -78,40 +85,67 @@ 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()) @@ -119,14 +153,14 @@ 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)) @@ -134,12 +168,12 @@ 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()) ``` diff --git a/tutorials/rotation.md b/tutorials/rotation.md deleted file mode 100644 index 7bcdb0681..000000000 --- a/tutorials/rotation.md +++ /dev/null @@ -1,104 +0,0 @@ -\page rotation Rotation example - -This example explains how to use quaternions and euler angles, and how to convert between them. - -## Compiling and running the code - -Go to `gz-math/examples` and use `cmake` to compile the code: - -```{.sh} -git clone https://github.com/gazebosim/gz-math/ -b gz-math8 -cd gz-math/examples -mkdir build -cd build -cmake .. -make -``` - -When the code is compiled, you can run two different examples, one which converts from quaternion to euler angles: - -```{.sh} -Usage: - ./quaternion_to_euler - -Example - ./quaternion_to_euler 0.5 0.5 0.5 0.5 -``` - -And the other which converts from euler to quaternion: - -```{.sh} -Usage (angles specified in radians): - quaternion_from_euler - -Example - quaternion_from_euler 0 0 1.57 -``` - -The ouput of each program, respectively: - -```{.sh} -./quaternion_to_euler 0.5 0.5 0.5 0.5 -Normalizing Quaternion components: - W 0.5 - X 0.5 - Y 0.5 - Z 0.5 -to - W 0.5 - X 0.5 - Y 0.5 - Z 0.5 - -Converting to Euler angles - roll 1.570796 radians - pitch -0.000000 radians - yaw 1.570796 radians - - roll 90.000000 degrees - pitch -0.000000 degrees - yaw 90.000000 degrees - -to Rotation matrix - 0.000000 0.000000 1.000000 - 1.000000 0.000000 0.000000 - 0.000000 1.000000 0.000000 -``` - -```{.sh} -./quaternion_from_euler 0 0 1.57 -Converting Euler angles: - roll 0.000000 radians - pitch 0.000000 radians - yaw 1.570000 radians - - roll 0.000000 degrees - pitch 0.000000 degrees - yaw 89.954374 degrees - -to Quaternion - W 0.707388 - X 0.000000 - Y 0.000000 - Z 0.706825 - -to Rotation matrix - 0.000796 -1.000000 0.000000 - 1.000000 0.000796 0.000000 - 0.000000 0.000000 1.000000 -``` - -## Code - -You can create some objects to express rotations: - -\snippet examples/quaternion_from_euler.cc constructor -\snippet examples/quaternion_to_euler.cc constructor - -To access the quaterions attributes: - -\snippet examples/quaternion_from_euler.cc access quaterion - -Or to acccess the rotation matrix elements: - -\snippet examples/quaternion_from_euler.cc rotation matrix diff --git a/tutorials/rotation_example.md b/tutorials/rotation_example.md index 0a077a62c..ecb892bd9 100644 --- a/tutorials/rotation_example.md +++ b/tutorials/rotation_example.md @@ -12,7 +12,11 @@ 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, you can run two different examples, one which converts from quaternion to euler angles: @@ -38,7 +42,10 @@ Example The ouput of each program, respectively: ```{.sh} +# Linux and macOS ./quaternion_to_euler 0.5 0.5 0.5 0.5 +# Windows +.\build\Release\quaternion_to_euler.exe 0.5 0.5 0.5 0.5 Normalizing Quaternion components: W 0.5 X 0.5 @@ -66,7 +73,10 @@ to Rotation matrix ``` ```{.sh} +# Linux and macOS ./quaternion_from_euler 0 0 1.57 +# Windows +.\build\Release\quaternion_from_euler.exe 0 0 1.57 Converting Euler angles: roll 0.000000 radians pitch 0.000000 radians diff --git a/tutorials/triangle.md b/tutorials/triangle.md deleted file mode 100644 index a2580cce0..000000000 --- a/tutorials/triangle.md +++ /dev/null @@ -1,72 +0,0 @@ -\page triangle Triangle example - -This tutorial explains how to use the `Triangle` class from Gazebo Math library. - -## Compile the code - -Go to `gz-math/examples` and use `cmake` to compile the code: - -```{.sh} -git clone https://github.com/gazebosim/gz-math/ -b gz-math8 -cd gz-math/examples -mkdir build -cd build -cmake .. -make -``` - -When the code is compiled, run: - -```{.sh} -./triangle_example -``` - -The ouput of the program: - -```{.sh} -Vertex 1: -1 0 -Vertex 2: 0 1 -Vertex 3: 1 0 -Side 1: -1 0 0 1 -Side 2: 0 1 1 0 -Side 3: 1 0 -1 0 -Perimeter=4.82843 Area=1 -Triangle contains the point 0, 0.5 -A line from (-2, 0.5) to (2, 0.5) intersects the triangle at the -following points: - Pt1=-0.5 0.5 - Pt2=0.5 0.5 -``` - -## Code - -Create a triangle with the following vertices: - -\snippet examples/triangle_example.cc constructor - -The individual vertices are accessible through the `[]` operator. - -\snippet examples/triangle_example.cc access1 - -Each side of the triangle is also accessible via the `Side` method. Each side consists of 2 vertices, the following code will print out the X and Y values of each vertex. - -\snippet examples/triangle_example.cc access2 - -It's also possible to set each vertex individually or set all the vertices at once. - -\snippet examples/triangle_example.cc vertex1 -\snippet examples/triangle_example.cc vertex2 - -You can get the perimeter length and area of the triangle - -\snippet examples/triangle_example.cc perimeter and area - -The `Contains` function checks if a line or point is inside the triangle. - -\snippet examples/triangle_example.cc contains - -The `Intersects` function checks if a line segment intersects the triangle. It also returns the points of intersection. - -\snippet examples/triangle_example.cc intersect - -There are more functions in `Triangle`. Take a look at the [API](https://gazebosim.org/api/math/6.4/index.html) diff --git a/tutorials/vector.md b/tutorials/vector.md deleted file mode 100644 index f32df1353..000000000 --- a/tutorials/vector.md +++ /dev/null @@ -1,145 +0,0 @@ -\page vector Vector example - -This tutorial explains how to use the `Vector` classes from Gazebo Math library. - -## C++ example - -### Compile the code - -To compile the code, go to `gz-math/examples` and use `cmake`: - -```{.sh} -git clone https://github.com/gazebosim/gz-math/ -b gz-math8 -cd gz-math/examples -mkdir build -cd build -cmake .. -make -``` - -When the code is compiled, run: - -```{.sh} -./vector2_example -``` - -The ouput of the program: - -```{.sh} -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 component of `vec2` can be set at anytime. - -\snippet examples/vector2_example.cc constructor - - -The `Vector2` class is a template, so you can also create a `Vector2` using `gz::math::Vector2`: - -\snippet examples/vector2_example.cc constructor2 - -It's also possible to set initial values. Here we are using a `Vector2` of floats: - -\snippet examples/vector2_example.cc constructor3 - -We can output the contents of each vector using `std::cout`. - -\snippet examples/vector2_example.cc stdout - -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]`. - -\snippet examples/vector2_example.cc access - -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. - -\snippet examples/vector2_example.cc distance - -**There are more functions in Vector2. Take a look at the [API](https://gazebosim.org/libs/math)** - -## Ruby examples - -This example will only work if the Ruby interface library was compiled and installed. For example, -on Ubuntu: - -```{.sh} -sudo apt install ruby-gz-math<#> -``` - -Modify the `RUBYLIB` environment variable to include the Gazebo Math library install path. For example, if you install to `/usr`: - -```{.sh} -export RUBYLIB=/usr/lib/ruby:$RUBYLIB -``` - -Move to the examples folder: - -```{.sh} -cd examples -``` - -Execute the examples: - -```{.sh} -ruby vector2_example.rb -ruby vector3_example.rb -``` - -### 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. - -```{.rb} -va = Gz::Math::Vector2d.new(1, 2) -``` - -You can get access to each component in the vector using the `X()`, `Y()` accessors. - -```{.rb} -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} -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} -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} -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()) -```