From 01538457a54cc188f1cfa3f70993cc498cc6cb1d Mon Sep 17 00:00:00 2001 From: Slendi Date: Wed, 11 Mar 2026 23:26:37 +0200 Subject: [PATCH] Add interop API Signed-off-by: Slendi --- include/smath.hpp | 17 ++++++++ tests/interop.cpp | 101 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 tests/interop.cpp diff --git a/include/smath.hpp b/include/smath.hpp index 5bda624..46e2899 100644 --- a/include/smath.hpp +++ b/include/smath.hpp @@ -1186,6 +1186,23 @@ template return m; } + +template struct interop_adapter; + +template +constexpr auto from_external(Ext const &ext) + -> typename interop_adapter::smath_type +{ + return interop_adapter::to_smath(ext); +} + +template +constexpr auto to_external(SMathT const &value) -> Ext +{ + return interop_adapter::from_smath(value); +} + + } // namespace smath template diff --git a/tests/interop.cpp b/tests/interop.cpp new file mode 100644 index 0000000..30f9d13 --- /dev/null +++ b/tests/interop.cpp @@ -0,0 +1,101 @@ +#include + +#include + +#include + +struct ExternalVec3f { + float x; + float y; + float z; +}; + +struct ExternalMat2f { + float m00; + float m01; + float m10; + float m11; +}; + +namespace smath { + +template<> struct interop_adapter { + using smath_type = Vec<3, float>; + + static constexpr auto to_smath(ExternalVec3f const &v) -> smath_type + { + return { v.x, v.y, v.z }; + } + + static constexpr auto from_smath(smath_type const &v) -> ExternalVec3f + { + return { v[0], v[1], v[2] }; + } +}; + +template<> struct interop_adapter { + using smath_type = Mat<2, 2, float>; + + static constexpr auto to_smath(ExternalMat2f const &m) -> smath_type + { + smath_type out {}; + out(0, 0) = m.m00; + out(0, 1) = m.m01; + out(1, 0) = m.m10; + out(1, 1) = m.m11; + return out; + } + + static constexpr auto from_smath(smath_type const &m) -> ExternalMat2f + { + return { m(0, 0), m(0, 1), m(1, 0), m(1, 1) }; + } +}; + +} // namespace smath + +TEST(Interop, FromExternalVec) { + ExternalVec3f ext { 1.0f, 2.0f, 3.0f }; + auto v = smath::from_external(ext); + + static_assert(std::is_same_v>); + EXPECT_EQ(v[0], 1.0f); + EXPECT_EQ(v[1], 2.0f); + EXPECT_EQ(v[2], 3.0f); +} + +TEST(Interop, ToExternalVec) { + smath::Vec3 v { 4.0f, 5.0f, 6.0f }; + auto ext = smath::to_external(v); + + EXPECT_EQ(ext.x, 4.0f); + EXPECT_EQ(ext.y, 5.0f); + EXPECT_EQ(ext.z, 6.0f); +} + +TEST(Interop, RoundtripVec) { + ExternalVec3f ext { 7.0f, 8.0f, 9.0f }; + auto v = smath::from_external(ext); + auto ext2 = smath::to_external(v); + + EXPECT_EQ(ext2.x, 7.0f); + EXPECT_EQ(ext2.y, 8.0f); + EXPECT_EQ(ext2.z, 9.0f); +} + +TEST(Interop, MatrixConversion) { + ExternalMat2f ext { 1.0f, 2.0f, 3.0f, 4.0f }; + auto m = smath::from_external(ext); + + static_assert(std::is_same_v>); + EXPECT_EQ(m(0, 0), 1.0f); + EXPECT_EQ(m(0, 1), 2.0f); + EXPECT_EQ(m(1, 0), 3.0f); + EXPECT_EQ(m(1, 1), 4.0f); + + auto ext2 = smath::to_external(m); + EXPECT_EQ(ext2.m00, 1.0f); + EXPECT_EQ(ext2.m01, 2.0f); + EXPECT_EQ(ext2.m10, 3.0f); + EXPECT_EQ(ext2.m11, 4.0f); +}