Skip to content

Commit

Permalink
Merge branch 'ign-math6' into ahcorde/pybind11/helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ahcorde authored Dec 23, 2021
2 parents 14ba23f + b63de2f commit c2509fa
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 1 deletion.
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ target_link_libraries(quaternion_to_euler ignition-math${IGN_MATH_VER}::ignition
add_executable(quaternion_from_euler quaternion_from_euler.cc)
target_link_libraries(quaternion_from_euler ignition-math${IGN_MATH_VER}::ignition-math${IGN_MATH_VER})

add_executable(rand_example rand_example.cc)
target_link_libraries(rand_example ignition-math${IGN_MATH_VER}::ignition-math${IGN_MATH_VER})

add_executable(graph_example graph_example.cc)
target_link_libraries(graph_example ignition-math${IGN_MATH_VER}::ignition-math${IGN_MATH_VER})

Expand Down
54 changes: 54 additions & 0 deletions examples/rand_example.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2021 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.
*
*/
//! [complete]
#include <iostream>
#include <ignition/math/Rand.hh>

// You can plot the data generated by this program by following these
// steps.
//
// 1. Run this program and save the output to a file:
// ./rand_example normal > normal.data
// ./rand_example uniform > uniform.data
//
// 2. Use gnuplot to create a plot:
// gnuplot -c rand_view_normal.gp > normal.jpg
// gnuplot -c rand_view_uniform.gp > uniform.jpg
int main(int argc, char **argv)
{
if (argc < 2)
{
std::cout << "./rand_example [normal, uniform]" << '\n';
return -1;
}
for (int i = 0; i < 100000; ++i)
{
double value;
if (std::string(argv[1]) == "normal")
{
value = ignition::math::Rand::DblNormal(0, 100);
}
else if (std::string(argv[1]) == "uniform")
{
value = ignition::math::Rand::DblUniform(0, 1000);
}
std::cout << value << std::endl;
}

return 0;
}
//! [complete]
37 changes: 37 additions & 0 deletions examples/rand_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright (C) 2021 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.

# You can plot the data generated by this program by following these
# steps.
#
# 1. Run this program and save the output to a file:
# ./rand_example normal > normal.data
# ./rand_example uniform > uniform.data
#
# 2. Use gnuplot to create a plot:
# gnuplot -c rand_view_normal.gp > normal.jpg
# gnuplot -c rand_view_uniform.gp > uniform.jpg
from ignition.math import Rand
import sys

if (len(sys.argv) < 2):
print("python rand_example [normal, uniform]")
else:
for i in range(100000):
value = 0
if (sys.argv[1] == "uniform"):
value = Rand.dbl_uniform(0, 1000);
elif (sys.argv[1] == "normal"):
value = Rand.dbl_normal(0, 100);
print(value)
12 changes: 12 additions & 0 deletions examples/rand_view_normal.gp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set terminal jpeg
binwidth = 5
bin(x,width)=width*floor(x/width)

set tics out nomirror
set style fill transparent solid 0.5 border lt -1
set xrange [-1000:1000]
set xtics binwidth
set boxwidth binwidth
set yrange [0:2500]

plot "normal.data" u (bin($1,binwidth)):(1.0) smooth freq with boxes notitle
12 changes: 12 additions & 0 deletions examples/rand_view_uniform.gp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set terminal jpeg
binwidth = 50
bin(x,width)=width*floor(x/width)

set tics out nomirror
set style fill transparent solid 0.5 border lt -1
set xrange [0:1000]
set xtics binwidth
set boxwidth binwidth
set yrange [0:10000]

plot "uniform.data" u (bin($1,binwidth)):(1.0) smooth freq with boxes notitle
1 change: 0 additions & 1 deletion src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ if (PYTHONLIBS_FOUND)
Pose3_TEST
python_TEST
Quaternion_TEST
Rand_TEST
RollingMean_TEST
RotationSpline_TEST
SemanticVersion_TEST
Expand Down
2 changes: 2 additions & 0 deletions src/python_pybind11/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ if (${pybind11_FOUND})
src/_ignition_math_pybind11.cc
src/Angle.cc
src/Helpers.cc
src/Rand.cc
)

target_link_libraries(math PRIVATE
Expand Down Expand Up @@ -70,6 +71,7 @@ if (${pybind11_FOUND})
set(python_tests
Angle_TEST
Helpers_TEST
Rand_TEST
Vector2_TEST
Vector3_TEST
Vector4_TEST
Expand Down
58 changes: 58 additions & 0 deletions src/python_pybind11/src/Rand.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2021 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 <string>

#include "Rand.hh"
#include <ignition/math/Rand.hh>

namespace ignition
{
namespace math
{
namespace python
{
void defineMathRand(py::module &m, const std::string &typestr)
{
using Class = ignition::math::Rand;
std::string pyclass_name = typestr;
py::class_<Class>(m,
pyclass_name.c_str(),
py::buffer_protocol(),
py::dynamic_attr())
.def(py::init<>())
.def("seed",
py::overload_cast<>(&ignition::math::Rand::Seed),
"Get the seed value.")
.def("seed",
py::overload_cast<unsigned int>(&ignition::math::Rand::Seed),
"Set the seed value.")
.def("dbl_uniform",
ignition::math::Rand::DblUniform,
"Get a double from a uniform distribution")
.def("dbl_normal",
ignition::math::Rand::DblNormal,
"Get a double from a normal distribution")
.def("int_uniform",
ignition::math::Rand::IntUniform,
"Get a integer from a uniform distribution")
.def("int_normal",
ignition::math::Rand::IntNormal,
"Get a integer from a normal distribution");
}
} // namespace python
} // namespace gazebo
} // namespace ignition
41 changes: 41 additions & 0 deletions src/python_pybind11/src/Rand.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2021 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.
*
*/

#ifndef IGNITION_MATH_PYTHON__RAND_HH_
#define IGNITION_MATH_PYTHON__RAND_HH_

#include <pybind11/pybind11.h>
#include <string>

namespace py = pybind11;

namespace ignition
{
namespace math
{
namespace python
{
/// Define a pybind11 wrapper for an ignition::math::Rand
/**
* \param[in] module a pybind11 module to add the definition to
*/
void defineMathRand(py::module &m, const std::string &typestr);
} // namespace python
} // namespace gazebo
} // namespace ignition

#endif // IGNITION_MATH_PYTHON__RAND_HH_
3 changes: 3 additions & 0 deletions src/python_pybind11/src/_ignition_math_pybind11.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "Angle.hh"
#include "Helpers.hh"
#include "Rand.hh"
#include "Vector2.hh"
#include "Vector3.hh"
#include "Vector4.hh"
Expand All @@ -30,6 +31,8 @@ PYBIND11_MODULE(math, m)

ignition::math::python::defineMathHelpers(m);

ignition::math::python::defineMathRand(m, "Rand");

ignition::math::python::defineMathVector2<double>(m, "Vector2d");
ignition::math::python::defineMathVector2<int>(m, "Vector2i");
ignition::math::python::defineMathVector2<float>(m, "Vector2f");
Expand Down
File renamed without changes.

0 comments on commit c2509fa

Please sign in to comment.