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

Added Temperature pybind11 interface #330

Merged
merged 8 commits into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 40 additions & 0 deletions include/ignition/math/Temperature.hh
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ namespace ignition
/// \return Reference to this instance
public: Temperature &operator=(const Temperature &_temp);

/// \brief Addition operator
/// \param[in] _temp Temperature in Kelvin
/// \return Resulting temperature
public: Temperature operator+(const double _temp) const;
ahcorde marked this conversation as resolved.
Show resolved Hide resolved

/// \brief Addition operator
/// \param[in] _temp Temperature object
/// \return Resulting temperature
public: Temperature operator+(const Temperature &_temp) const;

/// \brief Addition operator
/// \param[in] _temp Temperature in Kelvin
/// \return Resulting temperature
Expand Down Expand Up @@ -190,6 +200,16 @@ namespace ignition
/// \return Resulting temperature
public: Temperature operator-(const Temperature &_temp);

/// \brief Subtraction operator
/// \param[in] _temp Temperature in Kelvin
/// \return Resulting temperature
public: Temperature operator-(const double _temp) const;

/// \brief Subtraction operator
/// \param[in] _temp Temperature object
/// \return Resulting temperature
public: Temperature operator-(const Temperature &_temp) const;

/// \brief Subtraction operator for double type.
/// \param[in] _t Temperature in kelvin
/// \param[in] _temp Temperature object
Expand Down Expand Up @@ -219,6 +239,16 @@ namespace ignition
/// \return Resulting temperature
public: Temperature operator*(const Temperature &_temp);

/// \brief Multiplication operator
/// \param[in] _temp Temperature in Kelvin
/// \return Resulting temperature
public: Temperature operator*(const double _temp) const;

/// \brief Multiplication operator
/// \param[in] _temp Temperature object
/// \return Resulting temperature
public: Temperature operator*(const Temperature &_temp) const;

/// \brief Multiplication operator for double type.
/// \param[in] _t Temperature in kelvin
/// \param[in] _temp Temperature object
Expand Down Expand Up @@ -248,6 +278,16 @@ namespace ignition
/// \return Resulting temperature
public: Temperature operator/(const Temperature &_temp);

/// \brief Division operator
/// \param[in] _temp Temperature in Kelvin
/// \return Resulting temperature
public: Temperature operator/(const double _temp) const;

/// \brief Division operator
/// \param[in] _temp Temperature object
/// \return Resulting temperature
public: Temperature operator/(const Temperature &_temp) const;

/// \brief Division operator for double type.
/// \param[in] _t Temperature in kelvin
/// \param[in] _temp Temperature object
Expand Down
48 changes: 48 additions & 0 deletions src/Temperature.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ Temperature Temperature::operator+(const Temperature &_temp)
return this->dataPtr->kelvin + _temp;
}

/////////////////////////////////////////////////
Temperature Temperature::operator+(const double _temp) const
{
return this->dataPtr->kelvin + _temp;
}

/////////////////////////////////////////////////
Temperature Temperature::operator+(const Temperature &_temp) const
{
return this->dataPtr->kelvin + _temp;
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
}

/////////////////////////////////////////////////
const Temperature &Temperature::operator+=(const double _temp)
{
Expand All @@ -186,6 +198,18 @@ Temperature Temperature::operator-(const Temperature &_temp)
return this->dataPtr->kelvin - _temp.Kelvin();
}

/////////////////////////////////////////////////
Temperature Temperature::operator-(const double _temp) const
{
return this->dataPtr->kelvin - _temp;
}

/////////////////////////////////////////////////
Temperature Temperature::operator-(const Temperature &_temp) const
{
return this->dataPtr->kelvin - _temp.Kelvin();
}

/////////////////////////////////////////////////
const Temperature &Temperature::operator-=(const double _temp)
{
Expand All @@ -212,6 +236,18 @@ Temperature Temperature::operator*(const Temperature &_temp)
return Temperature(this->dataPtr->kelvin * _temp.Kelvin());
}

/////////////////////////////////////////////////
Temperature Temperature::operator*(const double _temp) const
{
return Temperature(this->dataPtr->kelvin * _temp);
}

/////////////////////////////////////////////////
Temperature Temperature::operator*(const Temperature &_temp) const
{
return Temperature(this->dataPtr->kelvin * _temp.Kelvin());
}

/////////////////////////////////////////////////
const Temperature &Temperature::operator*=(const double _temp)
{
Expand All @@ -238,6 +274,18 @@ Temperature Temperature::operator/(const Temperature &_temp)
return Temperature(this->dataPtr->kelvin / _temp.Kelvin());
}

/////////////////////////////////////////////////
Temperature Temperature::operator/(const double _temp) const
{
return Temperature(this->dataPtr->kelvin / _temp);
}

/////////////////////////////////////////////////
Temperature Temperature::operator/(const Temperature &_temp) const
{
return Temperature(this->dataPtr->kelvin / _temp.Kelvin());
}

