Skip to content

Commit

Permalink
feat(bindings): adds .numpy() conversion for vector types
Browse files Browse the repository at this point in the history
  • Loading branch information
wpumacay committed Apr 19, 2023
1 parent c520c39 commit 1e00ac7
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions python/math3d/bindings/mat2_py.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ auto bindings_matrix2(py::module& m, const char* class_name) -> void {
// cppcheck-suppress constParameter
MATRIX_GETSET_ITEM(2, T)
// clang-format on
.def("numpy",
[](const Class& self) -> py::array_t<T> {
return ::math::mat2_to_nparray<T>(self);
})
.def("flatten",
[](const Class& self) -> py::array_t<T> {
auto array_np = py::array_t<T>(Class::BUFFER_SIZE);
Expand Down
4 changes: 4 additions & 0 deletions python/math3d/bindings/mat3_py.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ auto bindings_matrix3(py::module& m, const char* class_name) -> void {
// cppcheck-suppress constParameter
MATRIX_GETSET_ITEM(3, T)
// clang-format on
.def("numpy",
[](const Class& self) -> py::array_t<T> {
return ::math::mat3_to_nparray<T>(self);
})
.def("flatten",
[](const Class& self) -> py::array_t<T> {
auto array_np = py::array_t<T>(Class::BUFFER_SIZE);
Expand Down
4 changes: 4 additions & 0 deletions python/math3d/bindings/mat4_py.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ auto bindings_matrix4(py::module& m, const char* class_name) -> void {
// cppcheck-suppress constParameter
MATRIX_GETSET_ITEM(4, T)
// clang-format on
.def("numpy",
[](const Class& self) -> py::array_t<T> {
return ::math::mat4_to_nparray<T>(self);
})
.def("flatten",
[](const Class& self) -> py::array_t<T> {
auto array_np = py::array_t<T>(Class::BUFFER_SIZE);
Expand Down
4 changes: 4 additions & 0 deletions python/math3d/bindings/vec2_py.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <math/vec2_t.hpp>

#include <common_py.hpp>
#include <conversions_py.hpp>

namespace py = pybind11;

Expand Down Expand Up @@ -50,6 +51,9 @@ auto bindings_vector2(py::module& m, const char* class_name) -> void {
VECTOR_METHODS(T)
VECTOR_GETSET_ITEM(2, T)
// clant-format on
.def("numpy", [](const Class& self) -> py::array_t<T> {
return ::math::vec2_to_nparray<T>(self);
})
// NOLINTNEXTLINE
.def_property_readonly("ndim", [](const Class&) {
return Class::VECTOR_NDIM;
Expand Down
4 changes: 4 additions & 0 deletions python/math3d/bindings/vec3_py.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <math/vec3_t.hpp>

#include <common_py.hpp>
#include <conversions_py.hpp>

namespace py = pybind11;

Expand Down Expand Up @@ -53,6 +54,9 @@ auto bindings_vector3(py::module& m, const char* class_name) -> void {
})
VECTOR_GETSET_ITEM(3, T)
// clant-format on
.def("numpy", [](const Class& self) -> py::array_t<T> {
return ::math::vec3_to_nparray<T>(self);
})
// NOLINTNEXTLINE
.def_property_readonly("ndim", [](const Class&) {
return Class::VECTOR_NDIM;
Expand Down
4 changes: 4 additions & 0 deletions python/math3d/bindings/vec4_py.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <math/vec4_t.hpp>

#include <common_py.hpp>
#include <conversions_py.hpp>

namespace py = pybind11;

Expand Down Expand Up @@ -52,6 +53,9 @@ auto bindings_vector4(py::module& m, const char* class_name) -> void {
VECTOR_METHODS(T)
VECTOR_GETSET_ITEM(4, T)
// clant-format on
.def("numpy", [](const Class& self) -> py::array_t<T> {
return ::math::vec4_to_nparray<T>(self);
})
// NOLINTNEXTLINE
.def_property_readonly("ndim", [](const Class&) {
return Class::VECTOR_NDIM;
Expand Down
3 changes: 3 additions & 0 deletions python/math3d/typings/math3d.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Vector2:
def squareNorm(self) -> float: ...
def normalize(self) -> Vector2: ...
def normalize_(self) -> None: ...
def numpy(self) -> ndarray: ...

# Structure should be the same, except for the internal usage of f32 and f64
Vector2f = Vector2
Expand Down Expand Up @@ -71,6 +72,7 @@ class Vector3:
def squareNorm(self) -> float: ...
def normalize(self) -> Vector3: ...
def normalize_(self) -> None: ...
def numpy(self) -> ndarray: ...

# Structure should be the same, except for the internal usage of f32 and f64
Vector3f = Vector3
Expand Down Expand Up @@ -114,6 +116,7 @@ class Vector4:
def squareNorm(self) -> float: ...
def normalize(self) -> Vector4: ...
def normalize_(self) -> None: ...
def numpy(self) -> ndarray: ...

# Structure should be the same, except for the internal usage of f32 and f64
Vector4f = Vector4
Expand Down
10 changes: 10 additions & 0 deletions tests/python/vec2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,13 @@ def test_dot_product(self, Class: Vector2Cls) -> None:
vec_a = Class(3.0, 5.0)
vec_b = Class(7.0, 11.0)
assert vec_a.dot(vec_b) == 76.0


@pytest.mark.parametrize(
"Vec2,FloatType", [(m3d.Vector2f, np.float32), (m3d.Vector2d, np.float64)]
)
def test_convert_to_numpy(Vec2: Vector2Cls, FloatType: type) -> None:
vec2 = Vec2(1.0, 2.0)
np_vec2 = vec2.numpy()
assert type(np_vec2) == np.ndarray
assert np.allclose(np_vec2, np.array([1.0, 2.0], dtype=FloatType))
10 changes: 10 additions & 0 deletions tests/python/vec3_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,13 @@ def test_cross_product(self, Class: Vector3Cls) -> None:
vec_c = vec_k.cross(vec_i)
expected_c = Class(0.0, 1.0, 0.0)
assert vec_c == expected_c


@pytest.mark.parametrize(
"Vec3,FloatType", [(m3d.Vector3f, np.float32), (m3d.Vector3d, np.float64)]
)
def test_convert_to_numpy(Vec3: Vector3Cls, FloatType: type) -> None:
vec3 = Vec3(1.0, 2.0, 3.0)
np_vec3 = vec3.numpy()
assert type(np_vec3) == np.ndarray
assert np.allclose(np_vec3, np.array([1.0, 2.0, 3.0], dtype=FloatType))
10 changes: 10 additions & 0 deletions tests/python/vec4_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,13 @@ def test_dot_product(self, Class: Vector4Cls) -> None:
vec_a = Class(3.0, 5.0, 7.0, 11.0)
vec_b = Class(13.0, 17.0, 19.0, 23.0)
assert np.abs(vec_a.dot(vec_b) - 510.0) < 1e-5


@pytest.mark.parametrize(
"Vec4,FloatType", [(m3d.Vector4f, np.float32), (m3d.Vector4d, np.float64)]
)
def test_convert_to_numpy(Vec4: Vector4Cls, FloatType: type) -> None:
vec4 = Vec4(1.0, 2.0, 3.0, 4.0)
np_vec4 = vec4.numpy()
assert type(np_vec4) == np.ndarray
assert np.allclose(np_vec4, np.array([1.0, 2.0, 3.0, 4.0], dtype=FloatType))

0 comments on commit 1e00ac7

Please sign in to comment.