|
| 1 | +/* |
| 2 | + * Copyright (C) 2021 Open Source Robotics Foundation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | +*/ |
| 17 | + |
| 18 | +#ifndef IGNITION_MATH_PYTHON__VECTOR2D_HH_ |
| 19 | +#define IGNITION_MATH_PYTHON__VECTOR2D_HH_ |
| 20 | + |
| 21 | +#include <sstream> |
| 22 | +#include <string> |
| 23 | + |
| 24 | +#include <pybind11/pybind11.h> |
| 25 | +#include <pybind11/operators.h> |
| 26 | + |
| 27 | +#include <ignition/math/Vector2.hh> |
| 28 | + |
| 29 | +namespace py = pybind11; |
| 30 | +using namespace pybind11::literals; |
| 31 | + |
| 32 | +namespace ignition |
| 33 | +{ |
| 34 | +namespace math |
| 35 | +{ |
| 36 | +namespace python |
| 37 | +{ |
| 38 | +/// Define a pybind11 wrapper for an ignition::math::Vector2 |
| 39 | +/** |
| 40 | + * \param[in] module a pybind11 module to add the definition to |
| 41 | + */ |
| 42 | +template<typename T> |
| 43 | +void defineMathVector2(py::module &m, const std::string &typestr) |
| 44 | +{ |
| 45 | + using Class = ignition::math::Vector2<T>; |
| 46 | + auto toString = [](const Class &si) { |
| 47 | + std::stringstream stream; |
| 48 | + stream << si; |
| 49 | + return stream.str(); |
| 50 | + }; |
| 51 | + std::string pyclass_name = typestr; |
| 52 | + py::class_<Class>(m, |
| 53 | + pyclass_name.c_str(), |
| 54 | + py::buffer_protocol(), |
| 55 | + py::dynamic_attr()) |
| 56 | + .def(py::init<>()) |
| 57 | + .def(py::init<const T&, const T&>()) |
| 58 | + .def(py::init<const Class>()) |
| 59 | + .def("sum", &Class::Sum, "Return the sum of the values") |
| 60 | + .def("distance", &Class::Distance, "Calc distance to the given point") |
| 61 | + .def("length", |
| 62 | + &Class::Length, |
| 63 | + "Returns the length (magnitude) of the vector") |
| 64 | + .def("squared_length", |
| 65 | + &Class::SquaredLength, |
| 66 | + "Return the square of the length (magnitude) of the vector") |
| 67 | + .def("normalize", &Class::Normalize, "Normalize the vector length") |
| 68 | + .def("normalized", &Class::Normalized, "Return a normalized vector") |
| 69 | + .def("round", |
| 70 | + &Class::Round, |
| 71 | + "Round to near whole number, return the result.") |
| 72 | + .def("rounded", &Class::Rounded, "Get a rounded version of this vector") |
| 73 | + .def("set", &Class::Set, "Set the contents of the vector") |
| 74 | + .def("dot", |
| 75 | + &Class::Dot, |
| 76 | + "Return the dot product of this vector and another vector") |
| 77 | + .def("abs_dot", &Class::AbsDot, |
| 78 | + "Return the absolute dot product of this vector and " |
| 79 | + "another vector. This is similar to the Dot function, except the " |
| 80 | + "absolute value of each component of the vector is used.") |
| 81 | + .def("abs", |
| 82 | + &Class::Abs, |
| 83 | + "Get the absolute value of the vector") |
| 84 | + .def("max", |
| 85 | + py::overload_cast<const Class&>(&Class::Max), |
| 86 | + "Set this vector's components to the maximum of itself and the " |
| 87 | + "passed in vector") |
| 88 | + .def("max", py::overload_cast<>(&Class::Max, py::const_), |
| 89 | + "Get the maximum value in the vector") |
| 90 | + .def("min", py::overload_cast<const Class&>(&Class::Min), |
| 91 | + "Set this vector's components to the minimum of itself and the " |
| 92 | + "passed in vector") |
| 93 | + .def("min", py::overload_cast<>(&Class::Min, py::const_), |
| 94 | + "Get the minimum value in the vector") |
| 95 | + .def(py::self + py::self) |
| 96 | + .def(py::self += py::self) |
| 97 | + .def(py::self + T()) |
| 98 | + .def(py::self += T()) |
| 99 | + .def(py::self * py::self) |
| 100 | + .def(py::self *= py::self) |
| 101 | + .def(py::self * T()) |
| 102 | + .def(py::self *= T()) |
| 103 | + .def(py::self - py::self) |
| 104 | + .def(py::self -= py::self) |
| 105 | + .def(py::self - T()) |
| 106 | + .def(py::self -= T()) |
| 107 | + .def(py::self / py::self) |
| 108 | + .def(py::self /= py::self) |
| 109 | + .def(py::self / T()) |
| 110 | + .def(py::self /= T()) |
| 111 | + .def(py::self != py::self) |
| 112 | + .def(py::self == py::self) |
| 113 | + .def(-py::self) |
| 114 | + .def("equal", &Class::Equal, "Equal to operator") |
| 115 | + .def("is_finite", |
| 116 | + &Class::IsFinite, |
| 117 | + "See if a point is finite (e.g., not nan)") |
| 118 | + .def("correct", &Class::Correct, "Corrects any nan values") |
| 119 | + .def("x", py::overload_cast<>(&Class::X), "Get the x value.") |
| 120 | + .def("y", py::overload_cast<>(&Class::Y), "Get the y value.") |
| 121 | + .def("x", py::overload_cast<const T&>(&Class::X), "Set the x value.") |
| 122 | + .def("y", py::overload_cast<const T&>(&Class::Y), "Set the y value.") |
| 123 | + .def_readonly_static("ZERO", &Class::Zero, "math::Vector2(0, 0)") |
| 124 | + .def_readonly_static("ONE", &Class::One, "math::Vector2(1, 1)") |
| 125 | + .def_readonly_static("NAN", &Class::NaN, "math::Vector3(NaN, NaN)") |
| 126 | + .def("__copy__", [](const Class &self) { |
| 127 | + return Class(self); |
| 128 | + }) |
| 129 | + .def("__deepcopy__", [](const Class &self, py::dict) { |
| 130 | + return Class(self); |
| 131 | + }, "memo"_a) |
| 132 | + .def("__getitem__", |
| 133 | + py::overload_cast<const std::size_t>(&Class::operator[], py::const_)) |
| 134 | + .def("__setitem__", |
| 135 | + [](Class* vec, unsigned index, T val) { (*vec)[index] = val; }) |
| 136 | + .def("__str__", toString) |
| 137 | + .def("__repr__", toString); |
| 138 | +} |
| 139 | + |
| 140 | +} // namespace python |
| 141 | +} // namespace gazebo |
| 142 | +} // namespace ignition |
| 143 | + |
| 144 | +#endif // IGNITION_MATH_PYTHON__VECTOR2D_HH_ |
0 commit comments