diff --git a/examples/angle_example.cc b/examples/angle_example.cc index 80c7263e1..eb2ea916c 100644 --- a/examples/angle_example.cc +++ b/examples/angle_example.cc @@ -20,21 +20,31 @@ int main(int argc, char **argv) { - // Create an angle. + +//! [Create an angle] ignition::math::Angle a; +//! [Create an angle] // A default constructed angle should be zero. std::cout << "The angle 'a' should be zero: " << a << std::endl; +//! [constant pi] + a = ignition::math::Angle::HalfPi; a = ignition::math::Angle::Pi; +//! [constant pi] - // Output the angle in radians and degrees. +//! [Output the angle in radians and degrees.] std::cout << "Pi in radians: " << a << std::endl; std::cout << "Pi in degrees: " << a.Degree() << std::endl; +//! [Output the angle in radians and degrees.] - // The Angle class overloads the +=, and many other, math operators. +//! [The Angle class overloads the +=, and many other, math operators.] a += ignition::math::Angle::HalfPi; +//! [The Angle class overloads the +=, and many other, math operators.] std::cout << "Pi + PI/2 in radians: " << a << std::endl; +//! [normalized] std::cout << "Normalized to the range -Pi and Pi: " << a.Normalized() << std::endl; +//! [normalized] + } //! [complete] diff --git a/examples/angle_example.rb b/examples/angle_example.rb index fcbe2c60b..e3b594d80 100644 --- a/examples/angle_example.rb +++ b/examples/angle_example.rb @@ -22,7 +22,9 @@ # require 'ignition/math' +#! [constant] printf("PI in degrees = %f\n", Ignition::Math::Angle.Pi.Degree) +#! [constant] a1 = Ignition::Math::Angle.new(1.5707) a2 = Ignition::Math::Angle.new(0.7854) diff --git a/examples/quaternion_from_euler.cc b/examples/quaternion_from_euler.cc index ebe7674bb..67ec3282b 100644 --- a/examples/quaternion_from_euler.cc +++ b/examples/quaternion_from_euler.cc @@ -69,13 +69,18 @@ int main(int argc, char **argv) IGN_RTOD(pitch), IGN_RTOD(yaw)); +//![constructor] ignition::math::Quaterniond q(roll, pitch, yaw); ignition::math::Matrix3d m(q); +//![constructor] std::cout << "\nto Quaternion\n"; +//! [access quaterion] printf(" W % .6f\n X % .6f\n Y % .6f\n Z % .6f\n", q.W(), q.X(), q.Y(), q.Z()); +//! [access quaterion] +//! [rotation matrix] std::cout << "\nto Rotation matrix\n"; printf(" % .6f % .6f % .6f\n" " % .6f % .6f % .6f\n" @@ -83,4 +88,5 @@ int main(int argc, char **argv) m(0, 0), m(0, 1), m(0, 2), m(1, 0), m(1, 1), m(1, 2), m(2, 0), m(2, 1), m(2, 2)); +//! [rotation matrix] } diff --git a/examples/quaternion_to_euler.cc b/examples/quaternion_to_euler.cc index e716e4dbe..0fe1d0566 100644 --- a/examples/quaternion_to_euler.cc +++ b/examples/quaternion_to_euler.cc @@ -75,7 +75,9 @@ int main(int argc, char **argv) << std::endl; ignition::math::Matrix3d m(q); +//![constructor] ignition::math::Vector3d euler(q.Euler()); +//![constructor] std::cout << "\nConverting to Euler angles\n"; printf(" roll % .6f radians\n" diff --git a/examples/triangle_example.cc b/examples/triangle_example.cc index ab92d89fb..a2adeaf99 100644 --- a/examples/triangle_example.cc +++ b/examples/triangle_example.cc @@ -24,43 +24,59 @@ int main(int argc, char **argv) // 1: x = -1, y = 0 // 2: x = 0, y = 1 // 3: x = 1, y = 0 +//! [constructor] ignition::math::Triangled tri( ignition::math::Vector2d(-1, 0), ignition::math::Vector2d(0, 1), ignition::math::Vector2d(1, 0)); +//! [constructor] // The individual vertices are accessible through the [] operator + +//! [access1] std::cout << "Vertex 1: " << tri[0] << "\n" << "Vertex 2: " << tri[1] << "\n" << "Vertex 3: " << tri[2] << "\n"; +//! [access1] // Each side of the triangle is also accessible via the Side function +//! [access2] std::cout << "Side 1: " << tri.Side(0) << "\n" << "Side 2: " << tri.Side(1) << "\n" << "Side 3: " << tri.Side(2) << "\n"; +//! [access2] // It's also possible to set each vertex individually. +//! [vertex1] tri.Set(0, ignition::math::Vector2d(-10, 0)); tri.Set(1, ignition::math::Vector2d(0, 20)); tri.Set(2, ignition::math::Vector2d(10, 2)); +//! [vertex1] // Or set all the vertices at once. +//! [vertex2] tri.Set(ignition::math::Vector2d(-1, 0), ignition::math::Vector2d(0, 1), ignition::math::Vector2d(1, 0)); +//! [vertex2] // You can get the perimeter length and area of the triangle +//! [perimeter and area] std::cout << "Perimeter=" << tri.Perimeter() << " Area=" << tri.Area() << "\n"; +//! [perimeter and area] // The Contains functions check if a line or point is inside the triangle +//! [contains] if (tri.Contains(ignition::math::Vector2d(0, 0.5))) std::cout << "Triangle contains the point 0, 0.5\n"; else std::cout << "Triangle does not contain the point 0, 0.5\n"; +//! [contains] // The Intersect function check if a line segment intersects the triangle. // It also returns the points of intersection +//! [intersect] ignition::math::Vector2d pt1, pt2; if (tri.Intersects(ignition::math::Line2d(-2, 0.5, 2, 0.5), pt1, pt2)) { @@ -74,6 +90,7 @@ int main(int argc, char **argv) std::cout << "A line from (-2, 0.5) to (2, 0.5) does not intersect " << "the triangle\n"; } +//! [intersect] // There are more functions in Triangle. Take a look at the API; // http://ignitionrobotics.org/libraries/ign_mat/api diff --git a/examples/vector2_example.cc b/examples/vector2_example.cc index b28ae65a2..e558a2327 100644 --- a/examples/vector2_example.cc +++ b/examples/vector2_example.cc @@ -22,44 +22,57 @@ int main(int argc, char **argv) { // Create a Vector2 called vec2 of doubles using the typedef Vector2d. // The initial x any y values are zero.\n\n"; - ignition::math::Vector2d vec2; - // The x and y component of vec2 can be set at anytime. +//! [constructor] + ignition::math::Vector2d vec2; vec2.Set(2.0, 4.0); +//! [constructor] // The Vector2 class is a template, so you can also create a Vector2 using // ignition::math::Vector2 + //! [constructor2] ignition::math::Vector2 vec2a; vec2a.Set(1.0, 2.0); + //! [constructor2] // It's also possible to set initial values. This time we are using // a Vector2 of floats + //! [constructor3] ignition::math::Vector2f vec2b(1.2f, 3.4f); + //! [constructor3] // We can output the contents of each vector using std::cout +//! [stdout] std::cout << "Vec2: " << vec2 << "\n" << "Vec2a: " << vec2a << "\n" << "Vec2b: " << vec2b << "\n"; +//! [stdout] // You can also get access to each component in the vector using the // X(), Y() accessors or the [] operator. + //! [access] 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"; + //! [access] // The [] operator is clamped to the range [0, 1] std::cout << vec2[3] << std::endl; // The Vector2 class overloads many common operators +//! [operators] std::cout << vec2 * vec2a << "\n" << vec2 + vec2a << "\n" << vec2 - vec2a << "\n" << vec2 / vec2a << "\n"; +//! [operators] // There are also many useful function such as finding the distance // between two vectors +//! [distance] std::cout << vec2.Distance(vec2a) << std::endl; +//! [distance] // There are more functions in Vector2. Take a look at the API: // https://ignitionrobotics.org/libs/math diff --git a/tutorials.md.in b/tutorials.md.in index 7b8852327..ee951f2ee 100644 --- a/tutorials.md.in +++ b/tutorials.md.in @@ -9,6 +9,10 @@ Ignition @IGN_DESIGNATION_CAP@ library and how to use the library effectively. 1. \subpage install "Installation" 2. \subpage cppgetstarted "C++ Get Started" +3. \subpage vector "Vector example" +4. \subpage angle "Angle example" +5. \subpage triangle "Triangle example" +6. \subpage rotation "Rotation example" ## License diff --git a/tutorials/angle.md b/tutorials/angle.md new file mode 100644 index 000000000..c51176b1e --- /dev/null +++ b/tutorials/angle.md @@ -0,0 +1,109 @@ +\page angle Angle example + +This tutorial explains how to use the `Angle` class from Ignition Math library. + +## C++ example + +### Compile the code + +Go to `ign-math/examples` and use `cmake` to compile the code: + +```{.sh} +git clone https://github.com/ignitionrobotics/ign-math/ -b ign-math6 +cd ign-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. Modify the `RUBYLIB` environment variable to include the Ignition Math library install path. For example, if you install to `/usr`: + +```{.sh} +export RUBYLIB=/usr/lib/ruby:$RUBYLIB +``` + +Execute the code: + +```{.sh} +ruby angle_example.rb +``` + +### Code + +There are some predefined values: + +```{.rb} +printf("PI in degrees = %f\n", Ignition::Math::Angle.Pi.Degree) +``` + +Create new objects: + +```{.rb} +a1 = Ignition::Math::Angle.new(1.5707) +a2 = Ignition::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 = Ignition::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/rotation.md b/tutorials/rotation.md new file mode 100644 index 000000000..7c84b7445 --- /dev/null +++ b/tutorials/rotation.md @@ -0,0 +1,104 @@ +\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 `ign-math/examples` and use `cmake` to compile the code: + +```{.sh} +git clone https://github.com/ignitionrobotics/ign-math/ -b ign-math6 +cd ign-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/triangle.md b/tutorials/triangle.md new file mode 100644 index 000000000..08fded07e --- /dev/null +++ b/tutorials/triangle.md @@ -0,0 +1,72 @@ +\page triangle Triangle example + +This tutorial explains how to use the `Triangle` class from Ignition Math library. + +## Compile the code + +Go to `ign-math/examples` and use `cmake` to compile the code: + +```{.sh} +git clone https://github.com/ignitionrobotics/ign-math/ -b ign-math6 +cd ign-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://ignitionrobotics.org/api/math/6.4/index.html) diff --git a/tutorials/vector.md b/tutorials/vector.md new file mode 100644 index 000000000..371317813 --- /dev/null +++ b/tutorials/vector.md @@ -0,0 +1,132 @@ +\page vector Vector example + +This tutorial explains how to use the `Vector` classes from Ignition Math library. + +## C++ example + +### Compile the code + +To compile the code, go to `ign-math/examples` and use `cmake`: + +```{.sh} +git clone https://github.com/ignitionrobotics/ign-math/ -b ign-math6 +cd ign-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 `ignition::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://ignitionrobotics.org/libs/math)** + +## Ruby examples + +This example will only work if the Ruby interface library was compiled and installed. Modify the `RUBYLIB` environment variable to include the Ignition Math library install path. For example, if you install to `/usr`: + +```{.sh} +export RUBYLIB=/usr/lib/ruby:$RUBYLIB +``` + +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 = Ignition::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 = Ignition::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()) +```