Skip to content

Commit

Permalink
Move template back to header file
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Peters <[email protected]>
  • Loading branch information
scpeters committed Jan 27, 2022
1 parent 490ae76 commit 213d924
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 105 deletions.
105 changes: 0 additions & 105 deletions src/python_pybind11/src/Vector2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,119 +17,14 @@

#include <string>

#include <ignition/math/Vector2.hh>
#include <pybind11/pybind11.h>
#include <pybind11/operators.h>

#include "Vector2.hh"

using namespace pybind11::literals;

namespace ignition
{
namespace math
{
namespace python
{
/// Template helper function
template<typename T>
void helpDefineMathVector2(py::module &m, const std::string &typestr)
{
using Class = ignition::math::Vector2<T>;
auto toString = [](const Class &si) {
std::stringstream stream;
stream << si;
return stream.str();
};
std::string pyclass_name = typestr;
py::class_<Class>(m,
pyclass_name.c_str(),
py::buffer_protocol(),
py::dynamic_attr())
.def(py::init<>())
.def(py::init<const T&, const T&>())
.def(py::init<const Class>())
.def("sum", &Class::Sum, "Return the sum of the values")
.def("distance", &Class::Distance, "Calc distance to the given point")
.def("length",
&Class::Length,
"Returns the length (magnitude) of the vector")
.def("squared_length",
&Class::SquaredLength,
"Return the square of the length (magnitude) of the vector")
.def("normalize", &Class::Normalize, "Normalize the vector length")
.def("normalized", &Class::Normalized, "Return a normalized vector")
.def("round",
&Class::Round,
"Round to near whole number, return the result.")
.def("rounded", &Class::Rounded, "Get a rounded version of this vector")
.def("set", &Class::Set, "Set the contents of the vector")
.def("dot",
&Class::Dot,
"Return the dot product of this vector and another vector")
.def("abs_dot", &Class::AbsDot,
"Return the absolute dot product of this vector and "
"another vector. This is similar to the Dot function, except the "
"absolute value of each component of the vector is used.")
.def("abs",
&Class::Abs,
"Get the absolute value of the vector")
.def("max",
py::overload_cast<const Class&>(&Class::Max),
"Set this vector's components to the maximum of itself and the "
"passed in vector")
.def("max", py::overload_cast<>(&Class::Max, py::const_),
"Get the maximum value in the vector")
.def("min", py::overload_cast<const Class&>(&Class::Min),
"Set this vector's components to the minimum of itself and the "
"passed in vector")
.def("min", py::overload_cast<>(&Class::Min, py::const_),
"Get the minimum value in the vector")
.def(py::self + py::self)
.def(py::self += py::self)
.def(py::self + T())
.def(py::self += T())
.def(py::self * py::self)
.def(py::self *= py::self)
.def(py::self * T())
.def(py::self *= T())
.def(py::self - py::self)
.def(py::self -= py::self)
.def(py::self - T())
.def(py::self -= T())
.def(py::self / py::self)
.def(py::self /= py::self)
.def(py::self / T())
.def(py::self /= T())
.def(py::self != py::self)
.def(py::self == py::self)
.def(-py::self)
.def("equal", &Class::Equal, "Equal to operator")
.def("is_finite",
&Class::IsFinite,
"See if a point is finite (e.g., not nan)")
.def("correct", &Class::Correct, "Corrects any nan values")
.def("x", py::overload_cast<>(&Class::X), "Get the x value.")
.def("y", py::overload_cast<>(&Class::Y), "Get the y value.")
.def("x", py::overload_cast<const T&>(&Class::X), "Set the x value.")
.def("y", py::overload_cast<const T&>(&Class::Y), "Set the y value.")
.def_readonly_static("ZERO", &Class::Zero, "math::Vector2(0, 0)")
.def_readonly_static("ONE", &Class::One, "math::Vector2(1, 1)")
.def_readonly_static("NAN", &Class::NaN, "math::Vector3(NaN, NaN)")
.def("__copy__", [](const Class &self) {
return Class(self);
})
.def("__deepcopy__", [](const Class &self, py::dict) {
return Class(self);
}, "memo"_a)
.def("__getitem__",
py::overload_cast<const std::size_t>(&Class::operator[], py::const_))
.def("__setitem__",
[](Class* vec, unsigned index, T val) { (*vec)[index] = val; })
.def("__str__", toString)
.def("__repr__", toString);
}

void defineMathVector2(py::module &m, const std::string &typestr)
{
helpDefineMathVector2<double>(m, typestr + "d");
Expand Down
106 changes: 106 additions & 0 deletions src/python_pybind11/src/Vector2.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,121 @@
#include <string>

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

#include <ignition/math/Vector2.hh>

namespace py = pybind11;
using namespace pybind11::literals;

namespace ignition
{
namespace math
{
namespace python
{
/// Help define a pybind11 wrapper for an ignition::math::Vector2
/**
* \param[in] module a pybind11 module to add the definition to
*/
template<typename T>
void helpDefineMathVector2(py::module &m, const std::string &typestr)
{
using Class = ignition::math::Vector2<T>;
auto toString = [](const Class &si) {
std::stringstream stream;
stream << si;
return stream.str();
};
std::string pyclass_name = typestr;
py::class_<Class>(m,
pyclass_name.c_str(),
py::buffer_protocol(),
py::dynamic_attr())
.def(py::init<>())
.def(py::init<const T&, const T&>())
.def(py::init<const Class>())
.def("sum", &Class::Sum, "Return the sum of the values")
.def("distance", &Class::Distance, "Calc distance to the given point")
.def("length",
&Class::Length,
"Returns the length (magnitude) of the vector")
.def("squared_length",
&Class::SquaredLength,
"Return the square of the length (magnitude) of the vector")
.def("normalize", &Class::Normalize, "Normalize the vector length")
.def("normalized", &Class::Normalized, "Return a normalized vector")
.def("round",
&Class::Round,
"Round to near whole number, return the result.")
.def("rounded", &Class::Rounded, "Get a rounded version of this vector")
.def("set", &Class::Set, "Set the contents of the vector")
.def("dot",
&Class::Dot,
"Return the dot product of this vector and another vector")
.def("abs_dot", &Class::AbsDot,
"Return the absolute dot product of this vector and "
"another vector. This is similar to the Dot function, except the "
"absolute value of each component of the vector is used.")
.def("abs",
&Class::Abs,
"Get the absolute value of the vector")
.def("max",
py::overload_cast<const Class&>(&Class::Max),
"Set this vector's components to the maximum of itself and the "
"passed in vector")
.def("max", py::overload_cast<>(&Class::Max, py::const_),
"Get the maximum value in the vector")
.def("min", py::overload_cast<const Class&>(&Class::Min),
"Set this vector's components to the minimum of itself and the "
"passed in vector")
.def("min", py::overload_cast<>(&Class::Min, py::const_),
"Get the minimum value in the vector")
.def(py::self + py::self)
.def(py::self += py::self)
.def(py::self + T())
.def(py::self += T())
.def(py::self * py::self)
.def(py::self *= py::self)
.def(py::self * T())
.def(py::self *= T())
.def(py::self - py::self)
.def(py::self -= py::self)
.def(py::self - T())
.def(py::self -= T())
.def(py::self / py::self)
.def(py::self /= py::self)
.def(py::self / T())
.def(py::self /= T())
.def(py::self != py::self)
.def(py::self == py::self)
.def(-py::self)
.def("equal", &Class::Equal, "Equal to operator")
.def("is_finite",
&Class::IsFinite,
"See if a point is finite (e.g., not nan)")
.def("correct", &Class::Correct, "Corrects any nan values")
.def("x", py::overload_cast<>(&Class::X), "Get the x value.")
.def("y", py::overload_cast<>(&Class::Y), "Get the y value.")
.def("x", py::overload_cast<const T&>(&Class::X), "Set the x value.")
.def("y", py::overload_cast<const T&>(&Class::Y), "Set the y value.")
.def_readonly_static("ZERO", &Class::Zero, "math::Vector2(0, 0)")
.def_readonly_static("ONE", &Class::One, "math::Vector2(1, 1)")
.def_readonly_static("NAN", &Class::NaN, "math::Vector3(NaN, NaN)")
.def("__copy__", [](const Class &self) {
return Class(self);
})
.def("__deepcopy__", [](const Class &self, py::dict) {
return Class(self);
}, "memo"_a)
.def("__getitem__",
py::overload_cast<const std::size_t>(&Class::operator[], py::const_))
.def("__setitem__",
[](Class* vec, unsigned index, T val) { (*vec)[index] = val; })
.def("__str__", toString)
.def("__repr__", toString);
}

/// Define a pybind11 wrapper for an ignition::math::Vector2
/**
* \param[in] module a pybind11 module to add the definition to
Expand Down

0 comments on commit 213d924

Please sign in to comment.