Browse Source

python: expose BoolVector.

pull/1/head
Vladimír Vondruš 7 years ago
parent
commit
e305eb4c7e
  1. 4
      src/python/magnum/__init__.py
  2. 52
      src/python/magnum/math.cpp
  3. 30
      src/python/magnum/test/test_math.py

4
src/python/magnum/__init__.py

@ -28,5 +28,7 @@
from ._magnum import *
__all__ = [
'Deg', 'Rad'
'Deg', 'Rad',
'BoolVector2', 'BoolVector3', 'BoolVector4',
]

52
src/python/magnum/math.cpp

@ -28,6 +28,7 @@
#include <pybind11/operators.h>
#include <Magnum/Magnum.h>
#include <Magnum/Math/Angle.h>
#include <Magnum/Math/BoolVector.h>
#include "magnum/bootstrap.h"
@ -107,6 +108,49 @@ template<class T> void angle(py::class_<T>& c) {
.def("__repr__", repr<T>, "Object representation");
}
template<class T> void boolVector(py::class_<T>& c) {
c
/* Constructors */
.def_static("zero_init", []() {
return T{Math::ZeroInit};
}, "Construct a zero-filled boolean vector")
.def(py::init(), "Default constructor")
.def(py::init<bool>(), "Construct a boolean vector with one value for all fields")
.def(py::init<UnsignedByte>(), "Construct a boolean vector from segment values")
/* Explicit conversion to bool */
.def("__bool__", &T::operator bool, "Boolean conversion")
/* Comparison */
.def(py::self == py::self, "Equality comparison")
.def(py::self != py::self, "Non-equality comparison")
/* Member functions */
.def("all", &T::all, "Whether all bits are set")
.def("none", &T::none, "Whether no bits are set")
.def("any", &T::any, "Whether any bit is set")
/* Set / get */
.def("__setitem__", &T::set, "Set a bit at given position", py::arg("i"), py::arg("value"))
.def("__getitem__", &T::operator[], "Bit at given position")
/* Operators */
.def(~py::self, "Bitwise inversion")
.def(py::self &= py::self, "Bitwise AND and assign")
.def(py::self & py::self, "Bitwise AND")
.def(py::self |= py::self, "Bitwise OR and assign")
.def(py::self | py::self, "Bitwise OR")
.def(py::self ^= py::self, "Bitwise XOR and assign")
.def(py::self ^ py::self, "Bitwise XOR")
.def("__repr__", repr<T>, "Object representation");
/* Vector length */
char lenDocstring[] = "Vector size. Returns _.";
lenDocstring[sizeof(lenDocstring) - 3] = '0' + T::Size;
c.def_static("__len__", []() { return int(T::Size); }, lenDocstring);
}
}
void math(py::module& root, py::module& m) {
@ -119,6 +163,14 @@ void math(py::module& root, py::module& m) {
rad.def(py::init<Degd>(), "Conversion from degrees");
angle(deg);
angle(rad);
/* BoolVector */
py::class_<Math::BoolVector<2>> boolVector2{root, "BoolVector2", "Two-component bool vector"};
py::class_<Math::BoolVector<3>> boolVector3{root, "BoolVector3", "Three-component bool vector"};
py::class_<Math::BoolVector<4>> boolVector4{root, "BoolVector4", "Four-component bool vector"};
boolVector(boolVector2);
boolVector(boolVector3);
boolVector(boolVector4);
}
}

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

@ -67,3 +67,33 @@ class Angle(unittest.TestCase):
def test_repr(self):
self.assertEqual(repr(Deg(45.3)), 'Deg(45.3)')
class BoolVector(unittest.TestCase):
def test_init(self):
a = BoolVector2()
b = BoolVector2.zero_init()
c = BoolVector2(0b11)
self.assertFalse(a.any())
self.assertTrue(c.all())
self.assertEqual(a, BoolVector2(0b00))
self.assertEqual(b, BoolVector2(0b00))
self.assertEqual(c, BoolVector2(0b11))
def test_length(self):
self.assertEqual(BoolVector3.__len__(), 3)
#self.assertEqual(len(BoolVector3), 3) TODO: Y not?
self.assertEqual(len(BoolVector4()), 4)
def test_set_get(self):
a = BoolVector4(0b1010)
self.assertEqual(a[1], 1)
a[2] = True
self.assertEqual(a, BoolVector4(0b1110))
def test_ops(self):
a = BoolVector3(0b101) | BoolVector3(0b010)
self.assertEqual(a, BoolVector3(0b111))
def test_repr(self):
self.assertEqual(repr(BoolVector4(0b0101)), 'BoolVector(0b0101)')

Loading…
Cancel
Save