/////////////////////////////////////////////////
const Temperature &Temperature::operator/=(const double _temp)
{
Expand Down
1 change: 0 additions & 1 deletion src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ if (PYTHONLIBS_FOUND)
SignalStats_TEST
Sphere_TEST
SphericalCoordinates_TEST
Temperature_TEST
Triangle3_TEST
Vector3Stats_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 @@ -22,6 +22,7 @@ if (${pybind11_FOUND})
src/SemanticVersion.cc
src/Spline.cc
src/StopWatch.cc
src/Temperature.cc
)

target_link_libraries(math PRIVATE
Expand Down Expand Up @@ -88,6 +89,7 @@ if (${pybind11_FOUND})
SemanticVersion_TEST
Spline_TEST
StopWatch_TEST
Temperature_TEST
Triangle_TEST
Vector2_TEST
Vector3_TEST
Expand Down
129 changes: 129 additions & 0 deletions src/python_pybind11/src/Temperature.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* 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 <ignition/math/Temperature.hh>

#include "Temperature.hh"
#include <pybind11/operators.h>

using namespace pybind11::literals;

namespace ignition
{
namespace math
{
namespace python
{
void defineMathTemperature(py::module &m, const std::string &typestr)
{
using Class = ignition::math::Temperature;
std::string pyclass_name = typestr;
auto toString = [](const Class &si) {
std::stringstream stream;
stream << si;
return stream.str();
};
py::class_<Class>(m,
pyclass_name.c_str(),
py::buffer_protocol(),
py::dynamic_attr())
.def(py::init<>())
.def(py::init<double>())
.def(py::init<Class&>())
.def("__call__", &Class::operator())
.def(py::self + py::self)
.def(py::self + double())
.def(double() + py::self)
.def(py::self += py::self)
.def(py::self += double())
.def(py::self - py::self)
.def(py::self - double())
.def(double() - py::self)
.def(py::self -= py::self)
.def(py::self -= double())
.def(py::self * py::self)
.def(py::self * double())
.def(double() * py::self)
.def(py::self *= py::self)
.def(py::self *= double())
.def(py::self / py::self)
.def(py::self / double())
.def(double() / py::self)
.def(py::self /= py::self)
.def(py::self /= double())
.def(py::self != double())
.def(py::self != py::self)
.def(py::self == py::self)
.def(py::self == double())
.def(py::self < double())
.def(py::self < py::self)
.def(py::self <= double())
.def(py::self <= py::self)
.def(py::self > double())
.def(py::self > py::self)
.def(py::self >= double())
.def(py::self >= py::self)
.def("kelvin_to_celsius",
&Class::KelvinToCelsius,
"Convert Kelvin to Celsius")
.def("kelvin_to_fahrenheit",
&Class::KelvinToFahrenheit,
"Convert Kelvin to Fahrenheit")
.def("celsius_to_fahrenheit",
&Class::CelsiusToFahrenheit,
"Convert Celsius to Fahrenheit")
.def("celsius_to_kelvin",
&Class::CelsiusToKelvin,
"Convert Celsius to Kelvin")
.def("fahrenheit_to_celsius",
&Class::FahrenheitToCelsius,
"Convert Fahrenheit to Celsius")
.def("fahrenheit_to_kelvin",
&Class::FahrenheitToKelvin,
"Convert Fahrenheit to Kelvin")
.def("kelvin",
&Class::Kelvin,
"Get the temperature in Kelvin")
.def("celsius",
&Class::Celsius,
"Get the temperature in Celsius")
.def("fahrenheit",
&Class::Fahrenheit,
"Get the temperature in Fahrenheit")
.def("set_kelvin",
&Class::SetKelvin,
"Set the temperature from a Kelvin value")
.def("set_celsius",
&Class::SetCelsius,
"Set the temperature from a Celsius value")
.def("set_fahrenheit",
&Class::SetFahrenheit,
"Set the temperature from a Fahrenheit value")
.def("__copy__", [](const Class &self) {
return Class(self);
})
.def("__deepcopy__", [](const Class &self, py::dict) {
return Class(self);
}, "memo"_a)
.def("__str__", toString)
.def("__repr__", toString);
}
} // namespace python
} // namespace math
} // namespace ignition
42 changes: 42 additions & 0 deletions src/python_pybind11/src/Temperature.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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__TEMPERATURE_HH_
#define IGNITION_MATH_PYTHON__TEMPERATURE_HH_

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

namespace py = pybind11;

namespace ignition
{
namespace math
{
namespace python
{
/// Define a pybind11 wrapper for an ignition::math::Temperature
/**
* \param[in] module a pybind11 module to add the definition to
* \param[in] typestr name of the type used by Python
*/
void defineMathTemperature(py::module &m, const std::string &typestr);
} // namespace python
} // namespace math
} // namespace ignition

#endif // IGNITION_MATH_PYTHON__TEMPERATURE_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 @@ -28,6 +28,7 @@
#include "SemanticVersion.hh"
#include "Spline.hh"
#include "StopWatch.hh"
#include "Temperature.hh"
#include "Triangle.hh"
#include "Vector2.hh"
#include "Vector3.hh"
Expand Down Expand Up @@ -62,6 +63,8 @@ PYBIND11_MODULE(math, m)

ignition::math::python::defineMathStopwatch(m, "Stopwatch");

ignition::math::python::defineMathTemperature(m, "Temperature");

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