Browse Source

python: make vectors implicitly convertible from tuples.

pull/1/head
Vladimír Vondruš 7 years ago
parent
commit
f6cca325dd
  1. 15
      src/python/magnum/math.vector.h
  2. 6
      src/python/magnum/test/test_math.py

15
src/python/magnum/math.vector.h

@ -110,9 +110,14 @@ template<class T> void vector(py::module& m, py::class_<T>& c) {
}
template<class T> void vector2(py::class_<Math::Vector2<T>>& c) {
py::implicitly_convertible<const std::tuple<T, T>&, Math::Vector2<T>>();
c
/* Constructors */
.def(py::init<T, T>(), "Constructor")
.def(py::init([](const std::tuple<T, T>& value) {
return Math::Vector2<T>{std::get<0>(value), std::get<1>(value)};
}), "Construct from a tuple")
/* Static constructors */
.def_static("x_axis", &Math::Vector2<T>::xAxis,
@ -140,9 +145,14 @@ template<class T> void vector2(py::class_<Math::Vector2<T>>& c) {
}
template<class T> void vector3(py::class_<Math::Vector3<T>>& c) {
py::implicitly_convertible<const std::tuple<T, T, T>&, Math::Vector3<T>>();
c
/* Constructors */
.def(py::init<T, T, T>(), "Constructor")
.def(py::init([](const std::tuple<T, T, T>& value) {
return Math::Vector3<T>{std::get<0>(value), std::get<1>(value), std::get<2>(value)};
}), "Construct from a tuple")
/* Static constructors */
.def_static("x_axis", &Math::Vector3<T>::xAxis,
@ -192,9 +202,14 @@ template<class T> void vector3(py::class_<Math::Vector3<T>>& c) {
}
template<class T> void vector4(py::class_<Math::Vector4<T>>& c) {
py::implicitly_convertible<const std::tuple<T, T, T, T>&, Math::Vector4<T>>();
c
/* Constructors */
.def(py::init<T, T, T, T>(), "Constructor")
.def(py::init([](const std::tuple<T, T, T, T>& value) {
return Math::Vector4<T>{std::get<0>(value), std::get<1>(value), std::get<2>(value), std::get<3>(value)};
}), "Construct from a tuple")
/* Properties */
.def_property("x",

6
src/python/magnum/test/test_math.py

@ -112,9 +112,13 @@ class Vector(unittest.TestCase):
a = Vector4i()
b = Vector3d.zero_init()
c = Vector2i(44, -3)
d = Vector3((1.0, 0.3, 1.1))
e = Vector4d((1.0, 0.3, 1.1, 0.5))
self.assertEqual(a, Vector4i(0, 0, 0, 0))
self.assertEqual(b, Vector3d(0.0, 0.0, 0.0))
self.assertEqual(c, Vector2i(44, -3))
self.assertEqual(d, Vector3(1.0, 0.3, 1.1))
self.assertEqual(e, Vector4d(1.0, 0.3, 1.1, 0.5))
def test_static_methods(self):
self.assertEqual(Vector2.y_scale(5), Vector2(1, 5))
@ -220,7 +224,7 @@ class Vector(unittest.TestCase):
b = Vector4i(3, 4, 5, 6)
b.b *= 3
b.xy += Vector2i(1, -1)
b.xy += (1, -1)
self.assertEqual(b, Vector4i(4, 3, 15, 6))
def test_iterate(self):

Loading…
Cancel
Save