diff --git a/src/python/magnum/math.matrix.h b/src/python/magnum/math.matrix.h index f8b4ae1..0f7835a 100644 --- a/src/python/magnum/math.matrix.h +++ b/src/python/magnum/math.matrix.h @@ -58,6 +58,15 @@ template void everyRectangularMatrix(py::class_(); c + .def_static("from_diagonal", [](const typename VectorTraits::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(), "Construct a matrix with one value for all components") + /* Buffer protocol, needed in order to make numpy treat the matric correctly as column-major. Has to be defined *before* the from-tuple constructor so it gets precedence for types that implement the @@ -119,16 +128,6 @@ template void rectangularMatrix(py::class_& c) { */ c - /* Constructors */ - .def_static("from_diagonal", [](const typename VectorTraits::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(), "Construct a matrix with one value for all components") - /* Buffer protocol, needed in order to make numpy treat the matric correctly as column-major. The constructor is defined in everyRectangularMatrix(). */ @@ -181,6 +180,12 @@ template void rectangularMatrix(py::class_& c) { has to be separate */ template void everyMatrix(py::class_& 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_orthogonal", &T::invertedOrthogonal, "Inverted orthogonal matrix") .def("__matmul__", [](const T& self, const T& other) -> T { @@ -193,11 +198,6 @@ template void everyMatrix(py::class_& c) { template void matrix(py::class_& 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 */ .def("is_orthogonal", &T::isOrthogonal, "Whether the matrix is orthogonal") .def("trace", &T::trace, "Trace of the matrix") @@ -504,14 +504,6 @@ template void matrices( .def_static("from", static_cast(*)(const Math::Matrix2x2&, const Math::Vector2&)>(&Math::Matrix3::from), "Create a matrix from a rotation/scaling part and a translation part", py::arg("rotation_scaling"), py::arg("translation")) - .def_static("zero_init", []() { - return Math::Matrix3{Math::ZeroInit}; - }, "Construct a zero-filled matrix") - .def_static("identity_init", [](T value) { - return Math::Matrix3{Math::IdentityInit, value}; - }, "Construct an identity matrix", py::arg("value") = T(1)) - .def(py::init(), "Default constructor") - .def(py::init(), "Construct a matrix with one value for all components") .def(py::init&, const Math::Vector3&, const Math::Vector3&>(), "Construct from column vectors") .def(py::init([](const std::tuple, Math::Vector3, Math::Vector3>& value) { @@ -632,14 +624,6 @@ template void matrices( .def_static("from", static_cast(*)(const Math::Matrix3x3&, const Math::Vector3&)>(&Math::Matrix4::from), "Create a matrix from a rotation/scaling part and a translation part", py::arg("rotation_scaling"), py::arg("translation")) - .def_static("zero_init", []() { - return Math::Matrix4{Math::ZeroInit}; - }, "Construct a zero-filled matrix") - .def_static("identity_init", [](T value) { - return Math::Matrix4{Math::IdentityInit, value}; - }, "Construct an identity matrix", py::arg("value") = T(1)) - .def(py::init(), "Default constructor") - .def(py::init(), "Construct a matrix with one value for all components") .def(py::init&, const Math::Vector4&, const Math::Vector4&, const Math::Vector4&>(), "Construct from column vectors") .def(py::init([](const std::tuple, Math::Vector4, Math::Vector4, Math::Vector4>& value) { diff --git a/src/python/magnum/test/test_math.py b/src/python/magnum/test/test_math.py index d672b3a..55e8a5b 100644 --- a/src/python/magnum/test/test_math.py +++ b/src/python/magnum/test/test_math.py @@ -666,6 +666,8 @@ class Matrix3_(unittest.TestCase): Matrix3.scaling(Vector2(1/3.0))) 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().transposed(), Matrix3) @@ -769,6 +771,8 @@ class Matrix4_(unittest.TestCase): Matrix4.scaling(Vector3(1/3.0))) 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().transposed(), Matrix4)