Browse Source

python: make sure all Matrix static constructors return proper type.

And test for those.
pull/2/head
Vladimír Vondruš 7 years ago
parent
commit
d5b5b55d2f
  1. 46
      src/python/magnum/math.matrix.h
  2. 4
      src/python/magnum/test/test_math.py

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

@ -58,6 +58,15 @@ template<class T, class ...Args> void everyRectangularMatrix(py::class_<T, Args.
py::implicitly_convertible<py::buffer, T>(); py::implicitly_convertible<py::buffer, T>();
c c
.def_static("from_diagonal", [](const typename VectorTraits<T::DiagonalSize, typename T::Type>::Type& vector) {
return T::fromDiagonal(vector);
}, "Construct a diagonal matrix")
.def_static("zero_init", []() {
return T{Math::ZeroInit};
}, "Construct a zero-filled matrix")
.def(py::init(), "Default constructor")
.def(py::init<typename T::Type>(), "Construct a matrix with one value for all components")
/* Buffer protocol, needed in order to make numpy treat the matric /* Buffer protocol, needed in order to make numpy treat the matric
correctly as column-major. Has to be defined *before* the from-tuple correctly as column-major. Has to be defined *before* the from-tuple
constructor so it gets precedence for types that implement the constructor so it gets precedence for types that implement the
@ -119,16 +128,6 @@ template<class T> void rectangularMatrix(py::class_<T>& c) {
*/ */
c c
/* Constructors */
.def_static("from_diagonal", [](const typename VectorTraits<T::DiagonalSize, typename T::Type>::Type& vector) {
return T::fromDiagonal(vector);
}, "Construct a diagonal matrix")
.def_static("zero_init", []() {
return T{Math::ZeroInit};
}, "Construct a zero-filled matrix")
.def(py::init(), "Default constructor")
.def(py::init<typename T::Type>(), "Construct a matrix with one value for all components")
/* Buffer protocol, needed in order to make numpy treat the matric /* Buffer protocol, needed in order to make numpy treat the matric
correctly as column-major. The constructor is defined in correctly as column-major. The constructor is defined in
everyRectangularMatrix(). */ everyRectangularMatrix(). */
@ -181,6 +180,12 @@ template<class T> void rectangularMatrix(py::class_<T>& c) {
has to be separate */ has to be separate */
template<class T, class ...Args> void everyMatrix(py::class_<T, Args...>& c) { template<class T, class ...Args> void everyMatrix(py::class_<T, Args...>& c) {
c c
/* Constructors */
.def_static("identity_init", [](typename T::Type value) {
return T{Math::IdentityInit, value};
}, "Construct an identity matrix", py::arg("value") = typename T::Type(1))
/* Methods */
.def("inverted", &T::inverted, "Inverted matrix") .def("inverted", &T::inverted, "Inverted matrix")
.def("inverted_orthogonal", &T::invertedOrthogonal, "Inverted orthogonal matrix") .def("inverted_orthogonal", &T::invertedOrthogonal, "Inverted orthogonal matrix")
.def("__matmul__", [](const T& self, const T& other) -> T { .def("__matmul__", [](const T& self, const T& other) -> T {
@ -193,11 +198,6 @@ template<class T, class ...Args> void everyMatrix(py::class_<T, Args...>& c) {
template<class T> void matrix(py::class_<T>& c) { template<class T> void matrix(py::class_<T>& c) {
c c
/* Constructors */
.def_static("identity_init", [](typename T::Type value) {
return T{Math::IdentityInit, value};
}, "Construct an identity matrix", py::arg("value") = typename T::Type(1))
/* Member functions for square matrices only */ /* Member functions for square matrices only */
.def("is_orthogonal", &T::isOrthogonal, "Whether the matrix is orthogonal") .def("is_orthogonal", &T::isOrthogonal, "Whether the matrix is orthogonal")
.def("trace", &T::trace, "Trace of the matrix") .def("trace", &T::trace, "Trace of the matrix")
@ -504,14 +504,6 @@ template<class T> void matrices(
.def_static("from", static_cast<Math::Matrix3<T>(*)(const Math::Matrix2x2<T>&, const Math::Vector2<T>&)>(&Math::Matrix3<T>::from), .def_static("from", static_cast<Math::Matrix3<T>(*)(const Math::Matrix2x2<T>&, const Math::Vector2<T>&)>(&Math::Matrix3<T>::from),
"Create a matrix from a rotation/scaling part and a translation part", "Create a matrix from a rotation/scaling part and a translation part",
py::arg("rotation_scaling"), py::arg("translation")) py::arg("rotation_scaling"), py::arg("translation"))
.def_static("zero_init", []() {
return Math::Matrix3<T>{Math::ZeroInit};
}, "Construct a zero-filled matrix")
.def_static("identity_init", [](T value) {
return Math::Matrix3<T>{Math::IdentityInit, value};
}, "Construct an identity matrix", py::arg("value") = T(1))
.def(py::init(), "Default constructor")
.def(py::init<T>(), "Construct a matrix with one value for all components")
.def(py::init<const Math::Vector3<T>&, const Math::Vector3<T>&, const Math::Vector3<T>&>(), .def(py::init<const Math::Vector3<T>&, const Math::Vector3<T>&, const Math::Vector3<T>&>(),
"Construct from column vectors") "Construct from column vectors")
.def(py::init([](const std::tuple<Math::Vector3<T>, Math::Vector3<T>, Math::Vector3<T>>& value) { .def(py::init([](const std::tuple<Math::Vector3<T>, Math::Vector3<T>, Math::Vector3<T>>& value) {
@ -632,14 +624,6 @@ template<class T> void matrices(
.def_static("from", static_cast<Math::Matrix4<T>(*)(const Math::Matrix3x3<T>&, const Math::Vector3<T>&)>(&Math::Matrix4<T>::from), .def_static("from", static_cast<Math::Matrix4<T>(*)(const Math::Matrix3x3<T>&, const Math::Vector3<T>&)>(&Math::Matrix4<T>::from),
"Create a matrix from a rotation/scaling part and a translation part", "Create a matrix from a rotation/scaling part and a translation part",
py::arg("rotation_scaling"), py::arg("translation")) py::arg("rotation_scaling"), py::arg("translation"))
.def_static("zero_init", []() {
return Math::Matrix4<T>{Math::ZeroInit};
}, "Construct a zero-filled matrix")
.def_static("identity_init", [](T value) {
return Math::Matrix4<T>{Math::IdentityInit, value};
}, "Construct an identity matrix", py::arg("value") = T(1))
.def(py::init(), "Default constructor")
.def(py::init<T>(), "Construct a matrix with one value for all components")
.def(py::init<const Math::Vector4<T>&, const Math::Vector4<T>&, const Math::Vector4<T>&, const Math::Vector4<T>&>(), .def(py::init<const Math::Vector4<T>&, const Math::Vector4<T>&, const Math::Vector4<T>&, const Math::Vector4<T>&>(),
"Construct from column vectors") "Construct from column vectors")
.def(py::init([](const std::tuple<Math::Vector4<T>, Math::Vector4<T>, Math::Vector4<T>, Math::Vector4<T>>& value) { .def(py::init([](const std::tuple<Math::Vector4<T>, Math::Vector4<T>, Math::Vector4<T>, Math::Vector4<T>>& value) {

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

@ -666,6 +666,8 @@ class Matrix3_(unittest.TestCase):
Matrix3.scaling(Vector2(1/3.0))) Matrix3.scaling(Vector2(1/3.0)))
def test_methods_return_type(self): def test_methods_return_type(self):
self.assertIsInstance(Matrix3.zero_init(), Matrix3)
self.assertIsInstance(Matrix3.from_diagonal((3.0, 1.0, 1.0)), Matrix3)
self.assertIsInstance(Matrix3()@Matrix3(), Matrix3) self.assertIsInstance(Matrix3()@Matrix3(), Matrix3)
self.assertIsInstance(Matrix3()+Matrix3(), Matrix3) self.assertIsInstance(Matrix3()+Matrix3(), Matrix3)
self.assertIsInstance(Matrix3().transposed(), Matrix3) self.assertIsInstance(Matrix3().transposed(), Matrix3)
@ -769,6 +771,8 @@ class Matrix4_(unittest.TestCase):
Matrix4.scaling(Vector3(1/3.0))) Matrix4.scaling(Vector3(1/3.0)))
def test_methods_return_type(self): def test_methods_return_type(self):
self.assertIsInstance(Matrix4.identity_init(), Matrix4)
self.assertIsInstance(Matrix4.from_diagonal((3.0, 1.5, 1.0, 1.0)), Matrix4)
self.assertIsInstance(Matrix4()@Matrix4(), Matrix4) self.assertIsInstance(Matrix4()@Matrix4(), Matrix4)
self.assertIsInstance(Matrix4()+Matrix4(), Matrix4) self.assertIsInstance(Matrix4()+Matrix4(), Matrix4)
self.assertIsInstance(Matrix4().transposed(), Matrix4) self.assertIsInstance(Matrix4().transposed(), Matrix4)

Loading…
Cancel
Save