Browse Source

python: adapt to Magnum changes.

pull/16/head
Vladimír Vondruš 4 years ago
parent
commit
62a07c38bc
  1. 4
      doc/python/magnum.math.rst
  2. 2
      src/python/magnum/__init__.py
  3. 10
      src/python/magnum/math.cpp
  4. 2
      src/python/magnum/math.matrix.h
  5. 4
      src/python/magnum/math.vector.h
  6. 42
      src/python/magnum/test/test_math.py

4
doc/python/magnum.math.rst

@ -212,8 +212,8 @@
- :py:`mat[a][b] = c` on matrices doesn't do the expected thing, use
:py:`mat[a, b] = c` instead
- :cpp:`Math::BoolVector::set()` doesn't exist, use ``[]`` instead
- While both boolean and bitwise operations on :cpp:`Math::BoolVector`
- :cpp:`Math::BitVector::set()` doesn't exist, use ``[]`` instead
- While both boolean and bitwise operations on :cpp:`Math::BitVector`
behave the same to ensure consistency in generic code, this is not
possible to do in Python. Here the boolean operations behave like
if :py:`any()` was applied before doing the operation.

2
src/python/magnum/__init__.py

@ -51,7 +51,7 @@ if 'scenegraph' in globals():
__all__ = [
'Deg', 'Rad',
'BoolVector2', 'BoolVector3', 'BoolVector4',
'BitVector2', 'BitVector3', 'BitVector4',
'Vector2', 'Vector3', 'Vector4',
'Vector2d', 'Vector3d', 'Vector4d',
'Vector2i', 'Vector3i', 'Vector4i',

10
src/python/magnum/math.cpp

@ -28,7 +28,7 @@
#include <pybind11/operators.h>
#include <Magnum/Magnum.h>
#include <Magnum/Math/Angle.h>
#include <Magnum/Math/BoolVector.h>
#include <Magnum/Math/BitVector.h>
#include <Magnum/Math/Functions.h>
#include <Magnum/Math/Quaternion.h>
@ -447,10 +447,10 @@ void math(py::module_& root, py::module_& m) {
py::implicitly_convertible<Radd, Degd>();
py::implicitly_convertible<Degd, Radd>();
/* 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"};
/* BitVector */
py::class_<Math::BitVector<2>> boolVector2{root, "BitVector2", "Two-component bool vector"};
py::class_<Math::BitVector<3>> boolVector3{root, "BitVector3", "Three-component bool vector"};
py::class_<Math::BitVector<4>> boolVector4{root, "BitVector4", "Four-component bool vector"};
boolVector(m, boolVector2);
boolVector(m, boolVector3);
boolVector(m, boolVector4);

2
src/python/magnum/math.matrix.h

@ -173,7 +173,7 @@ template<class T> void rectangularMatrix(py::class_<T>& c) {
Type
construction by slicing or expanding differently sized matrices
row() / setRow() (function? that's ugly. property? not sure how)
component-wise operations (would need BoolVector6 ... BoolVector16)
component-wise operations (would need BitVector6 ... BitVector16)
ij() (doesn't make sense in generic code as we don't have Matrix1)
*/

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

@ -223,8 +223,8 @@ template<class T> void vector(py::module_& m, py::class_<T>& c) {
.def("lerp", [](const T& a, const T& b, Double t) {
return T{Math::lerp(a, b, t)};
}, "Linear interpolation of two values", py::arg("a"), py::arg("b"), py::arg("t"))
/* The BoolVector overload has to be before the bool to match first */
.def("lerp", [](const T& a, const T& b, Math::BoolVector<T::Size> t) {
/* The BitVector overload has to be before the bool to match first */
.def("lerp", [](const T& a, const T& b, Math::BitVector<T::Size> t) {
return T{Math::lerp(a, b, t)};
}, "Linear interpolation of two values", py::arg("a"), py::arg("b"), py::arg("t"))
.def("lerp", [](const T& a, const T& b, bool t) {

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

@ -69,39 +69,39 @@ class Angle(unittest.TestCase):
def test_repr(self):
self.assertEqual(repr(Deg(45.3)), 'Deg(45.3)')
class BoolVector(unittest.TestCase):
class BitVector(unittest.TestCase):
def test_init(self):
a = BoolVector2()
b = BoolVector2.zero_init()
c = BoolVector2(0b11)
a = BitVector2()
b = BitVector2.zero_init()
c = BitVector2(0b11)
self.assertFalse(a.any())
self.assertTrue(c.all())
self.assertEqual(a, BoolVector2(0b00))
self.assertEqual(b, BoolVector2(0b00))
self.assertEqual(c, BoolVector2(0b11))
self.assertEqual(a, BitVector2(0b00))
self.assertEqual(b, BitVector2(0b00))
self.assertEqual(c, BitVector2(0b11))
def test_length(self):
self.assertEqual(BoolVector3.__len__(), 3)
#self.assertEqual(len(BoolVector3), 3) TODO: Y not?
self.assertEqual(len(BoolVector4()), 4)
self.assertEqual(BitVector3.__len__(), 3)
#self.assertEqual(len(BitVector3), 3) TODO: Y not?
self.assertEqual(len(BitVector4()), 4)
def test_set_get(self):
a = BoolVector4(0b1010)
a = BitVector4(0b1010)
self.assertEqual(a[1], 1)
a[2] = True
self.assertEqual(a, BoolVector4(0b1110))
self.assertEqual(a, BitVector4(0b1110))
def test_iterate(self):
a = [i for i in BoolVector4(0b1010)]
a = [i for i in BitVector4(0b1010)]
self.assertEqual(a, [False, True, False, True])
def test_ops(self):
a = BoolVector3(0b101) | BoolVector3(0b010)
self.assertEqual(a, BoolVector3(0b111))
a = BitVector3(0b101) | BitVector3(0b010)
self.assertEqual(a, BitVector3(0b111))
def test_repr(self):
self.assertEqual(repr(BoolVector4(0b0101)), 'BoolVector(0b0101)')
self.assertEqual(repr(BitVector4(0b0101)), 'BitVector(0b0101)')
class Constants(unittest.TestCase):
def test(self):
@ -177,8 +177,8 @@ class Functions(unittest.TestCase):
self.assertEqual(math.select(Rad(2.0), Rad(5.0), 0.6), Rad(2.0))
def test_vector(self):
self.assertEqual(math.isinf((math.inf, math.nan)), BoolVector2(0b01))
self.assertEqual(math.isnan((math.inf, math.nan)), BoolVector2(0b10))
self.assertEqual(math.isinf((math.inf, math.nan)), BitVector2(0b01))
self.assertEqual(math.isnan((math.inf, math.nan)), BitVector2(0b10))
self.assertEqual(math.min((15.0, 0.5), (3.0, 1.0)), (3.0, 0.5))
self.assertEqual(math.min((15.0, 0.5), 3.0), (3.0, 0.5))
@ -200,12 +200,12 @@ class Functions(unittest.TestCase):
self.assertEqual(math.fmod((5.1, 1.5), (3.0, 1.0)), (2.1, 0.5))
self.assertEqual(math.lerp((2.0, 1.0), (5.0, 2.0), 0.5), (3.5, 1.5))
self.assertEqual(math.lerp((2.0, 1.0), (5.0, 2.0), BoolVector2(0b01)), (5.0, 1.0))
self.assertEqual(math.lerp((2.0, 1.0), (5.0, 2.0), BitVector2(0b01)), (5.0, 1.0))
self.assertEqual(math.lerp((2.0, 1.0), (5.0, 2.0), False), (2.0, 1.0))
self.assertEqual(math.lerp((2, 1), (5, 2), 0.5), (3, 1))
self.assertEqual(math.lerp((2, 1), (5, 2), BoolVector2(0b01)), (5, 1))
self.assertEqual(math.lerp((2, 1), (5, 2), BitVector2(0b01)), (5, 1))
self.assertEqual(math.lerp((2, 1), (5, 2), True), (5, 2))
self.assertEqual(math.lerp(BoolVector4(0b1001), BoolVector4(0b0110), BoolVector4(0b0101)), BoolVector4(0b1100))
self.assertEqual(math.lerp(BitVector4(0b1001), BitVector4(0b0110), BitVector4(0b0101)), BitVector4(0b1100))
self.assertEqual(math.lerp_inverted((2.0, 1.0), (5.0, 2.0), (3.5, 1.5)), (0.5, 0.5))
self.assertEqual(math.select((2.0, 1.0), (5.0, 2.0), 0.6), (2.0, 1.0))
self.assertEqual(math.select((2, 1), (5, 2), 1.0), (5, 2))

Loading…
Cancel
Save