Browse Source

python: expose basic math functions.

pull/1/head
Vladimír Vondruš 7 years ago
parent
commit
2132bd0410
  1. 13
      src/python/magnum/math.cpp
  2. 9
      src/python/magnum/test/test_math.py

13
src/python/magnum/math.cpp

@ -29,6 +29,7 @@
#include <Magnum/Magnum.h>
#include <Magnum/Math/Angle.h>
#include <Magnum/Math/BoolVector.h>
#include <Magnum/Math/Functions.h>
#include <Magnum/Math/Quaternion.h>
#include "magnum/bootstrap.h"
@ -296,6 +297,18 @@ void math(py::module& root, py::module& m) {
m.attr("nan") = Constantsd::nan();
m.attr("inf") = Constantsd::inf();
/* Functions */
m
.def("sin", [](Radd angle) { return Math::sin(angle); }, "Sine")
.def("cos", [](Radd angle) { return Math::cos(angle); }, "Cosine")
.def("sincos", [](Radd angle) {
return Math::sincos(angle);
}, "Sine and cosine")
.def("tan", [](Radd angle) { return Math::tan(angle); }, "Tangent")
.def("asin", [](Double angle) { return Math::asin(angle); }, "Arc sine")
.def("acos", [](Double angle) { return Math::acos(angle); }, "Arc cosine")
.def("atan", [](Double angle) { return Math::atan(angle); }, "Arc tangent");
/* These are needed for the quaternion, so register them before */
magnum::mathVectorFloat(root, m);
magnum::mathVectorIntegral(root, m);

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

@ -107,6 +107,15 @@ class Constants(unittest.TestCase):
self.assertAlmostEqual(math.sqrt2**2, 2.0, 6)
self.assertAlmostEqual(math.sqrt3**2, 3.0)
class Functions(unittest.TestCase):
def test(self):
self.assertAlmostEqual(math.sin(Deg(45.0)), 0.7071067811865475)
self.assertAlmostEqual(Deg(math.asin(0.7071067811865475)), Deg(45.0))
sincos = math.sincos(Deg(90.0))
self.assertAlmostEqual(sincos[0], 1.0)
self.assertAlmostEqual(sincos[1], 0.0)
class Vector(unittest.TestCase):
def test_init(self):
a = Vector4i()

Loading…
Cancel
Save